blob: bebc9fd1666592cb288d96c6d927be52536b49b5 [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.
Chris Mason4008c042009-02-12 14:09:45 -050065 *
66 * held is used to keep lockdep happy, when lockdep is enabled
67 * we set held to a blocking lock before we go around and
68 * retake all the spinlocks in the path. You can safely use NULL
69 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050070 */
Chris Mason4008c042009-02-12 14:09:45 -050071noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
72 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050073{
74 int i;
Chris Mason4008c042009-02-12 14:09:45 -050075
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 /* lockdep really cares that we take all of these spinlocks
78 * in the right order. If any of the locks in the path are not
79 * currently blocking, it is going to complain. So, make really
80 * really sure by forcing the path to blocking before we clear
81 * the path blocking.
82 */
83 if (held)
84 btrfs_set_lock_blocking(held);
85 btrfs_set_path_blocking(p);
86#endif
87
88 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050089 if (p->nodes[i] && p->locks[i])
90 btrfs_clear_lock_blocking(p->nodes[i]);
91 }
Chris Mason4008c042009-02-12 14:09:45 -050092
93#ifdef CONFIG_DEBUG_LOCK_ALLOC
94 if (held)
95 btrfs_clear_lock_blocking(held);
96#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050097}
98
Chris Masond352ac62008-09-29 15:18:18 -040099/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400100void btrfs_free_path(struct btrfs_path *p)
101{
Chris Masondf24a2b2007-04-04 09:36:31 -0400102 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400103 kmem_cache_free(btrfs_path_cachep, p);
104}
105
Chris Masond352ac62008-09-29 15:18:18 -0400106/*
107 * path release drops references on the extent buffers in the path
108 * and it drops any locks held by this path
109 *
110 * It is safe to call this on paths that no locks or extent buffers held.
111 */
Chris Masond3977122009-01-05 21:25:51 -0500112noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500113{
114 int i;
Chris Masona2135012008-06-25 16:01:30 -0400115
Chris Mason234b63a2007-03-13 10:46:10 -0400116 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400117 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500118 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400119 continue;
120 if (p->locks[i]) {
121 btrfs_tree_unlock(p->nodes[i]);
122 p->locks[i] = 0;
123 }
Chris Mason5f39d392007-10-15 16:14:19 -0400124 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 }
127}
128
Chris Masond352ac62008-09-29 15:18:18 -0400129/*
130 * safely gets a reference on the root node of a tree. A lock
131 * is not taken, so a concurrent writer may put a different node
132 * at the root of the tree. See btrfs_lock_root_node for the
133 * looping required.
134 *
135 * The extent buffer returned by this has a reference taken, so
136 * it won't disappear. It may stop being the root of the tree
137 * at any time because there are no locks held.
138 */
Chris Mason925baed2008-06-25 16:01:30 -0400139struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
140{
141 struct extent_buffer *eb;
142 spin_lock(&root->node_lock);
143 eb = root->node;
144 extent_buffer_get(eb);
145 spin_unlock(&root->node_lock);
146 return eb;
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/* loop around taking references on and locking the root node of the
150 * tree until you end up with a lock on the root. A locked buffer
151 * is returned, with a reference held.
152 */
Chris Mason925baed2008-06-25 16:01:30 -0400153struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
154{
155 struct extent_buffer *eb;
156
Chris Masond3977122009-01-05 21:25:51 -0500157 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400158 eb = btrfs_root_node(root);
159 btrfs_tree_lock(eb);
160
161 spin_lock(&root->node_lock);
162 if (eb == root->node) {
163 spin_unlock(&root->node_lock);
164 break;
165 }
166 spin_unlock(&root->node_lock);
167
168 btrfs_tree_unlock(eb);
169 free_extent_buffer(eb);
170 }
171 return eb;
172}
173
Chris Masond352ac62008-09-29 15:18:18 -0400174/* cowonly root (everything not a reference counted cow subvolume), just get
175 * put onto a simple dirty list. transaction.c walks this to make sure they
176 * get properly updated on disk.
177 */
Chris Mason0b86a832008-03-24 15:01:56 -0400178static void add_root_to_dirty_list(struct btrfs_root *root)
179{
180 if (root->track_dirty && list_empty(&root->dirty_list)) {
181 list_add(&root->dirty_list,
182 &root->fs_info->dirty_cowonly_roots);
183 }
184}
185
Chris Masond352ac62008-09-29 15:18:18 -0400186/*
187 * used by snapshot creation to make a copy of a root for a tree with
188 * a given objectid. The buffer with the new root node is returned in
189 * cow_ret, and this func returns zero on success or a negative error code.
190 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500191int btrfs_copy_root(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root,
193 struct extent_buffer *buf,
194 struct extent_buffer **cow_ret, u64 new_root_objectid)
195{
196 struct extent_buffer *cow;
197 u32 nritems;
198 int ret = 0;
199 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500200 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201
Chris Mason4aec2b52007-12-18 16:25:45 -0500202 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
203 if (!new_root)
204 return -ENOMEM;
205
206 memcpy(new_root, root, sizeof(*new_root));
207 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208
209 WARN_ON(root->ref_cows && trans->transid !=
210 root->fs_info->running_transaction->transid);
211 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
212
213 level = btrfs_header_level(buf);
214 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400215
216 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
217 new_root_objectid, trans->transid,
218 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500219 if (IS_ERR(cow)) {
220 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500222 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
224 copy_extent_buffer(cow, buf, 0, 0, cow->len);
225 btrfs_set_header_bytenr(cow, cow->start);
226 btrfs_set_header_generation(cow, trans->transid);
227 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400228 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229
Yan Zheng2b820322008-11-17 21:11:30 -0500230 write_extent_buffer(cow, root->fs_info->fsid,
231 (unsigned long)btrfs_header_fsid(cow),
232 BTRFS_FSID_SIZE);
233
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400235 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500236 kfree(new_root);
237
Chris Masonbe20aa92007-12-17 20:14:01 -0500238 if (ret)
239 return ret;
240
241 btrfs_mark_buffer_dirty(cow);
242 *cow_ret = cow;
243 return 0;
244}
245
Chris Masond352ac62008-09-29 15:18:18 -0400246/*
Chris Masond3977122009-01-05 21:25:51 -0500247 * does the dirty work in cow of a single block. The parent block (if
248 * supplied) is updated to point to the new cow copy. The new buffer is marked
249 * dirty and returned locked. If you modify the block it needs to be marked
250 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400251 *
252 * search_start -- an allocation hint for the new block
253 *
Chris Masond3977122009-01-05 21:25:51 -0500254 * empty_size -- a hint that you plan on doing more cow. This is the size in
255 * bytes the allocator should try to find free next to the block it returns.
256 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400257 */
Chris Masond3977122009-01-05 21:25:51 -0500258static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400259 struct btrfs_root *root,
260 struct extent_buffer *buf,
261 struct extent_buffer *parent, int parent_slot,
262 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400263 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400264{
Zheng Yan31840ae2008-09-23 13:14:14 -0400265 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400266 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500267 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400268 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500269 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400270 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400271
Chris Mason925baed2008-06-25 16:01:30 -0400272 if (*cow_ret == buf)
273 unlock_orig = 1;
274
Chris Masonb9447ef82009-03-09 11:45:38 -0400275 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400276
Zheng Yan31840ae2008-09-23 13:14:14 -0400277 if (parent)
278 parent_start = parent->start;
279 else
280 parent_start = 0;
281
Chris Mason7bb86312007-12-11 09:25:06 -0500282 WARN_ON(root->ref_cows && trans->transid !=
283 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400284 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400285
Chris Mason7bb86312007-12-11 09:25:06 -0500286 level = btrfs_header_level(buf);
287 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400288
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400289 cow = btrfs_alloc_free_block(trans, root, buf->len,
290 parent_start, root->root_key.objectid,
291 trans->transid, level,
292 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400293 if (IS_ERR(cow))
294 return PTR_ERR(cow);
295
Chris Masonb4ce94d2009-02-04 09:25:08 -0500296 /* cow is set to blocking by btrfs_init_new_buffer */
297
Chris Mason5f39d392007-10-15 16:14:19 -0400298 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400299 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400300 btrfs_set_header_generation(cow, trans->transid);
301 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400302 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400303
Yan Zheng2b820322008-11-17 21:11:30 -0500304 write_extent_buffer(cow, root->fs_info->fsid,
305 (unsigned long)btrfs_header_fsid(cow),
306 BTRFS_FSID_SIZE);
307
Chris Mason5f39d392007-10-15 16:14:19 -0400308 WARN_ON(btrfs_header_generation(buf) > trans->transid);
309 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400310 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400311 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400312 if (ret)
313 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400314
315 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
316 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400317 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
318 /*
319 * There are only two places that can drop reference to
320 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400321 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400322 * we check reference count while tree block is locked.
323 * Furthermore, if reference count is one, it won't get
324 * increased by someone else.
325 */
326 u32 refs;
327 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
328 buf->len, &refs);
329 BUG_ON(ret);
330 if (refs == 1) {
331 ret = btrfs_update_ref(trans, root, buf, cow,
332 0, nritems);
333 clean_tree_block(trans, root, buf);
334 } else {
335 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
336 }
337 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400338 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400339 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
340 if (ret)
341 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400342 clean_tree_block(trans, root, buf);
343 }
344
Zheng Yan1a40e232008-09-26 10:09:34 -0400345 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400346 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
347 WARN_ON(ret);
348 }
349
Chris Mason6702ed42007-08-07 16:15:09 -0400350 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400351 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400352
353 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400354 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400355 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400356 spin_unlock(&root->node_lock);
357
Chris Mason6702ed42007-08-07 16:15:09 -0400358 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400359 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400360 buf->len, buf->start,
361 root->root_key.objectid,
362 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400363 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400364 }
Chris Mason5f39d392007-10-15 16:14:19 -0400365 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400366 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400367 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400368 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400369 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500370 WARN_ON(trans->transid == 0);
371 btrfs_set_node_ptr_generation(parent, parent_slot,
372 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400373 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400374 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500375 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400376 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400377 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400378 }
Chris Mason925baed2008-06-25 16:01:30 -0400379 if (unlock_orig)
380 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400381 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400382 btrfs_mark_buffer_dirty(cow);
383 *cow_ret = cow;
384 return 0;
385}
386
Chris Masond352ac62008-09-29 15:18:18 -0400387/*
388 * cows a single block, see __btrfs_cow_block for the real work.
389 * This version of it has extra checks so that a block isn't cow'd more than
390 * once per transaction, as long as it hasn't been written yet
391 */
Chris Masond3977122009-01-05 21:25:51 -0500392noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400393 struct btrfs_root *root, struct extent_buffer *buf,
394 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400395 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500396{
Chris Mason6702ed42007-08-07 16:15:09 -0400397 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400398 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500399
Chris Masonccd467d2007-06-28 15:57:36 -0400400 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500401 printk(KERN_CRIT "trans %llu running %llu\n",
402 (unsigned long long)trans->transid,
403 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400404 root->fs_info->running_transaction->transid);
405 WARN_ON(1);
406 }
407 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500408 printk(KERN_CRIT "trans %llu running %llu\n",
409 (unsigned long long)trans->transid,
410 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400411 WARN_ON(1);
412 }
Chris Masondc17ff82008-01-08 15:46:30 -0500413
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400414 if (btrfs_header_generation(buf) == trans->transid &&
415 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400416 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500417 *cow_ret = buf;
418 return 0;
419 }
Chris Masonc4876852009-02-04 09:24:25 -0500420
Chris Mason0b86a832008-03-24 15:01:56 -0400421 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500422
423 if (parent)
424 btrfs_set_lock_blocking(parent);
425 btrfs_set_lock_blocking(buf);
426
Chris Masonf510cfe2007-10-15 16:14:48 -0400427 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400428 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400429 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400430}
431
Chris Masond352ac62008-09-29 15:18:18 -0400432/*
433 * helper function for defrag to decide if two blocks pointed to by a
434 * node are actually close by
435 */
Chris Mason6b800532007-10-15 16:17:34 -0400436static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400437{
Chris Mason6b800532007-10-15 16:17:34 -0400438 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400439 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400440 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400441 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500442 return 0;
443}
444
Chris Mason081e9572007-11-06 10:26:24 -0500445/*
446 * compare two keys in a memcmp fashion
447 */
448static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
449{
450 struct btrfs_key k1;
451
452 btrfs_disk_key_to_cpu(&k1, disk);
453
454 if (k1.objectid > k2->objectid)
455 return 1;
456 if (k1.objectid < k2->objectid)
457 return -1;
458 if (k1.type > k2->type)
459 return 1;
460 if (k1.type < k2->type)
461 return -1;
462 if (k1.offset > k2->offset)
463 return 1;
464 if (k1.offset < k2->offset)
465 return -1;
466 return 0;
467}
468
Josef Bacikf3465ca2008-11-12 14:19:50 -0500469/*
470 * same as comp_keys only with two btrfs_key's
471 */
472static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
473{
474 if (k1->objectid > k2->objectid)
475 return 1;
476 if (k1->objectid < k2->objectid)
477 return -1;
478 if (k1->type > k2->type)
479 return 1;
480 if (k1->type < k2->type)
481 return -1;
482 if (k1->offset > k2->offset)
483 return 1;
484 if (k1->offset < k2->offset)
485 return -1;
486 return 0;
487}
Chris Mason081e9572007-11-06 10:26:24 -0500488
Chris Masond352ac62008-09-29 15:18:18 -0400489/*
490 * this is used by the defrag code to go through all the
491 * leaves pointed to by a node and reallocate them so that
492 * disk order is close to key order
493 */
Chris Mason6702ed42007-08-07 16:15:09 -0400494int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400495 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400496 int start_slot, int cache_only, u64 *last_ret,
497 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400498{
Chris Mason6b800532007-10-15 16:17:34 -0400499 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400500 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400501 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400502 u64 search_start = *last_ret;
503 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400504 u64 other;
505 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400506 int end_slot;
507 int i;
508 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400509 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400510 int uptodate;
511 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500512 int progress_passed = 0;
513 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400514
Chris Mason5708b952007-10-25 15:43:18 -0400515 parent_level = btrfs_header_level(parent);
516 if (cache_only && parent_level != 1)
517 return 0;
518
Chris Masond3977122009-01-05 21:25:51 -0500519 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400520 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500521 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400522 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400523
Chris Mason6b800532007-10-15 16:17:34 -0400524 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400525 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400526 end_slot = parent_nritems;
527
528 if (parent_nritems == 1)
529 return 0;
530
Chris Masonb4ce94d2009-02-04 09:25:08 -0500531 btrfs_set_lock_blocking(parent);
532
Chris Mason6702ed42007-08-07 16:15:09 -0400533 for (i = start_slot; i < end_slot; i++) {
534 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400535
Chris Mason5708b952007-10-25 15:43:18 -0400536 if (!parent->map_token) {
537 map_extent_buffer(parent,
538 btrfs_node_key_ptr_offset(i),
539 sizeof(struct btrfs_key_ptr),
540 &parent->map_token, &parent->kaddr,
541 &parent->map_start, &parent->map_len,
542 KM_USER1);
543 }
Chris Mason081e9572007-11-06 10:26:24 -0500544 btrfs_node_key(parent, &disk_key, i);
545 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
546 continue;
547
548 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400549 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400550 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400551 if (last_block == 0)
552 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400553
Chris Mason6702ed42007-08-07 16:15:09 -0400554 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400555 other = btrfs_node_blockptr(parent, i - 1);
556 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400557 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400558 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400559 other = btrfs_node_blockptr(parent, i + 1);
560 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400561 }
Chris Masone9d0b132007-08-10 14:06:19 -0400562 if (close) {
563 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400564 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400565 }
Chris Mason5708b952007-10-25 15:43:18 -0400566 if (parent->map_token) {
567 unmap_extent_buffer(parent, parent->map_token,
568 KM_USER1);
569 parent->map_token = NULL;
570 }
Chris Mason6702ed42007-08-07 16:15:09 -0400571
Chris Mason6b800532007-10-15 16:17:34 -0400572 cur = btrfs_find_tree_block(root, blocknr, blocksize);
573 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400574 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400575 else
576 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400577 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400578 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400579 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400580 continue;
581 }
Chris Mason6b800532007-10-15 16:17:34 -0400582 if (!cur) {
583 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400584 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400585 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400586 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400587 }
Chris Mason6702ed42007-08-07 16:15:09 -0400588 }
Chris Masone9d0b132007-08-10 14:06:19 -0400589 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400590 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400591
Chris Masone7a84562008-06-25 16:01:31 -0400592 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500593 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400594 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400595 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400596 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400597 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400598 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400599 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400600 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400601 break;
Yan252c38f2007-08-29 09:11:44 -0400602 }
Chris Masone7a84562008-06-25 16:01:31 -0400603 search_start = cur->start;
604 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400605 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400606 btrfs_tree_unlock(cur);
607 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400608 }
Chris Mason5708b952007-10-25 15:43:18 -0400609 if (parent->map_token) {
610 unmap_extent_buffer(parent, parent->map_token,
611 KM_USER1);
612 parent->map_token = NULL;
613 }
Chris Mason6702ed42007-08-07 16:15:09 -0400614 return err;
615}
616
Chris Mason74123bd2007-02-02 11:05:29 -0500617/*
618 * The leaf data grows from end-to-front in the node.
619 * this returns the address of the start of the last item,
620 * which is the stop of the leaf data stack
621 */
Chris Mason123abc82007-03-14 14:14:43 -0400622static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400623 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500624{
Chris Mason5f39d392007-10-15 16:14:19 -0400625 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500626 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400627 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400628 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500629}
630
Chris Masond352ac62008-09-29 15:18:18 -0400631/*
632 * extra debugging checks to make sure all the items in a key are
633 * well formed and in the proper order
634 */
Chris Mason123abc82007-03-14 14:14:43 -0400635static int check_node(struct btrfs_root *root, struct btrfs_path *path,
636 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500637{
Chris Mason5f39d392007-10-15 16:14:19 -0400638 struct extent_buffer *parent = NULL;
639 struct extent_buffer *node = path->nodes[level];
640 struct btrfs_disk_key parent_key;
641 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500642 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400643 int slot;
644 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400645 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500646
647 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400648 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400649
Chris Mason8d7be552007-05-10 11:24:42 -0400650 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400651 BUG_ON(nritems == 0);
652 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400653 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400654 btrfs_node_key(parent, &parent_key, parent_slot);
655 btrfs_node_key(node, &node_key, 0);
656 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400657 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400658 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400659 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500660 }
Chris Mason123abc82007-03-14 14:14:43 -0400661 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400662 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400663 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
664 btrfs_node_key(node, &node_key, slot);
665 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400666 }
667 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400668 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
669 btrfs_node_key(node, &node_key, slot);
670 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500671 }
672 return 0;
673}
674
Chris Masond352ac62008-09-29 15:18:18 -0400675/*
676 * extra checking to make sure all the items in a leaf are
677 * well formed and in the proper order
678 */
Chris Mason123abc82007-03-14 14:14:43 -0400679static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
680 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500681{
Chris Mason5f39d392007-10-15 16:14:19 -0400682 struct extent_buffer *leaf = path->nodes[level];
683 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500684 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400685 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400686 struct btrfs_disk_key parent_key;
687 struct btrfs_disk_key leaf_key;
688 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400689
Chris Mason5f39d392007-10-15 16:14:19 -0400690 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500691
692 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400693 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400694
695 if (nritems == 0)
696 return 0;
697
698 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400699 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400700 btrfs_node_key(parent, &parent_key, parent_slot);
701 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400702
Chris Mason5f39d392007-10-15 16:14:19 -0400703 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400704 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400705 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400706 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500707 }
Chris Mason5f39d392007-10-15 16:14:19 -0400708 if (slot != 0 && slot < nritems - 1) {
709 btrfs_item_key(leaf, &leaf_key, slot);
710 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
711 if (comp_keys(&leaf_key, &cpukey) <= 0) {
712 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500713 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400714 BUG_ON(1);
715 }
716 if (btrfs_item_offset_nr(leaf, slot - 1) !=
717 btrfs_item_end_nr(leaf, slot)) {
718 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500719 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400720 BUG_ON(1);
721 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500722 }
Chris Mason8d7be552007-05-10 11:24:42 -0400723 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400724 btrfs_item_key(leaf, &leaf_key, slot);
725 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
726 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
727 if (btrfs_item_offset_nr(leaf, slot) !=
728 btrfs_item_end_nr(leaf, slot + 1)) {
729 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500730 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400731 BUG_ON(1);
732 }
Chris Mason8d7be552007-05-10 11:24:42 -0400733 }
Chris Mason5f39d392007-10-15 16:14:19 -0400734 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
735 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500736 return 0;
737}
738
Chris Masond3977122009-01-05 21:25:51 -0500739static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500740 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741{
Chris Mason85d824c2008-04-10 10:23:19 -0400742 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500743 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400744 return check_leaf(root, path, level);
745 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500746}
747
Chris Mason74123bd2007-02-02 11:05:29 -0500748/*
Chris Mason5f39d392007-10-15 16:14:19 -0400749 * search for key in the extent_buffer. The items start at offset p,
750 * and they are item_size apart. There are 'max' items in p.
751 *
Chris Mason74123bd2007-02-02 11:05:29 -0500752 * the slot in the array is returned via slot, and it points to
753 * the place where you would insert key if it is not found in
754 * the array.
755 *
756 * slot may point to max if the key is bigger than all of the keys
757 */
Chris Masone02119d2008-09-05 16:13:11 -0400758static noinline int generic_bin_search(struct extent_buffer *eb,
759 unsigned long p,
760 int item_size, struct btrfs_key *key,
761 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500762{
763 int low = 0;
764 int high = max;
765 int mid;
766 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400767 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400768 struct btrfs_disk_key unaligned;
769 unsigned long offset;
770 char *map_token = NULL;
771 char *kaddr = NULL;
772 unsigned long map_start = 0;
773 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400774 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775
Chris Masond3977122009-01-05 21:25:51 -0500776 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400778 offset = p + mid * item_size;
779
780 if (!map_token || offset < map_start ||
781 (offset + sizeof(struct btrfs_disk_key)) >
782 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400783 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400784 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400785 map_token = NULL;
786 }
Chris Mason934d3752008-12-08 16:43:10 -0500787
788 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400789 sizeof(struct btrfs_disk_key),
790 &map_token, &kaddr,
791 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400792
Chris Mason479965d2007-10-15 16:14:27 -0400793 if (!err) {
794 tmp = (struct btrfs_disk_key *)(kaddr + offset -
795 map_start);
796 } else {
797 read_extent_buffer(eb, &unaligned,
798 offset, sizeof(unaligned));
799 tmp = &unaligned;
800 }
801
Chris Mason5f39d392007-10-15 16:14:19 -0400802 } else {
803 tmp = (struct btrfs_disk_key *)(kaddr + offset -
804 map_start);
805 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500806 ret = comp_keys(tmp, key);
807
808 if (ret < 0)
809 low = mid + 1;
810 else if (ret > 0)
811 high = mid;
812 else {
813 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400814 if (map_token)
815 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500816 return 0;
817 }
818 }
819 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400820 if (map_token)
821 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500822 return 1;
823}
824
Chris Mason97571fd2007-02-24 13:39:08 -0500825/*
826 * simple bin_search frontend that does the right thing for
827 * leaves vs nodes
828 */
Chris Mason5f39d392007-10-15 16:14:19 -0400829static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
830 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500831{
Chris Mason5f39d392007-10-15 16:14:19 -0400832 if (level == 0) {
833 return generic_bin_search(eb,
834 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400835 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400836 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400837 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500838 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400839 return generic_bin_search(eb,
840 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400841 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400842 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400843 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500844 }
845 return -1;
846}
847
Chris Masond352ac62008-09-29 15:18:18 -0400848/* given a node and slot number, this reads the blocks it points to. The
849 * extent buffer is returned with a reference taken (but unlocked).
850 * NULL is returned on error.
851 */
Chris Masone02119d2008-09-05 16:13:11 -0400852static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400853 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500854{
Chris Masonca7a79a2008-05-12 12:59:19 -0400855 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500856 if (slot < 0)
857 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400858 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500859 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400860
861 BUG_ON(level == 0);
862
Chris Masondb945352007-10-15 16:15:53 -0400863 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400864 btrfs_level_size(root, level - 1),
865 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500866}
867
Chris Masond352ac62008-09-29 15:18:18 -0400868/*
869 * node level balancing, used to make sure nodes are in proper order for
870 * item deletion. We balance from the top down, so we have to make sure
871 * that a deletion won't leave an node completely empty later on.
872 */
Chris Masone02119d2008-09-05 16:13:11 -0400873static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500874 struct btrfs_root *root,
875 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500876{
Chris Mason5f39d392007-10-15 16:14:19 -0400877 struct extent_buffer *right = NULL;
878 struct extent_buffer *mid;
879 struct extent_buffer *left = NULL;
880 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500881 int ret = 0;
882 int wret;
883 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500884 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400885 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500886 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500887
888 if (level == 0)
889 return 0;
890
Chris Mason5f39d392007-10-15 16:14:19 -0400891 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500892
Chris Mason925baed2008-06-25 16:01:30 -0400893 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500894 WARN_ON(btrfs_header_generation(mid) != trans->transid);
895
Chris Mason1d4f8a02007-03-13 09:28:32 -0400896 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500897
Chris Mason234b63a2007-03-13 10:46:10 -0400898 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400899 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500900 pslot = path->slots[level + 1];
901
Chris Mason40689472007-03-17 14:29:23 -0400902 /*
903 * deal with the case where there is only one pointer in the root
904 * by promoting the node below to a root
905 */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 if (!parent) {
907 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500908
Chris Mason5f39d392007-10-15 16:14:19 -0400909 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500910 return 0;
911
912 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500914 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400915 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500916 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400917 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -0500918 BUG_ON(ret);
919
Chris Mason925baed2008-06-25 16:01:30 -0400920 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500921 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400922 spin_unlock(&root->node_lock);
923
Zheng Yan31840ae2008-09-23 13:14:14 -0400924 ret = btrfs_update_extent_ref(trans, root, child->start,
Chris Mason56bec292009-03-13 10:10:06 -0400925 child->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400926 mid->start, child->start,
927 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400928 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400929 BUG_ON(ret);
930
Chris Mason0b86a832008-03-24 15:01:56 -0400931 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400932 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500933
Chris Mason925baed2008-06-25 16:01:30 -0400934 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500935 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400936 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400937 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500938 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500940 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400942 btrfs_header_generation(mid),
943 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500944 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400945 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400946 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500947 }
Chris Mason5f39d392007-10-15 16:14:19 -0400948 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400949 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500950 return 0;
951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400953 err_on_enospc = 1;
954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 left = read_node_slot(root, parent, pslot - 1);
956 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400957 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500958 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400959 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400960 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400961 if (wret) {
962 ret = wret;
963 goto enospc;
964 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400965 }
Chris Mason5f39d392007-10-15 16:14:19 -0400966 right = read_node_slot(root, parent, pslot + 1);
967 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500969 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400970 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400971 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400972 if (wret) {
973 ret = wret;
974 goto enospc;
975 }
976 }
977
978 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400979 if (left) {
980 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400981 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500982 if (wret < 0)
983 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400984 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500986 }
Chris Mason79f95c82007-03-01 15:16:26 -0500987
988 /*
989 * then try to empty the right most buffer into the middle
990 */
Chris Mason5f39d392007-10-15 16:14:19 -0400991 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400992 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400993 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500994 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400995 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400996 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500997 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400998 u32 blocksize = right->len;
999
Chris Mason5f39d392007-10-15 16:14:19 -04001000 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001001 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001002 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001003 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001004 wret = del_ptr(trans, root, path, level + 1, pslot +
1005 1);
Chris Masonbb803952007-03-01 12:04:21 -05001006 if (wret)
1007 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001008 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001009 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001010 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001011 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001012 if (wret)
1013 ret = wret;
1014 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001015 struct btrfs_disk_key right_key;
1016 btrfs_node_key(right, &right_key, 0);
1017 btrfs_set_node_key(parent, &right_key, pslot + 1);
1018 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001019 }
1020 }
Chris Mason5f39d392007-10-15 16:14:19 -04001021 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001022 /*
1023 * we're not allowed to leave a node with one item in the
1024 * tree during a delete. A deletion from lower in the tree
1025 * could try to delete the only pointer in this node.
1026 * So, pull some keys from the left.
1027 * There has to be a left pointer at this point because
1028 * otherwise we would have pulled some pointers from the
1029 * right
1030 */
Chris Mason5f39d392007-10-15 16:14:19 -04001031 BUG_ON(!left);
1032 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001033 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001034 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001035 goto enospc;
1036 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001037 if (wret == 1) {
1038 wret = push_node_left(trans, root, left, mid, 1);
1039 if (wret < 0)
1040 ret = wret;
1041 }
Chris Mason79f95c82007-03-01 15:16:26 -05001042 BUG_ON(wret == 1);
1043 }
Chris Mason5f39d392007-10-15 16:14:19 -04001044 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001045 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001046 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001047 u64 bytenr = mid->start;
1048 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001049
Chris Mason5f39d392007-10-15 16:14:19 -04001050 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001051 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001052 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001053 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001054 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001055 if (wret)
1056 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001057 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001058 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001059 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001060 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001061 if (wret)
1062 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001063 } else {
1064 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001065 struct btrfs_disk_key mid_key;
1066 btrfs_node_key(mid, &mid_key, 0);
1067 btrfs_set_node_key(parent, &mid_key, pslot);
1068 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001069 }
Chris Masonbb803952007-03-01 12:04:21 -05001070
Chris Mason79f95c82007-03-01 15:16:26 -05001071 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001072 if (left) {
1073 if (btrfs_header_nritems(left) > orig_slot) {
1074 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001075 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001076 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001077 path->slots[level + 1] -= 1;
1078 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001079 if (mid) {
1080 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001081 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001082 }
Chris Masonbb803952007-03-01 12:04:21 -05001083 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001084 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001085 path->slots[level] = orig_slot;
1086 }
1087 }
Chris Mason79f95c82007-03-01 15:16:26 -05001088 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001089 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001090 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001091 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001092 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001093enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001094 if (right) {
1095 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001096 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001097 }
1098 if (left) {
1099 if (path->nodes[level] != left)
1100 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001101 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001102 }
Chris Masonbb803952007-03-01 12:04:21 -05001103 return ret;
1104}
1105
Chris Masond352ac62008-09-29 15:18:18 -04001106/* Node balancing for insertion. Here we only split or push nodes around
1107 * when they are completely full. This is also done top down, so we
1108 * have to be pessimistic.
1109 */
Chris Masond3977122009-01-05 21:25:51 -05001110static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001111 struct btrfs_root *root,
1112 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001113{
Chris Mason5f39d392007-10-15 16:14:19 -04001114 struct extent_buffer *right = NULL;
1115 struct extent_buffer *mid;
1116 struct extent_buffer *left = NULL;
1117 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001118 int ret = 0;
1119 int wret;
1120 int pslot;
1121 int orig_slot = path->slots[level];
1122 u64 orig_ptr;
1123
1124 if (level == 0)
1125 return 1;
1126
Chris Mason5f39d392007-10-15 16:14:19 -04001127 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001128 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001129 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1130
1131 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001132 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001133 pslot = path->slots[level + 1];
1134
Chris Mason5f39d392007-10-15 16:14:19 -04001135 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001136 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001137
Chris Mason5f39d392007-10-15 16:14:19 -04001138 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001139
1140 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001141 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001142 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001143
1144 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001145 btrfs_set_lock_blocking(left);
1146
Chris Mason5f39d392007-10-15 16:14:19 -04001147 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001148 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1149 wret = 1;
1150 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001151 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001152 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001153 if (ret)
1154 wret = 1;
1155 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001156 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001157 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001158 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001159 }
Chris Masone66f7092007-04-20 13:16:02 -04001160 if (wret < 0)
1161 ret = wret;
1162 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001163 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001164 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001165 btrfs_node_key(mid, &disk_key, 0);
1166 btrfs_set_node_key(parent, &disk_key, pslot);
1167 btrfs_mark_buffer_dirty(parent);
1168 if (btrfs_header_nritems(left) > orig_slot) {
1169 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001170 path->slots[level + 1] -= 1;
1171 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001172 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001173 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001174 } else {
1175 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001176 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001177 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001178 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001179 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001180 }
Chris Masone66f7092007-04-20 13:16:02 -04001181 return 0;
1182 }
Chris Mason925baed2008-06-25 16:01:30 -04001183 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001185 }
Chris Mason925baed2008-06-25 16:01:30 -04001186 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001187
1188 /*
1189 * then try to empty the right most buffer into the middle
1190 */
Chris Mason5f39d392007-10-15 16:14:19 -04001191 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001192 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001193
Chris Mason925baed2008-06-25 16:01:30 -04001194 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001195 btrfs_set_lock_blocking(right);
1196
Chris Mason5f39d392007-10-15 16:14:19 -04001197 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001198 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1199 wret = 1;
1200 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001201 ret = btrfs_cow_block(trans, root, right,
1202 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001203 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001204 if (ret)
1205 wret = 1;
1206 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001207 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001208 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001209 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001210 }
Chris Masone66f7092007-04-20 13:16:02 -04001211 if (wret < 0)
1212 ret = wret;
1213 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001214 struct btrfs_disk_key disk_key;
1215
1216 btrfs_node_key(right, &disk_key, 0);
1217 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1218 btrfs_mark_buffer_dirty(parent);
1219
1220 if (btrfs_header_nritems(mid) <= orig_slot) {
1221 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001222 path->slots[level + 1] += 1;
1223 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001224 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001225 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001226 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001227 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001228 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001229 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001230 }
Chris Masone66f7092007-04-20 13:16:02 -04001231 return 0;
1232 }
Chris Mason925baed2008-06-25 16:01:30 -04001233 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001234 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001235 }
Chris Masone66f7092007-04-20 13:16:02 -04001236 return 1;
1237}
1238
Chris Mason74123bd2007-02-02 11:05:29 -05001239/*
Chris Masond352ac62008-09-29 15:18:18 -04001240 * readahead one full node of leaves, finding things that are close
1241 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001242 */
Chris Masone02119d2008-09-05 16:13:11 -04001243static noinline void reada_for_search(struct btrfs_root *root,
1244 struct btrfs_path *path,
1245 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001246{
Chris Mason5f39d392007-10-15 16:14:19 -04001247 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001248 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001249 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001250 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001251 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001252 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001253 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001254 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001255 u32 nr;
1256 u32 blocksize;
1257 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001258
Chris Masona6b6e752007-10-15 16:22:39 -04001259 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001260 return;
1261
Chris Mason6702ed42007-08-07 16:15:09 -04001262 if (!path->nodes[level])
1263 return;
1264
Chris Mason5f39d392007-10-15 16:14:19 -04001265 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001266
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001268 blocksize = btrfs_level_size(root, level - 1);
1269 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001270 if (eb) {
1271 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001272 return;
1273 }
1274
Chris Masona7175312009-01-22 09:23:10 -05001275 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001276
Chris Mason5f39d392007-10-15 16:14:19 -04001277 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001278 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001279 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001280 if (direction < 0) {
1281 if (nr == 0)
1282 break;
1283 nr--;
1284 } else if (direction > 0) {
1285 nr++;
1286 if (nr >= nritems)
1287 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001288 }
Chris Mason01f46652007-12-21 16:24:26 -05001289 if (path->reada < 0 && objectid) {
1290 btrfs_node_key(node, &disk_key, nr);
1291 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1292 break;
1293 }
Chris Mason6b800532007-10-15 16:17:34 -04001294 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001295 if ((search <= target && target - search <= 65536) ||
1296 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001297 readahead_tree_block(root, search, blocksize,
1298 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001299 nread += blocksize;
1300 }
1301 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001302 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001303 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001304 }
1305}
Chris Mason925baed2008-06-25 16:01:30 -04001306
Chris Masond352ac62008-09-29 15:18:18 -04001307/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001308 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1309 * cache
1310 */
1311static noinline int reada_for_balance(struct btrfs_root *root,
1312 struct btrfs_path *path, int level)
1313{
1314 int slot;
1315 int nritems;
1316 struct extent_buffer *parent;
1317 struct extent_buffer *eb;
1318 u64 gen;
1319 u64 block1 = 0;
1320 u64 block2 = 0;
1321 int ret = 0;
1322 int blocksize;
1323
1324 parent = path->nodes[level - 1];
1325 if (!parent)
1326 return 0;
1327
1328 nritems = btrfs_header_nritems(parent);
1329 slot = path->slots[level];
1330 blocksize = btrfs_level_size(root, level);
1331
1332 if (slot > 0) {
1333 block1 = btrfs_node_blockptr(parent, slot - 1);
1334 gen = btrfs_node_ptr_generation(parent, slot - 1);
1335 eb = btrfs_find_tree_block(root, block1, blocksize);
1336 if (eb && btrfs_buffer_uptodate(eb, gen))
1337 block1 = 0;
1338 free_extent_buffer(eb);
1339 }
1340 if (slot < nritems) {
1341 block2 = btrfs_node_blockptr(parent, slot + 1);
1342 gen = btrfs_node_ptr_generation(parent, slot + 1);
1343 eb = btrfs_find_tree_block(root, block2, blocksize);
1344 if (eb && btrfs_buffer_uptodate(eb, gen))
1345 block2 = 0;
1346 free_extent_buffer(eb);
1347 }
1348 if (block1 || block2) {
1349 ret = -EAGAIN;
1350 btrfs_release_path(root, path);
1351 if (block1)
1352 readahead_tree_block(root, block1, blocksize, 0);
1353 if (block2)
1354 readahead_tree_block(root, block2, blocksize, 0);
1355
1356 if (block1) {
1357 eb = read_tree_block(root, block1, blocksize, 0);
1358 free_extent_buffer(eb);
1359 }
1360 if (block1) {
1361 eb = read_tree_block(root, block2, blocksize, 0);
1362 free_extent_buffer(eb);
1363 }
1364 }
1365 return ret;
1366}
1367
1368
1369/*
Chris Masond3977122009-01-05 21:25:51 -05001370 * when we walk down the tree, it is usually safe to unlock the higher layers
1371 * in the tree. The exceptions are when our path goes through slot 0, because
1372 * operations on the tree might require changing key pointers higher up in the
1373 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001374 *
Chris Masond3977122009-01-05 21:25:51 -05001375 * callers might also have set path->keep_locks, which tells this code to keep
1376 * the lock if the path points to the last slot in the block. This is part of
1377 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001378 *
Chris Masond3977122009-01-05 21:25:51 -05001379 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1380 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001381 */
Chris Masone02119d2008-09-05 16:13:11 -04001382static noinline void unlock_up(struct btrfs_path *path, int level,
1383 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001384{
1385 int i;
1386 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001387 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001388 struct extent_buffer *t;
1389
1390 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1391 if (!path->nodes[i])
1392 break;
1393 if (!path->locks[i])
1394 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001395 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001396 skip_level = i + 1;
1397 continue;
1398 }
Chris Mason051e1b92008-06-25 16:01:30 -04001399 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001400 u32 nritems;
1401 t = path->nodes[i];
1402 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001404 skip_level = i + 1;
1405 continue;
1406 }
1407 }
Chris Mason051e1b92008-06-25 16:01:30 -04001408 if (skip_level < i && i >= lowest_unlock)
1409 no_skips = 1;
1410
Chris Mason925baed2008-06-25 16:01:30 -04001411 t = path->nodes[i];
1412 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1413 btrfs_tree_unlock(t);
1414 path->locks[i] = 0;
1415 }
1416 }
1417}
1418
Chris Mason3c69fae2007-08-07 15:52:22 -04001419/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001420 * This releases any locks held in the path starting at level and
1421 * going all the way up to the root.
1422 *
1423 * btrfs_search_slot will keep the lock held on higher nodes in a few
1424 * corner cases, such as COW of the block at slot zero in the node. This
1425 * ignores those rules, and it should only be called when there are no
1426 * more updates to be done higher up in the tree.
1427 */
1428noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1429{
1430 int i;
1431
1432 if (path->keep_locks || path->lowest_level)
1433 return;
1434
1435 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1436 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001437 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001438 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001439 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001440 btrfs_tree_unlock(path->nodes[i]);
1441 path->locks[i] = 0;
1442 }
1443}
1444
1445/*
Chris Mason74123bd2007-02-02 11:05:29 -05001446 * look for key in the tree. path is filled in with nodes along the way
1447 * if key is found, we return zero and you can find the item in the leaf
1448 * level of the path (level 0)
1449 *
1450 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001451 * be inserted, and 1 is returned. If there are other errors during the
1452 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001453 *
1454 * if ins_len > 0, nodes and leaves will be split as we walk down the
1455 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1456 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001457 */
Chris Masone089f052007-03-16 16:20:31 -04001458int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1459 *root, struct btrfs_key *key, struct btrfs_path *p, int
1460 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001461{
Chris Mason5f39d392007-10-15 16:14:19 -04001462 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001463 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001464 int slot;
1465 int ret;
1466 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001467 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001468 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001469 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001470 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001471 u64 blocknr;
1472 u64 gen;
Chris Mason9f3a7422007-08-07 15:52:19 -04001473
Chris Mason6702ed42007-08-07 16:15:09 -04001474 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001475 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001476 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001477
Chris Mason925baed2008-06-25 16:01:30 -04001478 if (ins_len < 0)
1479 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001480
Chris Masonbb803952007-03-01 12:04:21 -05001481again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001482 if (p->skip_locking)
1483 b = btrfs_root_node(root);
1484 else
1485 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001486
Chris Masoneb60cea2007-02-02 09:18:22 -05001487 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001488 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001489
1490 /*
1491 * setup the path here so we can release it under lock
1492 * contention with the cow code
1493 */
1494 p->nodes[level] = b;
1495 if (!p->skip_locking)
1496 p->locks[level] = 1;
1497
Chris Mason02217ed2007-03-02 16:08:05 -05001498 if (cow) {
1499 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001500
1501 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001502 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001503 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001504 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001505 goto cow_done;
1506 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001507 btrfs_set_path_blocking(p);
1508
Chris Masone20d96d2007-03-22 12:13:20 -04001509 wret = btrfs_cow_block(trans, root, b,
1510 p->nodes[level + 1],
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001511 p->slots[level + 1], &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001512 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001513 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001514 ret = wret;
1515 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001516 }
Chris Mason02217ed2007-03-02 16:08:05 -05001517 }
Chris Mason65b51a02008-08-01 15:11:20 -04001518cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001519 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001520 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001521 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001522 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001523
Chris Masoneb60cea2007-02-02 09:18:22 -05001524 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001525 if (!p->skip_locking)
1526 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001527
Chris Mason4008c042009-02-12 14:09:45 -05001528 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001529
1530 /*
1531 * we have a lock on b and as long as we aren't changing
1532 * the tree, there is no way to for the items in b to change.
1533 * It is safe to drop the lock on our parent before we
1534 * go through the expensive btree search on b.
1535 *
1536 * If cow is true, then we might be changing slot zero,
1537 * which may require changing the parent. So, we can't
1538 * drop the lock until after we know which slot we're
1539 * operating on.
1540 */
1541 if (!cow)
1542 btrfs_unlock_up_safe(p, level + 1);
1543
Chris Mason123abc82007-03-14 14:14:43 -04001544 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001545 if (ret) {
1546 ret = -1;
1547 goto done;
1548 }
Chris Mason925baed2008-06-25 16:01:30 -04001549
Chris Mason5f39d392007-10-15 16:14:19 -04001550 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001551
Chris Mason5f39d392007-10-15 16:14:19 -04001552 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001553 if (ret && slot > 0)
1554 slot -= 1;
1555 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001556 if ((p->search_for_split || ins_len > 0) &&
1557 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001558 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001559 int sret;
1560
1561 sret = reada_for_balance(root, p, level);
1562 if (sret)
1563 goto again;
1564
1565 btrfs_set_path_blocking(p);
1566 sret = split_node(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001567 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001568
Chris Mason5c680ed2007-02-22 11:39:13 -05001569 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001570 if (sret) {
1571 ret = sret;
1572 goto done;
1573 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001574 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001575 slot = p->slots[level];
Chris Mason7b78c172009-02-04 09:12:46 -05001576 } else if (ins_len < 0 &&
1577 btrfs_header_nritems(b) <
1578 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001579 int sret;
1580
1581 sret = reada_for_balance(root, p, level);
1582 if (sret)
1583 goto again;
1584
1585 btrfs_set_path_blocking(p);
1586 sret = balance_level(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001587 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001588
Chris Mason65b51a02008-08-01 15:11:20 -04001589 if (sret) {
1590 ret = sret;
1591 goto done;
1592 }
Chris Masonbb803952007-03-01 12:04:21 -05001593 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001594 if (!b) {
1595 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001596 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001597 }
Chris Masonbb803952007-03-01 12:04:21 -05001598 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001599 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001600 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001601 unlock_up(p, level, lowest_unlock);
1602
Chris Mason9f3a7422007-08-07 15:52:19 -04001603 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001604 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001605 ret = 0;
1606 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001607 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001608
Chris Mason594a24e2008-06-25 16:01:30 -04001609 blocknr = btrfs_node_blockptr(b, slot);
1610 gen = btrfs_node_ptr_generation(b, slot);
1611 blocksize = btrfs_level_size(root, level - 1);
1612
1613 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1614 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001615 b = tmp;
1616 } else {
1617 /*
1618 * reduce lock contention at high levels
1619 * of the btree by dropping locks before
1620 * we read.
1621 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001622 if (level > 0) {
Chris Mason051e1b92008-06-25 16:01:30 -04001623 btrfs_release_path(NULL, p);
1624 if (tmp)
1625 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001626 if (should_reada)
1627 reada_for_search(root, p,
1628 level, slot,
1629 key->objectid);
1630
Chris Mason594a24e2008-06-25 16:01:30 -04001631 tmp = read_tree_block(root, blocknr,
1632 blocksize, gen);
1633 if (tmp)
1634 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001635 goto again;
1636 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001637 btrfs_set_path_blocking(p);
Chris Masona74a4b92008-06-25 16:01:31 -04001638 if (tmp)
1639 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001640 if (should_reada)
1641 reada_for_search(root, p,
1642 level, slot,
1643 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001644 b = read_node_slot(root, b, slot);
1645 }
1646 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001647 if (!p->skip_locking) {
1648 int lret;
1649
Chris Mason4008c042009-02-12 14:09:45 -05001650 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001651 lret = btrfs_try_spin_lock(b);
1652
1653 if (!lret) {
1654 btrfs_set_path_blocking(p);
1655 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001656 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001657 }
1658 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001659 } else {
1660 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001661 if (ins_len > 0 &&
1662 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001663 int sret;
1664
1665 btrfs_set_path_blocking(p);
1666 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001667 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001668 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001669
Chris Mason5c680ed2007-02-22 11:39:13 -05001670 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001671 if (sret) {
1672 ret = sret;
1673 goto done;
1674 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001675 }
Chris Mason459931e2008-12-10 09:10:46 -05001676 if (!p->search_for_split)
1677 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001678 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001679 }
1680 }
Chris Mason65b51a02008-08-01 15:11:20 -04001681 ret = 1;
1682done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001683 /*
1684 * we don't really know what they plan on doing with the path
1685 * from here on, so for now just mark it as blocking
1686 */
1687 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001688 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001689}
1690
Zheng Yan1a40e232008-09-26 10:09:34 -04001691int btrfs_merge_path(struct btrfs_trans_handle *trans,
1692 struct btrfs_root *root,
1693 struct btrfs_key *node_keys,
1694 u64 *nodes, int lowest_level)
1695{
1696 struct extent_buffer *eb;
1697 struct extent_buffer *parent;
1698 struct btrfs_key key;
1699 u64 bytenr;
1700 u64 generation;
1701 u32 blocksize;
1702 int level;
1703 int slot;
1704 int key_match;
1705 int ret;
1706
1707 eb = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001708 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001709 BUG_ON(ret);
1710
Chris Masonb4ce94d2009-02-04 09:25:08 -05001711 btrfs_set_lock_blocking(eb);
1712
Zheng Yan1a40e232008-09-26 10:09:34 -04001713 parent = eb;
1714 while (1) {
1715 level = btrfs_header_level(parent);
1716 if (level == 0 || level <= lowest_level)
1717 break;
1718
1719 ret = bin_search(parent, &node_keys[lowest_level], level,
1720 &slot);
1721 if (ret && slot > 0)
1722 slot--;
1723
1724 bytenr = btrfs_node_blockptr(parent, slot);
1725 if (nodes[level - 1] == bytenr)
1726 break;
1727
1728 blocksize = btrfs_level_size(root, level - 1);
1729 generation = btrfs_node_ptr_generation(parent, slot);
1730 btrfs_node_key_to_cpu(eb, &key, slot);
1731 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1732
Yan Zhengf82d02d2008-10-29 14:49:05 -04001733 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001734 eb = read_tree_block(root, bytenr, blocksize,
1735 generation);
1736 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001737 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001738 }
1739
1740 /*
1741 * if node keys match and node pointer hasn't been modified
1742 * in the running transaction, we can merge the path. for
1743 * blocks owened by reloc trees, the node pointer check is
1744 * skipped, this is because these blocks are fully controlled
1745 * by the space balance code, no one else can modify them.
1746 */
1747 if (!nodes[level - 1] || !key_match ||
1748 (generation == trans->transid &&
1749 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1750 if (level == 1 || level == lowest_level + 1) {
1751 if (generation == trans->transid) {
1752 btrfs_tree_unlock(eb);
1753 free_extent_buffer(eb);
1754 }
1755 break;
1756 }
1757
1758 if (generation != trans->transid) {
1759 eb = read_tree_block(root, bytenr, blocksize,
1760 generation);
1761 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001762 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001763 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001764
1765 ret = btrfs_cow_block(trans, root, eb, parent, slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001766 &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001767 BUG_ON(ret);
1768
Yan Zhengf82d02d2008-10-29 14:49:05 -04001769 if (root->root_key.objectid ==
1770 BTRFS_TREE_RELOC_OBJECTID) {
1771 if (!nodes[level - 1]) {
1772 nodes[level - 1] = eb->start;
1773 memcpy(&node_keys[level - 1], &key,
1774 sizeof(node_keys[0]));
1775 } else {
1776 WARN_ON(1);
1777 }
1778 }
1779
Zheng Yan1a40e232008-09-26 10:09:34 -04001780 btrfs_tree_unlock(parent);
1781 free_extent_buffer(parent);
1782 parent = eb;
1783 continue;
1784 }
1785
Zheng Yan1a40e232008-09-26 10:09:34 -04001786 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1787 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1788 btrfs_mark_buffer_dirty(parent);
1789
1790 ret = btrfs_inc_extent_ref(trans, root,
1791 nodes[level - 1],
1792 blocksize, parent->start,
1793 btrfs_header_owner(parent),
1794 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001795 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001796 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001797
1798 /*
1799 * If the block was created in the running transaction,
1800 * it's possible this is the last reference to it, so we
1801 * should drop the subtree.
1802 */
1803 if (generation == trans->transid) {
1804 ret = btrfs_drop_subtree(trans, root, eb, parent);
1805 BUG_ON(ret);
1806 btrfs_tree_unlock(eb);
1807 free_extent_buffer(eb);
1808 } else {
1809 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001810 blocksize, parent->start,
1811 btrfs_header_owner(parent),
1812 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001813 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001814 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001815 }
1816 break;
1817 }
1818 btrfs_tree_unlock(parent);
1819 free_extent_buffer(parent);
1820 return 0;
1821}
1822
Chris Mason74123bd2007-02-02 11:05:29 -05001823/*
1824 * adjust the pointers going up the tree, starting at level
1825 * making sure the right key of each node is points to 'key'.
1826 * This is used after shifting pointers to the left, so it stops
1827 * fixing up pointers when a given leaf/node is not in slot 0 of the
1828 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001829 *
1830 * If this fails to write a tree block, it returns -1, but continues
1831 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001832 */
Chris Mason5f39d392007-10-15 16:14:19 -04001833static int fixup_low_keys(struct btrfs_trans_handle *trans,
1834 struct btrfs_root *root, struct btrfs_path *path,
1835 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836{
1837 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001838 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001839 struct extent_buffer *t;
1840
Chris Mason234b63a2007-03-13 10:46:10 -04001841 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001842 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001843 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001844 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001845 t = path->nodes[i];
1846 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001847 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001848 if (tslot != 0)
1849 break;
1850 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001851 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001852}
1853
Chris Mason74123bd2007-02-02 11:05:29 -05001854/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001855 * update item key.
1856 *
1857 * This function isn't completely safe. It's the caller's responsibility
1858 * that the new key won't break the order
1859 */
1860int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1861 struct btrfs_root *root, struct btrfs_path *path,
1862 struct btrfs_key *new_key)
1863{
1864 struct btrfs_disk_key disk_key;
1865 struct extent_buffer *eb;
1866 int slot;
1867
1868 eb = path->nodes[0];
1869 slot = path->slots[0];
1870 if (slot > 0) {
1871 btrfs_item_key(eb, &disk_key, slot - 1);
1872 if (comp_keys(&disk_key, new_key) >= 0)
1873 return -1;
1874 }
1875 if (slot < btrfs_header_nritems(eb) - 1) {
1876 btrfs_item_key(eb, &disk_key, slot + 1);
1877 if (comp_keys(&disk_key, new_key) <= 0)
1878 return -1;
1879 }
1880
1881 btrfs_cpu_key_to_disk(&disk_key, new_key);
1882 btrfs_set_item_key(eb, &disk_key, slot);
1883 btrfs_mark_buffer_dirty(eb);
1884 if (slot == 0)
1885 fixup_low_keys(trans, root, path, &disk_key, 1);
1886 return 0;
1887}
1888
1889/*
Chris Mason74123bd2007-02-02 11:05:29 -05001890 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001891 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001892 *
1893 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1894 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001895 */
Chris Mason98ed5172008-01-03 10:01:48 -05001896static int push_node_left(struct btrfs_trans_handle *trans,
1897 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001898 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001899{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001900 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001901 int src_nritems;
1902 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001903 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001904
Chris Mason5f39d392007-10-15 16:14:19 -04001905 src_nritems = btrfs_header_nritems(src);
1906 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001907 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001908 WARN_ON(btrfs_header_generation(src) != trans->transid);
1909 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001910
Chris Masonbce4eae2008-04-24 14:42:46 -04001911 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001912 return 1;
1913
Chris Masond3977122009-01-05 21:25:51 -05001914 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001915 return 1;
1916
Chris Masonbce4eae2008-04-24 14:42:46 -04001917 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001918 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001919 if (push_items < src_nritems) {
1920 /* leave at least 8 pointers in the node if
1921 * we aren't going to empty it
1922 */
1923 if (src_nritems - push_items < 8) {
1924 if (push_items <= 8)
1925 return 1;
1926 push_items -= 8;
1927 }
1928 }
1929 } else
1930 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001931
Chris Mason5f39d392007-10-15 16:14:19 -04001932 copy_extent_buffer(dst, src,
1933 btrfs_node_key_ptr_offset(dst_nritems),
1934 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001935 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001936
Chris Masonbb803952007-03-01 12:04:21 -05001937 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001938 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1939 btrfs_node_key_ptr_offset(push_items),
1940 (src_nritems - push_items) *
1941 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001942 }
Chris Mason5f39d392007-10-15 16:14:19 -04001943 btrfs_set_header_nritems(src, src_nritems - push_items);
1944 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1945 btrfs_mark_buffer_dirty(src);
1946 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001947
1948 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1949 BUG_ON(ret);
1950
Chris Masonbb803952007-03-01 12:04:21 -05001951 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001952}
1953
Chris Mason97571fd2007-02-24 13:39:08 -05001954/*
Chris Mason79f95c82007-03-01 15:16:26 -05001955 * try to push data from one node into the next node right in the
1956 * tree.
1957 *
1958 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1959 * error, and > 0 if there was no room in the right hand block.
1960 *
1961 * this will only push up to 1/2 the contents of the left node over
1962 */
Chris Mason5f39d392007-10-15 16:14:19 -04001963static int balance_node_right(struct btrfs_trans_handle *trans,
1964 struct btrfs_root *root,
1965 struct extent_buffer *dst,
1966 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001967{
Chris Mason79f95c82007-03-01 15:16:26 -05001968 int push_items = 0;
1969 int max_push;
1970 int src_nritems;
1971 int dst_nritems;
1972 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001973
Chris Mason7bb86312007-12-11 09:25:06 -05001974 WARN_ON(btrfs_header_generation(src) != trans->transid);
1975 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1976
Chris Mason5f39d392007-10-15 16:14:19 -04001977 src_nritems = btrfs_header_nritems(src);
1978 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001979 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001980 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001981 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001982
Chris Masond3977122009-01-05 21:25:51 -05001983 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001984 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001985
1986 max_push = src_nritems / 2 + 1;
1987 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001988 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001989 return 1;
Yan252c38f2007-08-29 09:11:44 -04001990
Chris Mason79f95c82007-03-01 15:16:26 -05001991 if (max_push < push_items)
1992 push_items = max_push;
1993
Chris Mason5f39d392007-10-15 16:14:19 -04001994 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1995 btrfs_node_key_ptr_offset(0),
1996 (dst_nritems) *
1997 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001998
Chris Mason5f39d392007-10-15 16:14:19 -04001999 copy_extent_buffer(dst, src,
2000 btrfs_node_key_ptr_offset(0),
2001 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002002 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002003
Chris Mason5f39d392007-10-15 16:14:19 -04002004 btrfs_set_header_nritems(src, src_nritems - push_items);
2005 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002006
Chris Mason5f39d392007-10-15 16:14:19 -04002007 btrfs_mark_buffer_dirty(src);
2008 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002009
2010 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2011 BUG_ON(ret);
2012
Chris Mason79f95c82007-03-01 15:16:26 -05002013 return ret;
2014}
2015
2016/*
Chris Mason97571fd2007-02-24 13:39:08 -05002017 * helper function to insert a new root level in the tree.
2018 * A new node is allocated, and a single item is inserted to
2019 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002020 *
2021 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002022 */
Chris Masond3977122009-01-05 21:25:51 -05002023static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002024 struct btrfs_root *root,
2025 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002026{
Chris Mason7bb86312007-12-11 09:25:06 -05002027 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002028 struct extent_buffer *lower;
2029 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002030 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002031 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002032 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002033
2034 BUG_ON(path->nodes[level]);
2035 BUG_ON(path->nodes[level-1] != root->node);
2036
Chris Mason7bb86312007-12-11 09:25:06 -05002037 lower = path->nodes[level-1];
2038 if (level == 1)
2039 btrfs_item_key(lower, &lower_key, 0);
2040 else
2041 btrfs_node_key(lower, &lower_key, 0);
2042
Zheng Yan31840ae2008-09-23 13:14:14 -04002043 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2044 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002045 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002046 if (IS_ERR(c))
2047 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002048
Chris Mason5f39d392007-10-15 16:14:19 -04002049 memset_extent_buffer(c, 0, 0, root->nodesize);
2050 btrfs_set_header_nritems(c, 1);
2051 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002052 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_set_header_generation(c, trans->transid);
2054 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002055
Chris Mason5f39d392007-10-15 16:14:19 -04002056 write_extent_buffer(c, root->fs_info->fsid,
2057 (unsigned long)btrfs_header_fsid(c),
2058 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002059
2060 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2061 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2062 BTRFS_UUID_SIZE);
2063
Chris Mason5f39d392007-10-15 16:14:19 -04002064 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002065 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002066 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002067 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002068
2069 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002070
2071 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002072
Chris Mason925baed2008-06-25 16:01:30 -04002073 spin_lock(&root->node_lock);
2074 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002075 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002076 spin_unlock(&root->node_lock);
2077
Zheng Yan31840ae2008-09-23 13:14:14 -04002078 ret = btrfs_update_extent_ref(trans, root, lower->start,
Chris Mason56bec292009-03-13 10:10:06 -04002079 lower->len, lower->start, c->start,
Zheng Yan31840ae2008-09-23 13:14:14 -04002080 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002081 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002082 BUG_ON(ret);
2083
Chris Mason925baed2008-06-25 16:01:30 -04002084 /* the super has an extra ref to root->node */
2085 free_extent_buffer(old);
2086
Chris Mason0b86a832008-03-24 15:01:56 -04002087 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 extent_buffer_get(c);
2089 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002090 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002091 path->slots[level] = 0;
2092 return 0;
2093}
2094
Chris Mason74123bd2007-02-02 11:05:29 -05002095/*
2096 * worker function to insert a single pointer in a node.
2097 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002098 *
Chris Mason74123bd2007-02-02 11:05:29 -05002099 * slot and level indicate where you want the key to go, and
2100 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002101 *
2102 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002103 */
Chris Masone089f052007-03-16 16:20:31 -04002104static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2105 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002106 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002107{
Chris Mason5f39d392007-10-15 16:14:19 -04002108 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002109 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002110
2111 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002112 lower = path->nodes[level];
2113 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002114 if (slot > nritems)
2115 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002116 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002117 BUG();
2118 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002119 memmove_extent_buffer(lower,
2120 btrfs_node_key_ptr_offset(slot + 1),
2121 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002122 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002123 }
Chris Mason5f39d392007-10-15 16:14:19 -04002124 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002125 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002126 WARN_ON(trans->transid == 0);
2127 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002128 btrfs_set_header_nritems(lower, nritems + 1);
2129 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002130 return 0;
2131}
2132
Chris Mason97571fd2007-02-24 13:39:08 -05002133/*
2134 * split the node at the specified level in path in two.
2135 * The path is corrected to point to the appropriate node after the split
2136 *
2137 * Before splitting this tries to make some room in the node by pushing
2138 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002139 *
2140 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002141 */
Chris Masone02119d2008-09-05 16:13:11 -04002142static noinline int split_node(struct btrfs_trans_handle *trans,
2143 struct btrfs_root *root,
2144 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002145{
Chris Mason5f39d392007-10-15 16:14:19 -04002146 struct extent_buffer *c;
2147 struct extent_buffer *split;
2148 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002149 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002150 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002151 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002152 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002153
Chris Mason5f39d392007-10-15 16:14:19 -04002154 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002155 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002156 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002157 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002158 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002159 if (ret)
2160 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002161 } else {
2162 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002163 c = path->nodes[level];
2164 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002165 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002166 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002167 if (ret < 0)
2168 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169 }
Chris Masone66f7092007-04-20 13:16:02 -04002170
Chris Mason5f39d392007-10-15 16:14:19 -04002171 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002172
Chris Mason925baed2008-06-25 16:01:30 -04002173 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002174 path->nodes[level + 1]->start,
2175 root->root_key.objectid,
2176 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002177 if (IS_ERR(split))
2178 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002179
Chris Mason5f39d392007-10-15 16:14:19 -04002180 btrfs_set_header_flags(split, btrfs_header_flags(c));
2181 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002182 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002183 btrfs_set_header_generation(split, trans->transid);
2184 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002185 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002186 write_extent_buffer(split, root->fs_info->fsid,
2187 (unsigned long)btrfs_header_fsid(split),
2188 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002189 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2190 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2191 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002192
Chris Mason7518a232007-03-12 12:01:18 -04002193 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002194
2195 copy_extent_buffer(split, c,
2196 btrfs_node_key_ptr_offset(0),
2197 btrfs_node_key_ptr_offset(mid),
2198 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2199 btrfs_set_header_nritems(split, c_nritems - mid);
2200 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002201 ret = 0;
2202
Chris Mason5f39d392007-10-15 16:14:19 -04002203 btrfs_mark_buffer_dirty(c);
2204 btrfs_mark_buffer_dirty(split);
2205
2206 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002207 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002208 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002209 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002210 if (wret)
2211 ret = wret;
2212
Zheng Yan31840ae2008-09-23 13:14:14 -04002213 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2214 BUG_ON(ret);
2215
Chris Mason5de08d72007-02-24 06:24:44 -05002216 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002217 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002218 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002219 free_extent_buffer(c);
2220 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002221 path->slots[level + 1] += 1;
2222 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002223 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002224 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002225 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002226 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002227}
2228
Chris Mason74123bd2007-02-02 11:05:29 -05002229/*
2230 * how many bytes are required to store the items in a leaf. start
2231 * and nr indicate which items in the leaf to check. This totals up the
2232 * space used both by the item structs and the item data
2233 */
Chris Mason5f39d392007-10-15 16:14:19 -04002234static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002235{
2236 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002237 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002238 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002239
2240 if (!nr)
2241 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002242 data_len = btrfs_item_end_nr(l, start);
2243 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002244 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002245 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002246 return data_len;
2247}
2248
Chris Mason74123bd2007-02-02 11:05:29 -05002249/*
Chris Masond4dbff92007-04-04 14:08:15 -04002250 * The space between the end of the leaf items and
2251 * the start of the leaf data. IOW, how much room
2252 * the leaf has left for both items and data
2253 */
Chris Masond3977122009-01-05 21:25:51 -05002254noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002255 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002256{
Chris Mason5f39d392007-10-15 16:14:19 -04002257 int nritems = btrfs_header_nritems(leaf);
2258 int ret;
2259 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2260 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002261 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2262 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002263 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002264 leaf_space_used(leaf, 0, nritems), nritems);
2265 }
2266 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002267}
2268
2269/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002270 * push some data in the path leaf to the right, trying to free up at
2271 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002272 *
2273 * returns 1 if the push failed because the other node didn't have enough
2274 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002275 */
Chris Masone089f052007-03-16 16:20:31 -04002276static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002277 *root, struct btrfs_path *path, int data_size,
2278 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002279{
Chris Mason5f39d392007-10-15 16:14:19 -04002280 struct extent_buffer *left = path->nodes[0];
2281 struct extent_buffer *right;
2282 struct extent_buffer *upper;
2283 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002284 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002285 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002286 int free_space;
2287 int push_space = 0;
2288 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002289 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002290 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002291 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002292 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002293 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002294 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002295 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002296
2297 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002298 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002299 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002300
Chris Mason00ec4c52007-02-24 12:47:20 -05002301 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002302 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002303 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002304
Chris Masonb9447ef82009-03-09 11:45:38 -04002305 btrfs_assert_tree_locked(path->nodes[1]);
Chris Masona2135012008-06-25 16:01:30 -04002306
Chris Masonca7a79a2008-05-12 12:59:19 -04002307 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002308 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002309 btrfs_set_lock_blocking(right);
2310
Chris Mason123abc82007-03-14 14:14:43 -04002311 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002312 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002313 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002314
Chris Mason5f39d392007-10-15 16:14:19 -04002315 /* cow and double check */
2316 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002317 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04002318 if (ret)
2319 goto out_unlock;
2320
Chris Mason5f39d392007-10-15 16:14:19 -04002321 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002322 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002323 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002324
2325 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002326 if (left_nritems == 0)
2327 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002328
Chris Mason34a38212007-11-07 13:31:03 -05002329 if (empty)
2330 nr = 0;
2331 else
2332 nr = 1;
2333
Zheng Yan31840ae2008-09-23 13:14:14 -04002334 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002335 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002336
Chris Mason34a38212007-11-07 13:31:03 -05002337 i = left_nritems - 1;
2338 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002339 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002340
Zheng Yan31840ae2008-09-23 13:14:14 -04002341 if (!empty && push_items > 0) {
2342 if (path->slots[0] > i)
2343 break;
2344 if (path->slots[0] == i) {
2345 int space = btrfs_leaf_free_space(root, left);
2346 if (space + push_space * 2 > free_space)
2347 break;
2348 }
2349 }
2350
Chris Mason00ec4c52007-02-24 12:47:20 -05002351 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002352 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002353
2354 if (!left->map_token) {
2355 map_extent_buffer(left, (unsigned long)item,
2356 sizeof(struct btrfs_item),
2357 &left->map_token, &left->kaddr,
2358 &left->map_start, &left->map_len,
2359 KM_USER1);
2360 }
2361
2362 this_item_size = btrfs_item_size(left, item);
2363 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002364 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002365
Chris Mason00ec4c52007-02-24 12:47:20 -05002366 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002367 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002368 if (i == 0)
2369 break;
2370 i--;
Chris Masondb945352007-10-15 16:15:53 -04002371 }
2372 if (left->map_token) {
2373 unmap_extent_buffer(left, left->map_token, KM_USER1);
2374 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002375 }
Chris Mason5f39d392007-10-15 16:14:19 -04002376
Chris Mason925baed2008-06-25 16:01:30 -04002377 if (push_items == 0)
2378 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002379
Chris Mason34a38212007-11-07 13:31:03 -05002380 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002381 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002382
Chris Mason00ec4c52007-02-24 12:47:20 -05002383 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002384 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002385
Chris Mason5f39d392007-10-15 16:14:19 -04002386 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002387 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002388
Chris Mason00ec4c52007-02-24 12:47:20 -05002389 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002390 data_end = leaf_data_end(root, right);
2391 memmove_extent_buffer(right,
2392 btrfs_leaf_data(right) + data_end - push_space,
2393 btrfs_leaf_data(right) + data_end,
2394 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2395
Chris Mason00ec4c52007-02-24 12:47:20 -05002396 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002397 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002398 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2399 btrfs_leaf_data(left) + leaf_data_end(root, left),
2400 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002401
2402 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2403 btrfs_item_nr_offset(0),
2404 right_nritems * sizeof(struct btrfs_item));
2405
Chris Mason00ec4c52007-02-24 12:47:20 -05002406 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002407 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2408 btrfs_item_nr_offset(left_nritems - push_items),
2409 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002410
2411 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002412 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002413 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002414 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002415 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002416 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002417 if (!right->map_token) {
2418 map_extent_buffer(right, (unsigned long)item,
2419 sizeof(struct btrfs_item),
2420 &right->map_token, &right->kaddr,
2421 &right->map_start, &right->map_len,
2422 KM_USER1);
2423 }
2424 push_space -= btrfs_item_size(right, item);
2425 btrfs_set_item_offset(right, item, push_space);
2426 }
2427
2428 if (right->map_token) {
2429 unmap_extent_buffer(right, right->map_token, KM_USER1);
2430 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002431 }
Chris Mason7518a232007-03-12 12:01:18 -04002432 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002433 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002434
Chris Mason34a38212007-11-07 13:31:03 -05002435 if (left_nritems)
2436 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002437 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002438
Zheng Yan31840ae2008-09-23 13:14:14 -04002439 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2440 BUG_ON(ret);
2441
Chris Mason5f39d392007-10-15 16:14:19 -04002442 btrfs_item_key(right, &disk_key, 0);
2443 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002444 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002445
Chris Mason00ec4c52007-02-24 12:47:20 -05002446 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002447 if (path->slots[0] >= left_nritems) {
2448 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002449 if (btrfs_header_nritems(path->nodes[0]) == 0)
2450 clean_tree_block(trans, root, path->nodes[0]);
2451 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002452 free_extent_buffer(path->nodes[0]);
2453 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002454 path->slots[1] += 1;
2455 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002456 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002457 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002458 }
2459 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002460
2461out_unlock:
2462 btrfs_tree_unlock(right);
2463 free_extent_buffer(right);
2464 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002465}
Chris Mason925baed2008-06-25 16:01:30 -04002466
Chris Mason00ec4c52007-02-24 12:47:20 -05002467/*
Chris Mason74123bd2007-02-02 11:05:29 -05002468 * push some data in the path leaf to the left, trying to free up at
2469 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2470 */
Chris Masone089f052007-03-16 16:20:31 -04002471static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002472 *root, struct btrfs_path *path, int data_size,
2473 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002474{
Chris Mason5f39d392007-10-15 16:14:19 -04002475 struct btrfs_disk_key disk_key;
2476 struct extent_buffer *right = path->nodes[0];
2477 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002478 int slot;
2479 int i;
2480 int free_space;
2481 int push_space = 0;
2482 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002483 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002484 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002485 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002486 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002487 int ret = 0;
2488 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002489 u32 this_item_size;
2490 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491
2492 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002493 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002494 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002495 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002496 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002497
Chris Mason3685f792007-10-19 09:23:27 -04002498 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002499 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002500 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002501
Chris Masonb9447ef82009-03-09 11:45:38 -04002502 btrfs_assert_tree_locked(path->nodes[1]);
Chris Masona2135012008-06-25 16:01:30 -04002503
Chris Masonca7a79a2008-05-12 12:59:19 -04002504 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002505 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002506 btrfs_set_lock_blocking(left);
2507
Chris Mason123abc82007-03-14 14:14:43 -04002508 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002509 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002510 ret = 1;
2511 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002512 }
Chris Mason02217ed2007-03-02 16:08:05 -05002513
2514 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002515 ret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002516 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002517 if (ret) {
2518 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002519 ret = 1;
2520 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002521 }
Chris Mason3685f792007-10-19 09:23:27 -04002522
Chris Mason123abc82007-03-14 14:14:43 -04002523 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002524 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002525 ret = 1;
2526 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002527 }
2528
Chris Mason34a38212007-11-07 13:31:03 -05002529 if (empty)
2530 nr = right_nritems;
2531 else
2532 nr = right_nritems - 1;
2533
2534 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002535 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002536 if (!right->map_token) {
2537 map_extent_buffer(right, (unsigned long)item,
2538 sizeof(struct btrfs_item),
2539 &right->map_token, &right->kaddr,
2540 &right->map_start, &right->map_len,
2541 KM_USER1);
2542 }
2543
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 if (!empty && push_items > 0) {
2545 if (path->slots[0] < i)
2546 break;
2547 if (path->slots[0] == i) {
2548 int space = btrfs_leaf_free_space(root, right);
2549 if (space + push_space * 2 > free_space)
2550 break;
2551 }
2552 }
2553
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002555 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002556
2557 this_item_size = btrfs_item_size(right, item);
2558 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 break;
Chris Masondb945352007-10-15 16:15:53 -04002560
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002562 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 }
Chris Masondb945352007-10-15 16:15:53 -04002564
2565 if (right->map_token) {
2566 unmap_extent_buffer(right, right->map_token, KM_USER1);
2567 right->map_token = NULL;
2568 }
2569
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002571 ret = 1;
2572 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 }
Chris Mason34a38212007-11-07 13:31:03 -05002574 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002575 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002576
Chris Masonbe0e5c02007-01-26 15:51:26 -05002577 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002578 copy_extent_buffer(left, right,
2579 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2580 btrfs_item_nr_offset(0),
2581 push_items * sizeof(struct btrfs_item));
2582
Chris Mason123abc82007-03-14 14:14:43 -04002583 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002584 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002585
2586 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002587 leaf_data_end(root, left) - push_space,
2588 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002589 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002590 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002591 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002592 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002593
Chris Masondb945352007-10-15 16:15:53 -04002594 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002595 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002596 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002597
Chris Mason5f39d392007-10-15 16:14:19 -04002598 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002599 if (!left->map_token) {
2600 map_extent_buffer(left, (unsigned long)item,
2601 sizeof(struct btrfs_item),
2602 &left->map_token, &left->kaddr,
2603 &left->map_start, &left->map_len,
2604 KM_USER1);
2605 }
2606
Chris Mason5f39d392007-10-15 16:14:19 -04002607 ioff = btrfs_item_offset(left, item);
2608 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002609 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 }
Chris Mason5f39d392007-10-15 16:14:19 -04002611 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002612 if (left->map_token) {
2613 unmap_extent_buffer(left, left->map_token, KM_USER1);
2614 left->map_token = NULL;
2615 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616
2617 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002618 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002619 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2620 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002621 WARN_ON(1);
2622 }
Chris Mason5f39d392007-10-15 16:14:19 -04002623
Chris Mason34a38212007-11-07 13:31:03 -05002624 if (push_items < right_nritems) {
2625 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2626 leaf_data_end(root, right);
2627 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2628 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2629 btrfs_leaf_data(right) +
2630 leaf_data_end(root, right), push_space);
2631
2632 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002633 btrfs_item_nr_offset(push_items),
2634 (btrfs_header_nritems(right) - push_items) *
2635 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002636 }
Yaneef1c492007-11-26 10:58:13 -05002637 right_nritems -= push_items;
2638 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002639 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002640 for (i = 0; i < right_nritems; i++) {
2641 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002642
2643 if (!right->map_token) {
2644 map_extent_buffer(right, (unsigned long)item,
2645 sizeof(struct btrfs_item),
2646 &right->map_token, &right->kaddr,
2647 &right->map_start, &right->map_len,
2648 KM_USER1);
2649 }
2650
2651 push_space = push_space - btrfs_item_size(right, item);
2652 btrfs_set_item_offset(right, item, push_space);
2653 }
2654 if (right->map_token) {
2655 unmap_extent_buffer(right, right->map_token, KM_USER1);
2656 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002657 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002658
Chris Mason5f39d392007-10-15 16:14:19 -04002659 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002660 if (right_nritems)
2661 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002662
Zheng Yan31840ae2008-09-23 13:14:14 -04002663 ret = btrfs_update_ref(trans, root, right, left,
2664 old_left_nritems, push_items);
2665 BUG_ON(ret);
2666
Chris Mason5f39d392007-10-15 16:14:19 -04002667 btrfs_item_key(right, &disk_key, 0);
2668 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002669 if (wret)
2670 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002671
2672 /* then fixup the leaf pointer in the path */
2673 if (path->slots[0] < push_items) {
2674 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002675 if (btrfs_header_nritems(path->nodes[0]) == 0)
2676 clean_tree_block(trans, root, path->nodes[0]);
2677 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002678 free_extent_buffer(path->nodes[0]);
2679 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680 path->slots[1] -= 1;
2681 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002682 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002683 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002684 path->slots[0] -= push_items;
2685 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002686 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002687 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002688out:
2689 btrfs_tree_unlock(left);
2690 free_extent_buffer(left);
2691 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002692}
2693
Chris Mason74123bd2007-02-02 11:05:29 -05002694/*
2695 * split the path's leaf in two, making sure there is at least data_size
2696 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002697 *
2698 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002699 */
Chris Masone02119d2008-09-05 16:13:11 -04002700static noinline int split_leaf(struct btrfs_trans_handle *trans,
2701 struct btrfs_root *root,
2702 struct btrfs_key *ins_key,
2703 struct btrfs_path *path, int data_size,
2704 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002705{
Chris Mason5f39d392007-10-15 16:14:19 -04002706 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002707 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002708 int mid;
2709 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002710 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002711 int data_copy_size;
2712 int rt_data_off;
2713 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002714 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002715 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002716 int double_split;
2717 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002718 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002719
Chris Mason40689472007-03-17 14:29:23 -04002720 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002721 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002722 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002723 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002724 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002725 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002726 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002727 if (wret < 0)
2728 return wret;
2729 }
2730 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002731
Chris Mason3685f792007-10-19 09:23:27 -04002732 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002733 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002734 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002735 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002736
Chris Mason5c680ed2007-02-22 11:39:13 -05002737 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002738 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002739 if (ret)
2740 return ret;
2741 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002742again:
2743 double_split = 0;
2744 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002745 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002746 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002747 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002748
Chris Mason925baed2008-06-25 16:01:30 -04002749 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002750 path->nodes[1]->start,
2751 root->root_key.objectid,
2752 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002753 if (IS_ERR(right)) {
2754 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002755 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002756 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002757
Chris Mason5f39d392007-10-15 16:14:19 -04002758 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002759 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002760 btrfs_set_header_generation(right, trans->transid);
2761 btrfs_set_header_owner(right, root->root_key.objectid);
2762 btrfs_set_header_level(right, 0);
2763 write_extent_buffer(right, root->fs_info->fsid,
2764 (unsigned long)btrfs_header_fsid(right),
2765 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002766
2767 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2768 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2769 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002770 if (mid <= slot) {
2771 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002772 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002773 BTRFS_LEAF_DATA_SIZE(root)) {
2774 if (slot >= nritems) {
2775 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002776 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002777 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002778 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002779 path->slots[1] + 1, 1);
2780 if (wret)
2781 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002782
2783 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002784 free_extent_buffer(path->nodes[0]);
2785 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002786 path->slots[0] = 0;
2787 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002788 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002789 return ret;
2790 }
2791 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002792 if (mid != nritems &&
2793 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002794 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002795 double_split = 1;
2796 }
Chris Masond4dbff92007-04-04 14:08:15 -04002797 }
2798 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002799 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002800 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002801 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002802 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002803 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002804 wret = insert_ptr(trans, root, path,
2805 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002806 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002807 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002808 if (wret)
2809 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002810 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002811 free_extent_buffer(path->nodes[0]);
2812 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002813 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002814 if (path->slots[1] == 0) {
2815 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002816 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002817 if (wret)
2818 ret = wret;
2819 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002820 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002821 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002822 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002823 mid = 1;
2824 } else {
2825 mid = slot;
2826 if (mid != nritems &&
2827 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002828 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002829 double_split = 1;
2830 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002831 }
Chris Masond4dbff92007-04-04 14:08:15 -04002832 }
2833 }
Chris Mason5f39d392007-10-15 16:14:19 -04002834 nritems = nritems - mid;
2835 btrfs_set_header_nritems(right, nritems);
2836 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2837
2838 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2839 btrfs_item_nr_offset(mid),
2840 nritems * sizeof(struct btrfs_item));
2841
2842 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002843 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2844 data_copy_size, btrfs_leaf_data(l) +
2845 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002846
Chris Mason5f39d392007-10-15 16:14:19 -04002847 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2848 btrfs_item_end_nr(l, mid);
2849
2850 for (i = 0; i < nritems; i++) {
2851 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002852 u32 ioff;
2853
2854 if (!right->map_token) {
2855 map_extent_buffer(right, (unsigned long)item,
2856 sizeof(struct btrfs_item),
2857 &right->map_token, &right->kaddr,
2858 &right->map_start, &right->map_len,
2859 KM_USER1);
2860 }
2861
2862 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002863 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002864 }
Chris Mason74123bd2007-02-02 11:05:29 -05002865
Chris Masondb945352007-10-15 16:15:53 -04002866 if (right->map_token) {
2867 unmap_extent_buffer(right, right->map_token, KM_USER1);
2868 right->map_token = NULL;
2869 }
2870
Chris Mason5f39d392007-10-15 16:14:19 -04002871 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002872 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002873 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002874 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2875 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002876 if (wret)
2877 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002878
2879 btrfs_mark_buffer_dirty(right);
2880 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002881 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002882
Zheng Yan31840ae2008-09-23 13:14:14 -04002883 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2884 BUG_ON(ret);
2885
Chris Masonbe0e5c02007-01-26 15:51:26 -05002886 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002887 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002888 free_extent_buffer(path->nodes[0]);
2889 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002890 path->slots[0] -= mid;
2891 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002892 } else {
2893 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002894 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002895 }
Chris Mason5f39d392007-10-15 16:14:19 -04002896
Chris Masoneb60cea2007-02-02 09:18:22 -05002897 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002898
Chris Masoncc0c5532007-10-25 15:42:57 -04002899 if (double_split) {
2900 BUG_ON(num_doubles != 0);
2901 num_doubles++;
2902 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002903 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002904 return ret;
2905}
2906
Chris Masond352ac62008-09-29 15:18:18 -04002907/*
Chris Mason459931e2008-12-10 09:10:46 -05002908 * This function splits a single item into two items,
2909 * giving 'new_key' to the new item and splitting the
2910 * old one at split_offset (from the start of the item).
2911 *
2912 * The path may be released by this operation. After
2913 * the split, the path is pointing to the old item. The
2914 * new item is going to be in the same node as the old one.
2915 *
2916 * Note, the item being split must be smaller enough to live alone on
2917 * a tree block with room for one extra struct btrfs_item
2918 *
2919 * This allows us to split the item in place, keeping a lock on the
2920 * leaf the entire time.
2921 */
2922int btrfs_split_item(struct btrfs_trans_handle *trans,
2923 struct btrfs_root *root,
2924 struct btrfs_path *path,
2925 struct btrfs_key *new_key,
2926 unsigned long split_offset)
2927{
2928 u32 item_size;
2929 struct extent_buffer *leaf;
2930 struct btrfs_key orig_key;
2931 struct btrfs_item *item;
2932 struct btrfs_item *new_item;
2933 int ret = 0;
2934 int slot;
2935 u32 nritems;
2936 u32 orig_offset;
2937 struct btrfs_disk_key disk_key;
2938 char *buf;
2939
2940 leaf = path->nodes[0];
2941 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2942 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2943 goto split;
2944
2945 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2946 btrfs_release_path(root, path);
2947
2948 path->search_for_split = 1;
2949 path->keep_locks = 1;
2950
2951 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2952 path->search_for_split = 0;
2953
2954 /* if our item isn't there or got smaller, return now */
2955 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2956 path->slots[0])) {
2957 path->keep_locks = 0;
2958 return -EAGAIN;
2959 }
2960
Yan Zheng87b29b22008-12-17 10:21:48 -05002961 ret = split_leaf(trans, root, &orig_key, path,
2962 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05002963 path->keep_locks = 0;
2964 BUG_ON(ret);
2965
Chris Masonb4ce94d2009-02-04 09:25:08 -05002966 /*
2967 * make sure any changes to the path from split_leaf leave it
2968 * in a blocking state
2969 */
2970 btrfs_set_path_blocking(path);
2971
Chris Mason459931e2008-12-10 09:10:46 -05002972 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05002973 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05002974
2975split:
2976 item = btrfs_item_nr(leaf, path->slots[0]);
2977 orig_offset = btrfs_item_offset(leaf, item);
2978 item_size = btrfs_item_size(leaf, item);
2979
2980
2981 buf = kmalloc(item_size, GFP_NOFS);
2982 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2983 path->slots[0]), item_size);
2984 slot = path->slots[0] + 1;
2985 leaf = path->nodes[0];
2986
2987 nritems = btrfs_header_nritems(leaf);
2988
2989 if (slot != nritems) {
2990 /* shift the items */
2991 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2992 btrfs_item_nr_offset(slot),
2993 (nritems - slot) * sizeof(struct btrfs_item));
2994
2995 }
2996
2997 btrfs_cpu_key_to_disk(&disk_key, new_key);
2998 btrfs_set_item_key(leaf, &disk_key, slot);
2999
3000 new_item = btrfs_item_nr(leaf, slot);
3001
3002 btrfs_set_item_offset(leaf, new_item, orig_offset);
3003 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3004
3005 btrfs_set_item_offset(leaf, item,
3006 orig_offset + item_size - split_offset);
3007 btrfs_set_item_size(leaf, item, split_offset);
3008
3009 btrfs_set_header_nritems(leaf, nritems + 1);
3010
3011 /* write the data for the start of the original item */
3012 write_extent_buffer(leaf, buf,
3013 btrfs_item_ptr_offset(leaf, path->slots[0]),
3014 split_offset);
3015
3016 /* write the data for the new item */
3017 write_extent_buffer(leaf, buf + split_offset,
3018 btrfs_item_ptr_offset(leaf, slot),
3019 item_size - split_offset);
3020 btrfs_mark_buffer_dirty(leaf);
3021
3022 ret = 0;
3023 if (btrfs_leaf_free_space(root, leaf) < 0) {
3024 btrfs_print_leaf(root, leaf);
3025 BUG();
3026 }
3027 kfree(buf);
3028 return ret;
3029}
3030
3031/*
Chris Masond352ac62008-09-29 15:18:18 -04003032 * make the item pointed to by the path smaller. new_size indicates
3033 * how small to make it, and from_end tells us if we just chop bytes
3034 * off the end of the item or if we shift the item to chop bytes off
3035 * the front.
3036 */
Chris Masonb18c6682007-04-17 13:26:50 -04003037int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3038 struct btrfs_root *root,
3039 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003040 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003041{
3042 int ret = 0;
3043 int slot;
3044 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003045 struct extent_buffer *leaf;
3046 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003047 u32 nritems;
3048 unsigned int data_end;
3049 unsigned int old_data_start;
3050 unsigned int old_size;
3051 unsigned int size_diff;
3052 int i;
3053
3054 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003055 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003056 slot = path->slots[0];
3057
3058 old_size = btrfs_item_size_nr(leaf, slot);
3059 if (old_size == new_size)
3060 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003061
Chris Mason5f39d392007-10-15 16:14:19 -04003062 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003063 data_end = leaf_data_end(root, leaf);
3064
Chris Mason5f39d392007-10-15 16:14:19 -04003065 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003066
Chris Masonb18c6682007-04-17 13:26:50 -04003067 size_diff = old_size - new_size;
3068
3069 BUG_ON(slot < 0);
3070 BUG_ON(slot >= nritems);
3071
3072 /*
3073 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3074 */
3075 /* first correct the data pointers */
3076 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003077 u32 ioff;
3078 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003079
3080 if (!leaf->map_token) {
3081 map_extent_buffer(leaf, (unsigned long)item,
3082 sizeof(struct btrfs_item),
3083 &leaf->map_token, &leaf->kaddr,
3084 &leaf->map_start, &leaf->map_len,
3085 KM_USER1);
3086 }
3087
Chris Mason5f39d392007-10-15 16:14:19 -04003088 ioff = btrfs_item_offset(leaf, item);
3089 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003090 }
Chris Masondb945352007-10-15 16:15:53 -04003091
3092 if (leaf->map_token) {
3093 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3094 leaf->map_token = NULL;
3095 }
3096
Chris Masonb18c6682007-04-17 13:26:50 -04003097 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003098 if (from_end) {
3099 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3100 data_end + size_diff, btrfs_leaf_data(leaf) +
3101 data_end, old_data_start + new_size - data_end);
3102 } else {
3103 struct btrfs_disk_key disk_key;
3104 u64 offset;
3105
3106 btrfs_item_key(leaf, &disk_key, slot);
3107
3108 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3109 unsigned long ptr;
3110 struct btrfs_file_extent_item *fi;
3111
3112 fi = btrfs_item_ptr(leaf, slot,
3113 struct btrfs_file_extent_item);
3114 fi = (struct btrfs_file_extent_item *)(
3115 (unsigned long)fi - size_diff);
3116
3117 if (btrfs_file_extent_type(leaf, fi) ==
3118 BTRFS_FILE_EXTENT_INLINE) {
3119 ptr = btrfs_item_ptr_offset(leaf, slot);
3120 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003121 (unsigned long)fi,
3122 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003123 disk_bytenr));
3124 }
3125 }
3126
3127 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3128 data_end + size_diff, btrfs_leaf_data(leaf) +
3129 data_end, old_data_start - data_end);
3130
3131 offset = btrfs_disk_key_offset(&disk_key);
3132 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3133 btrfs_set_item_key(leaf, &disk_key, slot);
3134 if (slot == 0)
3135 fixup_low_keys(trans, root, path, &disk_key, 1);
3136 }
Chris Mason5f39d392007-10-15 16:14:19 -04003137
3138 item = btrfs_item_nr(leaf, slot);
3139 btrfs_set_item_size(leaf, item, new_size);
3140 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003141
3142 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003143 if (btrfs_leaf_free_space(root, leaf) < 0) {
3144 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003145 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003146 }
Chris Masonb18c6682007-04-17 13:26:50 -04003147 return ret;
3148}
3149
Chris Masond352ac62008-09-29 15:18:18 -04003150/*
3151 * make the item pointed to by the path bigger, data_size is the new size.
3152 */
Chris Mason5f39d392007-10-15 16:14:19 -04003153int btrfs_extend_item(struct btrfs_trans_handle *trans,
3154 struct btrfs_root *root, struct btrfs_path *path,
3155 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003156{
3157 int ret = 0;
3158 int slot;
3159 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003160 struct extent_buffer *leaf;
3161 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003162 u32 nritems;
3163 unsigned int data_end;
3164 unsigned int old_data;
3165 unsigned int old_size;
3166 int i;
3167
3168 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003169 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003170
Chris Mason5f39d392007-10-15 16:14:19 -04003171 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003172 data_end = leaf_data_end(root, leaf);
3173
Chris Mason5f39d392007-10-15 16:14:19 -04003174 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3175 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003176 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003177 }
Chris Mason6567e832007-04-16 09:22:45 -04003178 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003179 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003180
3181 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003182 if (slot >= nritems) {
3183 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003184 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3185 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003186 BUG_ON(1);
3187 }
Chris Mason6567e832007-04-16 09:22:45 -04003188
3189 /*
3190 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3191 */
3192 /* first correct the data pointers */
3193 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003194 u32 ioff;
3195 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003196
3197 if (!leaf->map_token) {
3198 map_extent_buffer(leaf, (unsigned long)item,
3199 sizeof(struct btrfs_item),
3200 &leaf->map_token, &leaf->kaddr,
3201 &leaf->map_start, &leaf->map_len,
3202 KM_USER1);
3203 }
Chris Mason5f39d392007-10-15 16:14:19 -04003204 ioff = btrfs_item_offset(leaf, item);
3205 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003206 }
Chris Mason5f39d392007-10-15 16:14:19 -04003207
Chris Masondb945352007-10-15 16:15:53 -04003208 if (leaf->map_token) {
3209 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3210 leaf->map_token = NULL;
3211 }
3212
Chris Mason6567e832007-04-16 09:22:45 -04003213 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003214 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003215 data_end - data_size, btrfs_leaf_data(leaf) +
3216 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003217
Chris Mason6567e832007-04-16 09:22:45 -04003218 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003219 old_size = btrfs_item_size_nr(leaf, slot);
3220 item = btrfs_item_nr(leaf, slot);
3221 btrfs_set_item_size(leaf, item, old_size + data_size);
3222 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003223
3224 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 if (btrfs_leaf_free_space(root, leaf) < 0) {
3226 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003227 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003228 }
Chris Mason6567e832007-04-16 09:22:45 -04003229 return ret;
3230}
3231
Chris Mason74123bd2007-02-02 11:05:29 -05003232/*
Chris Masond352ac62008-09-29 15:18:18 -04003233 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003234 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003235 * Returns the number of keys that were inserted.
3236 */
3237int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3238 struct btrfs_root *root,
3239 struct btrfs_path *path,
3240 struct btrfs_key *cpu_key, u32 *data_size,
3241 int nr)
3242{
3243 struct extent_buffer *leaf;
3244 struct btrfs_item *item;
3245 int ret = 0;
3246 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003247 int i;
3248 u32 nritems;
3249 u32 total_data = 0;
3250 u32 total_size = 0;
3251 unsigned int data_end;
3252 struct btrfs_disk_key disk_key;
3253 struct btrfs_key found_key;
3254
Yan Zheng87b29b22008-12-17 10:21:48 -05003255 for (i = 0; i < nr; i++) {
3256 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3257 BTRFS_LEAF_DATA_SIZE(root)) {
3258 break;
3259 nr = i;
3260 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003261 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003262 total_size += data_size[i] + sizeof(struct btrfs_item);
3263 }
3264 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003265
Josef Bacikf3465ca2008-11-12 14:19:50 -05003266 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3267 if (ret == 0)
3268 return -EEXIST;
3269 if (ret < 0)
3270 goto out;
3271
Josef Bacikf3465ca2008-11-12 14:19:50 -05003272 leaf = path->nodes[0];
3273
3274 nritems = btrfs_header_nritems(leaf);
3275 data_end = leaf_data_end(root, leaf);
3276
3277 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3278 for (i = nr; i >= 0; i--) {
3279 total_data -= data_size[i];
3280 total_size -= data_size[i] + sizeof(struct btrfs_item);
3281 if (total_size < btrfs_leaf_free_space(root, leaf))
3282 break;
3283 }
3284 nr = i;
3285 }
3286
3287 slot = path->slots[0];
3288 BUG_ON(slot < 0);
3289
3290 if (slot != nritems) {
3291 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3292
3293 item = btrfs_item_nr(leaf, slot);
3294 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3295
3296 /* figure out how many keys we can insert in here */
3297 total_data = data_size[0];
3298 for (i = 1; i < nr; i++) {
3299 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3300 break;
3301 total_data += data_size[i];
3302 }
3303 nr = i;
3304
3305 if (old_data < data_end) {
3306 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003307 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003308 slot, old_data, data_end);
3309 BUG_ON(1);
3310 }
3311 /*
3312 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3313 */
3314 /* first correct the data pointers */
3315 WARN_ON(leaf->map_token);
3316 for (i = slot; i < nritems; i++) {
3317 u32 ioff;
3318
3319 item = btrfs_item_nr(leaf, i);
3320 if (!leaf->map_token) {
3321 map_extent_buffer(leaf, (unsigned long)item,
3322 sizeof(struct btrfs_item),
3323 &leaf->map_token, &leaf->kaddr,
3324 &leaf->map_start, &leaf->map_len,
3325 KM_USER1);
3326 }
3327
3328 ioff = btrfs_item_offset(leaf, item);
3329 btrfs_set_item_offset(leaf, item, ioff - total_data);
3330 }
3331 if (leaf->map_token) {
3332 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3333 leaf->map_token = NULL;
3334 }
3335
3336 /* shift the items */
3337 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3338 btrfs_item_nr_offset(slot),
3339 (nritems - slot) * sizeof(struct btrfs_item));
3340
3341 /* shift the data */
3342 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3343 data_end - total_data, btrfs_leaf_data(leaf) +
3344 data_end, old_data - data_end);
3345 data_end = old_data;
3346 } else {
3347 /*
3348 * this sucks but it has to be done, if we are inserting at
3349 * the end of the leaf only insert 1 of the items, since we
3350 * have no way of knowing whats on the next leaf and we'd have
3351 * to drop our current locks to figure it out
3352 */
3353 nr = 1;
3354 }
3355
3356 /* setup the item for the new data */
3357 for (i = 0; i < nr; i++) {
3358 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3359 btrfs_set_item_key(leaf, &disk_key, slot + i);
3360 item = btrfs_item_nr(leaf, slot + i);
3361 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3362 data_end -= data_size[i];
3363 btrfs_set_item_size(leaf, item, data_size[i]);
3364 }
3365 btrfs_set_header_nritems(leaf, nritems + nr);
3366 btrfs_mark_buffer_dirty(leaf);
3367
3368 ret = 0;
3369 if (slot == 0) {
3370 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3371 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3372 }
3373
3374 if (btrfs_leaf_free_space(root, leaf) < 0) {
3375 btrfs_print_leaf(root, leaf);
3376 BUG();
3377 }
3378out:
3379 if (!ret)
3380 ret = nr;
3381 return ret;
3382}
3383
3384/*
3385 * Given a key and some data, insert items into the tree.
3386 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003387 */
Chris Mason9c583092008-01-29 15:15:18 -05003388int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003389 struct btrfs_root *root,
3390 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003391 struct btrfs_key *cpu_key, u32 *data_size,
3392 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003393{
Chris Mason5f39d392007-10-15 16:14:19 -04003394 struct extent_buffer *leaf;
3395 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003396 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003397 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003398 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003399 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003400 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003401 u32 total_size = 0;
3402 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003403 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003404 struct btrfs_disk_key disk_key;
3405
Chris Masond3977122009-01-05 21:25:51 -05003406 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003407 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003408
Josef Bacik7b128762008-07-24 12:17:14 -04003409 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003410 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003411 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003412 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003413 if (ret < 0)
3414 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003415
Chris Mason62e27492007-03-15 12:56:47 -04003416 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003417 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003418
Chris Mason5f39d392007-10-15 16:14:19 -04003419 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003420 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003421
Chris Masonf25956c2008-09-12 15:32:53 -04003422 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003423 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003424 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003425 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003426 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003427 }
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Chris Mason62e27492007-03-15 12:56:47 -04003429 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003430 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003431
Chris Masonbe0e5c02007-01-26 15:51:26 -05003432 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003433 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003434
Chris Mason5f39d392007-10-15 16:14:19 -04003435 if (old_data < data_end) {
3436 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003437 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003438 slot, old_data, data_end);
3439 BUG_ON(1);
3440 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003441 /*
3442 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3443 */
3444 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003445 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003446 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003447 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003448
Chris Mason5f39d392007-10-15 16:14:19 -04003449 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003450 if (!leaf->map_token) {
3451 map_extent_buffer(leaf, (unsigned long)item,
3452 sizeof(struct btrfs_item),
3453 &leaf->map_token, &leaf->kaddr,
3454 &leaf->map_start, &leaf->map_len,
3455 KM_USER1);
3456 }
3457
Chris Mason5f39d392007-10-15 16:14:19 -04003458 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003459 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003460 }
Chris Masondb945352007-10-15 16:15:53 -04003461 if (leaf->map_token) {
3462 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3463 leaf->map_token = NULL;
3464 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003465
3466 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003467 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003468 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003469 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003470
3471 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003472 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003473 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003474 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003475 data_end = old_data;
3476 }
Chris Mason5f39d392007-10-15 16:14:19 -04003477
Chris Mason62e27492007-03-15 12:56:47 -04003478 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003479 for (i = 0; i < nr; i++) {
3480 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3481 btrfs_set_item_key(leaf, &disk_key, slot + i);
3482 item = btrfs_item_nr(leaf, slot + i);
3483 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3484 data_end -= data_size[i];
3485 btrfs_set_item_size(leaf, item, data_size[i]);
3486 }
3487 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003488 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003489
3490 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003491 if (slot == 0) {
3492 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003493 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003494 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003495
Chris Mason5f39d392007-10-15 16:14:19 -04003496 if (btrfs_leaf_free_space(root, leaf) < 0) {
3497 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003498 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003499 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003500out:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003501 btrfs_unlock_up_safe(path, 1);
Chris Mason62e27492007-03-15 12:56:47 -04003502 return ret;
3503}
3504
3505/*
3506 * Given a key and some data, insert an item into the tree.
3507 * This does all the path init required, making room in the tree if needed.
3508 */
Chris Masone089f052007-03-16 16:20:31 -04003509int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3510 *root, struct btrfs_key *cpu_key, void *data, u32
3511 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003512{
3513 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003514 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003515 struct extent_buffer *leaf;
3516 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003517
Chris Mason2c90e5d2007-04-02 10:50:19 -04003518 path = btrfs_alloc_path();
3519 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003520 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003521 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003522 leaf = path->nodes[0];
3523 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3524 write_extent_buffer(leaf, data, ptr, data_size);
3525 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003526 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003527 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003528 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003529}
3530
Chris Mason74123bd2007-02-02 11:05:29 -05003531/*
Chris Mason5de08d72007-02-24 06:24:44 -05003532 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003533 *
Chris Masond352ac62008-09-29 15:18:18 -04003534 * the tree should have been previously balanced so the deletion does not
3535 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003536 */
Chris Masone089f052007-03-16 16:20:31 -04003537static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3538 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003539{
Chris Mason5f39d392007-10-15 16:14:19 -04003540 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003541 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003542 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003543 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544
Chris Mason5f39d392007-10-15 16:14:19 -04003545 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003546 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003547 memmove_extent_buffer(parent,
3548 btrfs_node_key_ptr_offset(slot),
3549 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003550 sizeof(struct btrfs_key_ptr) *
3551 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003552 }
Chris Mason7518a232007-03-12 12:01:18 -04003553 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003554 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003555 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003556 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003557 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003558 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003559 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003560 struct btrfs_disk_key disk_key;
3561
3562 btrfs_node_key(parent, &disk_key, 0);
3563 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003564 if (wret)
3565 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003566 }
Chris Masond6025572007-03-30 14:27:56 -04003567 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003568 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003569}
3570
Chris Mason74123bd2007-02-02 11:05:29 -05003571/*
Chris Mason323ac952008-10-01 19:05:46 -04003572 * a helper function to delete the leaf pointed to by path->slots[1] and
3573 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3574 * already know it, it is faster to have them pass it down than to
3575 * read it out of the node again.
3576 *
3577 * This deletes the pointer in path->nodes[1] and frees the leaf
3578 * block extent. zero is returned if it all worked out, < 0 otherwise.
3579 *
3580 * The path must have already been setup for deleting the leaf, including
3581 * all the proper balancing. path->nodes[1] must be locked.
3582 */
3583noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3584 struct btrfs_root *root,
3585 struct btrfs_path *path, u64 bytenr)
3586{
3587 int ret;
3588 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003589 u64 parent_start = path->nodes[1]->start;
3590 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003591
3592 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3593 if (ret)
3594 return ret;
3595
Chris Mason4d081c42009-02-04 09:31:28 -05003596 /*
3597 * btrfs_free_extent is expensive, we want to make sure we
3598 * aren't holding any locks when we call it
3599 */
3600 btrfs_unlock_up_safe(path, 0);
3601
Chris Mason323ac952008-10-01 19:05:46 -04003602 ret = btrfs_free_extent(trans, root, bytenr,
3603 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003604 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003605 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003606 return ret;
3607}
3608/*
Chris Mason74123bd2007-02-02 11:05:29 -05003609 * delete the item at the leaf level in path. If that empties
3610 * the leaf, remove it from the tree
3611 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003612int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3613 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003614{
Chris Mason5f39d392007-10-15 16:14:19 -04003615 struct extent_buffer *leaf;
3616 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003617 int last_off;
3618 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003619 int ret = 0;
3620 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003621 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003622 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003623
Chris Mason5f39d392007-10-15 16:14:19 -04003624 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003625 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3626
3627 for (i = 0; i < nr; i++)
3628 dsize += btrfs_item_size_nr(leaf, slot + i);
3629
Chris Mason5f39d392007-10-15 16:14:19 -04003630 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003631
Chris Mason85e21ba2008-01-29 15:11:36 -05003632 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003633 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
3635 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003636 data_end + dsize,
3637 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003638 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003639
Chris Mason85e21ba2008-01-29 15:11:36 -05003640 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003641 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003642
Chris Mason5f39d392007-10-15 16:14:19 -04003643 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003644 if (!leaf->map_token) {
3645 map_extent_buffer(leaf, (unsigned long)item,
3646 sizeof(struct btrfs_item),
3647 &leaf->map_token, &leaf->kaddr,
3648 &leaf->map_start, &leaf->map_len,
3649 KM_USER1);
3650 }
Chris Mason5f39d392007-10-15 16:14:19 -04003651 ioff = btrfs_item_offset(leaf, item);
3652 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003653 }
Chris Masondb945352007-10-15 16:15:53 -04003654
3655 if (leaf->map_token) {
3656 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3657 leaf->map_token = NULL;
3658 }
3659
Chris Mason5f39d392007-10-15 16:14:19 -04003660 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003661 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003662 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003663 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003664 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003665 btrfs_set_header_nritems(leaf, nritems - nr);
3666 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003667
Chris Mason74123bd2007-02-02 11:05:29 -05003668 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003669 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003670 if (leaf == root->node) {
3671 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003672 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003673 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3674 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003675 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003676 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003677 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003678 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003679 struct btrfs_disk_key disk_key;
3680
3681 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003682 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003683 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003684 if (wret)
3685 ret = wret;
3686 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003687
Chris Mason74123bd2007-02-02 11:05:29 -05003688 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003689 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003690 /* push_leaf_left fixes the path.
3691 * make sure the path still points to our leaf
3692 * for possible call to del_ptr below
3693 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003694 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003695 extent_buffer_get(leaf);
3696
Chris Mason85e21ba2008-01-29 15:11:36 -05003697 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003698 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003699 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003700
3701 if (path->nodes[0] == leaf &&
3702 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003703 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003704 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003705 ret = wret;
3706 }
Chris Mason5f39d392007-10-15 16:14:19 -04003707
3708 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003709 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003710 ret = btrfs_del_leaf(trans, root, path,
3711 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003712 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003713 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003714 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003715 /* if we're still in the path, make sure
3716 * we're dirty. Otherwise, one of the
3717 * push_leaf functions must have already
3718 * dirtied this buffer
3719 */
3720 if (path->nodes[0] == leaf)
3721 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003722 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003723 }
Chris Masond5719762007-03-23 10:01:08 -04003724 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003725 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003726 }
3727 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003728 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003729}
3730
Chris Mason97571fd2007-02-24 13:39:08 -05003731/*
Chris Mason925baed2008-06-25 16:01:30 -04003732 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003733 * returns 0 if it found something or 1 if there are no lesser leaves.
3734 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003735 *
3736 * This may release the path, and so you may lose any locks held at the
3737 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003738 */
3739int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3740{
Chris Mason925baed2008-06-25 16:01:30 -04003741 struct btrfs_key key;
3742 struct btrfs_disk_key found_key;
3743 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003744
Chris Mason925baed2008-06-25 16:01:30 -04003745 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003746
Chris Mason925baed2008-06-25 16:01:30 -04003747 if (key.offset > 0)
3748 key.offset--;
3749 else if (key.type > 0)
3750 key.type--;
3751 else if (key.objectid > 0)
3752 key.objectid--;
3753 else
3754 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003755
Chris Mason925baed2008-06-25 16:01:30 -04003756 btrfs_release_path(root, path);
3757 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3758 if (ret < 0)
3759 return ret;
3760 btrfs_item_key(path->nodes[0], &found_key, 0);
3761 ret = comp_keys(&found_key, &key);
3762 if (ret < 0)
3763 return 0;
3764 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003765}
3766
Chris Mason3f157a22008-06-25 16:01:31 -04003767/*
3768 * A helper function to walk down the tree starting at min_key, and looking
3769 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003770 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003771 *
3772 * This does not cow, but it does stuff the starting key it finds back
3773 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3774 * key and get a writable path.
3775 *
3776 * This does lock as it descends, and path->keep_locks should be set
3777 * to 1 by the caller.
3778 *
3779 * This honors path->lowest_level to prevent descent past a given level
3780 * of the tree.
3781 *
Chris Masond352ac62008-09-29 15:18:18 -04003782 * min_trans indicates the oldest transaction that you are interested
3783 * in walking through. Any nodes or leaves older than min_trans are
3784 * skipped over (without reading them).
3785 *
Chris Mason3f157a22008-06-25 16:01:31 -04003786 * returns zero if something useful was found, < 0 on error and 1 if there
3787 * was nothing in the tree that matched the search criteria.
3788 */
3789int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003790 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003791 struct btrfs_path *path, int cache_only,
3792 u64 min_trans)
3793{
3794 struct extent_buffer *cur;
3795 struct btrfs_key found_key;
3796 int slot;
Yan96524802008-07-24 12:19:49 -04003797 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003798 u32 nritems;
3799 int level;
3800 int ret = 1;
3801
Chris Mason934d3752008-12-08 16:43:10 -05003802 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003803again:
3804 cur = btrfs_lock_root_node(root);
3805 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003806 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003807 path->nodes[level] = cur;
3808 path->locks[level] = 1;
3809
3810 if (btrfs_header_generation(cur) < min_trans) {
3811 ret = 1;
3812 goto out;
3813 }
Chris Masond3977122009-01-05 21:25:51 -05003814 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003815 nritems = btrfs_header_nritems(cur);
3816 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003817 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003818
Chris Mason323ac952008-10-01 19:05:46 -04003819 /* at the lowest level, we're done, setup the path and exit */
3820 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003821 if (slot >= nritems)
3822 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003823 ret = 0;
3824 path->slots[level] = slot;
3825 btrfs_item_key_to_cpu(cur, &found_key, slot);
3826 goto out;
3827 }
Yan96524802008-07-24 12:19:49 -04003828 if (sret && slot > 0)
3829 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003830 /*
3831 * check this node pointer against the cache_only and
3832 * min_trans parameters. If it isn't in cache or is too
3833 * old, skip to the next one.
3834 */
Chris Masond3977122009-01-05 21:25:51 -05003835 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003836 u64 blockptr;
3837 u64 gen;
3838 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003839 struct btrfs_disk_key disk_key;
3840
Chris Mason3f157a22008-06-25 16:01:31 -04003841 blockptr = btrfs_node_blockptr(cur, slot);
3842 gen = btrfs_node_ptr_generation(cur, slot);
3843 if (gen < min_trans) {
3844 slot++;
3845 continue;
3846 }
3847 if (!cache_only)
3848 break;
3849
Chris Masone02119d2008-09-05 16:13:11 -04003850 if (max_key) {
3851 btrfs_node_key(cur, &disk_key, slot);
3852 if (comp_keys(&disk_key, max_key) >= 0) {
3853 ret = 1;
3854 goto out;
3855 }
3856 }
3857
Chris Mason3f157a22008-06-25 16:01:31 -04003858 tmp = btrfs_find_tree_block(root, blockptr,
3859 btrfs_level_size(root, level - 1));
3860
3861 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3862 free_extent_buffer(tmp);
3863 break;
3864 }
3865 if (tmp)
3866 free_extent_buffer(tmp);
3867 slot++;
3868 }
Chris Masone02119d2008-09-05 16:13:11 -04003869find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003870 /*
3871 * we didn't find a candidate key in this node, walk forward
3872 * and find another one
3873 */
3874 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003875 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003876 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003877 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003878 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003879 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003880 btrfs_release_path(root, path);
3881 goto again;
3882 } else {
3883 goto out;
3884 }
3885 }
3886 /* save our key for returning back */
3887 btrfs_node_key_to_cpu(cur, &found_key, slot);
3888 path->slots[level] = slot;
3889 if (level == path->lowest_level) {
3890 ret = 0;
3891 unlock_up(path, level, 1);
3892 goto out;
3893 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003894 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003895 cur = read_node_slot(root, cur, slot);
3896
3897 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003898
Chris Mason3f157a22008-06-25 16:01:31 -04003899 path->locks[level - 1] = 1;
3900 path->nodes[level - 1] = cur;
3901 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05003902 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04003903 }
3904out:
3905 if (ret == 0)
3906 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05003907 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003908 return ret;
3909}
3910
3911/*
3912 * this is similar to btrfs_next_leaf, but does not try to preserve
3913 * and fixup the path. It looks for and returns the next key in the
3914 * tree based on the current path and the cache_only and min_trans
3915 * parameters.
3916 *
3917 * 0 is returned if another key is found, < 0 if there are any errors
3918 * and 1 is returned if there are no higher keys in the tree
3919 *
3920 * path->keep_locks should be set to 1 on the search made before
3921 * calling this function.
3922 */
Chris Masone7a84562008-06-25 16:01:31 -04003923int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003924 struct btrfs_key *key, int lowest_level,
3925 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003926{
3927 int level = lowest_level;
3928 int slot;
3929 struct extent_buffer *c;
3930
Chris Mason934d3752008-12-08 16:43:10 -05003931 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003932 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003933 if (!path->nodes[level])
3934 return 1;
3935
3936 slot = path->slots[level] + 1;
3937 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003938next:
Chris Masone7a84562008-06-25 16:01:31 -04003939 if (slot >= btrfs_header_nritems(c)) {
3940 level++;
Chris Masond3977122009-01-05 21:25:51 -05003941 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04003942 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04003943 continue;
3944 }
3945 if (level == 0)
3946 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003947 else {
3948 u64 blockptr = btrfs_node_blockptr(c, slot);
3949 u64 gen = btrfs_node_ptr_generation(c, slot);
3950
3951 if (cache_only) {
3952 struct extent_buffer *cur;
3953 cur = btrfs_find_tree_block(root, blockptr,
3954 btrfs_level_size(root, level - 1));
3955 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3956 slot++;
3957 if (cur)
3958 free_extent_buffer(cur);
3959 goto next;
3960 }
3961 free_extent_buffer(cur);
3962 }
3963 if (gen < min_trans) {
3964 slot++;
3965 goto next;
3966 }
Chris Masone7a84562008-06-25 16:01:31 -04003967 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003968 }
Chris Masone7a84562008-06-25 16:01:31 -04003969 return 0;
3970 }
3971 return 1;
3972}
3973
Chris Mason7bb86312007-12-11 09:25:06 -05003974/*
Chris Mason925baed2008-06-25 16:01:30 -04003975 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003976 * returns 0 if it found something or 1 if there are no greater leaves.
3977 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003978 */
Chris Mason234b63a2007-03-13 10:46:10 -04003979int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003980{
3981 int slot;
3982 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003983 struct extent_buffer *c;
3984 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003985 struct btrfs_key key;
3986 u32 nritems;
3987 int ret;
3988
3989 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05003990 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04003991 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003992
3993 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3994
Chris Mason925baed2008-06-25 16:01:30 -04003995 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003996 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003997 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3998 path->keep_locks = 0;
3999
4000 if (ret < 0)
4001 return ret;
4002
Chris Masonb4ce94d2009-02-04 09:25:08 -05004003 btrfs_set_path_blocking(path);
Chris Masona2135012008-06-25 16:01:30 -04004004 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004005 /*
4006 * by releasing the path above we dropped all our locks. A balance
4007 * could have added more items next to the key that used to be
4008 * at the very end of the block. So, check again here and
4009 * advance the path if there are now more items available.
4010 */
Chris Masona2135012008-06-25 16:01:30 -04004011 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004012 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04004013 goto done;
4014 }
Chris Masond97e63b2007-02-20 16:40:44 -05004015
Chris Masond3977122009-01-05 21:25:51 -05004016 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05004017 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05004018 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004019
Chris Masond97e63b2007-02-20 16:40:44 -05004020 slot = path->slots[level] + 1;
4021 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004022 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004023 level++;
Chris Masond3977122009-01-05 21:25:51 -05004024 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05004025 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004026 continue;
4027 }
Chris Mason5f39d392007-10-15 16:14:19 -04004028
Chris Mason925baed2008-06-25 16:01:30 -04004029 if (next) {
4030 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004031 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004032 }
Chris Mason5f39d392007-10-15 16:14:19 -04004033
Chris Masonb4ce94d2009-02-04 09:25:08 -05004034 /* the path was set to blocking above */
Chris Mason0bd40a72008-07-17 12:54:43 -04004035 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4036 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05004037 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004038
Chris Masonca7a79a2008-05-12 12:59:19 -04004039 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04004040 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004041 btrfs_assert_tree_locked(c);
Chris Mason5cd57b22008-06-25 16:01:30 -04004042 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004043 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004044 }
Chris Masond97e63b2007-02-20 16:40:44 -05004045 break;
4046 }
4047 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004048 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004049 level--;
4050 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004051 if (path->locks[level])
4052 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04004053 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004054 path->nodes[level] = next;
4055 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004056 if (!path->skip_locking)
4057 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004058 if (!level)
4059 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004060
4061 btrfs_set_path_blocking(path);
Chris Mason925baed2008-06-25 16:01:30 -04004062 if (level == 1 && path->locks[1] && path->reada)
4063 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04004064 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04004065 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004066 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5cd57b22008-06-25 16:01:30 -04004067 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004068 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004069 }
Chris Masond97e63b2007-02-20 16:40:44 -05004070 }
Chris Mason925baed2008-06-25 16:01:30 -04004071done:
4072 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05004073 return 0;
4074}
Chris Mason0b86a832008-03-24 15:01:56 -04004075
Chris Mason3f157a22008-06-25 16:01:31 -04004076/*
4077 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4078 * searching until it gets past min_objectid or finds an item of 'type'
4079 *
4080 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4081 */
Chris Mason0b86a832008-03-24 15:01:56 -04004082int btrfs_previous_item(struct btrfs_root *root,
4083 struct btrfs_path *path, u64 min_objectid,
4084 int type)
4085{
4086 struct btrfs_key found_key;
4087 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004088 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004089 int ret;
4090
Chris Masond3977122009-01-05 21:25:51 -05004091 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004092 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004093 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004094 ret = btrfs_prev_leaf(root, path);
4095 if (ret != 0)
4096 return ret;
4097 } else {
4098 path->slots[0]--;
4099 }
4100 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004101 nritems = btrfs_header_nritems(leaf);
4102 if (nritems == 0)
4103 return 1;
4104 if (path->slots[0] == nritems)
4105 path->slots[0]--;
4106
Chris Mason0b86a832008-03-24 15:01:56 -04004107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4108 if (found_key.type == type)
4109 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004110 if (found_key.objectid < min_objectid)
4111 break;
4112 if (found_key.objectid == min_objectid &&
4113 found_key.type < type)
4114 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004115 }
4116 return 1;
4117}