blob: dbb7241246335373dde1fa8597744faec6748942 [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 Masonb9447ef2009-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];
Aneesha1f39632007-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) {
Aneesha1f39632007-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) {
Aneesha1f39632007-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 Masona4b6e072009-03-16 10:59:57 -0400952 if (trans->transaction->delayed_refs.flushing &&
953 btrfs_header_nritems(mid) > 2)
954 return 0;
955
Chris Mason5f39d392007-10-15 16:14:19 -0400956 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400957 err_on_enospc = 1;
958
Chris Mason5f39d392007-10-15 16:14:19 -0400959 left = read_node_slot(root, parent, pslot - 1);
960 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400961 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500962 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400963 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400964 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400965 if (wret) {
966 ret = wret;
967 goto enospc;
968 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400969 }
Chris Mason5f39d392007-10-15 16:14:19 -0400970 right = read_node_slot(root, parent, pslot + 1);
971 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400972 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500973 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400974 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400975 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400976 if (wret) {
977 ret = wret;
978 goto enospc;
979 }
980 }
981
982 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400983 if (left) {
984 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400985 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500986 if (wret < 0)
987 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400988 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400989 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500990 }
Chris Mason79f95c82007-03-01 15:16:26 -0500991
992 /*
993 * then try to empty the right most buffer into the middle
994 */
Chris Mason5f39d392007-10-15 16:14:19 -0400995 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400996 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400997 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500998 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400999 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001000 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -05001001 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001002 u32 blocksize = right->len;
1003
Chris Mason5f39d392007-10-15 16:14:19 -04001004 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001005 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001006 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001007 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001008 wret = del_ptr(trans, root, path, level + 1, pslot +
1009 1);
Chris Masonbb803952007-03-01 12:04:21 -05001010 if (wret)
1011 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001012 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001013 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001014 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001015 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001016 if (wret)
1017 ret = wret;
1018 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001019 struct btrfs_disk_key right_key;
1020 btrfs_node_key(right, &right_key, 0);
1021 btrfs_set_node_key(parent, &right_key, pslot + 1);
1022 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001023 }
1024 }
Chris Mason5f39d392007-10-15 16:14:19 -04001025 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001026 /*
1027 * we're not allowed to leave a node with one item in the
1028 * tree during a delete. A deletion from lower in the tree
1029 * could try to delete the only pointer in this node.
1030 * So, pull some keys from the left.
1031 * There has to be a left pointer at this point because
1032 * otherwise we would have pulled some pointers from the
1033 * right
1034 */
Chris Mason5f39d392007-10-15 16:14:19 -04001035 BUG_ON(!left);
1036 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001037 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001038 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001039 goto enospc;
1040 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001041 if (wret == 1) {
1042 wret = push_node_left(trans, root, left, mid, 1);
1043 if (wret < 0)
1044 ret = wret;
1045 }
Chris Mason79f95c82007-03-01 15:16:26 -05001046 BUG_ON(wret == 1);
1047 }
Chris Mason5f39d392007-10-15 16:14:19 -04001048 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001049 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001050 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001051 u64 bytenr = mid->start;
1052 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001053
Chris Mason5f39d392007-10-15 16:14:19 -04001054 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001055 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001056 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001057 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001058 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001059 if (wret)
1060 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001061 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001062 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001063 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001064 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001065 if (wret)
1066 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001067 } else {
1068 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001069 struct btrfs_disk_key mid_key;
1070 btrfs_node_key(mid, &mid_key, 0);
1071 btrfs_set_node_key(parent, &mid_key, pslot);
1072 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001073 }
Chris Masonbb803952007-03-01 12:04:21 -05001074
Chris Mason79f95c82007-03-01 15:16:26 -05001075 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001076 if (left) {
1077 if (btrfs_header_nritems(left) > orig_slot) {
1078 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001079 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001080 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001081 path->slots[level + 1] -= 1;
1082 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001083 if (mid) {
1084 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001085 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001086 }
Chris Masonbb803952007-03-01 12:04:21 -05001087 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001088 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001089 path->slots[level] = orig_slot;
1090 }
1091 }
Chris Mason79f95c82007-03-01 15:16:26 -05001092 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001093 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001094 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001095 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001096 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001097enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001098 if (right) {
1099 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001100 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001101 }
1102 if (left) {
1103 if (path->nodes[level] != left)
1104 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001105 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001106 }
Chris Masonbb803952007-03-01 12:04:21 -05001107 return ret;
1108}
1109
Chris Masond352ac62008-09-29 15:18:18 -04001110/* Node balancing for insertion. Here we only split or push nodes around
1111 * when they are completely full. This is also done top down, so we
1112 * have to be pessimistic.
1113 */
Chris Masond3977122009-01-05 21:25:51 -05001114static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001115 struct btrfs_root *root,
1116 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001117{
Chris Mason5f39d392007-10-15 16:14:19 -04001118 struct extent_buffer *right = NULL;
1119 struct extent_buffer *mid;
1120 struct extent_buffer *left = NULL;
1121 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001122 int ret = 0;
1123 int wret;
1124 int pslot;
1125 int orig_slot = path->slots[level];
1126 u64 orig_ptr;
1127
1128 if (level == 0)
1129 return 1;
1130
Chris Mason5f39d392007-10-15 16:14:19 -04001131 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001132 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001133 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1134
1135 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001136 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001137 pslot = path->slots[level + 1];
1138
Chris Mason5f39d392007-10-15 16:14:19 -04001139 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001140 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001141
Chris Mason5f39d392007-10-15 16:14:19 -04001142 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001143
1144 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001145 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001146 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001147
1148 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001149 btrfs_set_lock_blocking(left);
1150
Chris Mason5f39d392007-10-15 16:14:19 -04001151 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001152 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1153 wret = 1;
1154 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001155 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001156 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001157 if (ret)
1158 wret = 1;
1159 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001160 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001161 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001162 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001163 }
Chris Masone66f7092007-04-20 13:16:02 -04001164 if (wret < 0)
1165 ret = wret;
1166 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001167 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001168 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001169 btrfs_node_key(mid, &disk_key, 0);
1170 btrfs_set_node_key(parent, &disk_key, pslot);
1171 btrfs_mark_buffer_dirty(parent);
1172 if (btrfs_header_nritems(left) > orig_slot) {
1173 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001174 path->slots[level + 1] -= 1;
1175 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001176 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001177 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001178 } else {
1179 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001180 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001181 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001182 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001183 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001184 }
Chris Masone66f7092007-04-20 13:16:02 -04001185 return 0;
1186 }
Chris Mason925baed2008-06-25 16:01:30 -04001187 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001188 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001189 }
Chris Mason925baed2008-06-25 16:01:30 -04001190 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001191
1192 /*
1193 * then try to empty the right most buffer into the middle
1194 */
Chris Mason5f39d392007-10-15 16:14:19 -04001195 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001196 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001197
Chris Mason925baed2008-06-25 16:01:30 -04001198 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001199 btrfs_set_lock_blocking(right);
1200
Chris Mason5f39d392007-10-15 16:14:19 -04001201 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001202 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1203 wret = 1;
1204 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001205 ret = btrfs_cow_block(trans, root, right,
1206 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001207 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001208 if (ret)
1209 wret = 1;
1210 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001211 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001212 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001213 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001214 }
Chris Masone66f7092007-04-20 13:16:02 -04001215 if (wret < 0)
1216 ret = wret;
1217 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001218 struct btrfs_disk_key disk_key;
1219
1220 btrfs_node_key(right, &disk_key, 0);
1221 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1222 btrfs_mark_buffer_dirty(parent);
1223
1224 if (btrfs_header_nritems(mid) <= orig_slot) {
1225 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001226 path->slots[level + 1] += 1;
1227 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001228 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001229 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001230 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001231 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001232 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001233 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001234 }
Chris Masone66f7092007-04-20 13:16:02 -04001235 return 0;
1236 }
Chris Mason925baed2008-06-25 16:01:30 -04001237 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001238 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001239 }
Chris Masone66f7092007-04-20 13:16:02 -04001240 return 1;
1241}
1242
Chris Mason74123bd2007-02-02 11:05:29 -05001243/*
Chris Masond352ac62008-09-29 15:18:18 -04001244 * readahead one full node of leaves, finding things that are close
1245 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001246 */
Chris Masone02119d2008-09-05 16:13:11 -04001247static noinline void reada_for_search(struct btrfs_root *root,
1248 struct btrfs_path *path,
1249 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001250{
Chris Mason5f39d392007-10-15 16:14:19 -04001251 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001252 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001253 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001254 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001255 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001256 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001257 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001258 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001259 u32 nr;
1260 u32 blocksize;
1261 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001262
Chris Masona6b6e752007-10-15 16:22:39 -04001263 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001264 return;
1265
Chris Mason6702ed42007-08-07 16:15:09 -04001266 if (!path->nodes[level])
1267 return;
1268
Chris Mason5f39d392007-10-15 16:14:19 -04001269 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001270
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001272 blocksize = btrfs_level_size(root, level - 1);
1273 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001274 if (eb) {
1275 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001276 return;
1277 }
1278
Chris Masona7175312009-01-22 09:23:10 -05001279 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001280
Chris Mason5f39d392007-10-15 16:14:19 -04001281 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001282 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001283 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001284 if (direction < 0) {
1285 if (nr == 0)
1286 break;
1287 nr--;
1288 } else if (direction > 0) {
1289 nr++;
1290 if (nr >= nritems)
1291 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001292 }
Chris Mason01f46652007-12-21 16:24:26 -05001293 if (path->reada < 0 && objectid) {
1294 btrfs_node_key(node, &disk_key, nr);
1295 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1296 break;
1297 }
Chris Mason6b800532007-10-15 16:17:34 -04001298 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001299 if ((search <= target && target - search <= 65536) ||
1300 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001301 readahead_tree_block(root, search, blocksize,
1302 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001303 nread += blocksize;
1304 }
1305 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001306 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001307 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001308 }
1309}
Chris Mason925baed2008-06-25 16:01:30 -04001310
Chris Masond352ac62008-09-29 15:18:18 -04001311/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001312 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1313 * cache
1314 */
1315static noinline int reada_for_balance(struct btrfs_root *root,
1316 struct btrfs_path *path, int level)
1317{
1318 int slot;
1319 int nritems;
1320 struct extent_buffer *parent;
1321 struct extent_buffer *eb;
1322 u64 gen;
1323 u64 block1 = 0;
1324 u64 block2 = 0;
1325 int ret = 0;
1326 int blocksize;
1327
1328 parent = path->nodes[level - 1];
1329 if (!parent)
1330 return 0;
1331
1332 nritems = btrfs_header_nritems(parent);
1333 slot = path->slots[level];
1334 blocksize = btrfs_level_size(root, level);
1335
1336 if (slot > 0) {
1337 block1 = btrfs_node_blockptr(parent, slot - 1);
1338 gen = btrfs_node_ptr_generation(parent, slot - 1);
1339 eb = btrfs_find_tree_block(root, block1, blocksize);
1340 if (eb && btrfs_buffer_uptodate(eb, gen))
1341 block1 = 0;
1342 free_extent_buffer(eb);
1343 }
1344 if (slot < nritems) {
1345 block2 = btrfs_node_blockptr(parent, slot + 1);
1346 gen = btrfs_node_ptr_generation(parent, slot + 1);
1347 eb = btrfs_find_tree_block(root, block2, blocksize);
1348 if (eb && btrfs_buffer_uptodate(eb, gen))
1349 block2 = 0;
1350 free_extent_buffer(eb);
1351 }
1352 if (block1 || block2) {
1353 ret = -EAGAIN;
1354 btrfs_release_path(root, path);
1355 if (block1)
1356 readahead_tree_block(root, block1, blocksize, 0);
1357 if (block2)
1358 readahead_tree_block(root, block2, blocksize, 0);
1359
1360 if (block1) {
1361 eb = read_tree_block(root, block1, blocksize, 0);
1362 free_extent_buffer(eb);
1363 }
1364 if (block1) {
1365 eb = read_tree_block(root, block2, blocksize, 0);
1366 free_extent_buffer(eb);
1367 }
1368 }
1369 return ret;
1370}
1371
1372
1373/*
Chris Masond3977122009-01-05 21:25:51 -05001374 * when we walk down the tree, it is usually safe to unlock the higher layers
1375 * in the tree. The exceptions are when our path goes through slot 0, because
1376 * operations on the tree might require changing key pointers higher up in the
1377 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001378 *
Chris Masond3977122009-01-05 21:25:51 -05001379 * callers might also have set path->keep_locks, which tells this code to keep
1380 * the lock if the path points to the last slot in the block. This is part of
1381 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001382 *
Chris Masond3977122009-01-05 21:25:51 -05001383 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1384 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001385 */
Chris Masone02119d2008-09-05 16:13:11 -04001386static noinline void unlock_up(struct btrfs_path *path, int level,
1387 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001388{
1389 int i;
1390 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001391 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001392 struct extent_buffer *t;
1393
1394 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1395 if (!path->nodes[i])
1396 break;
1397 if (!path->locks[i])
1398 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001399 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001400 skip_level = i + 1;
1401 continue;
1402 }
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001404 u32 nritems;
1405 t = path->nodes[i];
1406 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001407 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001408 skip_level = i + 1;
1409 continue;
1410 }
1411 }
Chris Mason051e1b92008-06-25 16:01:30 -04001412 if (skip_level < i && i >= lowest_unlock)
1413 no_skips = 1;
1414
Chris Mason925baed2008-06-25 16:01:30 -04001415 t = path->nodes[i];
1416 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1417 btrfs_tree_unlock(t);
1418 path->locks[i] = 0;
1419 }
1420 }
1421}
1422
Chris Mason3c69fae2007-08-07 15:52:22 -04001423/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001424 * This releases any locks held in the path starting at level and
1425 * going all the way up to the root.
1426 *
1427 * btrfs_search_slot will keep the lock held on higher nodes in a few
1428 * corner cases, such as COW of the block at slot zero in the node. This
1429 * ignores those rules, and it should only be called when there are no
1430 * more updates to be done higher up in the tree.
1431 */
1432noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1433{
1434 int i;
1435
1436 if (path->keep_locks || path->lowest_level)
1437 return;
1438
1439 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1440 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001441 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001442 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001443 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001444 btrfs_tree_unlock(path->nodes[i]);
1445 path->locks[i] = 0;
1446 }
1447}
1448
1449/*
Chris Mason74123bd2007-02-02 11:05:29 -05001450 * look for key in the tree. path is filled in with nodes along the way
1451 * if key is found, we return zero and you can find the item in the leaf
1452 * level of the path (level 0)
1453 *
1454 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001455 * be inserted, and 1 is returned. If there are other errors during the
1456 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001457 *
1458 * if ins_len > 0, nodes and leaves will be split as we walk down the
1459 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1460 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001461 */
Chris Masone089f052007-03-16 16:20:31 -04001462int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1463 *root, struct btrfs_key *key, struct btrfs_path *p, int
1464 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001465{
Chris Mason5f39d392007-10-15 16:14:19 -04001466 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001467 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001468 int slot;
1469 int ret;
1470 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001471 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001472 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001473 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001474 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001475 u64 blocknr;
1476 u64 gen;
Chris Mason9f3a7422007-08-07 15:52:19 -04001477
Chris Mason6702ed42007-08-07 16:15:09 -04001478 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001479 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001480 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001481
Chris Mason925baed2008-06-25 16:01:30 -04001482 if (ins_len < 0)
1483 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001484
Chris Masonbb803952007-03-01 12:04:21 -05001485again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001486 if (p->skip_locking)
1487 b = btrfs_root_node(root);
1488 else
1489 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001490
Chris Masoneb60cea2007-02-02 09:18:22 -05001491 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001492 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001493
1494 /*
1495 * setup the path here so we can release it under lock
1496 * contention with the cow code
1497 */
1498 p->nodes[level] = b;
1499 if (!p->skip_locking)
1500 p->locks[level] = 1;
1501
Chris Mason02217ed2007-03-02 16:08:05 -05001502 if (cow) {
1503 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001504
1505 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001506 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001507 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001508 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001509 goto cow_done;
1510 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001511 btrfs_set_path_blocking(p);
1512
Chris Masone20d96d2007-03-22 12:13:20 -04001513 wret = btrfs_cow_block(trans, root, b,
1514 p->nodes[level + 1],
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001515 p->slots[level + 1], &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001516 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001517 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001518 ret = wret;
1519 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001520 }
Chris Mason02217ed2007-03-02 16:08:05 -05001521 }
Chris Mason65b51a02008-08-01 15:11:20 -04001522cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001523 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001524 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001525 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001526 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001527
Chris Masoneb60cea2007-02-02 09:18:22 -05001528 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001529 if (!p->skip_locking)
1530 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001531
Chris Mason4008c042009-02-12 14:09:45 -05001532 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001533
1534 /*
1535 * we have a lock on b and as long as we aren't changing
1536 * the tree, there is no way to for the items in b to change.
1537 * It is safe to drop the lock on our parent before we
1538 * go through the expensive btree search on b.
1539 *
1540 * If cow is true, then we might be changing slot zero,
1541 * which may require changing the parent. So, we can't
1542 * drop the lock until after we know which slot we're
1543 * operating on.
1544 */
1545 if (!cow)
1546 btrfs_unlock_up_safe(p, level + 1);
1547
Chris Mason123abc82007-03-14 14:14:43 -04001548 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001549 if (ret) {
1550 ret = -1;
1551 goto done;
1552 }
Chris Mason925baed2008-06-25 16:01:30 -04001553
Chris Mason5f39d392007-10-15 16:14:19 -04001554 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001555
Chris Mason5f39d392007-10-15 16:14:19 -04001556 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001557 if (ret && slot > 0)
1558 slot -= 1;
1559 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001560 if ((p->search_for_split || ins_len > 0) &&
1561 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001562 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001563 int sret;
1564
1565 sret = reada_for_balance(root, p, level);
1566 if (sret)
1567 goto again;
1568
1569 btrfs_set_path_blocking(p);
1570 sret = split_node(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001571 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001572
Chris Mason5c680ed2007-02-22 11:39:13 -05001573 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001574 if (sret) {
1575 ret = sret;
1576 goto done;
1577 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001578 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001579 slot = p->slots[level];
Chris Mason7b78c172009-02-04 09:12:46 -05001580 } else if (ins_len < 0 &&
1581 btrfs_header_nritems(b) <
1582 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001583 int sret;
1584
1585 sret = reada_for_balance(root, p, level);
1586 if (sret)
1587 goto again;
1588
1589 btrfs_set_path_blocking(p);
1590 sret = balance_level(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001591 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001592
Chris Mason65b51a02008-08-01 15:11:20 -04001593 if (sret) {
1594 ret = sret;
1595 goto done;
1596 }
Chris Masonbb803952007-03-01 12:04:21 -05001597 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001598 if (!b) {
1599 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001600 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001601 }
Chris Masonbb803952007-03-01 12:04:21 -05001602 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001603 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001604 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001605 unlock_up(p, level, lowest_unlock);
1606
Chris Mason9f3a7422007-08-07 15:52:19 -04001607 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001608 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001609 ret = 0;
1610 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001611 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001612
Chris Mason594a24e2008-06-25 16:01:30 -04001613 blocknr = btrfs_node_blockptr(b, slot);
1614 gen = btrfs_node_ptr_generation(b, slot);
1615 blocksize = btrfs_level_size(root, level - 1);
1616
1617 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1618 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001619 b = tmp;
1620 } else {
1621 /*
1622 * reduce lock contention at high levels
1623 * of the btree by dropping locks before
1624 * we read.
1625 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001626 if (level > 0) {
Chris Mason051e1b92008-06-25 16:01:30 -04001627 btrfs_release_path(NULL, p);
1628 if (tmp)
1629 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001630 if (should_reada)
1631 reada_for_search(root, p,
1632 level, slot,
1633 key->objectid);
1634
Chris Mason594a24e2008-06-25 16:01:30 -04001635 tmp = read_tree_block(root, blocknr,
1636 blocksize, gen);
1637 if (tmp)
1638 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001639 goto again;
1640 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001641 btrfs_set_path_blocking(p);
Chris Masona74a4b92008-06-25 16:01:31 -04001642 if (tmp)
1643 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001644 if (should_reada)
1645 reada_for_search(root, p,
1646 level, slot,
1647 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001648 b = read_node_slot(root, b, slot);
1649 }
1650 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001651 if (!p->skip_locking) {
1652 int lret;
1653
Chris Mason4008c042009-02-12 14:09:45 -05001654 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001655 lret = btrfs_try_spin_lock(b);
1656
1657 if (!lret) {
1658 btrfs_set_path_blocking(p);
1659 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001660 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001661 }
1662 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001663 } else {
1664 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001665 if (ins_len > 0 &&
1666 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001667 int sret;
1668
1669 btrfs_set_path_blocking(p);
1670 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001671 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001672 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001673
Chris Mason5c680ed2007-02-22 11:39:13 -05001674 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001675 if (sret) {
1676 ret = sret;
1677 goto done;
1678 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001679 }
Chris Mason459931e2008-12-10 09:10:46 -05001680 if (!p->search_for_split)
1681 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001682 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001683 }
1684 }
Chris Mason65b51a02008-08-01 15:11:20 -04001685 ret = 1;
1686done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001687 /*
1688 * we don't really know what they plan on doing with the path
1689 * from here on, so for now just mark it as blocking
1690 */
Chris Masonb9473432009-03-13 11:00:37 -04001691 if (!p->leave_spinning)
1692 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001693 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001694}
1695
Zheng Yan1a40e232008-09-26 10:09:34 -04001696int btrfs_merge_path(struct btrfs_trans_handle *trans,
1697 struct btrfs_root *root,
1698 struct btrfs_key *node_keys,
1699 u64 *nodes, int lowest_level)
1700{
1701 struct extent_buffer *eb;
1702 struct extent_buffer *parent;
1703 struct btrfs_key key;
1704 u64 bytenr;
1705 u64 generation;
1706 u32 blocksize;
1707 int level;
1708 int slot;
1709 int key_match;
1710 int ret;
1711
1712 eb = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001713 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001714 BUG_ON(ret);
1715
Chris Masonb4ce94d2009-02-04 09:25:08 -05001716 btrfs_set_lock_blocking(eb);
1717
Zheng Yan1a40e232008-09-26 10:09:34 -04001718 parent = eb;
1719 while (1) {
1720 level = btrfs_header_level(parent);
1721 if (level == 0 || level <= lowest_level)
1722 break;
1723
1724 ret = bin_search(parent, &node_keys[lowest_level], level,
1725 &slot);
1726 if (ret && slot > 0)
1727 slot--;
1728
1729 bytenr = btrfs_node_blockptr(parent, slot);
1730 if (nodes[level - 1] == bytenr)
1731 break;
1732
1733 blocksize = btrfs_level_size(root, level - 1);
1734 generation = btrfs_node_ptr_generation(parent, slot);
1735 btrfs_node_key_to_cpu(eb, &key, slot);
1736 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1737
Yan Zhengf82d02d2008-10-29 14:49:05 -04001738 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001739 eb = read_tree_block(root, bytenr, blocksize,
1740 generation);
1741 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001742 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001743 }
1744
1745 /*
1746 * if node keys match and node pointer hasn't been modified
1747 * in the running transaction, we can merge the path. for
1748 * blocks owened by reloc trees, the node pointer check is
1749 * skipped, this is because these blocks are fully controlled
1750 * by the space balance code, no one else can modify them.
1751 */
1752 if (!nodes[level - 1] || !key_match ||
1753 (generation == trans->transid &&
1754 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1755 if (level == 1 || level == lowest_level + 1) {
1756 if (generation == trans->transid) {
1757 btrfs_tree_unlock(eb);
1758 free_extent_buffer(eb);
1759 }
1760 break;
1761 }
1762
1763 if (generation != trans->transid) {
1764 eb = read_tree_block(root, bytenr, blocksize,
1765 generation);
1766 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001767 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001768 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001769
1770 ret = btrfs_cow_block(trans, root, eb, parent, slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001771 &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001772 BUG_ON(ret);
1773
Yan Zhengf82d02d2008-10-29 14:49:05 -04001774 if (root->root_key.objectid ==
1775 BTRFS_TREE_RELOC_OBJECTID) {
1776 if (!nodes[level - 1]) {
1777 nodes[level - 1] = eb->start;
1778 memcpy(&node_keys[level - 1], &key,
1779 sizeof(node_keys[0]));
1780 } else {
1781 WARN_ON(1);
1782 }
1783 }
1784
Zheng Yan1a40e232008-09-26 10:09:34 -04001785 btrfs_tree_unlock(parent);
1786 free_extent_buffer(parent);
1787 parent = eb;
1788 continue;
1789 }
1790
Zheng Yan1a40e232008-09-26 10:09:34 -04001791 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1792 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1793 btrfs_mark_buffer_dirty(parent);
1794
1795 ret = btrfs_inc_extent_ref(trans, root,
1796 nodes[level - 1],
1797 blocksize, parent->start,
1798 btrfs_header_owner(parent),
1799 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001800 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001801 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001802
1803 /*
1804 * If the block was created in the running transaction,
1805 * it's possible this is the last reference to it, so we
1806 * should drop the subtree.
1807 */
1808 if (generation == trans->transid) {
1809 ret = btrfs_drop_subtree(trans, root, eb, parent);
1810 BUG_ON(ret);
1811 btrfs_tree_unlock(eb);
1812 free_extent_buffer(eb);
1813 } else {
1814 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001815 blocksize, parent->start,
1816 btrfs_header_owner(parent),
1817 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001818 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001819 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001820 }
1821 break;
1822 }
1823 btrfs_tree_unlock(parent);
1824 free_extent_buffer(parent);
1825 return 0;
1826}
1827
Chris Mason74123bd2007-02-02 11:05:29 -05001828/*
1829 * adjust the pointers going up the tree, starting at level
1830 * making sure the right key of each node is points to 'key'.
1831 * This is used after shifting pointers to the left, so it stops
1832 * fixing up pointers when a given leaf/node is not in slot 0 of the
1833 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001834 *
1835 * If this fails to write a tree block, it returns -1, but continues
1836 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001837 */
Chris Mason5f39d392007-10-15 16:14:19 -04001838static int fixup_low_keys(struct btrfs_trans_handle *trans,
1839 struct btrfs_root *root, struct btrfs_path *path,
1840 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001841{
1842 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001843 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001844 struct extent_buffer *t;
1845
Chris Mason234b63a2007-03-13 10:46:10 -04001846 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001847 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001848 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001849 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001850 t = path->nodes[i];
1851 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001852 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001853 if (tslot != 0)
1854 break;
1855 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001856 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001857}
1858
Chris Mason74123bd2007-02-02 11:05:29 -05001859/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001860 * update item key.
1861 *
1862 * This function isn't completely safe. It's the caller's responsibility
1863 * that the new key won't break the order
1864 */
1865int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1866 struct btrfs_root *root, struct btrfs_path *path,
1867 struct btrfs_key *new_key)
1868{
1869 struct btrfs_disk_key disk_key;
1870 struct extent_buffer *eb;
1871 int slot;
1872
1873 eb = path->nodes[0];
1874 slot = path->slots[0];
1875 if (slot > 0) {
1876 btrfs_item_key(eb, &disk_key, slot - 1);
1877 if (comp_keys(&disk_key, new_key) >= 0)
1878 return -1;
1879 }
1880 if (slot < btrfs_header_nritems(eb) - 1) {
1881 btrfs_item_key(eb, &disk_key, slot + 1);
1882 if (comp_keys(&disk_key, new_key) <= 0)
1883 return -1;
1884 }
1885
1886 btrfs_cpu_key_to_disk(&disk_key, new_key);
1887 btrfs_set_item_key(eb, &disk_key, slot);
1888 btrfs_mark_buffer_dirty(eb);
1889 if (slot == 0)
1890 fixup_low_keys(trans, root, path, &disk_key, 1);
1891 return 0;
1892}
1893
1894/*
Chris Mason74123bd2007-02-02 11:05:29 -05001895 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001896 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001897 *
1898 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1899 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001900 */
Chris Mason98ed5172008-01-03 10:01:48 -05001901static int push_node_left(struct btrfs_trans_handle *trans,
1902 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001903 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001904{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001905 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001906 int src_nritems;
1907 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001908 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001909
Chris Mason5f39d392007-10-15 16:14:19 -04001910 src_nritems = btrfs_header_nritems(src);
1911 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001912 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001913 WARN_ON(btrfs_header_generation(src) != trans->transid);
1914 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001915
Chris Masonbce4eae2008-04-24 14:42:46 -04001916 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001917 return 1;
1918
Chris Masond3977122009-01-05 21:25:51 -05001919 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001920 return 1;
1921
Chris Masonbce4eae2008-04-24 14:42:46 -04001922 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001923 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001924 if (push_items < src_nritems) {
1925 /* leave at least 8 pointers in the node if
1926 * we aren't going to empty it
1927 */
1928 if (src_nritems - push_items < 8) {
1929 if (push_items <= 8)
1930 return 1;
1931 push_items -= 8;
1932 }
1933 }
1934 } else
1935 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001936
Chris Mason5f39d392007-10-15 16:14:19 -04001937 copy_extent_buffer(dst, src,
1938 btrfs_node_key_ptr_offset(dst_nritems),
1939 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001940 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001941
Chris Masonbb803952007-03-01 12:04:21 -05001942 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001943 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1944 btrfs_node_key_ptr_offset(push_items),
1945 (src_nritems - push_items) *
1946 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001947 }
Chris Mason5f39d392007-10-15 16:14:19 -04001948 btrfs_set_header_nritems(src, src_nritems - push_items);
1949 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1950 btrfs_mark_buffer_dirty(src);
1951 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001952
1953 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1954 BUG_ON(ret);
1955
Chris Masonbb803952007-03-01 12:04:21 -05001956 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001957}
1958
Chris Mason97571fd2007-02-24 13:39:08 -05001959/*
Chris Mason79f95c82007-03-01 15:16:26 -05001960 * try to push data from one node into the next node right in the
1961 * tree.
1962 *
1963 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1964 * error, and > 0 if there was no room in the right hand block.
1965 *
1966 * this will only push up to 1/2 the contents of the left node over
1967 */
Chris Mason5f39d392007-10-15 16:14:19 -04001968static int balance_node_right(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *root,
1970 struct extent_buffer *dst,
1971 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001972{
Chris Mason79f95c82007-03-01 15:16:26 -05001973 int push_items = 0;
1974 int max_push;
1975 int src_nritems;
1976 int dst_nritems;
1977 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001978
Chris Mason7bb86312007-12-11 09:25:06 -05001979 WARN_ON(btrfs_header_generation(src) != trans->transid);
1980 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1981
Chris Mason5f39d392007-10-15 16:14:19 -04001982 src_nritems = btrfs_header_nritems(src);
1983 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001984 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001985 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001986 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001987
Chris Masond3977122009-01-05 21:25:51 -05001988 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001989 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001990
1991 max_push = src_nritems / 2 + 1;
1992 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001993 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001994 return 1;
Yan252c38f2007-08-29 09:11:44 -04001995
Chris Mason79f95c82007-03-01 15:16:26 -05001996 if (max_push < push_items)
1997 push_items = max_push;
1998
Chris Mason5f39d392007-10-15 16:14:19 -04001999 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2000 btrfs_node_key_ptr_offset(0),
2001 (dst_nritems) *
2002 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002003
Chris Mason5f39d392007-10-15 16:14:19 -04002004 copy_extent_buffer(dst, src,
2005 btrfs_node_key_ptr_offset(0),
2006 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002007 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002008
Chris Mason5f39d392007-10-15 16:14:19 -04002009 btrfs_set_header_nritems(src, src_nritems - push_items);
2010 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002011
Chris Mason5f39d392007-10-15 16:14:19 -04002012 btrfs_mark_buffer_dirty(src);
2013 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002014
2015 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2016 BUG_ON(ret);
2017
Chris Mason79f95c82007-03-01 15:16:26 -05002018 return ret;
2019}
2020
2021/*
Chris Mason97571fd2007-02-24 13:39:08 -05002022 * helper function to insert a new root level in the tree.
2023 * A new node is allocated, and a single item is inserted to
2024 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002025 *
2026 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002027 */
Chris Masond3977122009-01-05 21:25:51 -05002028static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002029 struct btrfs_root *root,
2030 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002031{
Chris Mason7bb86312007-12-11 09:25:06 -05002032 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct extent_buffer *lower;
2034 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002035 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002036 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002037 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002038
2039 BUG_ON(path->nodes[level]);
2040 BUG_ON(path->nodes[level-1] != root->node);
2041
Chris Mason7bb86312007-12-11 09:25:06 -05002042 lower = path->nodes[level-1];
2043 if (level == 1)
2044 btrfs_item_key(lower, &lower_key, 0);
2045 else
2046 btrfs_node_key(lower, &lower_key, 0);
2047
Zheng Yan31840ae2008-09-23 13:14:14 -04002048 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2049 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002050 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 if (IS_ERR(c))
2052 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002053
Chris Mason5f39d392007-10-15 16:14:19 -04002054 memset_extent_buffer(c, 0, 0, root->nodesize);
2055 btrfs_set_header_nritems(c, 1);
2056 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002057 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002058 btrfs_set_header_generation(c, trans->transid);
2059 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002060
Chris Mason5f39d392007-10-15 16:14:19 -04002061 write_extent_buffer(c, root->fs_info->fsid,
2062 (unsigned long)btrfs_header_fsid(c),
2063 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002064
2065 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2066 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2067 BTRFS_UUID_SIZE);
2068
Chris Mason5f39d392007-10-15 16:14:19 -04002069 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002070 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002071 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002072 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002073
2074 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002075
2076 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002077
Chris Mason925baed2008-06-25 16:01:30 -04002078 spin_lock(&root->node_lock);
2079 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002080 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002081 spin_unlock(&root->node_lock);
2082
Zheng Yan31840ae2008-09-23 13:14:14 -04002083 ret = btrfs_update_extent_ref(trans, root, lower->start,
Chris Mason56bec292009-03-13 10:10:06 -04002084 lower->len, lower->start, c->start,
Zheng Yan31840ae2008-09-23 13:14:14 -04002085 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002086 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002087 BUG_ON(ret);
2088
Chris Mason925baed2008-06-25 16:01:30 -04002089 /* the super has an extra ref to root->node */
2090 free_extent_buffer(old);
2091
Chris Mason0b86a832008-03-24 15:01:56 -04002092 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002093 extent_buffer_get(c);
2094 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002095 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002096 path->slots[level] = 0;
2097 return 0;
2098}
2099
Chris Mason74123bd2007-02-02 11:05:29 -05002100/*
2101 * worker function to insert a single pointer in a node.
2102 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002103 *
Chris Mason74123bd2007-02-02 11:05:29 -05002104 * slot and level indicate where you want the key to go, and
2105 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002106 *
2107 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002108 */
Chris Masone089f052007-03-16 16:20:31 -04002109static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2110 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002111 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002112{
Chris Mason5f39d392007-10-15 16:14:19 -04002113 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002114 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002115
2116 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002117 lower = path->nodes[level];
2118 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002119 if (slot > nritems)
2120 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002121 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002122 BUG();
2123 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002124 memmove_extent_buffer(lower,
2125 btrfs_node_key_ptr_offset(slot + 1),
2126 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002127 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002128 }
Chris Mason5f39d392007-10-15 16:14:19 -04002129 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002130 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002131 WARN_ON(trans->transid == 0);
2132 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002133 btrfs_set_header_nritems(lower, nritems + 1);
2134 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002135 return 0;
2136}
2137
Chris Mason97571fd2007-02-24 13:39:08 -05002138/*
2139 * split the node at the specified level in path in two.
2140 * The path is corrected to point to the appropriate node after the split
2141 *
2142 * Before splitting this tries to make some room in the node by pushing
2143 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002144 *
2145 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002146 */
Chris Masone02119d2008-09-05 16:13:11 -04002147static noinline int split_node(struct btrfs_trans_handle *trans,
2148 struct btrfs_root *root,
2149 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002150{
Chris Mason5f39d392007-10-15 16:14:19 -04002151 struct extent_buffer *c;
2152 struct extent_buffer *split;
2153 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002154 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002155 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002156 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002157 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002158
Chris Mason5f39d392007-10-15 16:14:19 -04002159 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002160 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002162 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002163 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002164 if (ret)
2165 return ret;
Chris Masona4b6e072009-03-16 10:59:57 -04002166 } else if (!trans->transaction->delayed_refs.flushing) {
Chris Masone66f7092007-04-20 13:16:02 -04002167 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 c = path->nodes[level];
2169 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002170 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002171 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002172 if (ret < 0)
2173 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002174 }
Chris Masone66f7092007-04-20 13:16:02 -04002175
Chris Mason5f39d392007-10-15 16:14:19 -04002176 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002177
Chris Mason925baed2008-06-25 16:01:30 -04002178 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002179 path->nodes[level + 1]->start,
2180 root->root_key.objectid,
2181 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 if (IS_ERR(split))
2183 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002184
Chris Mason5f39d392007-10-15 16:14:19 -04002185 btrfs_set_header_flags(split, btrfs_header_flags(c));
2186 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002187 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002188 btrfs_set_header_generation(split, trans->transid);
2189 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002190 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002191 write_extent_buffer(split, root->fs_info->fsid,
2192 (unsigned long)btrfs_header_fsid(split),
2193 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002194 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2195 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2196 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002197
Chris Mason7518a232007-03-12 12:01:18 -04002198 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002199
2200 copy_extent_buffer(split, c,
2201 btrfs_node_key_ptr_offset(0),
2202 btrfs_node_key_ptr_offset(mid),
2203 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2204 btrfs_set_header_nritems(split, c_nritems - mid);
2205 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002206 ret = 0;
2207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 btrfs_mark_buffer_dirty(c);
2209 btrfs_mark_buffer_dirty(split);
2210
2211 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002212 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002213 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002214 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002215 if (wret)
2216 ret = wret;
2217
Zheng Yan31840ae2008-09-23 13:14:14 -04002218 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2219 BUG_ON(ret);
2220
Chris Mason5de08d72007-02-24 06:24:44 -05002221 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002222 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002223 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002224 free_extent_buffer(c);
2225 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002226 path->slots[level + 1] += 1;
2227 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002228 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002229 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002230 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002231 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002232}
2233
Chris Mason74123bd2007-02-02 11:05:29 -05002234/*
2235 * how many bytes are required to store the items in a leaf. start
2236 * and nr indicate which items in the leaf to check. This totals up the
2237 * space used both by the item structs and the item data
2238 */
Chris Mason5f39d392007-10-15 16:14:19 -04002239static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002240{
2241 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002242 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002243 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002244
2245 if (!nr)
2246 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002247 data_len = btrfs_item_end_nr(l, start);
2248 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002249 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002250 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002251 return data_len;
2252}
2253
Chris Mason74123bd2007-02-02 11:05:29 -05002254/*
Chris Masond4dbff92007-04-04 14:08:15 -04002255 * The space between the end of the leaf items and
2256 * the start of the leaf data. IOW, how much room
2257 * the leaf has left for both items and data
2258 */
Chris Masond3977122009-01-05 21:25:51 -05002259noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002260 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002261{
Chris Mason5f39d392007-10-15 16:14:19 -04002262 int nritems = btrfs_header_nritems(leaf);
2263 int ret;
2264 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2265 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002266 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2267 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002268 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002269 leaf_space_used(leaf, 0, nritems), nritems);
2270 }
2271 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002272}
2273
Chris Mason44871b12009-03-13 10:04:31 -04002274static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2275 struct btrfs_root *root,
2276 struct btrfs_path *path,
2277 int data_size, int empty,
2278 struct extent_buffer *right,
2279 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002280{
Chris Mason5f39d392007-10-15 16:14:19 -04002281 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002282 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002283 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 push_space = 0;
2287 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002288 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002289 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002290 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002291 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002292 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002293 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002294
Chris Mason34a38212007-11-07 13:31:03 -05002295 if (empty)
2296 nr = 0;
2297 else
2298 nr = 1;
2299
Zheng Yan31840ae2008-09-23 13:14:14 -04002300 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002301 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002302
Chris Mason44871b12009-03-13 10:04:31 -04002303 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002304 i = left_nritems - 1;
2305 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002306 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002307
Zheng Yan31840ae2008-09-23 13:14:14 -04002308 if (!empty && push_items > 0) {
2309 if (path->slots[0] > i)
2310 break;
2311 if (path->slots[0] == i) {
2312 int space = btrfs_leaf_free_space(root, left);
2313 if (space + push_space * 2 > free_space)
2314 break;
2315 }
2316 }
2317
Chris Mason00ec4c52007-02-24 12:47:20 -05002318 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002319 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002320
2321 if (!left->map_token) {
2322 map_extent_buffer(left, (unsigned long)item,
2323 sizeof(struct btrfs_item),
2324 &left->map_token, &left->kaddr,
2325 &left->map_start, &left->map_len,
2326 KM_USER1);
2327 }
2328
2329 this_item_size = btrfs_item_size(left, item);
2330 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002331 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002332
Chris Mason00ec4c52007-02-24 12:47:20 -05002333 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002334 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002335 if (i == 0)
2336 break;
2337 i--;
Chris Masondb945352007-10-15 16:15:53 -04002338 }
2339 if (left->map_token) {
2340 unmap_extent_buffer(left, left->map_token, KM_USER1);
2341 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002342 }
Chris Mason5f39d392007-10-15 16:14:19 -04002343
Chris Mason925baed2008-06-25 16:01:30 -04002344 if (push_items == 0)
2345 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002346
Chris Mason34a38212007-11-07 13:31:03 -05002347 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002348 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002349
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002351 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002352
Chris Mason5f39d392007-10-15 16:14:19 -04002353 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002354 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002355
Chris Mason00ec4c52007-02-24 12:47:20 -05002356 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002357 data_end = leaf_data_end(root, right);
2358 memmove_extent_buffer(right,
2359 btrfs_leaf_data(right) + data_end - push_space,
2360 btrfs_leaf_data(right) + data_end,
2361 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2362
Chris Mason00ec4c52007-02-24 12:47:20 -05002363 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002364 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002365 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2366 btrfs_leaf_data(left) + leaf_data_end(root, left),
2367 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002368
2369 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2370 btrfs_item_nr_offset(0),
2371 right_nritems * sizeof(struct btrfs_item));
2372
Chris Mason00ec4c52007-02-24 12:47:20 -05002373 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002374 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2375 btrfs_item_nr_offset(left_nritems - push_items),
2376 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002377
2378 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002379 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002380 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002381 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002382 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002383 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002384 if (!right->map_token) {
2385 map_extent_buffer(right, (unsigned long)item,
2386 sizeof(struct btrfs_item),
2387 &right->map_token, &right->kaddr,
2388 &right->map_start, &right->map_len,
2389 KM_USER1);
2390 }
2391 push_space -= btrfs_item_size(right, item);
2392 btrfs_set_item_offset(right, item, push_space);
2393 }
2394
2395 if (right->map_token) {
2396 unmap_extent_buffer(right, right->map_token, KM_USER1);
2397 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002398 }
Chris Mason7518a232007-03-12 12:01:18 -04002399 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002400 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002401
Chris Mason34a38212007-11-07 13:31:03 -05002402 if (left_nritems)
2403 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002404 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002405
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2407 BUG_ON(ret);
2408
Chris Mason5f39d392007-10-15 16:14:19 -04002409 btrfs_item_key(right, &disk_key, 0);
2410 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002411 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002412
Chris Mason00ec4c52007-02-24 12:47:20 -05002413 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002414 if (path->slots[0] >= left_nritems) {
2415 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002416 if (btrfs_header_nritems(path->nodes[0]) == 0)
2417 clean_tree_block(trans, root, path->nodes[0]);
2418 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002419 free_extent_buffer(path->nodes[0]);
2420 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002421 path->slots[1] += 1;
2422 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002423 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002424 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002425 }
2426 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002427
2428out_unlock:
2429 btrfs_tree_unlock(right);
2430 free_extent_buffer(right);
2431 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002432}
Chris Mason925baed2008-06-25 16:01:30 -04002433
Chris Mason00ec4c52007-02-24 12:47:20 -05002434/*
Chris Mason44871b12009-03-13 10:04:31 -04002435 * push some data in the path leaf to the right, trying to free up at
2436 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2437 *
2438 * returns 1 if the push failed because the other node didn't have enough
2439 * room, 0 if everything worked out and < 0 if there were major errors.
2440 */
2441static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2442 *root, struct btrfs_path *path, int data_size,
2443 int empty)
2444{
2445 struct extent_buffer *left = path->nodes[0];
2446 struct extent_buffer *right;
2447 struct extent_buffer *upper;
2448 int slot;
2449 int free_space;
2450 u32 left_nritems;
2451 int ret;
2452
2453 if (!path->nodes[1])
2454 return 1;
2455
2456 slot = path->slots[1];
2457 upper = path->nodes[1];
2458 if (slot >= btrfs_header_nritems(upper) - 1)
2459 return 1;
2460
2461 btrfs_assert_tree_locked(path->nodes[1]);
2462
2463 right = read_node_slot(root, upper, slot + 1);
2464 btrfs_tree_lock(right);
2465 btrfs_set_lock_blocking(right);
2466
2467 free_space = btrfs_leaf_free_space(root, right);
2468 if (free_space < data_size)
2469 goto out_unlock;
2470
2471 /* cow and double check */
2472 ret = btrfs_cow_block(trans, root, right, upper,
2473 slot + 1, &right);
2474 if (ret)
2475 goto out_unlock;
2476
2477 free_space = btrfs_leaf_free_space(root, right);
2478 if (free_space < data_size)
2479 goto out_unlock;
2480
2481 left_nritems = btrfs_header_nritems(left);
2482 if (left_nritems == 0)
2483 goto out_unlock;
2484
2485 return __push_leaf_right(trans, root, path, data_size, empty,
2486 right, free_space, left_nritems);
2487out_unlock:
2488 btrfs_tree_unlock(right);
2489 free_extent_buffer(right);
2490 return 1;
2491}
2492
2493/*
Chris Mason74123bd2007-02-02 11:05:29 -05002494 * push some data in the path leaf to the left, trying to free up at
2495 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2496 */
Chris Mason44871b12009-03-13 10:04:31 -04002497static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2498 struct btrfs_root *root,
2499 struct btrfs_path *path, int data_size,
2500 int empty, struct extent_buffer *left,
2501 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002502{
Chris Mason5f39d392007-10-15 16:14:19 -04002503 struct btrfs_disk_key disk_key;
2504 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002505 int slot;
2506 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507 int push_space = 0;
2508 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002509 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002510 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002511 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002512 int ret = 0;
2513 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002514 u32 this_item_size;
2515 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002516
2517 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002518
Chris Mason34a38212007-11-07 13:31:03 -05002519 if (empty)
2520 nr = right_nritems;
2521 else
2522 nr = right_nritems - 1;
2523
2524 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002525 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002526 if (!right->map_token) {
2527 map_extent_buffer(right, (unsigned long)item,
2528 sizeof(struct btrfs_item),
2529 &right->map_token, &right->kaddr,
2530 &right->map_start, &right->map_len,
2531 KM_USER1);
2532 }
2533
Zheng Yan31840ae2008-09-23 13:14:14 -04002534 if (!empty && push_items > 0) {
2535 if (path->slots[0] < i)
2536 break;
2537 if (path->slots[0] == i) {
2538 int space = btrfs_leaf_free_space(root, right);
2539 if (space + push_space * 2 > free_space)
2540 break;
2541 }
2542 }
2543
Chris Masonbe0e5c02007-01-26 15:51:26 -05002544 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002545 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002546
2547 this_item_size = btrfs_item_size(right, item);
2548 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002549 break;
Chris Masondb945352007-10-15 16:15:53 -04002550
Chris Masonbe0e5c02007-01-26 15:51:26 -05002551 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002552 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002553 }
Chris Masondb945352007-10-15 16:15:53 -04002554
2555 if (right->map_token) {
2556 unmap_extent_buffer(right, right->map_token, KM_USER1);
2557 right->map_token = NULL;
2558 }
2559
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002561 ret = 1;
2562 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 }
Chris Mason34a38212007-11-07 13:31:03 -05002564 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002565 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002566
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002568 copy_extent_buffer(left, right,
2569 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2570 btrfs_item_nr_offset(0),
2571 push_items * sizeof(struct btrfs_item));
2572
Chris Mason123abc82007-03-14 14:14:43 -04002573 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002574 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002575
2576 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002577 leaf_data_end(root, left) - push_space,
2578 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002579 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002580 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002581 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002582 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002583
Chris Masondb945352007-10-15 16:15:53 -04002584 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002585 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002586 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002587
Chris Mason5f39d392007-10-15 16:14:19 -04002588 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002589 if (!left->map_token) {
2590 map_extent_buffer(left, (unsigned long)item,
2591 sizeof(struct btrfs_item),
2592 &left->map_token, &left->kaddr,
2593 &left->map_start, &left->map_len,
2594 KM_USER1);
2595 }
2596
Chris Mason5f39d392007-10-15 16:14:19 -04002597 ioff = btrfs_item_offset(left, item);
2598 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002599 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 }
Chris Mason5f39d392007-10-15 16:14:19 -04002601 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002602 if (left->map_token) {
2603 unmap_extent_buffer(left, left->map_token, KM_USER1);
2604 left->map_token = NULL;
2605 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002606
2607 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002608 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002609 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2610 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002611 WARN_ON(1);
2612 }
Chris Mason5f39d392007-10-15 16:14:19 -04002613
Chris Mason34a38212007-11-07 13:31:03 -05002614 if (push_items < right_nritems) {
2615 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2616 leaf_data_end(root, right);
2617 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2618 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2619 btrfs_leaf_data(right) +
2620 leaf_data_end(root, right), push_space);
2621
2622 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002623 btrfs_item_nr_offset(push_items),
2624 (btrfs_header_nritems(right) - push_items) *
2625 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002626 }
Yaneef1c492007-11-26 10:58:13 -05002627 right_nritems -= push_items;
2628 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002629 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002630 for (i = 0; i < right_nritems; i++) {
2631 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002632
2633 if (!right->map_token) {
2634 map_extent_buffer(right, (unsigned long)item,
2635 sizeof(struct btrfs_item),
2636 &right->map_token, &right->kaddr,
2637 &right->map_start, &right->map_len,
2638 KM_USER1);
2639 }
2640
2641 push_space = push_space - btrfs_item_size(right, item);
2642 btrfs_set_item_offset(right, item, push_space);
2643 }
2644 if (right->map_token) {
2645 unmap_extent_buffer(right, right->map_token, KM_USER1);
2646 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002647 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002648
Chris Mason5f39d392007-10-15 16:14:19 -04002649 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002650 if (right_nritems)
2651 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002652
Zheng Yan31840ae2008-09-23 13:14:14 -04002653 ret = btrfs_update_ref(trans, root, right, left,
2654 old_left_nritems, push_items);
2655 BUG_ON(ret);
2656
Chris Mason5f39d392007-10-15 16:14:19 -04002657 btrfs_item_key(right, &disk_key, 0);
2658 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002659 if (wret)
2660 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002661
2662 /* then fixup the leaf pointer in the path */
2663 if (path->slots[0] < push_items) {
2664 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002665 if (btrfs_header_nritems(path->nodes[0]) == 0)
2666 clean_tree_block(trans, root, path->nodes[0]);
2667 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002668 free_extent_buffer(path->nodes[0]);
2669 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670 path->slots[1] -= 1;
2671 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002672 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002673 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002674 path->slots[0] -= push_items;
2675 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002676 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002677 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002678out:
2679 btrfs_tree_unlock(left);
2680 free_extent_buffer(left);
2681 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002682}
2683
Chris Mason74123bd2007-02-02 11:05:29 -05002684/*
Chris Mason44871b12009-03-13 10:04:31 -04002685 * push some data in the path leaf to the left, trying to free up at
2686 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2687 */
2688static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2689 *root, struct btrfs_path *path, int data_size,
2690 int empty)
2691{
2692 struct extent_buffer *right = path->nodes[0];
2693 struct extent_buffer *left;
2694 int slot;
2695 int free_space;
2696 u32 right_nritems;
2697 int ret = 0;
2698
2699 slot = path->slots[1];
2700 if (slot == 0)
2701 return 1;
2702 if (!path->nodes[1])
2703 return 1;
2704
2705 right_nritems = btrfs_header_nritems(right);
2706 if (right_nritems == 0)
2707 return 1;
2708
2709 btrfs_assert_tree_locked(path->nodes[1]);
2710
2711 left = read_node_slot(root, path->nodes[1], slot - 1);
2712 btrfs_tree_lock(left);
2713 btrfs_set_lock_blocking(left);
2714
2715 free_space = btrfs_leaf_free_space(root, left);
2716 if (free_space < data_size) {
2717 ret = 1;
2718 goto out;
2719 }
2720
2721 /* cow and double check */
2722 ret = btrfs_cow_block(trans, root, left,
2723 path->nodes[1], slot - 1, &left);
2724 if (ret) {
2725 /* we hit -ENOSPC, but it isn't fatal here */
2726 ret = 1;
2727 goto out;
2728 }
2729
2730 free_space = btrfs_leaf_free_space(root, left);
2731 if (free_space < data_size) {
2732 ret = 1;
2733 goto out;
2734 }
2735
2736 return __push_leaf_left(trans, root, path, data_size,
2737 empty, left, free_space, right_nritems);
2738out:
2739 btrfs_tree_unlock(left);
2740 free_extent_buffer(left);
2741 return ret;
2742}
2743
2744/*
Chris Mason74123bd2007-02-02 11:05:29 -05002745 * split the path's leaf in two, making sure there is at least data_size
2746 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002747 *
2748 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002749 */
Chris Mason44871b12009-03-13 10:04:31 -04002750static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002751 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002752 struct btrfs_path *path,
2753 struct extent_buffer *l,
2754 struct extent_buffer *right,
2755 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002756{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002757 int data_copy_size;
2758 int rt_data_off;
2759 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002760 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002761 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002762 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002763
Chris Mason5f39d392007-10-15 16:14:19 -04002764 nritems = nritems - mid;
2765 btrfs_set_header_nritems(right, nritems);
2766 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2767
2768 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2769 btrfs_item_nr_offset(mid),
2770 nritems * sizeof(struct btrfs_item));
2771
2772 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002773 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2774 data_copy_size, btrfs_leaf_data(l) +
2775 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002776
Chris Mason5f39d392007-10-15 16:14:19 -04002777 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2778 btrfs_item_end_nr(l, mid);
2779
2780 for (i = 0; i < nritems; i++) {
2781 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002782 u32 ioff;
2783
2784 if (!right->map_token) {
2785 map_extent_buffer(right, (unsigned long)item,
2786 sizeof(struct btrfs_item),
2787 &right->map_token, &right->kaddr,
2788 &right->map_start, &right->map_len,
2789 KM_USER1);
2790 }
2791
2792 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002793 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002794 }
Chris Mason74123bd2007-02-02 11:05:29 -05002795
Chris Masondb945352007-10-15 16:15:53 -04002796 if (right->map_token) {
2797 unmap_extent_buffer(right, right->map_token, KM_USER1);
2798 right->map_token = NULL;
2799 }
2800
Chris Mason5f39d392007-10-15 16:14:19 -04002801 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002802 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002803 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002804 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2805 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002806 if (wret)
2807 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002808
2809 btrfs_mark_buffer_dirty(right);
2810 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002811 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002812
Zheng Yan31840ae2008-09-23 13:14:14 -04002813 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2814 BUG_ON(ret);
2815
Chris Masonbe0e5c02007-01-26 15:51:26 -05002816 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002817 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002818 free_extent_buffer(path->nodes[0]);
2819 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002820 path->slots[0] -= mid;
2821 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002822 } else {
2823 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002824 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002825 }
Chris Mason5f39d392007-10-15 16:14:19 -04002826
Chris Masoneb60cea2007-02-02 09:18:22 -05002827 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002828
Chris Mason44871b12009-03-13 10:04:31 -04002829 return ret;
2830}
2831
2832/*
2833 * split the path's leaf in two, making sure there is at least data_size
2834 * available for the resulting leaf level of the path.
2835 *
2836 * returns 0 if all went well and < 0 on failure.
2837 */
2838static noinline int split_leaf(struct btrfs_trans_handle *trans,
2839 struct btrfs_root *root,
2840 struct btrfs_key *ins_key,
2841 struct btrfs_path *path, int data_size,
2842 int extend)
2843{
2844 struct extent_buffer *l;
2845 u32 nritems;
2846 int mid;
2847 int slot;
2848 struct extent_buffer *right;
2849 int ret = 0;
2850 int wret;
2851 int double_split;
2852 int num_doubles = 0;
2853
2854 /* first try to make some room by pushing left and right */
Chris Masona4b6e072009-03-16 10:59:57 -04002855 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY &&
2856 !trans->transaction->delayed_refs.flushing) {
Chris Mason44871b12009-03-13 10:04:31 -04002857 wret = push_leaf_right(trans, root, path, data_size, 0);
2858 if (wret < 0)
2859 return wret;
2860 if (wret) {
2861 wret = push_leaf_left(trans, root, path, data_size, 0);
2862 if (wret < 0)
2863 return wret;
2864 }
2865 l = path->nodes[0];
2866
2867 /* did the pushes work? */
2868 if (btrfs_leaf_free_space(root, l) >= data_size)
2869 return 0;
2870 }
2871
2872 if (!path->nodes[1]) {
2873 ret = insert_new_root(trans, root, path, 1);
2874 if (ret)
2875 return ret;
2876 }
2877again:
2878 double_split = 0;
2879 l = path->nodes[0];
2880 slot = path->slots[0];
2881 nritems = btrfs_header_nritems(l);
2882 mid = (nritems + 1) / 2;
2883
2884 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2885 path->nodes[1]->start,
2886 root->root_key.objectid,
2887 trans->transid, 0, l->start, 0);
2888 if (IS_ERR(right)) {
2889 BUG_ON(1);
2890 return PTR_ERR(right);
2891 }
2892
2893 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2894 btrfs_set_header_bytenr(right, right->start);
2895 btrfs_set_header_generation(right, trans->transid);
2896 btrfs_set_header_owner(right, root->root_key.objectid);
2897 btrfs_set_header_level(right, 0);
2898 write_extent_buffer(right, root->fs_info->fsid,
2899 (unsigned long)btrfs_header_fsid(right),
2900 BTRFS_FSID_SIZE);
2901
2902 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2903 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2904 BTRFS_UUID_SIZE);
2905
2906 if (mid <= slot) {
2907 if (nritems == 1 ||
2908 leaf_space_used(l, mid, nritems - mid) + data_size >
2909 BTRFS_LEAF_DATA_SIZE(root)) {
2910 if (slot >= nritems) {
2911 struct btrfs_disk_key disk_key;
2912
2913 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2914 btrfs_set_header_nritems(right, 0);
2915 wret = insert_ptr(trans, root, path,
2916 &disk_key, right->start,
2917 path->slots[1] + 1, 1);
2918 if (wret)
2919 ret = wret;
2920
2921 btrfs_tree_unlock(path->nodes[0]);
2922 free_extent_buffer(path->nodes[0]);
2923 path->nodes[0] = right;
2924 path->slots[0] = 0;
2925 path->slots[1] += 1;
2926 btrfs_mark_buffer_dirty(right);
2927 return ret;
2928 }
2929 mid = slot;
2930 if (mid != nritems &&
2931 leaf_space_used(l, mid, nritems - mid) +
2932 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2933 double_split = 1;
2934 }
2935 }
2936 } else {
2937 if (leaf_space_used(l, 0, mid) + data_size >
2938 BTRFS_LEAF_DATA_SIZE(root)) {
2939 if (!extend && data_size && slot == 0) {
2940 struct btrfs_disk_key disk_key;
2941
2942 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2943 btrfs_set_header_nritems(right, 0);
2944 wret = insert_ptr(trans, root, path,
2945 &disk_key,
2946 right->start,
2947 path->slots[1], 1);
2948 if (wret)
2949 ret = wret;
2950 btrfs_tree_unlock(path->nodes[0]);
2951 free_extent_buffer(path->nodes[0]);
2952 path->nodes[0] = right;
2953 path->slots[0] = 0;
2954 if (path->slots[1] == 0) {
2955 wret = fixup_low_keys(trans, root,
2956 path, &disk_key, 1);
2957 if (wret)
2958 ret = wret;
2959 }
2960 btrfs_mark_buffer_dirty(right);
2961 return ret;
2962 } else if ((extend || !data_size) && slot == 0) {
2963 mid = 1;
2964 } else {
2965 mid = slot;
2966 if (mid != nritems &&
2967 leaf_space_used(l, mid, nritems - mid) +
2968 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2969 double_split = 1;
2970 }
2971 }
2972 }
2973 }
2974
2975 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2976 BUG_ON(ret);
2977
Chris Masoncc0c5532007-10-25 15:42:57 -04002978 if (double_split) {
2979 BUG_ON(num_doubles != 0);
2980 num_doubles++;
2981 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002982 }
Chris Mason44871b12009-03-13 10:04:31 -04002983
Chris Masonbe0e5c02007-01-26 15:51:26 -05002984 return ret;
2985}
2986
Chris Masond352ac62008-09-29 15:18:18 -04002987/*
Chris Mason459931e2008-12-10 09:10:46 -05002988 * This function splits a single item into two items,
2989 * giving 'new_key' to the new item and splitting the
2990 * old one at split_offset (from the start of the item).
2991 *
2992 * The path may be released by this operation. After
2993 * the split, the path is pointing to the old item. The
2994 * new item is going to be in the same node as the old one.
2995 *
2996 * Note, the item being split must be smaller enough to live alone on
2997 * a tree block with room for one extra struct btrfs_item
2998 *
2999 * This allows us to split the item in place, keeping a lock on the
3000 * leaf the entire time.
3001 */
3002int btrfs_split_item(struct btrfs_trans_handle *trans,
3003 struct btrfs_root *root,
3004 struct btrfs_path *path,
3005 struct btrfs_key *new_key,
3006 unsigned long split_offset)
3007{
3008 u32 item_size;
3009 struct extent_buffer *leaf;
3010 struct btrfs_key orig_key;
3011 struct btrfs_item *item;
3012 struct btrfs_item *new_item;
3013 int ret = 0;
3014 int slot;
3015 u32 nritems;
3016 u32 orig_offset;
3017 struct btrfs_disk_key disk_key;
3018 char *buf;
3019
3020 leaf = path->nodes[0];
3021 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
3022 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
3023 goto split;
3024
3025 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3026 btrfs_release_path(root, path);
3027
3028 path->search_for_split = 1;
3029 path->keep_locks = 1;
3030
3031 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
3032 path->search_for_split = 0;
3033
3034 /* if our item isn't there or got smaller, return now */
3035 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3036 path->slots[0])) {
3037 path->keep_locks = 0;
3038 return -EAGAIN;
3039 }
3040
Chris Masonb9473432009-03-13 11:00:37 -04003041 btrfs_set_path_blocking(path);
Yan Zheng87b29b22008-12-17 10:21:48 -05003042 ret = split_leaf(trans, root, &orig_key, path,
3043 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003044 path->keep_locks = 0;
3045 BUG_ON(ret);
3046
Chris Masonb9473432009-03-13 11:00:37 -04003047 btrfs_unlock_up_safe(path, 1);
3048 leaf = path->nodes[0];
3049 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3050
3051split:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003052 /*
3053 * make sure any changes to the path from split_leaf leave it
3054 * in a blocking state
3055 */
3056 btrfs_set_path_blocking(path);
3057
Chris Mason459931e2008-12-10 09:10:46 -05003058 item = btrfs_item_nr(leaf, path->slots[0]);
3059 orig_offset = btrfs_item_offset(leaf, item);
3060 item_size = btrfs_item_size(leaf, item);
3061
Chris Mason459931e2008-12-10 09:10:46 -05003062 buf = kmalloc(item_size, GFP_NOFS);
3063 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3064 path->slots[0]), item_size);
3065 slot = path->slots[0] + 1;
3066 leaf = path->nodes[0];
3067
3068 nritems = btrfs_header_nritems(leaf);
3069
3070 if (slot != nritems) {
3071 /* shift the items */
3072 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3073 btrfs_item_nr_offset(slot),
3074 (nritems - slot) * sizeof(struct btrfs_item));
3075
3076 }
3077
3078 btrfs_cpu_key_to_disk(&disk_key, new_key);
3079 btrfs_set_item_key(leaf, &disk_key, slot);
3080
3081 new_item = btrfs_item_nr(leaf, slot);
3082
3083 btrfs_set_item_offset(leaf, new_item, orig_offset);
3084 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3085
3086 btrfs_set_item_offset(leaf, item,
3087 orig_offset + item_size - split_offset);
3088 btrfs_set_item_size(leaf, item, split_offset);
3089
3090 btrfs_set_header_nritems(leaf, nritems + 1);
3091
3092 /* write the data for the start of the original item */
3093 write_extent_buffer(leaf, buf,
3094 btrfs_item_ptr_offset(leaf, path->slots[0]),
3095 split_offset);
3096
3097 /* write the data for the new item */
3098 write_extent_buffer(leaf, buf + split_offset,
3099 btrfs_item_ptr_offset(leaf, slot),
3100 item_size - split_offset);
3101 btrfs_mark_buffer_dirty(leaf);
3102
3103 ret = 0;
3104 if (btrfs_leaf_free_space(root, leaf) < 0) {
3105 btrfs_print_leaf(root, leaf);
3106 BUG();
3107 }
3108 kfree(buf);
3109 return ret;
3110}
3111
3112/*
Chris Masond352ac62008-09-29 15:18:18 -04003113 * make the item pointed to by the path smaller. new_size indicates
3114 * how small to make it, and from_end tells us if we just chop bytes
3115 * off the end of the item or if we shift the item to chop bytes off
3116 * the front.
3117 */
Chris Masonb18c6682007-04-17 13:26:50 -04003118int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3119 struct btrfs_root *root,
3120 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003121 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003122{
3123 int ret = 0;
3124 int slot;
3125 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003126 struct extent_buffer *leaf;
3127 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003128 u32 nritems;
3129 unsigned int data_end;
3130 unsigned int old_data_start;
3131 unsigned int old_size;
3132 unsigned int size_diff;
3133 int i;
3134
3135 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003136 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003137 slot = path->slots[0];
3138
3139 old_size = btrfs_item_size_nr(leaf, slot);
3140 if (old_size == new_size)
3141 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003142
Chris Mason5f39d392007-10-15 16:14:19 -04003143 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003144 data_end = leaf_data_end(root, leaf);
3145
Chris Mason5f39d392007-10-15 16:14:19 -04003146 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003147
Chris Masonb18c6682007-04-17 13:26:50 -04003148 size_diff = old_size - new_size;
3149
3150 BUG_ON(slot < 0);
3151 BUG_ON(slot >= nritems);
3152
3153 /*
3154 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3155 */
3156 /* first correct the data pointers */
3157 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003158 u32 ioff;
3159 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003160
3161 if (!leaf->map_token) {
3162 map_extent_buffer(leaf, (unsigned long)item,
3163 sizeof(struct btrfs_item),
3164 &leaf->map_token, &leaf->kaddr,
3165 &leaf->map_start, &leaf->map_len,
3166 KM_USER1);
3167 }
3168
Chris Mason5f39d392007-10-15 16:14:19 -04003169 ioff = btrfs_item_offset(leaf, item);
3170 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003171 }
Chris Masondb945352007-10-15 16:15:53 -04003172
3173 if (leaf->map_token) {
3174 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3175 leaf->map_token = NULL;
3176 }
3177
Chris Masonb18c6682007-04-17 13:26:50 -04003178 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003179 if (from_end) {
3180 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3181 data_end + size_diff, btrfs_leaf_data(leaf) +
3182 data_end, old_data_start + new_size - data_end);
3183 } else {
3184 struct btrfs_disk_key disk_key;
3185 u64 offset;
3186
3187 btrfs_item_key(leaf, &disk_key, slot);
3188
3189 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3190 unsigned long ptr;
3191 struct btrfs_file_extent_item *fi;
3192
3193 fi = btrfs_item_ptr(leaf, slot,
3194 struct btrfs_file_extent_item);
3195 fi = (struct btrfs_file_extent_item *)(
3196 (unsigned long)fi - size_diff);
3197
3198 if (btrfs_file_extent_type(leaf, fi) ==
3199 BTRFS_FILE_EXTENT_INLINE) {
3200 ptr = btrfs_item_ptr_offset(leaf, slot);
3201 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003202 (unsigned long)fi,
3203 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003204 disk_bytenr));
3205 }
3206 }
3207
3208 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3209 data_end + size_diff, btrfs_leaf_data(leaf) +
3210 data_end, old_data_start - data_end);
3211
3212 offset = btrfs_disk_key_offset(&disk_key);
3213 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3214 btrfs_set_item_key(leaf, &disk_key, slot);
3215 if (slot == 0)
3216 fixup_low_keys(trans, root, path, &disk_key, 1);
3217 }
Chris Mason5f39d392007-10-15 16:14:19 -04003218
3219 item = btrfs_item_nr(leaf, slot);
3220 btrfs_set_item_size(leaf, item, new_size);
3221 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003222
3223 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003224 if (btrfs_leaf_free_space(root, leaf) < 0) {
3225 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003226 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003227 }
Chris Masonb18c6682007-04-17 13:26:50 -04003228 return ret;
3229}
3230
Chris Masond352ac62008-09-29 15:18:18 -04003231/*
3232 * make the item pointed to by the path bigger, data_size is the new size.
3233 */
Chris Mason5f39d392007-10-15 16:14:19 -04003234int btrfs_extend_item(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root, struct btrfs_path *path,
3236 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003237{
3238 int ret = 0;
3239 int slot;
3240 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003241 struct extent_buffer *leaf;
3242 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003243 u32 nritems;
3244 unsigned int data_end;
3245 unsigned int old_data;
3246 unsigned int old_size;
3247 int i;
3248
3249 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003250 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003251
Chris Mason5f39d392007-10-15 16:14:19 -04003252 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003253 data_end = leaf_data_end(root, leaf);
3254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3256 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003257 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003258 }
Chris Mason6567e832007-04-16 09:22:45 -04003259 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003260 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003261
3262 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003263 if (slot >= nritems) {
3264 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003265 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3266 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003267 BUG_ON(1);
3268 }
Chris Mason6567e832007-04-16 09:22:45 -04003269
3270 /*
3271 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3272 */
3273 /* first correct the data pointers */
3274 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003275 u32 ioff;
3276 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003277
3278 if (!leaf->map_token) {
3279 map_extent_buffer(leaf, (unsigned long)item,
3280 sizeof(struct btrfs_item),
3281 &leaf->map_token, &leaf->kaddr,
3282 &leaf->map_start, &leaf->map_len,
3283 KM_USER1);
3284 }
Chris Mason5f39d392007-10-15 16:14:19 -04003285 ioff = btrfs_item_offset(leaf, item);
3286 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003287 }
Chris Mason5f39d392007-10-15 16:14:19 -04003288
Chris Masondb945352007-10-15 16:15:53 -04003289 if (leaf->map_token) {
3290 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3291 leaf->map_token = NULL;
3292 }
3293
Chris Mason6567e832007-04-16 09:22:45 -04003294 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003295 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003296 data_end - data_size, btrfs_leaf_data(leaf) +
3297 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003298
Chris Mason6567e832007-04-16 09:22:45 -04003299 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003300 old_size = btrfs_item_size_nr(leaf, slot);
3301 item = btrfs_item_nr(leaf, slot);
3302 btrfs_set_item_size(leaf, item, old_size + data_size);
3303 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003304
3305 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003306 if (btrfs_leaf_free_space(root, leaf) < 0) {
3307 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003308 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003309 }
Chris Mason6567e832007-04-16 09:22:45 -04003310 return ret;
3311}
3312
Chris Mason74123bd2007-02-02 11:05:29 -05003313/*
Chris Masond352ac62008-09-29 15:18:18 -04003314 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003315 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003316 * Returns the number of keys that were inserted.
3317 */
3318int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3319 struct btrfs_root *root,
3320 struct btrfs_path *path,
3321 struct btrfs_key *cpu_key, u32 *data_size,
3322 int nr)
3323{
3324 struct extent_buffer *leaf;
3325 struct btrfs_item *item;
3326 int ret = 0;
3327 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003328 int i;
3329 u32 nritems;
3330 u32 total_data = 0;
3331 u32 total_size = 0;
3332 unsigned int data_end;
3333 struct btrfs_disk_key disk_key;
3334 struct btrfs_key found_key;
3335
Yan Zheng87b29b22008-12-17 10:21:48 -05003336 for (i = 0; i < nr; i++) {
3337 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3338 BTRFS_LEAF_DATA_SIZE(root)) {
3339 break;
3340 nr = i;
3341 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003342 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003343 total_size += data_size[i] + sizeof(struct btrfs_item);
3344 }
3345 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003346
Josef Bacikf3465ca2008-11-12 14:19:50 -05003347 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3348 if (ret == 0)
3349 return -EEXIST;
3350 if (ret < 0)
3351 goto out;
3352
Josef Bacikf3465ca2008-11-12 14:19:50 -05003353 leaf = path->nodes[0];
3354
3355 nritems = btrfs_header_nritems(leaf);
3356 data_end = leaf_data_end(root, leaf);
3357
3358 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3359 for (i = nr; i >= 0; i--) {
3360 total_data -= data_size[i];
3361 total_size -= data_size[i] + sizeof(struct btrfs_item);
3362 if (total_size < btrfs_leaf_free_space(root, leaf))
3363 break;
3364 }
3365 nr = i;
3366 }
3367
3368 slot = path->slots[0];
3369 BUG_ON(slot < 0);
3370
3371 if (slot != nritems) {
3372 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3373
3374 item = btrfs_item_nr(leaf, slot);
3375 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3376
3377 /* figure out how many keys we can insert in here */
3378 total_data = data_size[0];
3379 for (i = 1; i < nr; i++) {
3380 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3381 break;
3382 total_data += data_size[i];
3383 }
3384 nr = i;
3385
3386 if (old_data < data_end) {
3387 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003388 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003389 slot, old_data, data_end);
3390 BUG_ON(1);
3391 }
3392 /*
3393 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3394 */
3395 /* first correct the data pointers */
3396 WARN_ON(leaf->map_token);
3397 for (i = slot; i < nritems; i++) {
3398 u32 ioff;
3399
3400 item = btrfs_item_nr(leaf, i);
3401 if (!leaf->map_token) {
3402 map_extent_buffer(leaf, (unsigned long)item,
3403 sizeof(struct btrfs_item),
3404 &leaf->map_token, &leaf->kaddr,
3405 &leaf->map_start, &leaf->map_len,
3406 KM_USER1);
3407 }
3408
3409 ioff = btrfs_item_offset(leaf, item);
3410 btrfs_set_item_offset(leaf, item, ioff - total_data);
3411 }
3412 if (leaf->map_token) {
3413 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3414 leaf->map_token = NULL;
3415 }
3416
3417 /* shift the items */
3418 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3419 btrfs_item_nr_offset(slot),
3420 (nritems - slot) * sizeof(struct btrfs_item));
3421
3422 /* shift the data */
3423 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3424 data_end - total_data, btrfs_leaf_data(leaf) +
3425 data_end, old_data - data_end);
3426 data_end = old_data;
3427 } else {
3428 /*
3429 * this sucks but it has to be done, if we are inserting at
3430 * the end of the leaf only insert 1 of the items, since we
3431 * have no way of knowing whats on the next leaf and we'd have
3432 * to drop our current locks to figure it out
3433 */
3434 nr = 1;
3435 }
3436
3437 /* setup the item for the new data */
3438 for (i = 0; i < nr; i++) {
3439 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3440 btrfs_set_item_key(leaf, &disk_key, slot + i);
3441 item = btrfs_item_nr(leaf, slot + i);
3442 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3443 data_end -= data_size[i];
3444 btrfs_set_item_size(leaf, item, data_size[i]);
3445 }
3446 btrfs_set_header_nritems(leaf, nritems + nr);
3447 btrfs_mark_buffer_dirty(leaf);
3448
3449 ret = 0;
3450 if (slot == 0) {
3451 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3452 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3453 }
3454
3455 if (btrfs_leaf_free_space(root, leaf) < 0) {
3456 btrfs_print_leaf(root, leaf);
3457 BUG();
3458 }
3459out:
3460 if (!ret)
3461 ret = nr;
3462 return ret;
3463}
3464
3465/*
Chris Mason44871b12009-03-13 10:04:31 -04003466 * this is a helper for btrfs_insert_empty_items, the main goal here is
3467 * to save stack depth by doing the bulk of the work in a function
3468 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003469 */
Chris Mason44871b12009-03-13 10:04:31 -04003470static noinline_for_stack int
3471setup_items_for_insert(struct btrfs_trans_handle *trans,
3472 struct btrfs_root *root, struct btrfs_path *path,
3473 struct btrfs_key *cpu_key, u32 *data_size,
3474 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003475{
Chris Mason5f39d392007-10-15 16:14:19 -04003476 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003477 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003478 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003479 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003480 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003481 int ret;
3482 struct extent_buffer *leaf;
3483 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003484
Chris Mason5f39d392007-10-15 16:14:19 -04003485 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003486 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003489 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003490
Chris Masonf25956c2008-09-12 15:32:53 -04003491 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003492 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003493 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003494 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003495 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003496 }
Chris Mason5f39d392007-10-15 16:14:19 -04003497
Chris Masonbe0e5c02007-01-26 15:51:26 -05003498 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003499 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003500
Chris Mason5f39d392007-10-15 16:14:19 -04003501 if (old_data < data_end) {
3502 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003503 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003504 slot, old_data, data_end);
3505 BUG_ON(1);
3506 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003507 /*
3508 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3509 */
3510 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003511 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003512 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003513 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003514
Chris Mason5f39d392007-10-15 16:14:19 -04003515 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003516 if (!leaf->map_token) {
3517 map_extent_buffer(leaf, (unsigned long)item,
3518 sizeof(struct btrfs_item),
3519 &leaf->map_token, &leaf->kaddr,
3520 &leaf->map_start, &leaf->map_len,
3521 KM_USER1);
3522 }
3523
Chris Mason5f39d392007-10-15 16:14:19 -04003524 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003525 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003526 }
Chris Masondb945352007-10-15 16:15:53 -04003527 if (leaf->map_token) {
3528 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3529 leaf->map_token = NULL;
3530 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003531
3532 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003533 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003534 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003535 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003536
3537 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003538 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003539 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003540 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003541 data_end = old_data;
3542 }
Chris Mason5f39d392007-10-15 16:14:19 -04003543
Chris Mason62e27492007-03-15 12:56:47 -04003544 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003545 for (i = 0; i < nr; i++) {
3546 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3547 btrfs_set_item_key(leaf, &disk_key, slot + i);
3548 item = btrfs_item_nr(leaf, slot + i);
3549 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3550 data_end -= data_size[i];
3551 btrfs_set_item_size(leaf, item, data_size[i]);
3552 }
Chris Mason44871b12009-03-13 10:04:31 -04003553
Chris Mason9c583092008-01-29 15:15:18 -05003554 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003555
3556 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003557 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003558 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003559 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003560 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003561 }
Chris Masonb9473432009-03-13 11:00:37 -04003562 btrfs_unlock_up_safe(path, 1);
3563 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003564
Chris Mason5f39d392007-10-15 16:14:19 -04003565 if (btrfs_leaf_free_space(root, leaf) < 0) {
3566 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003568 }
Chris Mason44871b12009-03-13 10:04:31 -04003569 return ret;
3570}
3571
3572/*
3573 * Given a key and some data, insert items into the tree.
3574 * This does all the path init required, making room in the tree if needed.
3575 */
3576int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3577 struct btrfs_root *root,
3578 struct btrfs_path *path,
3579 struct btrfs_key *cpu_key, u32 *data_size,
3580 int nr)
3581{
3582 struct extent_buffer *leaf;
3583 int ret = 0;
3584 int slot;
3585 int i;
3586 u32 total_size = 0;
3587 u32 total_data = 0;
3588
3589 for (i = 0; i < nr; i++)
3590 total_data += data_size[i];
3591
3592 total_size = total_data + (nr * sizeof(struct btrfs_item));
3593 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3594 if (ret == 0)
3595 return -EEXIST;
3596 if (ret < 0)
3597 goto out;
3598
3599 leaf = path->nodes[0];
3600 slot = path->slots[0];
3601 BUG_ON(slot < 0);
3602
3603 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3604 total_data, total_size, nr);
3605
Chris Masoned2ff2c2007-03-01 18:59:40 -05003606out:
Chris Mason62e27492007-03-15 12:56:47 -04003607 return ret;
3608}
3609
3610/*
3611 * Given a key and some data, insert an item into the tree.
3612 * This does all the path init required, making room in the tree if needed.
3613 */
Chris Masone089f052007-03-16 16:20:31 -04003614int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3615 *root, struct btrfs_key *cpu_key, void *data, u32
3616 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003617{
3618 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003619 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003620 struct extent_buffer *leaf;
3621 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003622
Chris Mason2c90e5d2007-04-02 10:50:19 -04003623 path = btrfs_alloc_path();
3624 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003625 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003626 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003627 leaf = path->nodes[0];
3628 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3629 write_extent_buffer(leaf, data, ptr, data_size);
3630 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003631 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003632 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003633 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003634}
3635
Chris Mason74123bd2007-02-02 11:05:29 -05003636/*
Chris Mason5de08d72007-02-24 06:24:44 -05003637 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003638 *
Chris Masond352ac62008-09-29 15:18:18 -04003639 * the tree should have been previously balanced so the deletion does not
3640 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003641 */
Chris Masone089f052007-03-16 16:20:31 -04003642static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3643 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003644{
Chris Mason5f39d392007-10-15 16:14:19 -04003645 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003646 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003647 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003648 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003649
Chris Mason5f39d392007-10-15 16:14:19 -04003650 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003651 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003652 memmove_extent_buffer(parent,
3653 btrfs_node_key_ptr_offset(slot),
3654 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003655 sizeof(struct btrfs_key_ptr) *
3656 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003657 }
Chris Mason7518a232007-03-12 12:01:18 -04003658 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003659 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003660 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003661 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003662 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003663 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003664 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003665 struct btrfs_disk_key disk_key;
3666
3667 btrfs_node_key(parent, &disk_key, 0);
3668 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003669 if (wret)
3670 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003671 }
Chris Masond6025572007-03-30 14:27:56 -04003672 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003673 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003674}
3675
Chris Mason74123bd2007-02-02 11:05:29 -05003676/*
Chris Mason323ac952008-10-01 19:05:46 -04003677 * a helper function to delete the leaf pointed to by path->slots[1] and
3678 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3679 * already know it, it is faster to have them pass it down than to
3680 * read it out of the node again.
3681 *
3682 * This deletes the pointer in path->nodes[1] and frees the leaf
3683 * block extent. zero is returned if it all worked out, < 0 otherwise.
3684 *
3685 * The path must have already been setup for deleting the leaf, including
3686 * all the proper balancing. path->nodes[1] must be locked.
3687 */
3688noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3689 struct btrfs_root *root,
3690 struct btrfs_path *path, u64 bytenr)
3691{
3692 int ret;
3693 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003694 u64 parent_start = path->nodes[1]->start;
3695 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003696
3697 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3698 if (ret)
3699 return ret;
3700
Chris Mason4d081c42009-02-04 09:31:28 -05003701 /*
3702 * btrfs_free_extent is expensive, we want to make sure we
3703 * aren't holding any locks when we call it
3704 */
3705 btrfs_unlock_up_safe(path, 0);
3706
Chris Mason323ac952008-10-01 19:05:46 -04003707 ret = btrfs_free_extent(trans, root, bytenr,
3708 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003709 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003710 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003711 return ret;
3712}
3713/*
Chris Mason74123bd2007-02-02 11:05:29 -05003714 * delete the item at the leaf level in path. If that empties
3715 * the leaf, remove it from the tree
3716 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003717int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3718 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003719{
Chris Mason5f39d392007-10-15 16:14:19 -04003720 struct extent_buffer *leaf;
3721 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003722 int last_off;
3723 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003724 int ret = 0;
3725 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003726 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003727 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003728
Chris Mason5f39d392007-10-15 16:14:19 -04003729 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003730 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3731
3732 for (i = 0; i < nr; i++)
3733 dsize += btrfs_item_size_nr(leaf, slot + i);
3734
Chris Mason5f39d392007-10-15 16:14:19 -04003735 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003736
Chris Mason85e21ba2008-01-29 15:11:36 -05003737 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003738 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003739
3740 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003741 data_end + dsize,
3742 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003743 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
Chris Mason85e21ba2008-01-29 15:11:36 -05003745 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003746 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003747
Chris Mason5f39d392007-10-15 16:14:19 -04003748 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003749 if (!leaf->map_token) {
3750 map_extent_buffer(leaf, (unsigned long)item,
3751 sizeof(struct btrfs_item),
3752 &leaf->map_token, &leaf->kaddr,
3753 &leaf->map_start, &leaf->map_len,
3754 KM_USER1);
3755 }
Chris Mason5f39d392007-10-15 16:14:19 -04003756 ioff = btrfs_item_offset(leaf, item);
3757 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003758 }
Chris Masondb945352007-10-15 16:15:53 -04003759
3760 if (leaf->map_token) {
3761 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3762 leaf->map_token = NULL;
3763 }
3764
Chris Mason5f39d392007-10-15 16:14:19 -04003765 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003766 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003767 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003768 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003769 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003770 btrfs_set_header_nritems(leaf, nritems - nr);
3771 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003772
Chris Mason74123bd2007-02-02 11:05:29 -05003773 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003774 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003775 if (leaf == root->node) {
3776 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003777 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003778 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3779 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003780 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003781 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003782 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003783 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003784 struct btrfs_disk_key disk_key;
3785
3786 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003787 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003788 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003789 if (wret)
3790 ret = wret;
3791 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003792
Chris Mason74123bd2007-02-02 11:05:29 -05003793 /* delete the leaf if it is mostly empty */
Chris Masona4b6e072009-03-16 10:59:57 -04003794 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4 &&
3795 !trans->transaction->delayed_refs.flushing) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003796 /* push_leaf_left fixes the path.
3797 * make sure the path still points to our leaf
3798 * for possible call to del_ptr below
3799 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003800 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003801 extent_buffer_get(leaf);
3802
Chris Masonb9473432009-03-13 11:00:37 -04003803 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003804 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003805 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003806 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003807
3808 if (path->nodes[0] == leaf &&
3809 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003810 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003811 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003812 ret = wret;
3813 }
Chris Mason5f39d392007-10-15 16:14:19 -04003814
3815 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003816 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003817 ret = btrfs_del_leaf(trans, root, path,
3818 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003819 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003820 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003821 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003822 /* if we're still in the path, make sure
3823 * we're dirty. Otherwise, one of the
3824 * push_leaf functions must have already
3825 * dirtied this buffer
3826 */
3827 if (path->nodes[0] == leaf)
3828 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003829 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003830 }
Chris Masond5719762007-03-23 10:01:08 -04003831 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003832 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003833 }
3834 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003835 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003836}
3837
Chris Mason97571fd2007-02-24 13:39:08 -05003838/*
Chris Mason925baed2008-06-25 16:01:30 -04003839 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003840 * returns 0 if it found something or 1 if there are no lesser leaves.
3841 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003842 *
3843 * This may release the path, and so you may lose any locks held at the
3844 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003845 */
3846int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3847{
Chris Mason925baed2008-06-25 16:01:30 -04003848 struct btrfs_key key;
3849 struct btrfs_disk_key found_key;
3850 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003851
Chris Mason925baed2008-06-25 16:01:30 -04003852 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003853
Chris Mason925baed2008-06-25 16:01:30 -04003854 if (key.offset > 0)
3855 key.offset--;
3856 else if (key.type > 0)
3857 key.type--;
3858 else if (key.objectid > 0)
3859 key.objectid--;
3860 else
3861 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003862
Chris Mason925baed2008-06-25 16:01:30 -04003863 btrfs_release_path(root, path);
3864 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3865 if (ret < 0)
3866 return ret;
3867 btrfs_item_key(path->nodes[0], &found_key, 0);
3868 ret = comp_keys(&found_key, &key);
3869 if (ret < 0)
3870 return 0;
3871 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003872}
3873
Chris Mason3f157a22008-06-25 16:01:31 -04003874/*
3875 * A helper function to walk down the tree starting at min_key, and looking
3876 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003877 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003878 *
3879 * This does not cow, but it does stuff the starting key it finds back
3880 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3881 * key and get a writable path.
3882 *
3883 * This does lock as it descends, and path->keep_locks should be set
3884 * to 1 by the caller.
3885 *
3886 * This honors path->lowest_level to prevent descent past a given level
3887 * of the tree.
3888 *
Chris Masond352ac62008-09-29 15:18:18 -04003889 * min_trans indicates the oldest transaction that you are interested
3890 * in walking through. Any nodes or leaves older than min_trans are
3891 * skipped over (without reading them).
3892 *
Chris Mason3f157a22008-06-25 16:01:31 -04003893 * returns zero if something useful was found, < 0 on error and 1 if there
3894 * was nothing in the tree that matched the search criteria.
3895 */
3896int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003897 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003898 struct btrfs_path *path, int cache_only,
3899 u64 min_trans)
3900{
3901 struct extent_buffer *cur;
3902 struct btrfs_key found_key;
3903 int slot;
Yan96524802008-07-24 12:19:49 -04003904 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003905 u32 nritems;
3906 int level;
3907 int ret = 1;
3908
Chris Mason934d3752008-12-08 16:43:10 -05003909 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003910again:
3911 cur = btrfs_lock_root_node(root);
3912 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003913 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003914 path->nodes[level] = cur;
3915 path->locks[level] = 1;
3916
3917 if (btrfs_header_generation(cur) < min_trans) {
3918 ret = 1;
3919 goto out;
3920 }
Chris Masond3977122009-01-05 21:25:51 -05003921 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003922 nritems = btrfs_header_nritems(cur);
3923 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003924 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003925
Chris Mason323ac952008-10-01 19:05:46 -04003926 /* at the lowest level, we're done, setup the path and exit */
3927 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003928 if (slot >= nritems)
3929 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003930 ret = 0;
3931 path->slots[level] = slot;
3932 btrfs_item_key_to_cpu(cur, &found_key, slot);
3933 goto out;
3934 }
Yan96524802008-07-24 12:19:49 -04003935 if (sret && slot > 0)
3936 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003937 /*
3938 * check this node pointer against the cache_only and
3939 * min_trans parameters. If it isn't in cache or is too
3940 * old, skip to the next one.
3941 */
Chris Masond3977122009-01-05 21:25:51 -05003942 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003943 u64 blockptr;
3944 u64 gen;
3945 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003946 struct btrfs_disk_key disk_key;
3947
Chris Mason3f157a22008-06-25 16:01:31 -04003948 blockptr = btrfs_node_blockptr(cur, slot);
3949 gen = btrfs_node_ptr_generation(cur, slot);
3950 if (gen < min_trans) {
3951 slot++;
3952 continue;
3953 }
3954 if (!cache_only)
3955 break;
3956
Chris Masone02119d2008-09-05 16:13:11 -04003957 if (max_key) {
3958 btrfs_node_key(cur, &disk_key, slot);
3959 if (comp_keys(&disk_key, max_key) >= 0) {
3960 ret = 1;
3961 goto out;
3962 }
3963 }
3964
Chris Mason3f157a22008-06-25 16:01:31 -04003965 tmp = btrfs_find_tree_block(root, blockptr,
3966 btrfs_level_size(root, level - 1));
3967
3968 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3969 free_extent_buffer(tmp);
3970 break;
3971 }
3972 if (tmp)
3973 free_extent_buffer(tmp);
3974 slot++;
3975 }
Chris Masone02119d2008-09-05 16:13:11 -04003976find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003977 /*
3978 * we didn't find a candidate key in this node, walk forward
3979 * and find another one
3980 */
3981 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003982 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003983 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003984 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003985 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003986 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003987 btrfs_release_path(root, path);
3988 goto again;
3989 } else {
3990 goto out;
3991 }
3992 }
3993 /* save our key for returning back */
3994 btrfs_node_key_to_cpu(cur, &found_key, slot);
3995 path->slots[level] = slot;
3996 if (level == path->lowest_level) {
3997 ret = 0;
3998 unlock_up(path, level, 1);
3999 goto out;
4000 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004001 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004002 cur = read_node_slot(root, cur, slot);
4003
4004 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004005
Chris Mason3f157a22008-06-25 16:01:31 -04004006 path->locks[level - 1] = 1;
4007 path->nodes[level - 1] = cur;
4008 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004009 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004010 }
4011out:
4012 if (ret == 0)
4013 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004014 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004015 return ret;
4016}
4017
4018/*
4019 * this is similar to btrfs_next_leaf, but does not try to preserve
4020 * and fixup the path. It looks for and returns the next key in the
4021 * tree based on the current path and the cache_only and min_trans
4022 * parameters.
4023 *
4024 * 0 is returned if another key is found, < 0 if there are any errors
4025 * and 1 is returned if there are no higher keys in the tree
4026 *
4027 * path->keep_locks should be set to 1 on the search made before
4028 * calling this function.
4029 */
Chris Masone7a84562008-06-25 16:01:31 -04004030int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004031 struct btrfs_key *key, int lowest_level,
4032 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004033{
4034 int level = lowest_level;
4035 int slot;
4036 struct extent_buffer *c;
4037
Chris Mason934d3752008-12-08 16:43:10 -05004038 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004039 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004040 if (!path->nodes[level])
4041 return 1;
4042
4043 slot = path->slots[level] + 1;
4044 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004045next:
Chris Masone7a84562008-06-25 16:01:31 -04004046 if (slot >= btrfs_header_nritems(c)) {
4047 level++;
Chris Masond3977122009-01-05 21:25:51 -05004048 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04004049 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04004050 continue;
4051 }
4052 if (level == 0)
4053 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004054 else {
4055 u64 blockptr = btrfs_node_blockptr(c, slot);
4056 u64 gen = btrfs_node_ptr_generation(c, slot);
4057
4058 if (cache_only) {
4059 struct extent_buffer *cur;
4060 cur = btrfs_find_tree_block(root, blockptr,
4061 btrfs_level_size(root, level - 1));
4062 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4063 slot++;
4064 if (cur)
4065 free_extent_buffer(cur);
4066 goto next;
4067 }
4068 free_extent_buffer(cur);
4069 }
4070 if (gen < min_trans) {
4071 slot++;
4072 goto next;
4073 }
Chris Masone7a84562008-06-25 16:01:31 -04004074 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004075 }
Chris Masone7a84562008-06-25 16:01:31 -04004076 return 0;
4077 }
4078 return 1;
4079}
4080
Chris Mason7bb86312007-12-11 09:25:06 -05004081/*
Chris Mason925baed2008-06-25 16:01:30 -04004082 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004083 * returns 0 if it found something or 1 if there are no greater leaves.
4084 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004085 */
Chris Mason234b63a2007-03-13 10:46:10 -04004086int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004087{
4088 int slot;
4089 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004090 struct extent_buffer *c;
4091 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004092 struct btrfs_key key;
4093 u32 nritems;
4094 int ret;
4095
4096 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004097 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004098 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004099
4100 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4101
Chris Mason925baed2008-06-25 16:01:30 -04004102 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04004103 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04004104 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4105 path->keep_locks = 0;
4106
4107 if (ret < 0)
4108 return ret;
4109
Chris Masonb4ce94d2009-02-04 09:25:08 -05004110 btrfs_set_path_blocking(path);
Chris Masona2135012008-06-25 16:01:30 -04004111 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004112 /*
4113 * by releasing the path above we dropped all our locks. A balance
4114 * could have added more items next to the key that used to be
4115 * at the very end of the block. So, check again here and
4116 * advance the path if there are now more items available.
4117 */
Chris Masona2135012008-06-25 16:01:30 -04004118 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004119 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04004120 goto done;
4121 }
Chris Masond97e63b2007-02-20 16:40:44 -05004122
Chris Masond3977122009-01-05 21:25:51 -05004123 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05004124 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05004125 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004126
Chris Masond97e63b2007-02-20 16:40:44 -05004127 slot = path->slots[level] + 1;
4128 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004129 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004130 level++;
Chris Masond3977122009-01-05 21:25:51 -05004131 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05004132 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004133 continue;
4134 }
Chris Mason5f39d392007-10-15 16:14:19 -04004135
Chris Mason925baed2008-06-25 16:01:30 -04004136 if (next) {
4137 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004138 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004139 }
Chris Mason5f39d392007-10-15 16:14:19 -04004140
Chris Masonb4ce94d2009-02-04 09:25:08 -05004141 /* the path was set to blocking above */
Chris Mason0bd40a72008-07-17 12:54:43 -04004142 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4143 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05004144 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004145
Chris Masonca7a79a2008-05-12 12:59:19 -04004146 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04004147 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004148 btrfs_assert_tree_locked(c);
Chris Mason5cd57b22008-06-25 16:01:30 -04004149 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004150 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004151 }
Chris Masond97e63b2007-02-20 16:40:44 -05004152 break;
4153 }
4154 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004155 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004156 level--;
4157 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004158 if (path->locks[level])
4159 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04004160 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004161 path->nodes[level] = next;
4162 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004163 if (!path->skip_locking)
4164 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004165 if (!level)
4166 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004167
4168 btrfs_set_path_blocking(path);
Chris Mason925baed2008-06-25 16:01:30 -04004169 if (level == 1 && path->locks[1] && path->reada)
4170 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04004171 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04004172 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004173 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5cd57b22008-06-25 16:01:30 -04004174 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004175 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004176 }
Chris Masond97e63b2007-02-20 16:40:44 -05004177 }
Chris Mason925baed2008-06-25 16:01:30 -04004178done:
4179 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05004180 return 0;
4181}
Chris Mason0b86a832008-03-24 15:01:56 -04004182
Chris Mason3f157a22008-06-25 16:01:31 -04004183/*
4184 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4185 * searching until it gets past min_objectid or finds an item of 'type'
4186 *
4187 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4188 */
Chris Mason0b86a832008-03-24 15:01:56 -04004189int btrfs_previous_item(struct btrfs_root *root,
4190 struct btrfs_path *path, u64 min_objectid,
4191 int type)
4192{
4193 struct btrfs_key found_key;
4194 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004195 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004196 int ret;
4197
Chris Masond3977122009-01-05 21:25:51 -05004198 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004199 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004200 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004201 ret = btrfs_prev_leaf(root, path);
4202 if (ret != 0)
4203 return ret;
4204 } else {
4205 path->slots[0]--;
4206 }
4207 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004208 nritems = btrfs_header_nritems(leaf);
4209 if (nritems == 0)
4210 return 1;
4211 if (path->slots[0] == nritems)
4212 path->slots[0]--;
4213
Chris Mason0b86a832008-03-24 15:01:56 -04004214 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4215 if (found_key.type == type)
4216 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004217 if (found_key.objectid < min_objectid)
4218 break;
4219 if (found_key.objectid == min_objectid &&
4220 found_key.type < type)
4221 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004222 }
4223 return 1;
4224}