blob: a99f1c2a710d771a416b6b34b6199d0fe44178ca [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 Masonc8c42862009-04-03 10:14:18 -04001247static 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
Chris Mason8c594ea2009-04-20 15:50:10 -04001328 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001329 if (!parent)
1330 return 0;
1331
1332 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001333 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001334 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 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001344 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001345 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;
Chris Mason8c594ea2009-04-20 15:50:10 -04001354
1355 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001356 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001357
1358 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001359 if (block1)
1360 readahead_tree_block(root, block1, blocksize, 0);
1361 if (block2)
1362 readahead_tree_block(root, block2, blocksize, 0);
1363
1364 if (block1) {
1365 eb = read_tree_block(root, block1, blocksize, 0);
1366 free_extent_buffer(eb);
1367 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001368 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001369 eb = read_tree_block(root, block2, blocksize, 0);
1370 free_extent_buffer(eb);
1371 }
1372 }
1373 return ret;
1374}
1375
1376
1377/*
Chris Masond3977122009-01-05 21:25:51 -05001378 * when we walk down the tree, it is usually safe to unlock the higher layers
1379 * in the tree. The exceptions are when our path goes through slot 0, because
1380 * operations on the tree might require changing key pointers higher up in the
1381 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001382 *
Chris Masond3977122009-01-05 21:25:51 -05001383 * callers might also have set path->keep_locks, which tells this code to keep
1384 * the lock if the path points to the last slot in the block. This is part of
1385 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001386 *
Chris Masond3977122009-01-05 21:25:51 -05001387 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1388 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001389 */
Chris Masone02119d2008-09-05 16:13:11 -04001390static noinline void unlock_up(struct btrfs_path *path, int level,
1391 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001392{
1393 int i;
1394 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001395 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001396 struct extent_buffer *t;
1397
1398 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1399 if (!path->nodes[i])
1400 break;
1401 if (!path->locks[i])
1402 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001404 skip_level = i + 1;
1405 continue;
1406 }
Chris Mason051e1b92008-06-25 16:01:30 -04001407 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001408 u32 nritems;
1409 t = path->nodes[i];
1410 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001411 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001412 skip_level = i + 1;
1413 continue;
1414 }
1415 }
Chris Mason051e1b92008-06-25 16:01:30 -04001416 if (skip_level < i && i >= lowest_unlock)
1417 no_skips = 1;
1418
Chris Mason925baed2008-06-25 16:01:30 -04001419 t = path->nodes[i];
1420 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1421 btrfs_tree_unlock(t);
1422 path->locks[i] = 0;
1423 }
1424 }
1425}
1426
Chris Mason3c69fae2007-08-07 15:52:22 -04001427/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001428 * This releases any locks held in the path starting at level and
1429 * going all the way up to the root.
1430 *
1431 * btrfs_search_slot will keep the lock held on higher nodes in a few
1432 * corner cases, such as COW of the block at slot zero in the node. This
1433 * ignores those rules, and it should only be called when there are no
1434 * more updates to be done higher up in the tree.
1435 */
1436noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1437{
1438 int i;
1439
1440 if (path->keep_locks || path->lowest_level)
1441 return;
1442
1443 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1444 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001445 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001446 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001447 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001448 btrfs_tree_unlock(path->nodes[i]);
1449 path->locks[i] = 0;
1450 }
1451}
1452
1453/*
Chris Masonc8c42862009-04-03 10:14:18 -04001454 * helper function for btrfs_search_slot. The goal is to find a block
1455 * in cache without setting the path to blocking. If we find the block
1456 * we return zero and the path is unchanged.
1457 *
1458 * If we can't find the block, we set the path blocking and do some
1459 * reada. -EAGAIN is returned and the search must be repeated.
1460 */
1461static int
1462read_block_for_search(struct btrfs_trans_handle *trans,
1463 struct btrfs_root *root, struct btrfs_path *p,
1464 struct extent_buffer **eb_ret, int level, int slot,
1465 struct btrfs_key *key)
1466{
1467 u64 blocknr;
1468 u64 gen;
1469 u32 blocksize;
1470 struct extent_buffer *b = *eb_ret;
1471 struct extent_buffer *tmp;
1472
1473 blocknr = btrfs_node_blockptr(b, slot);
1474 gen = btrfs_node_ptr_generation(b, slot);
1475 blocksize = btrfs_level_size(root, level - 1);
1476
1477 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1478 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1479 *eb_ret = tmp;
1480 return 0;
1481 }
1482
1483 /*
1484 * reduce lock contention at high levels
1485 * of the btree by dropping locks before
1486 * we read.
1487 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001488 btrfs_unlock_up_safe(p, level + 1);
1489 btrfs_set_path_blocking(p);
1490
Chris Masonc8c42862009-04-03 10:14:18 -04001491 if (tmp)
1492 free_extent_buffer(tmp);
1493 if (p->reada)
1494 reada_for_search(root, p, level, slot, key->objectid);
1495
Chris Mason8c594ea2009-04-20 15:50:10 -04001496 btrfs_release_path(NULL, p);
Chris Masonc8c42862009-04-03 10:14:18 -04001497 tmp = read_tree_block(root, blocknr, blocksize, gen);
1498 if (tmp)
1499 free_extent_buffer(tmp);
1500 return -EAGAIN;
1501}
1502
1503/*
1504 * helper function for btrfs_search_slot. This does all of the checks
1505 * for node-level blocks and does any balancing required based on
1506 * the ins_len.
1507 *
1508 * If no extra work was required, zero is returned. If we had to
1509 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1510 * start over
1511 */
1512static int
1513setup_nodes_for_search(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *root, struct btrfs_path *p,
1515 struct extent_buffer *b, int level, int ins_len)
1516{
1517 int ret;
1518 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1519 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1520 int sret;
1521
1522 sret = reada_for_balance(root, p, level);
1523 if (sret)
1524 goto again;
1525
1526 btrfs_set_path_blocking(p);
1527 sret = split_node(trans, root, p, level);
1528 btrfs_clear_path_blocking(p, NULL);
1529
1530 BUG_ON(sret > 0);
1531 if (sret) {
1532 ret = sret;
1533 goto done;
1534 }
1535 b = p->nodes[level];
1536 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1537 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
1538 int sret;
1539
1540 sret = reada_for_balance(root, p, level);
1541 if (sret)
1542 goto again;
1543
1544 btrfs_set_path_blocking(p);
1545 sret = balance_level(trans, root, p, level);
1546 btrfs_clear_path_blocking(p, NULL);
1547
1548 if (sret) {
1549 ret = sret;
1550 goto done;
1551 }
1552 b = p->nodes[level];
1553 if (!b) {
1554 btrfs_release_path(NULL, p);
1555 goto again;
1556 }
1557 BUG_ON(btrfs_header_nritems(b) == 1);
1558 }
1559 return 0;
1560
1561again:
1562 ret = -EAGAIN;
1563done:
1564 return ret;
1565}
1566
1567/*
Chris Mason74123bd2007-02-02 11:05:29 -05001568 * look for key in the tree. path is filled in with nodes along the way
1569 * if key is found, we return zero and you can find the item in the leaf
1570 * level of the path (level 0)
1571 *
1572 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001573 * be inserted, and 1 is returned. If there are other errors during the
1574 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001575 *
1576 * if ins_len > 0, nodes and leaves will be split as we walk down the
1577 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1578 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001579 */
Chris Masone089f052007-03-16 16:20:31 -04001580int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1581 *root, struct btrfs_key *key, struct btrfs_path *p, int
1582 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001583{
Chris Mason5f39d392007-10-15 16:14:19 -04001584 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001585 int slot;
1586 int ret;
1587 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001588 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001589 u8 lowest_level = 0;
1590
Chris Mason6702ed42007-08-07 16:15:09 -04001591 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001592 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001593 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001594
Chris Mason925baed2008-06-25 16:01:30 -04001595 if (ins_len < 0)
1596 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001597
Chris Masonbb803952007-03-01 12:04:21 -05001598again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001599 if (p->skip_locking)
1600 b = btrfs_root_node(root);
1601 else
1602 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001603
Chris Masoneb60cea2007-02-02 09:18:22 -05001604 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001605 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001606
1607 /*
1608 * setup the path here so we can release it under lock
1609 * contention with the cow code
1610 */
1611 p->nodes[level] = b;
1612 if (!p->skip_locking)
1613 p->locks[level] = 1;
1614
Chris Mason02217ed2007-03-02 16:08:05 -05001615 if (cow) {
1616 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001617
Chris Masonc8c42862009-04-03 10:14:18 -04001618 /*
1619 * if we don't really need to cow this block
1620 * then we don't want to set the path blocking,
1621 * so we test it here
1622 */
Chris Mason65b51a02008-08-01 15:11:20 -04001623 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001624 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001625 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001626 goto cow_done;
1627 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001628 btrfs_set_path_blocking(p);
1629
Chris Masone20d96d2007-03-22 12:13:20 -04001630 wret = btrfs_cow_block(trans, root, b,
1631 p->nodes[level + 1],
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001632 p->slots[level + 1], &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001633 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001634 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001635 ret = wret;
1636 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001637 }
Chris Mason02217ed2007-03-02 16:08:05 -05001638 }
Chris Mason65b51a02008-08-01 15:11:20 -04001639cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001640 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001641 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001642 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001643 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001644
Chris Masoneb60cea2007-02-02 09:18:22 -05001645 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001646 if (!p->skip_locking)
1647 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001648
Chris Mason4008c042009-02-12 14:09:45 -05001649 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001650
1651 /*
1652 * we have a lock on b and as long as we aren't changing
1653 * the tree, there is no way to for the items in b to change.
1654 * It is safe to drop the lock on our parent before we
1655 * go through the expensive btree search on b.
1656 *
1657 * If cow is true, then we might be changing slot zero,
1658 * which may require changing the parent. So, we can't
1659 * drop the lock until after we know which slot we're
1660 * operating on.
1661 */
1662 if (!cow)
1663 btrfs_unlock_up_safe(p, level + 1);
1664
Chris Mason123abc82007-03-14 14:14:43 -04001665 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001666 if (ret) {
1667 ret = -1;
1668 goto done;
1669 }
Chris Mason925baed2008-06-25 16:01:30 -04001670
Chris Mason5f39d392007-10-15 16:14:19 -04001671 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001672
Chris Mason5f39d392007-10-15 16:14:19 -04001673 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001674 if (ret && slot > 0)
1675 slot -= 1;
1676 p->slots[level] = slot;
Chris Masonc8c42862009-04-03 10:14:18 -04001677 ret = setup_nodes_for_search(trans, root, p, b, level,
1678 ins_len);
1679 if (ret == -EAGAIN)
1680 goto again;
1681 else if (ret)
1682 goto done;
1683 b = p->nodes[level];
1684 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001685
Chris Masonf9efa9c2008-06-25 16:14:04 -04001686 unlock_up(p, level, lowest_unlock);
1687
Chris Mason9f3a7422007-08-07 15:52:19 -04001688 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001689 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001690 ret = 0;
1691 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001692 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001693
Chris Masonc8c42862009-04-03 10:14:18 -04001694 ret = read_block_for_search(trans, root, p,
1695 &b, level, slot, key);
1696 if (ret == -EAGAIN)
1697 goto again;
Chris Mason594a24e2008-06-25 16:01:30 -04001698
Chris Masonb4ce94d2009-02-04 09:25:08 -05001699 if (!p->skip_locking) {
1700 int lret;
1701
Chris Mason4008c042009-02-12 14:09:45 -05001702 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001703 lret = btrfs_try_spin_lock(b);
1704
1705 if (!lret) {
1706 btrfs_set_path_blocking(p);
1707 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001708 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001709 }
1710 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001711 } else {
1712 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001713 if (ins_len > 0 &&
1714 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001715 int sret;
1716
1717 btrfs_set_path_blocking(p);
1718 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001719 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001720 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001721
Chris Mason5c680ed2007-02-22 11:39:13 -05001722 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001723 if (sret) {
1724 ret = sret;
1725 goto done;
1726 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001727 }
Chris Mason459931e2008-12-10 09:10:46 -05001728 if (!p->search_for_split)
1729 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001730 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001731 }
1732 }
Chris Mason65b51a02008-08-01 15:11:20 -04001733 ret = 1;
1734done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001735 /*
1736 * we don't really know what they plan on doing with the path
1737 * from here on, so for now just mark it as blocking
1738 */
Chris Masonb9473432009-03-13 11:00:37 -04001739 if (!p->leave_spinning)
1740 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001741 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001742}
1743
Zheng Yan1a40e232008-09-26 10:09:34 -04001744int btrfs_merge_path(struct btrfs_trans_handle *trans,
1745 struct btrfs_root *root,
1746 struct btrfs_key *node_keys,
1747 u64 *nodes, int lowest_level)
1748{
1749 struct extent_buffer *eb;
1750 struct extent_buffer *parent;
1751 struct btrfs_key key;
1752 u64 bytenr;
1753 u64 generation;
1754 u32 blocksize;
1755 int level;
1756 int slot;
1757 int key_match;
1758 int ret;
1759
1760 eb = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001761 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001762 BUG_ON(ret);
1763
Chris Masonb4ce94d2009-02-04 09:25:08 -05001764 btrfs_set_lock_blocking(eb);
1765
Zheng Yan1a40e232008-09-26 10:09:34 -04001766 parent = eb;
1767 while (1) {
1768 level = btrfs_header_level(parent);
1769 if (level == 0 || level <= lowest_level)
1770 break;
1771
1772 ret = bin_search(parent, &node_keys[lowest_level], level,
1773 &slot);
1774 if (ret && slot > 0)
1775 slot--;
1776
1777 bytenr = btrfs_node_blockptr(parent, slot);
1778 if (nodes[level - 1] == bytenr)
1779 break;
1780
1781 blocksize = btrfs_level_size(root, level - 1);
1782 generation = btrfs_node_ptr_generation(parent, slot);
1783 btrfs_node_key_to_cpu(eb, &key, slot);
1784 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1785
Yan Zhengf82d02d2008-10-29 14:49:05 -04001786 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001787 eb = read_tree_block(root, bytenr, blocksize,
1788 generation);
1789 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001790 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001791 }
1792
1793 /*
1794 * if node keys match and node pointer hasn't been modified
1795 * in the running transaction, we can merge the path. for
1796 * blocks owened by reloc trees, the node pointer check is
1797 * skipped, this is because these blocks are fully controlled
1798 * by the space balance code, no one else can modify them.
1799 */
1800 if (!nodes[level - 1] || !key_match ||
1801 (generation == trans->transid &&
1802 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1803 if (level == 1 || level == lowest_level + 1) {
1804 if (generation == trans->transid) {
1805 btrfs_tree_unlock(eb);
1806 free_extent_buffer(eb);
1807 }
1808 break;
1809 }
1810
1811 if (generation != trans->transid) {
1812 eb = read_tree_block(root, bytenr, blocksize,
1813 generation);
1814 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001815 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001816 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001817
1818 ret = btrfs_cow_block(trans, root, eb, parent, slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001819 &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001820 BUG_ON(ret);
1821
Yan Zhengf82d02d2008-10-29 14:49:05 -04001822 if (root->root_key.objectid ==
1823 BTRFS_TREE_RELOC_OBJECTID) {
1824 if (!nodes[level - 1]) {
1825 nodes[level - 1] = eb->start;
1826 memcpy(&node_keys[level - 1], &key,
1827 sizeof(node_keys[0]));
1828 } else {
1829 WARN_ON(1);
1830 }
1831 }
1832
Zheng Yan1a40e232008-09-26 10:09:34 -04001833 btrfs_tree_unlock(parent);
1834 free_extent_buffer(parent);
1835 parent = eb;
1836 continue;
1837 }
1838
Zheng Yan1a40e232008-09-26 10:09:34 -04001839 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1840 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1841 btrfs_mark_buffer_dirty(parent);
1842
1843 ret = btrfs_inc_extent_ref(trans, root,
1844 nodes[level - 1],
1845 blocksize, parent->start,
1846 btrfs_header_owner(parent),
1847 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001848 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001849 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001850
1851 /*
1852 * If the block was created in the running transaction,
1853 * it's possible this is the last reference to it, so we
1854 * should drop the subtree.
1855 */
1856 if (generation == trans->transid) {
1857 ret = btrfs_drop_subtree(trans, root, eb, parent);
1858 BUG_ON(ret);
1859 btrfs_tree_unlock(eb);
1860 free_extent_buffer(eb);
1861 } else {
1862 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001863 blocksize, parent->start,
1864 btrfs_header_owner(parent),
1865 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001866 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001867 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001868 }
1869 break;
1870 }
1871 btrfs_tree_unlock(parent);
1872 free_extent_buffer(parent);
1873 return 0;
1874}
1875
Chris Mason74123bd2007-02-02 11:05:29 -05001876/*
1877 * adjust the pointers going up the tree, starting at level
1878 * making sure the right key of each node is points to 'key'.
1879 * This is used after shifting pointers to the left, so it stops
1880 * fixing up pointers when a given leaf/node is not in slot 0 of the
1881 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001882 *
1883 * If this fails to write a tree block, it returns -1, but continues
1884 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001885 */
Chris Mason5f39d392007-10-15 16:14:19 -04001886static int fixup_low_keys(struct btrfs_trans_handle *trans,
1887 struct btrfs_root *root, struct btrfs_path *path,
1888 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001889{
1890 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001891 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001892 struct extent_buffer *t;
1893
Chris Mason234b63a2007-03-13 10:46:10 -04001894 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001895 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001896 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001898 t = path->nodes[i];
1899 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001900 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001901 if (tslot != 0)
1902 break;
1903 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001904 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001905}
1906
Chris Mason74123bd2007-02-02 11:05:29 -05001907/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001908 * update item key.
1909 *
1910 * This function isn't completely safe. It's the caller's responsibility
1911 * that the new key won't break the order
1912 */
1913int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root, struct btrfs_path *path,
1915 struct btrfs_key *new_key)
1916{
1917 struct btrfs_disk_key disk_key;
1918 struct extent_buffer *eb;
1919 int slot;
1920
1921 eb = path->nodes[0];
1922 slot = path->slots[0];
1923 if (slot > 0) {
1924 btrfs_item_key(eb, &disk_key, slot - 1);
1925 if (comp_keys(&disk_key, new_key) >= 0)
1926 return -1;
1927 }
1928 if (slot < btrfs_header_nritems(eb) - 1) {
1929 btrfs_item_key(eb, &disk_key, slot + 1);
1930 if (comp_keys(&disk_key, new_key) <= 0)
1931 return -1;
1932 }
1933
1934 btrfs_cpu_key_to_disk(&disk_key, new_key);
1935 btrfs_set_item_key(eb, &disk_key, slot);
1936 btrfs_mark_buffer_dirty(eb);
1937 if (slot == 0)
1938 fixup_low_keys(trans, root, path, &disk_key, 1);
1939 return 0;
1940}
1941
1942/*
Chris Mason74123bd2007-02-02 11:05:29 -05001943 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001944 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001945 *
1946 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1947 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001948 */
Chris Mason98ed5172008-01-03 10:01:48 -05001949static int push_node_left(struct btrfs_trans_handle *trans,
1950 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001951 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001952{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001953 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001954 int src_nritems;
1955 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001956 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001957
Chris Mason5f39d392007-10-15 16:14:19 -04001958 src_nritems = btrfs_header_nritems(src);
1959 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001960 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001961 WARN_ON(btrfs_header_generation(src) != trans->transid);
1962 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001963
Chris Masonbce4eae2008-04-24 14:42:46 -04001964 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001965 return 1;
1966
Chris Masond3977122009-01-05 21:25:51 -05001967 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001968 return 1;
1969
Chris Masonbce4eae2008-04-24 14:42:46 -04001970 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001971 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001972 if (push_items < src_nritems) {
1973 /* leave at least 8 pointers in the node if
1974 * we aren't going to empty it
1975 */
1976 if (src_nritems - push_items < 8) {
1977 if (push_items <= 8)
1978 return 1;
1979 push_items -= 8;
1980 }
1981 }
1982 } else
1983 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001984
Chris Mason5f39d392007-10-15 16:14:19 -04001985 copy_extent_buffer(dst, src,
1986 btrfs_node_key_ptr_offset(dst_nritems),
1987 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001988 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001989
Chris Masonbb803952007-03-01 12:04:21 -05001990 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001991 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1992 btrfs_node_key_ptr_offset(push_items),
1993 (src_nritems - push_items) *
1994 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001995 }
Chris Mason5f39d392007-10-15 16:14:19 -04001996 btrfs_set_header_nritems(src, src_nritems - push_items);
1997 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1998 btrfs_mark_buffer_dirty(src);
1999 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002000
2001 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
2002 BUG_ON(ret);
2003
Chris Masonbb803952007-03-01 12:04:21 -05002004 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002005}
2006
Chris Mason97571fd2007-02-24 13:39:08 -05002007/*
Chris Mason79f95c82007-03-01 15:16:26 -05002008 * try to push data from one node into the next node right in the
2009 * tree.
2010 *
2011 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2012 * error, and > 0 if there was no room in the right hand block.
2013 *
2014 * this will only push up to 1/2 the contents of the left node over
2015 */
Chris Mason5f39d392007-10-15 16:14:19 -04002016static int balance_node_right(struct btrfs_trans_handle *trans,
2017 struct btrfs_root *root,
2018 struct extent_buffer *dst,
2019 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002020{
Chris Mason79f95c82007-03-01 15:16:26 -05002021 int push_items = 0;
2022 int max_push;
2023 int src_nritems;
2024 int dst_nritems;
2025 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002026
Chris Mason7bb86312007-12-11 09:25:06 -05002027 WARN_ON(btrfs_header_generation(src) != trans->transid);
2028 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 src_nritems = btrfs_header_nritems(src);
2031 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002032 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002033 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002034 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002035
Chris Masond3977122009-01-05 21:25:51 -05002036 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002037 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002038
2039 max_push = src_nritems / 2 + 1;
2040 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002041 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002042 return 1;
Yan252c38f2007-08-29 09:11:44 -04002043
Chris Mason79f95c82007-03-01 15:16:26 -05002044 if (max_push < push_items)
2045 push_items = max_push;
2046
Chris Mason5f39d392007-10-15 16:14:19 -04002047 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2048 btrfs_node_key_ptr_offset(0),
2049 (dst_nritems) *
2050 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002051
Chris Mason5f39d392007-10-15 16:14:19 -04002052 copy_extent_buffer(dst, src,
2053 btrfs_node_key_ptr_offset(0),
2054 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002055 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002056
Chris Mason5f39d392007-10-15 16:14:19 -04002057 btrfs_set_header_nritems(src, src_nritems - push_items);
2058 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002059
Chris Mason5f39d392007-10-15 16:14:19 -04002060 btrfs_mark_buffer_dirty(src);
2061 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002062
2063 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2064 BUG_ON(ret);
2065
Chris Mason79f95c82007-03-01 15:16:26 -05002066 return ret;
2067}
2068
2069/*
Chris Mason97571fd2007-02-24 13:39:08 -05002070 * helper function to insert a new root level in the tree.
2071 * A new node is allocated, and a single item is inserted to
2072 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002073 *
2074 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002075 */
Chris Masond3977122009-01-05 21:25:51 -05002076static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002077 struct btrfs_root *root,
2078 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002079{
Chris Mason7bb86312007-12-11 09:25:06 -05002080 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002081 struct extent_buffer *lower;
2082 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002083 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002084 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002085 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002086
2087 BUG_ON(path->nodes[level]);
2088 BUG_ON(path->nodes[level-1] != root->node);
2089
Chris Mason7bb86312007-12-11 09:25:06 -05002090 lower = path->nodes[level-1];
2091 if (level == 1)
2092 btrfs_item_key(lower, &lower_key, 0);
2093 else
2094 btrfs_node_key(lower, &lower_key, 0);
2095
Zheng Yan31840ae2008-09-23 13:14:14 -04002096 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2097 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002098 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 if (IS_ERR(c))
2100 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002101
Chris Mason5f39d392007-10-15 16:14:19 -04002102 memset_extent_buffer(c, 0, 0, root->nodesize);
2103 btrfs_set_header_nritems(c, 1);
2104 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002105 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 btrfs_set_header_generation(c, trans->transid);
2107 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 write_extent_buffer(c, root->fs_info->fsid,
2110 (unsigned long)btrfs_header_fsid(c),
2111 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002112
2113 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2114 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2115 BTRFS_UUID_SIZE);
2116
Chris Mason5f39d392007-10-15 16:14:19 -04002117 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002118 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002119 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002120 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002121
2122 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002123
2124 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002125
Chris Mason925baed2008-06-25 16:01:30 -04002126 spin_lock(&root->node_lock);
2127 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002128 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002129 spin_unlock(&root->node_lock);
2130
Zheng Yan31840ae2008-09-23 13:14:14 -04002131 ret = btrfs_update_extent_ref(trans, root, lower->start,
Chris Mason56bec292009-03-13 10:10:06 -04002132 lower->len, lower->start, c->start,
Zheng Yan31840ae2008-09-23 13:14:14 -04002133 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002134 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002135 BUG_ON(ret);
2136
Chris Mason925baed2008-06-25 16:01:30 -04002137 /* the super has an extra ref to root->node */
2138 free_extent_buffer(old);
2139
Chris Mason0b86a832008-03-24 15:01:56 -04002140 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002141 extent_buffer_get(c);
2142 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002143 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002144 path->slots[level] = 0;
2145 return 0;
2146}
2147
Chris Mason74123bd2007-02-02 11:05:29 -05002148/*
2149 * worker function to insert a single pointer in a node.
2150 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002151 *
Chris Mason74123bd2007-02-02 11:05:29 -05002152 * slot and level indicate where you want the key to go, and
2153 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002154 *
2155 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002156 */
Chris Masone089f052007-03-16 16:20:31 -04002157static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2158 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002159 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002160{
Chris Mason5f39d392007-10-15 16:14:19 -04002161 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002162 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002163
2164 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 lower = path->nodes[level];
2166 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002167 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002168 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002169 BUG();
2170 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002171 memmove_extent_buffer(lower,
2172 btrfs_node_key_ptr_offset(slot + 1),
2173 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002174 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002175 }
Chris Mason5f39d392007-10-15 16:14:19 -04002176 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002177 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002178 WARN_ON(trans->transid == 0);
2179 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002180 btrfs_set_header_nritems(lower, nritems + 1);
2181 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002182 return 0;
2183}
2184
Chris Mason97571fd2007-02-24 13:39:08 -05002185/*
2186 * split the node at the specified level in path in two.
2187 * The path is corrected to point to the appropriate node after the split
2188 *
2189 * Before splitting this tries to make some room in the node by pushing
2190 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002191 *
2192 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002193 */
Chris Masone02119d2008-09-05 16:13:11 -04002194static noinline int split_node(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002197{
Chris Mason5f39d392007-10-15 16:14:19 -04002198 struct extent_buffer *c;
2199 struct extent_buffer *split;
2200 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002201 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002202 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002203 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002204 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002205
Chris Mason5f39d392007-10-15 16:14:19 -04002206 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002207 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002208 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002209 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002210 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002211 if (ret)
2212 return ret;
Chris Masona4b6e072009-03-16 10:59:57 -04002213 } else if (!trans->transaction->delayed_refs.flushing) {
Chris Masone66f7092007-04-20 13:16:02 -04002214 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 c = path->nodes[level];
2216 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002217 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002218 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002219 if (ret < 0)
2220 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002221 }
Chris Masone66f7092007-04-20 13:16:02 -04002222
Chris Mason5f39d392007-10-15 16:14:19 -04002223 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002224
Chris Mason925baed2008-06-25 16:01:30 -04002225 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002226 path->nodes[level + 1]->start,
2227 root->root_key.objectid,
2228 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002229 if (IS_ERR(split))
2230 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 btrfs_set_header_flags(split, btrfs_header_flags(c));
2233 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002234 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002235 btrfs_set_header_generation(split, trans->transid);
2236 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002237 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002238 write_extent_buffer(split, root->fs_info->fsid,
2239 (unsigned long)btrfs_header_fsid(split),
2240 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002241 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2242 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2243 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002244
Chris Mason7518a232007-03-12 12:01:18 -04002245 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002246
2247 copy_extent_buffer(split, c,
2248 btrfs_node_key_ptr_offset(0),
2249 btrfs_node_key_ptr_offset(mid),
2250 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2251 btrfs_set_header_nritems(split, c_nritems - mid);
2252 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002253 ret = 0;
2254
Chris Mason5f39d392007-10-15 16:14:19 -04002255 btrfs_mark_buffer_dirty(c);
2256 btrfs_mark_buffer_dirty(split);
2257
2258 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002259 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002260 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002261 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002262 if (wret)
2263 ret = wret;
2264
Zheng Yan31840ae2008-09-23 13:14:14 -04002265 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2266 BUG_ON(ret);
2267
Chris Mason5de08d72007-02-24 06:24:44 -05002268 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002269 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002270 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002271 free_extent_buffer(c);
2272 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002273 path->slots[level + 1] += 1;
2274 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002275 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002276 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002277 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002278 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002279}
2280
Chris Mason74123bd2007-02-02 11:05:29 -05002281/*
2282 * how many bytes are required to store the items in a leaf. start
2283 * and nr indicate which items in the leaf to check. This totals up the
2284 * space used both by the item structs and the item data
2285 */
Chris Mason5f39d392007-10-15 16:14:19 -04002286static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002287{
2288 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002289 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002290 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002291
2292 if (!nr)
2293 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002294 data_len = btrfs_item_end_nr(l, start);
2295 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002296 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002297 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002298 return data_len;
2299}
2300
Chris Mason74123bd2007-02-02 11:05:29 -05002301/*
Chris Masond4dbff92007-04-04 14:08:15 -04002302 * The space between the end of the leaf items and
2303 * the start of the leaf data. IOW, how much room
2304 * the leaf has left for both items and data
2305 */
Chris Masond3977122009-01-05 21:25:51 -05002306noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002307 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002308{
Chris Mason5f39d392007-10-15 16:14:19 -04002309 int nritems = btrfs_header_nritems(leaf);
2310 int ret;
2311 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2312 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002313 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2314 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002315 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002316 leaf_space_used(leaf, 0, nritems), nritems);
2317 }
2318 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002319}
2320
Chris Mason44871b12009-03-13 10:04:31 -04002321static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2322 struct btrfs_root *root,
2323 struct btrfs_path *path,
2324 int data_size, int empty,
2325 struct extent_buffer *right,
2326 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002327{
Chris Mason5f39d392007-10-15 16:14:19 -04002328 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002329 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002330 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002331 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002332 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002333 int push_space = 0;
2334 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002335 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002336 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002337 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002338 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002339 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002340 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002341
Chris Mason34a38212007-11-07 13:31:03 -05002342 if (empty)
2343 nr = 0;
2344 else
2345 nr = 1;
2346
Zheng Yan31840ae2008-09-23 13:14:14 -04002347 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002348 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002349
Chris Mason44871b12009-03-13 10:04:31 -04002350 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002351 i = left_nritems - 1;
2352 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002353 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002354
Zheng Yan31840ae2008-09-23 13:14:14 -04002355 if (!empty && push_items > 0) {
2356 if (path->slots[0] > i)
2357 break;
2358 if (path->slots[0] == i) {
2359 int space = btrfs_leaf_free_space(root, left);
2360 if (space + push_space * 2 > free_space)
2361 break;
2362 }
2363 }
2364
Chris Mason00ec4c52007-02-24 12:47:20 -05002365 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002366 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002367
2368 if (!left->map_token) {
2369 map_extent_buffer(left, (unsigned long)item,
2370 sizeof(struct btrfs_item),
2371 &left->map_token, &left->kaddr,
2372 &left->map_start, &left->map_len,
2373 KM_USER1);
2374 }
2375
2376 this_item_size = btrfs_item_size(left, item);
2377 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002378 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002379
Chris Mason00ec4c52007-02-24 12:47:20 -05002380 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002381 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002382 if (i == 0)
2383 break;
2384 i--;
Chris Masondb945352007-10-15 16:15:53 -04002385 }
2386 if (left->map_token) {
2387 unmap_extent_buffer(left, left->map_token, KM_USER1);
2388 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002389 }
Chris Mason5f39d392007-10-15 16:14:19 -04002390
Chris Mason925baed2008-06-25 16:01:30 -04002391 if (push_items == 0)
2392 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002393
Chris Mason34a38212007-11-07 13:31:03 -05002394 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002395 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002396
Chris Mason00ec4c52007-02-24 12:47:20 -05002397 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002398 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002399
Chris Mason5f39d392007-10-15 16:14:19 -04002400 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002401 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002402
Chris Mason00ec4c52007-02-24 12:47:20 -05002403 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002404 data_end = leaf_data_end(root, right);
2405 memmove_extent_buffer(right,
2406 btrfs_leaf_data(right) + data_end - push_space,
2407 btrfs_leaf_data(right) + data_end,
2408 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2409
Chris Mason00ec4c52007-02-24 12:47:20 -05002410 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002411 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002412 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2413 btrfs_leaf_data(left) + leaf_data_end(root, left),
2414 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002415
2416 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2417 btrfs_item_nr_offset(0),
2418 right_nritems * sizeof(struct btrfs_item));
2419
Chris Mason00ec4c52007-02-24 12:47:20 -05002420 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002421 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2422 btrfs_item_nr_offset(left_nritems - push_items),
2423 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002424
2425 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002426 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002427 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002428 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002429 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002430 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002431 if (!right->map_token) {
2432 map_extent_buffer(right, (unsigned long)item,
2433 sizeof(struct btrfs_item),
2434 &right->map_token, &right->kaddr,
2435 &right->map_start, &right->map_len,
2436 KM_USER1);
2437 }
2438 push_space -= btrfs_item_size(right, item);
2439 btrfs_set_item_offset(right, item, push_space);
2440 }
2441
2442 if (right->map_token) {
2443 unmap_extent_buffer(right, right->map_token, KM_USER1);
2444 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002445 }
Chris Mason7518a232007-03-12 12:01:18 -04002446 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002447 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002448
Chris Mason34a38212007-11-07 13:31:03 -05002449 if (left_nritems)
2450 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002451 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002452
Zheng Yan31840ae2008-09-23 13:14:14 -04002453 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2454 BUG_ON(ret);
2455
Chris Mason5f39d392007-10-15 16:14:19 -04002456 btrfs_item_key(right, &disk_key, 0);
2457 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002458 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002459
Chris Mason00ec4c52007-02-24 12:47:20 -05002460 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002461 if (path->slots[0] >= left_nritems) {
2462 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002463 if (btrfs_header_nritems(path->nodes[0]) == 0)
2464 clean_tree_block(trans, root, path->nodes[0]);
2465 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002466 free_extent_buffer(path->nodes[0]);
2467 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002468 path->slots[1] += 1;
2469 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002470 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002471 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002472 }
2473 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002474
2475out_unlock:
2476 btrfs_tree_unlock(right);
2477 free_extent_buffer(right);
2478 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002479}
Chris Mason925baed2008-06-25 16:01:30 -04002480
Chris Mason00ec4c52007-02-24 12:47:20 -05002481/*
Chris Mason44871b12009-03-13 10:04:31 -04002482 * push some data in the path leaf to the right, trying to free up at
2483 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2484 *
2485 * returns 1 if the push failed because the other node didn't have enough
2486 * room, 0 if everything worked out and < 0 if there were major errors.
2487 */
2488static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2489 *root, struct btrfs_path *path, int data_size,
2490 int empty)
2491{
2492 struct extent_buffer *left = path->nodes[0];
2493 struct extent_buffer *right;
2494 struct extent_buffer *upper;
2495 int slot;
2496 int free_space;
2497 u32 left_nritems;
2498 int ret;
2499
2500 if (!path->nodes[1])
2501 return 1;
2502
2503 slot = path->slots[1];
2504 upper = path->nodes[1];
2505 if (slot >= btrfs_header_nritems(upper) - 1)
2506 return 1;
2507
2508 btrfs_assert_tree_locked(path->nodes[1]);
2509
2510 right = read_node_slot(root, upper, slot + 1);
2511 btrfs_tree_lock(right);
2512 btrfs_set_lock_blocking(right);
2513
2514 free_space = btrfs_leaf_free_space(root, right);
2515 if (free_space < data_size)
2516 goto out_unlock;
2517
2518 /* cow and double check */
2519 ret = btrfs_cow_block(trans, root, right, upper,
2520 slot + 1, &right);
2521 if (ret)
2522 goto out_unlock;
2523
2524 free_space = btrfs_leaf_free_space(root, right);
2525 if (free_space < data_size)
2526 goto out_unlock;
2527
2528 left_nritems = btrfs_header_nritems(left);
2529 if (left_nritems == 0)
2530 goto out_unlock;
2531
2532 return __push_leaf_right(trans, root, path, data_size, empty,
2533 right, free_space, left_nritems);
2534out_unlock:
2535 btrfs_tree_unlock(right);
2536 free_extent_buffer(right);
2537 return 1;
2538}
2539
2540/*
Chris Mason74123bd2007-02-02 11:05:29 -05002541 * push some data in the path leaf to the left, trying to free up at
2542 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2543 */
Chris Mason44871b12009-03-13 10:04:31 -04002544static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2545 struct btrfs_root *root,
2546 struct btrfs_path *path, int data_size,
2547 int empty, struct extent_buffer *left,
2548 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002549{
Chris Mason5f39d392007-10-15 16:14:19 -04002550 struct btrfs_disk_key disk_key;
2551 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002552 int slot;
2553 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 int push_space = 0;
2555 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002556 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002557 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002558 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002559 int ret = 0;
2560 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002561 u32 this_item_size;
2562 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563
2564 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002565
Chris Mason34a38212007-11-07 13:31:03 -05002566 if (empty)
2567 nr = right_nritems;
2568 else
2569 nr = right_nritems - 1;
2570
2571 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002572 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002573 if (!right->map_token) {
2574 map_extent_buffer(right, (unsigned long)item,
2575 sizeof(struct btrfs_item),
2576 &right->map_token, &right->kaddr,
2577 &right->map_start, &right->map_len,
2578 KM_USER1);
2579 }
2580
Zheng Yan31840ae2008-09-23 13:14:14 -04002581 if (!empty && push_items > 0) {
2582 if (path->slots[0] < i)
2583 break;
2584 if (path->slots[0] == i) {
2585 int space = btrfs_leaf_free_space(root, right);
2586 if (space + push_space * 2 > free_space)
2587 break;
2588 }
2589 }
2590
Chris Masonbe0e5c02007-01-26 15:51:26 -05002591 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002592 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002593
2594 this_item_size = btrfs_item_size(right, item);
2595 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596 break;
Chris Masondb945352007-10-15 16:15:53 -04002597
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002599 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 }
Chris Masondb945352007-10-15 16:15:53 -04002601
2602 if (right->map_token) {
2603 unmap_extent_buffer(right, right->map_token, KM_USER1);
2604 right->map_token = NULL;
2605 }
2606
Chris Masonbe0e5c02007-01-26 15:51:26 -05002607 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002608 ret = 1;
2609 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 }
Chris Mason34a38212007-11-07 13:31:03 -05002611 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002612 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002613
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002615 copy_extent_buffer(left, right,
2616 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2617 btrfs_item_nr_offset(0),
2618 push_items * sizeof(struct btrfs_item));
2619
Chris Mason123abc82007-03-14 14:14:43 -04002620 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002621 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002622
2623 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002624 leaf_data_end(root, left) - push_space,
2625 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002626 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002627 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002628 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002629 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002630
Chris Masondb945352007-10-15 16:15:53 -04002631 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002632 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002633 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002634
Chris Mason5f39d392007-10-15 16:14:19 -04002635 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002636 if (!left->map_token) {
2637 map_extent_buffer(left, (unsigned long)item,
2638 sizeof(struct btrfs_item),
2639 &left->map_token, &left->kaddr,
2640 &left->map_start, &left->map_len,
2641 KM_USER1);
2642 }
2643
Chris Mason5f39d392007-10-15 16:14:19 -04002644 ioff = btrfs_item_offset(left, item);
2645 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002646 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002647 }
Chris Mason5f39d392007-10-15 16:14:19 -04002648 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002649 if (left->map_token) {
2650 unmap_extent_buffer(left, left->map_token, KM_USER1);
2651 left->map_token = NULL;
2652 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002653
2654 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002655 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002656 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2657 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002658 WARN_ON(1);
2659 }
Chris Mason5f39d392007-10-15 16:14:19 -04002660
Chris Mason34a38212007-11-07 13:31:03 -05002661 if (push_items < right_nritems) {
2662 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2663 leaf_data_end(root, right);
2664 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2665 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2666 btrfs_leaf_data(right) +
2667 leaf_data_end(root, right), push_space);
2668
2669 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002670 btrfs_item_nr_offset(push_items),
2671 (btrfs_header_nritems(right) - push_items) *
2672 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002673 }
Yaneef1c492007-11-26 10:58:13 -05002674 right_nritems -= push_items;
2675 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002676 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 for (i = 0; i < right_nritems; i++) {
2678 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002679
2680 if (!right->map_token) {
2681 map_extent_buffer(right, (unsigned long)item,
2682 sizeof(struct btrfs_item),
2683 &right->map_token, &right->kaddr,
2684 &right->map_start, &right->map_len,
2685 KM_USER1);
2686 }
2687
2688 push_space = push_space - btrfs_item_size(right, item);
2689 btrfs_set_item_offset(right, item, push_space);
2690 }
2691 if (right->map_token) {
2692 unmap_extent_buffer(right, right->map_token, KM_USER1);
2693 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002694 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002695
Chris Mason5f39d392007-10-15 16:14:19 -04002696 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002697 if (right_nritems)
2698 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002699
Zheng Yan31840ae2008-09-23 13:14:14 -04002700 ret = btrfs_update_ref(trans, root, right, left,
2701 old_left_nritems, push_items);
2702 BUG_ON(ret);
2703
Chris Mason5f39d392007-10-15 16:14:19 -04002704 btrfs_item_key(right, &disk_key, 0);
2705 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002706 if (wret)
2707 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002708
2709 /* then fixup the leaf pointer in the path */
2710 if (path->slots[0] < push_items) {
2711 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002712 if (btrfs_header_nritems(path->nodes[0]) == 0)
2713 clean_tree_block(trans, root, path->nodes[0]);
2714 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002715 free_extent_buffer(path->nodes[0]);
2716 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002717 path->slots[1] -= 1;
2718 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002719 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002720 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002721 path->slots[0] -= push_items;
2722 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002723 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002724 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002725out:
2726 btrfs_tree_unlock(left);
2727 free_extent_buffer(left);
2728 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002729}
2730
Chris Mason74123bd2007-02-02 11:05:29 -05002731/*
Chris Mason44871b12009-03-13 10:04:31 -04002732 * push some data in the path leaf to the left, trying to free up at
2733 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2734 */
2735static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2736 *root, struct btrfs_path *path, int data_size,
2737 int empty)
2738{
2739 struct extent_buffer *right = path->nodes[0];
2740 struct extent_buffer *left;
2741 int slot;
2742 int free_space;
2743 u32 right_nritems;
2744 int ret = 0;
2745
2746 slot = path->slots[1];
2747 if (slot == 0)
2748 return 1;
2749 if (!path->nodes[1])
2750 return 1;
2751
2752 right_nritems = btrfs_header_nritems(right);
2753 if (right_nritems == 0)
2754 return 1;
2755
2756 btrfs_assert_tree_locked(path->nodes[1]);
2757
2758 left = read_node_slot(root, path->nodes[1], slot - 1);
2759 btrfs_tree_lock(left);
2760 btrfs_set_lock_blocking(left);
2761
2762 free_space = btrfs_leaf_free_space(root, left);
2763 if (free_space < data_size) {
2764 ret = 1;
2765 goto out;
2766 }
2767
2768 /* cow and double check */
2769 ret = btrfs_cow_block(trans, root, left,
2770 path->nodes[1], slot - 1, &left);
2771 if (ret) {
2772 /* we hit -ENOSPC, but it isn't fatal here */
2773 ret = 1;
2774 goto out;
2775 }
2776
2777 free_space = btrfs_leaf_free_space(root, left);
2778 if (free_space < data_size) {
2779 ret = 1;
2780 goto out;
2781 }
2782
2783 return __push_leaf_left(trans, root, path, data_size,
2784 empty, left, free_space, right_nritems);
2785out:
2786 btrfs_tree_unlock(left);
2787 free_extent_buffer(left);
2788 return ret;
2789}
2790
2791/*
Chris Mason74123bd2007-02-02 11:05:29 -05002792 * split the path's leaf in two, making sure there is at least data_size
2793 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002794 *
2795 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002796 */
Chris Mason44871b12009-03-13 10:04:31 -04002797static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002798 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002799 struct btrfs_path *path,
2800 struct extent_buffer *l,
2801 struct extent_buffer *right,
2802 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002803{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002804 int data_copy_size;
2805 int rt_data_off;
2806 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002807 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002808 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002809 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002810
Chris Mason5f39d392007-10-15 16:14:19 -04002811 nritems = nritems - mid;
2812 btrfs_set_header_nritems(right, nritems);
2813 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2814
2815 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2816 btrfs_item_nr_offset(mid),
2817 nritems * sizeof(struct btrfs_item));
2818
2819 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002820 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2821 data_copy_size, btrfs_leaf_data(l) +
2822 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002823
Chris Mason5f39d392007-10-15 16:14:19 -04002824 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2825 btrfs_item_end_nr(l, mid);
2826
2827 for (i = 0; i < nritems; i++) {
2828 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002829 u32 ioff;
2830
2831 if (!right->map_token) {
2832 map_extent_buffer(right, (unsigned long)item,
2833 sizeof(struct btrfs_item),
2834 &right->map_token, &right->kaddr,
2835 &right->map_start, &right->map_len,
2836 KM_USER1);
2837 }
2838
2839 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002840 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002841 }
Chris Mason74123bd2007-02-02 11:05:29 -05002842
Chris Masondb945352007-10-15 16:15:53 -04002843 if (right->map_token) {
2844 unmap_extent_buffer(right, right->map_token, KM_USER1);
2845 right->map_token = NULL;
2846 }
2847
Chris Mason5f39d392007-10-15 16:14:19 -04002848 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002849 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002850 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002851 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2852 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002853 if (wret)
2854 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002855
2856 btrfs_mark_buffer_dirty(right);
2857 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002858 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002859
Zheng Yan31840ae2008-09-23 13:14:14 -04002860 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2861 BUG_ON(ret);
2862
Chris Masonbe0e5c02007-01-26 15:51:26 -05002863 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002864 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002865 free_extent_buffer(path->nodes[0]);
2866 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002867 path->slots[0] -= mid;
2868 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002869 } else {
2870 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002871 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002872 }
Chris Mason5f39d392007-10-15 16:14:19 -04002873
Chris Masoneb60cea2007-02-02 09:18:22 -05002874 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002875
Chris Mason44871b12009-03-13 10:04:31 -04002876 return ret;
2877}
2878
2879/*
2880 * split the path's leaf in two, making sure there is at least data_size
2881 * available for the resulting leaf level of the path.
2882 *
2883 * returns 0 if all went well and < 0 on failure.
2884 */
2885static noinline int split_leaf(struct btrfs_trans_handle *trans,
2886 struct btrfs_root *root,
2887 struct btrfs_key *ins_key,
2888 struct btrfs_path *path, int data_size,
2889 int extend)
2890{
2891 struct extent_buffer *l;
2892 u32 nritems;
2893 int mid;
2894 int slot;
2895 struct extent_buffer *right;
2896 int ret = 0;
2897 int wret;
2898 int double_split;
2899 int num_doubles = 0;
2900
2901 /* first try to make some room by pushing left and right */
Chris Masona4b6e072009-03-16 10:59:57 -04002902 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY &&
2903 !trans->transaction->delayed_refs.flushing) {
Chris Mason44871b12009-03-13 10:04:31 -04002904 wret = push_leaf_right(trans, root, path, data_size, 0);
2905 if (wret < 0)
2906 return wret;
2907 if (wret) {
2908 wret = push_leaf_left(trans, root, path, data_size, 0);
2909 if (wret < 0)
2910 return wret;
2911 }
2912 l = path->nodes[0];
2913
2914 /* did the pushes work? */
2915 if (btrfs_leaf_free_space(root, l) >= data_size)
2916 return 0;
2917 }
2918
2919 if (!path->nodes[1]) {
2920 ret = insert_new_root(trans, root, path, 1);
2921 if (ret)
2922 return ret;
2923 }
2924again:
2925 double_split = 0;
2926 l = path->nodes[0];
2927 slot = path->slots[0];
2928 nritems = btrfs_header_nritems(l);
2929 mid = (nritems + 1) / 2;
2930
2931 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2932 path->nodes[1]->start,
2933 root->root_key.objectid,
2934 trans->transid, 0, l->start, 0);
2935 if (IS_ERR(right)) {
2936 BUG_ON(1);
2937 return PTR_ERR(right);
2938 }
2939
2940 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2941 btrfs_set_header_bytenr(right, right->start);
2942 btrfs_set_header_generation(right, trans->transid);
2943 btrfs_set_header_owner(right, root->root_key.objectid);
2944 btrfs_set_header_level(right, 0);
2945 write_extent_buffer(right, root->fs_info->fsid,
2946 (unsigned long)btrfs_header_fsid(right),
2947 BTRFS_FSID_SIZE);
2948
2949 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2950 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2951 BTRFS_UUID_SIZE);
2952
2953 if (mid <= slot) {
2954 if (nritems == 1 ||
2955 leaf_space_used(l, mid, nritems - mid) + data_size >
2956 BTRFS_LEAF_DATA_SIZE(root)) {
2957 if (slot >= nritems) {
2958 struct btrfs_disk_key disk_key;
2959
2960 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2961 btrfs_set_header_nritems(right, 0);
2962 wret = insert_ptr(trans, root, path,
2963 &disk_key, right->start,
2964 path->slots[1] + 1, 1);
2965 if (wret)
2966 ret = wret;
2967
2968 btrfs_tree_unlock(path->nodes[0]);
2969 free_extent_buffer(path->nodes[0]);
2970 path->nodes[0] = right;
2971 path->slots[0] = 0;
2972 path->slots[1] += 1;
2973 btrfs_mark_buffer_dirty(right);
2974 return ret;
2975 }
2976 mid = slot;
2977 if (mid != nritems &&
2978 leaf_space_used(l, mid, nritems - mid) +
2979 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2980 double_split = 1;
2981 }
2982 }
2983 } else {
2984 if (leaf_space_used(l, 0, mid) + data_size >
2985 BTRFS_LEAF_DATA_SIZE(root)) {
2986 if (!extend && data_size && slot == 0) {
2987 struct btrfs_disk_key disk_key;
2988
2989 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2990 btrfs_set_header_nritems(right, 0);
2991 wret = insert_ptr(trans, root, path,
2992 &disk_key,
2993 right->start,
2994 path->slots[1], 1);
2995 if (wret)
2996 ret = wret;
2997 btrfs_tree_unlock(path->nodes[0]);
2998 free_extent_buffer(path->nodes[0]);
2999 path->nodes[0] = right;
3000 path->slots[0] = 0;
3001 if (path->slots[1] == 0) {
3002 wret = fixup_low_keys(trans, root,
3003 path, &disk_key, 1);
3004 if (wret)
3005 ret = wret;
3006 }
3007 btrfs_mark_buffer_dirty(right);
3008 return ret;
3009 } else if ((extend || !data_size) && slot == 0) {
3010 mid = 1;
3011 } else {
3012 mid = slot;
3013 if (mid != nritems &&
3014 leaf_space_used(l, mid, nritems - mid) +
3015 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
3016 double_split = 1;
3017 }
3018 }
3019 }
3020 }
3021
3022 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3023 BUG_ON(ret);
3024
Chris Masoncc0c5532007-10-25 15:42:57 -04003025 if (double_split) {
3026 BUG_ON(num_doubles != 0);
3027 num_doubles++;
3028 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003029 }
Chris Mason44871b12009-03-13 10:04:31 -04003030
Chris Masonbe0e5c02007-01-26 15:51:26 -05003031 return ret;
3032}
3033
Chris Masond352ac62008-09-29 15:18:18 -04003034/*
Chris Mason459931e2008-12-10 09:10:46 -05003035 * This function splits a single item into two items,
3036 * giving 'new_key' to the new item and splitting the
3037 * old one at split_offset (from the start of the item).
3038 *
3039 * The path may be released by this operation. After
3040 * the split, the path is pointing to the old item. The
3041 * new item is going to be in the same node as the old one.
3042 *
3043 * Note, the item being split must be smaller enough to live alone on
3044 * a tree block with room for one extra struct btrfs_item
3045 *
3046 * This allows us to split the item in place, keeping a lock on the
3047 * leaf the entire time.
3048 */
3049int btrfs_split_item(struct btrfs_trans_handle *trans,
3050 struct btrfs_root *root,
3051 struct btrfs_path *path,
3052 struct btrfs_key *new_key,
3053 unsigned long split_offset)
3054{
3055 u32 item_size;
3056 struct extent_buffer *leaf;
3057 struct btrfs_key orig_key;
3058 struct btrfs_item *item;
3059 struct btrfs_item *new_item;
3060 int ret = 0;
3061 int slot;
3062 u32 nritems;
3063 u32 orig_offset;
3064 struct btrfs_disk_key disk_key;
3065 char *buf;
3066
3067 leaf = path->nodes[0];
3068 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
3069 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
3070 goto split;
3071
3072 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3073 btrfs_release_path(root, path);
3074
3075 path->search_for_split = 1;
3076 path->keep_locks = 1;
3077
3078 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
3079 path->search_for_split = 0;
3080
3081 /* if our item isn't there or got smaller, return now */
3082 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3083 path->slots[0])) {
3084 path->keep_locks = 0;
3085 return -EAGAIN;
3086 }
3087
Chris Masonb9473432009-03-13 11:00:37 -04003088 btrfs_set_path_blocking(path);
Yan Zheng87b29b22008-12-17 10:21:48 -05003089 ret = split_leaf(trans, root, &orig_key, path,
3090 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003091 path->keep_locks = 0;
3092 BUG_ON(ret);
3093
Chris Masonb9473432009-03-13 11:00:37 -04003094 btrfs_unlock_up_safe(path, 1);
3095 leaf = path->nodes[0];
3096 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3097
3098split:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003099 /*
3100 * make sure any changes to the path from split_leaf leave it
3101 * in a blocking state
3102 */
3103 btrfs_set_path_blocking(path);
3104
Chris Mason459931e2008-12-10 09:10:46 -05003105 item = btrfs_item_nr(leaf, path->slots[0]);
3106 orig_offset = btrfs_item_offset(leaf, item);
3107 item_size = btrfs_item_size(leaf, item);
3108
Chris Mason459931e2008-12-10 09:10:46 -05003109 buf = kmalloc(item_size, GFP_NOFS);
3110 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3111 path->slots[0]), item_size);
3112 slot = path->slots[0] + 1;
3113 leaf = path->nodes[0];
3114
3115 nritems = btrfs_header_nritems(leaf);
3116
3117 if (slot != nritems) {
3118 /* shift the items */
3119 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3120 btrfs_item_nr_offset(slot),
3121 (nritems - slot) * sizeof(struct btrfs_item));
3122
3123 }
3124
3125 btrfs_cpu_key_to_disk(&disk_key, new_key);
3126 btrfs_set_item_key(leaf, &disk_key, slot);
3127
3128 new_item = btrfs_item_nr(leaf, slot);
3129
3130 btrfs_set_item_offset(leaf, new_item, orig_offset);
3131 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3132
3133 btrfs_set_item_offset(leaf, item,
3134 orig_offset + item_size - split_offset);
3135 btrfs_set_item_size(leaf, item, split_offset);
3136
3137 btrfs_set_header_nritems(leaf, nritems + 1);
3138
3139 /* write the data for the start of the original item */
3140 write_extent_buffer(leaf, buf,
3141 btrfs_item_ptr_offset(leaf, path->slots[0]),
3142 split_offset);
3143
3144 /* write the data for the new item */
3145 write_extent_buffer(leaf, buf + split_offset,
3146 btrfs_item_ptr_offset(leaf, slot),
3147 item_size - split_offset);
3148 btrfs_mark_buffer_dirty(leaf);
3149
3150 ret = 0;
3151 if (btrfs_leaf_free_space(root, leaf) < 0) {
3152 btrfs_print_leaf(root, leaf);
3153 BUG();
3154 }
3155 kfree(buf);
3156 return ret;
3157}
3158
3159/*
Chris Masond352ac62008-09-29 15:18:18 -04003160 * make the item pointed to by the path smaller. new_size indicates
3161 * how small to make it, and from_end tells us if we just chop bytes
3162 * off the end of the item or if we shift the item to chop bytes off
3163 * the front.
3164 */
Chris Masonb18c6682007-04-17 13:26:50 -04003165int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3166 struct btrfs_root *root,
3167 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003168 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003169{
3170 int ret = 0;
3171 int slot;
3172 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003173 struct extent_buffer *leaf;
3174 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003175 u32 nritems;
3176 unsigned int data_end;
3177 unsigned int old_data_start;
3178 unsigned int old_size;
3179 unsigned int size_diff;
3180 int i;
3181
3182 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003183 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003184 slot = path->slots[0];
3185
3186 old_size = btrfs_item_size_nr(leaf, slot);
3187 if (old_size == new_size)
3188 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003189
Chris Mason5f39d392007-10-15 16:14:19 -04003190 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003191 data_end = leaf_data_end(root, leaf);
3192
Chris Mason5f39d392007-10-15 16:14:19 -04003193 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003194
Chris Masonb18c6682007-04-17 13:26:50 -04003195 size_diff = old_size - new_size;
3196
3197 BUG_ON(slot < 0);
3198 BUG_ON(slot >= nritems);
3199
3200 /*
3201 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3202 */
3203 /* first correct the data pointers */
3204 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003205 u32 ioff;
3206 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003207
3208 if (!leaf->map_token) {
3209 map_extent_buffer(leaf, (unsigned long)item,
3210 sizeof(struct btrfs_item),
3211 &leaf->map_token, &leaf->kaddr,
3212 &leaf->map_start, &leaf->map_len,
3213 KM_USER1);
3214 }
3215
Chris Mason5f39d392007-10-15 16:14:19 -04003216 ioff = btrfs_item_offset(leaf, item);
3217 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003218 }
Chris Masondb945352007-10-15 16:15:53 -04003219
3220 if (leaf->map_token) {
3221 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3222 leaf->map_token = NULL;
3223 }
3224
Chris Masonb18c6682007-04-17 13:26:50 -04003225 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003226 if (from_end) {
3227 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3228 data_end + size_diff, btrfs_leaf_data(leaf) +
3229 data_end, old_data_start + new_size - data_end);
3230 } else {
3231 struct btrfs_disk_key disk_key;
3232 u64 offset;
3233
3234 btrfs_item_key(leaf, &disk_key, slot);
3235
3236 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3237 unsigned long ptr;
3238 struct btrfs_file_extent_item *fi;
3239
3240 fi = btrfs_item_ptr(leaf, slot,
3241 struct btrfs_file_extent_item);
3242 fi = (struct btrfs_file_extent_item *)(
3243 (unsigned long)fi - size_diff);
3244
3245 if (btrfs_file_extent_type(leaf, fi) ==
3246 BTRFS_FILE_EXTENT_INLINE) {
3247 ptr = btrfs_item_ptr_offset(leaf, slot);
3248 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003249 (unsigned long)fi,
3250 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003251 disk_bytenr));
3252 }
3253 }
3254
3255 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3256 data_end + size_diff, btrfs_leaf_data(leaf) +
3257 data_end, old_data_start - data_end);
3258
3259 offset = btrfs_disk_key_offset(&disk_key);
3260 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3261 btrfs_set_item_key(leaf, &disk_key, slot);
3262 if (slot == 0)
3263 fixup_low_keys(trans, root, path, &disk_key, 1);
3264 }
Chris Mason5f39d392007-10-15 16:14:19 -04003265
3266 item = btrfs_item_nr(leaf, slot);
3267 btrfs_set_item_size(leaf, item, new_size);
3268 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003269
3270 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003271 if (btrfs_leaf_free_space(root, leaf) < 0) {
3272 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003273 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003274 }
Chris Masonb18c6682007-04-17 13:26:50 -04003275 return ret;
3276}
3277
Chris Masond352ac62008-09-29 15:18:18 -04003278/*
3279 * make the item pointed to by the path bigger, data_size is the new size.
3280 */
Chris Mason5f39d392007-10-15 16:14:19 -04003281int btrfs_extend_item(struct btrfs_trans_handle *trans,
3282 struct btrfs_root *root, struct btrfs_path *path,
3283 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003284{
3285 int ret = 0;
3286 int slot;
3287 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003288 struct extent_buffer *leaf;
3289 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003290 u32 nritems;
3291 unsigned int data_end;
3292 unsigned int old_data;
3293 unsigned int old_size;
3294 int i;
3295
3296 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003297 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003298
Chris Mason5f39d392007-10-15 16:14:19 -04003299 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003300 data_end = leaf_data_end(root, leaf);
3301
Chris Mason5f39d392007-10-15 16:14:19 -04003302 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3303 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003304 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003305 }
Chris Mason6567e832007-04-16 09:22:45 -04003306 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003307 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003308
3309 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003310 if (slot >= nritems) {
3311 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003312 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3313 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003314 BUG_ON(1);
3315 }
Chris Mason6567e832007-04-16 09:22:45 -04003316
3317 /*
3318 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3319 */
3320 /* first correct the data pointers */
3321 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003322 u32 ioff;
3323 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003324
3325 if (!leaf->map_token) {
3326 map_extent_buffer(leaf, (unsigned long)item,
3327 sizeof(struct btrfs_item),
3328 &leaf->map_token, &leaf->kaddr,
3329 &leaf->map_start, &leaf->map_len,
3330 KM_USER1);
3331 }
Chris Mason5f39d392007-10-15 16:14:19 -04003332 ioff = btrfs_item_offset(leaf, item);
3333 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003334 }
Chris Mason5f39d392007-10-15 16:14:19 -04003335
Chris Masondb945352007-10-15 16:15:53 -04003336 if (leaf->map_token) {
3337 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3338 leaf->map_token = NULL;
3339 }
3340
Chris Mason6567e832007-04-16 09:22:45 -04003341 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003342 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003343 data_end - data_size, btrfs_leaf_data(leaf) +
3344 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003345
Chris Mason6567e832007-04-16 09:22:45 -04003346 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003347 old_size = btrfs_item_size_nr(leaf, slot);
3348 item = btrfs_item_nr(leaf, slot);
3349 btrfs_set_item_size(leaf, item, old_size + data_size);
3350 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003351
3352 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 if (btrfs_leaf_free_space(root, leaf) < 0) {
3354 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003355 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003356 }
Chris Mason6567e832007-04-16 09:22:45 -04003357 return ret;
3358}
3359
Chris Mason74123bd2007-02-02 11:05:29 -05003360/*
Chris Masond352ac62008-09-29 15:18:18 -04003361 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003362 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003363 * Returns the number of keys that were inserted.
3364 */
3365int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3366 struct btrfs_root *root,
3367 struct btrfs_path *path,
3368 struct btrfs_key *cpu_key, u32 *data_size,
3369 int nr)
3370{
3371 struct extent_buffer *leaf;
3372 struct btrfs_item *item;
3373 int ret = 0;
3374 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003375 int i;
3376 u32 nritems;
3377 u32 total_data = 0;
3378 u32 total_size = 0;
3379 unsigned int data_end;
3380 struct btrfs_disk_key disk_key;
3381 struct btrfs_key found_key;
3382
Yan Zheng87b29b22008-12-17 10:21:48 -05003383 for (i = 0; i < nr; i++) {
3384 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3385 BTRFS_LEAF_DATA_SIZE(root)) {
3386 break;
3387 nr = i;
3388 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003389 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003390 total_size += data_size[i] + sizeof(struct btrfs_item);
3391 }
3392 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003393
Josef Bacikf3465ca2008-11-12 14:19:50 -05003394 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3395 if (ret == 0)
3396 return -EEXIST;
3397 if (ret < 0)
3398 goto out;
3399
Josef Bacikf3465ca2008-11-12 14:19:50 -05003400 leaf = path->nodes[0];
3401
3402 nritems = btrfs_header_nritems(leaf);
3403 data_end = leaf_data_end(root, leaf);
3404
3405 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3406 for (i = nr; i >= 0; i--) {
3407 total_data -= data_size[i];
3408 total_size -= data_size[i] + sizeof(struct btrfs_item);
3409 if (total_size < btrfs_leaf_free_space(root, leaf))
3410 break;
3411 }
3412 nr = i;
3413 }
3414
3415 slot = path->slots[0];
3416 BUG_ON(slot < 0);
3417
3418 if (slot != nritems) {
3419 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3420
3421 item = btrfs_item_nr(leaf, slot);
3422 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3423
3424 /* figure out how many keys we can insert in here */
3425 total_data = data_size[0];
3426 for (i = 1; i < nr; i++) {
3427 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3428 break;
3429 total_data += data_size[i];
3430 }
3431 nr = i;
3432
3433 if (old_data < data_end) {
3434 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003435 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003436 slot, old_data, data_end);
3437 BUG_ON(1);
3438 }
3439 /*
3440 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3441 */
3442 /* first correct the data pointers */
3443 WARN_ON(leaf->map_token);
3444 for (i = slot; i < nritems; i++) {
3445 u32 ioff;
3446
3447 item = btrfs_item_nr(leaf, i);
3448 if (!leaf->map_token) {
3449 map_extent_buffer(leaf, (unsigned long)item,
3450 sizeof(struct btrfs_item),
3451 &leaf->map_token, &leaf->kaddr,
3452 &leaf->map_start, &leaf->map_len,
3453 KM_USER1);
3454 }
3455
3456 ioff = btrfs_item_offset(leaf, item);
3457 btrfs_set_item_offset(leaf, item, ioff - total_data);
3458 }
3459 if (leaf->map_token) {
3460 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3461 leaf->map_token = NULL;
3462 }
3463
3464 /* shift the items */
3465 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3466 btrfs_item_nr_offset(slot),
3467 (nritems - slot) * sizeof(struct btrfs_item));
3468
3469 /* shift the data */
3470 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3471 data_end - total_data, btrfs_leaf_data(leaf) +
3472 data_end, old_data - data_end);
3473 data_end = old_data;
3474 } else {
3475 /*
3476 * this sucks but it has to be done, if we are inserting at
3477 * the end of the leaf only insert 1 of the items, since we
3478 * have no way of knowing whats on the next leaf and we'd have
3479 * to drop our current locks to figure it out
3480 */
3481 nr = 1;
3482 }
3483
3484 /* setup the item for the new data */
3485 for (i = 0; i < nr; i++) {
3486 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3487 btrfs_set_item_key(leaf, &disk_key, slot + i);
3488 item = btrfs_item_nr(leaf, slot + i);
3489 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3490 data_end -= data_size[i];
3491 btrfs_set_item_size(leaf, item, data_size[i]);
3492 }
3493 btrfs_set_header_nritems(leaf, nritems + nr);
3494 btrfs_mark_buffer_dirty(leaf);
3495
3496 ret = 0;
3497 if (slot == 0) {
3498 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3499 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3500 }
3501
3502 if (btrfs_leaf_free_space(root, leaf) < 0) {
3503 btrfs_print_leaf(root, leaf);
3504 BUG();
3505 }
3506out:
3507 if (!ret)
3508 ret = nr;
3509 return ret;
3510}
3511
3512/*
Chris Mason44871b12009-03-13 10:04:31 -04003513 * this is a helper for btrfs_insert_empty_items, the main goal here is
3514 * to save stack depth by doing the bulk of the work in a function
3515 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003516 */
Chris Mason44871b12009-03-13 10:04:31 -04003517static noinline_for_stack int
3518setup_items_for_insert(struct btrfs_trans_handle *trans,
3519 struct btrfs_root *root, struct btrfs_path *path,
3520 struct btrfs_key *cpu_key, u32 *data_size,
3521 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003522{
Chris Mason5f39d392007-10-15 16:14:19 -04003523 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003524 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003525 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003526 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003527 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003528 int ret;
3529 struct extent_buffer *leaf;
3530 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003531
Chris Mason5f39d392007-10-15 16:14:19 -04003532 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003533 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003534
Chris Mason5f39d392007-10-15 16:14:19 -04003535 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003536 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003537
Chris Masonf25956c2008-09-12 15:32:53 -04003538 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003539 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003540 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003541 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003542 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003543 }
Chris Mason5f39d392007-10-15 16:14:19 -04003544
Chris Masonbe0e5c02007-01-26 15:51:26 -05003545 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003546 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003547
Chris Mason5f39d392007-10-15 16:14:19 -04003548 if (old_data < data_end) {
3549 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003550 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003551 slot, old_data, data_end);
3552 BUG_ON(1);
3553 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003554 /*
3555 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3556 */
3557 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003558 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003559 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003560 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003561
Chris Mason5f39d392007-10-15 16:14:19 -04003562 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003563 if (!leaf->map_token) {
3564 map_extent_buffer(leaf, (unsigned long)item,
3565 sizeof(struct btrfs_item),
3566 &leaf->map_token, &leaf->kaddr,
3567 &leaf->map_start, &leaf->map_len,
3568 KM_USER1);
3569 }
3570
Chris Mason5f39d392007-10-15 16:14:19 -04003571 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003572 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003573 }
Chris Masondb945352007-10-15 16:15:53 -04003574 if (leaf->map_token) {
3575 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3576 leaf->map_token = NULL;
3577 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003578
3579 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003580 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003581 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003582 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583
3584 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003585 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003586 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003587 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003588 data_end = old_data;
3589 }
Chris Mason5f39d392007-10-15 16:14:19 -04003590
Chris Mason62e27492007-03-15 12:56:47 -04003591 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003592 for (i = 0; i < nr; i++) {
3593 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3594 btrfs_set_item_key(leaf, &disk_key, slot + i);
3595 item = btrfs_item_nr(leaf, slot + i);
3596 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3597 data_end -= data_size[i];
3598 btrfs_set_item_size(leaf, item, data_size[i]);
3599 }
Chris Mason44871b12009-03-13 10:04:31 -04003600
Chris Mason9c583092008-01-29 15:15:18 -05003601 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003602
3603 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003604 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003605 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003606 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003607 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003608 }
Chris Masonb9473432009-03-13 11:00:37 -04003609 btrfs_unlock_up_safe(path, 1);
3610 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003611
Chris Mason5f39d392007-10-15 16:14:19 -04003612 if (btrfs_leaf_free_space(root, leaf) < 0) {
3613 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003614 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003615 }
Chris Mason44871b12009-03-13 10:04:31 -04003616 return ret;
3617}
3618
3619/*
3620 * Given a key and some data, insert items into the tree.
3621 * This does all the path init required, making room in the tree if needed.
3622 */
3623int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3624 struct btrfs_root *root,
3625 struct btrfs_path *path,
3626 struct btrfs_key *cpu_key, u32 *data_size,
3627 int nr)
3628{
3629 struct extent_buffer *leaf;
3630 int ret = 0;
3631 int slot;
3632 int i;
3633 u32 total_size = 0;
3634 u32 total_data = 0;
3635
3636 for (i = 0; i < nr; i++)
3637 total_data += data_size[i];
3638
3639 total_size = total_data + (nr * sizeof(struct btrfs_item));
3640 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3641 if (ret == 0)
3642 return -EEXIST;
3643 if (ret < 0)
3644 goto out;
3645
3646 leaf = path->nodes[0];
3647 slot = path->slots[0];
3648 BUG_ON(slot < 0);
3649
3650 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3651 total_data, total_size, nr);
3652
Chris Masoned2ff2c2007-03-01 18:59:40 -05003653out:
Chris Mason62e27492007-03-15 12:56:47 -04003654 return ret;
3655}
3656
3657/*
3658 * Given a key and some data, insert an item into the tree.
3659 * This does all the path init required, making room in the tree if needed.
3660 */
Chris Masone089f052007-03-16 16:20:31 -04003661int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3662 *root, struct btrfs_key *cpu_key, void *data, u32
3663 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003664{
3665 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003666 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003667 struct extent_buffer *leaf;
3668 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003669
Chris Mason2c90e5d2007-04-02 10:50:19 -04003670 path = btrfs_alloc_path();
3671 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003672 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003673 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003674 leaf = path->nodes[0];
3675 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3676 write_extent_buffer(leaf, data, ptr, data_size);
3677 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003678 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003679 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003680 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003681}
3682
Chris Mason74123bd2007-02-02 11:05:29 -05003683/*
Chris Mason5de08d72007-02-24 06:24:44 -05003684 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003685 *
Chris Masond352ac62008-09-29 15:18:18 -04003686 * the tree should have been previously balanced so the deletion does not
3687 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003688 */
Chris Masone089f052007-03-16 16:20:31 -04003689static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3690 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003691{
Chris Mason5f39d392007-10-15 16:14:19 -04003692 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003693 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003694 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003695 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003696
Chris Mason5f39d392007-10-15 16:14:19 -04003697 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003698 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003699 memmove_extent_buffer(parent,
3700 btrfs_node_key_ptr_offset(slot),
3701 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003702 sizeof(struct btrfs_key_ptr) *
3703 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003704 }
Chris Mason7518a232007-03-12 12:01:18 -04003705 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003706 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003707 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003708 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003709 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003710 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003711 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003712 struct btrfs_disk_key disk_key;
3713
3714 btrfs_node_key(parent, &disk_key, 0);
3715 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003716 if (wret)
3717 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003718 }
Chris Masond6025572007-03-30 14:27:56 -04003719 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003720 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003721}
3722
Chris Mason74123bd2007-02-02 11:05:29 -05003723/*
Chris Mason323ac952008-10-01 19:05:46 -04003724 * a helper function to delete the leaf pointed to by path->slots[1] and
3725 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3726 * already know it, it is faster to have them pass it down than to
3727 * read it out of the node again.
3728 *
3729 * This deletes the pointer in path->nodes[1] and frees the leaf
3730 * block extent. zero is returned if it all worked out, < 0 otherwise.
3731 *
3732 * The path must have already been setup for deleting the leaf, including
3733 * all the proper balancing. path->nodes[1] must be locked.
3734 */
3735noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3736 struct btrfs_root *root,
3737 struct btrfs_path *path, u64 bytenr)
3738{
3739 int ret;
3740 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003741 u64 parent_start = path->nodes[1]->start;
3742 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003743
3744 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3745 if (ret)
3746 return ret;
3747
Chris Mason4d081c42009-02-04 09:31:28 -05003748 /*
3749 * btrfs_free_extent is expensive, we want to make sure we
3750 * aren't holding any locks when we call it
3751 */
3752 btrfs_unlock_up_safe(path, 0);
3753
Chris Mason323ac952008-10-01 19:05:46 -04003754 ret = btrfs_free_extent(trans, root, bytenr,
3755 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003756 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003757 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003758 return ret;
3759}
3760/*
Chris Mason74123bd2007-02-02 11:05:29 -05003761 * delete the item at the leaf level in path. If that empties
3762 * the leaf, remove it from the tree
3763 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003764int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3765 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003766{
Chris Mason5f39d392007-10-15 16:14:19 -04003767 struct extent_buffer *leaf;
3768 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003769 int last_off;
3770 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003771 int ret = 0;
3772 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003773 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003774 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003775
Chris Mason5f39d392007-10-15 16:14:19 -04003776 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003777 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3778
3779 for (i = 0; i < nr; i++)
3780 dsize += btrfs_item_size_nr(leaf, slot + i);
3781
Chris Mason5f39d392007-10-15 16:14:19 -04003782 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003783
Chris Mason85e21ba2008-01-29 15:11:36 -05003784 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003785 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003786
3787 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003788 data_end + dsize,
3789 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003790 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003791
Chris Mason85e21ba2008-01-29 15:11:36 -05003792 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003793 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003794
Chris Mason5f39d392007-10-15 16:14:19 -04003795 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003796 if (!leaf->map_token) {
3797 map_extent_buffer(leaf, (unsigned long)item,
3798 sizeof(struct btrfs_item),
3799 &leaf->map_token, &leaf->kaddr,
3800 &leaf->map_start, &leaf->map_len,
3801 KM_USER1);
3802 }
Chris Mason5f39d392007-10-15 16:14:19 -04003803 ioff = btrfs_item_offset(leaf, item);
3804 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003805 }
Chris Masondb945352007-10-15 16:15:53 -04003806
3807 if (leaf->map_token) {
3808 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3809 leaf->map_token = NULL;
3810 }
3811
Chris Mason5f39d392007-10-15 16:14:19 -04003812 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003813 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003814 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003815 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003816 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003817 btrfs_set_header_nritems(leaf, nritems - nr);
3818 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003819
Chris Mason74123bd2007-02-02 11:05:29 -05003820 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003821 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003822 if (leaf == root->node) {
3823 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003824 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003825 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3826 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003827 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003828 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003829 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003830 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003831 struct btrfs_disk_key disk_key;
3832
3833 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003834 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003835 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003836 if (wret)
3837 ret = wret;
3838 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003839
Chris Mason74123bd2007-02-02 11:05:29 -05003840 /* delete the leaf if it is mostly empty */
Chris Masona4b6e072009-03-16 10:59:57 -04003841 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4 &&
3842 !trans->transaction->delayed_refs.flushing) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003843 /* push_leaf_left fixes the path.
3844 * make sure the path still points to our leaf
3845 * for possible call to del_ptr below
3846 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003847 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003848 extent_buffer_get(leaf);
3849
Chris Masonb9473432009-03-13 11:00:37 -04003850 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003851 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003852 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003853 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003854
3855 if (path->nodes[0] == leaf &&
3856 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003857 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003858 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003859 ret = wret;
3860 }
Chris Mason5f39d392007-10-15 16:14:19 -04003861
3862 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003863 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003864 ret = btrfs_del_leaf(trans, root, path,
3865 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003866 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003867 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003868 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003869 /* if we're still in the path, make sure
3870 * we're dirty. Otherwise, one of the
3871 * push_leaf functions must have already
3872 * dirtied this buffer
3873 */
3874 if (path->nodes[0] == leaf)
3875 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003876 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003877 }
Chris Masond5719762007-03-23 10:01:08 -04003878 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003879 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003880 }
3881 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003882 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883}
3884
Chris Mason97571fd2007-02-24 13:39:08 -05003885/*
Chris Mason925baed2008-06-25 16:01:30 -04003886 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003887 * returns 0 if it found something or 1 if there are no lesser leaves.
3888 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003889 *
3890 * This may release the path, and so you may lose any locks held at the
3891 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003892 */
3893int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3894{
Chris Mason925baed2008-06-25 16:01:30 -04003895 struct btrfs_key key;
3896 struct btrfs_disk_key found_key;
3897 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003898
Chris Mason925baed2008-06-25 16:01:30 -04003899 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003900
Chris Mason925baed2008-06-25 16:01:30 -04003901 if (key.offset > 0)
3902 key.offset--;
3903 else if (key.type > 0)
3904 key.type--;
3905 else if (key.objectid > 0)
3906 key.objectid--;
3907 else
3908 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003909
Chris Mason925baed2008-06-25 16:01:30 -04003910 btrfs_release_path(root, path);
3911 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3912 if (ret < 0)
3913 return ret;
3914 btrfs_item_key(path->nodes[0], &found_key, 0);
3915 ret = comp_keys(&found_key, &key);
3916 if (ret < 0)
3917 return 0;
3918 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003919}
3920
Chris Mason3f157a22008-06-25 16:01:31 -04003921/*
3922 * A helper function to walk down the tree starting at min_key, and looking
3923 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003924 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003925 *
3926 * This does not cow, but it does stuff the starting key it finds back
3927 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3928 * key and get a writable path.
3929 *
3930 * This does lock as it descends, and path->keep_locks should be set
3931 * to 1 by the caller.
3932 *
3933 * This honors path->lowest_level to prevent descent past a given level
3934 * of the tree.
3935 *
Chris Masond352ac62008-09-29 15:18:18 -04003936 * min_trans indicates the oldest transaction that you are interested
3937 * in walking through. Any nodes or leaves older than min_trans are
3938 * skipped over (without reading them).
3939 *
Chris Mason3f157a22008-06-25 16:01:31 -04003940 * returns zero if something useful was found, < 0 on error and 1 if there
3941 * was nothing in the tree that matched the search criteria.
3942 */
3943int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003944 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003945 struct btrfs_path *path, int cache_only,
3946 u64 min_trans)
3947{
3948 struct extent_buffer *cur;
3949 struct btrfs_key found_key;
3950 int slot;
Yan96524802008-07-24 12:19:49 -04003951 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003952 u32 nritems;
3953 int level;
3954 int ret = 1;
3955
Chris Mason934d3752008-12-08 16:43:10 -05003956 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003957again:
3958 cur = btrfs_lock_root_node(root);
3959 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003960 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003961 path->nodes[level] = cur;
3962 path->locks[level] = 1;
3963
3964 if (btrfs_header_generation(cur) < min_trans) {
3965 ret = 1;
3966 goto out;
3967 }
Chris Masond3977122009-01-05 21:25:51 -05003968 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003969 nritems = btrfs_header_nritems(cur);
3970 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003971 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003972
Chris Mason323ac952008-10-01 19:05:46 -04003973 /* at the lowest level, we're done, setup the path and exit */
3974 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003975 if (slot >= nritems)
3976 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003977 ret = 0;
3978 path->slots[level] = slot;
3979 btrfs_item_key_to_cpu(cur, &found_key, slot);
3980 goto out;
3981 }
Yan96524802008-07-24 12:19:49 -04003982 if (sret && slot > 0)
3983 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003984 /*
3985 * check this node pointer against the cache_only and
3986 * min_trans parameters. If it isn't in cache or is too
3987 * old, skip to the next one.
3988 */
Chris Masond3977122009-01-05 21:25:51 -05003989 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003990 u64 blockptr;
3991 u64 gen;
3992 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003993 struct btrfs_disk_key disk_key;
3994
Chris Mason3f157a22008-06-25 16:01:31 -04003995 blockptr = btrfs_node_blockptr(cur, slot);
3996 gen = btrfs_node_ptr_generation(cur, slot);
3997 if (gen < min_trans) {
3998 slot++;
3999 continue;
4000 }
4001 if (!cache_only)
4002 break;
4003
Chris Masone02119d2008-09-05 16:13:11 -04004004 if (max_key) {
4005 btrfs_node_key(cur, &disk_key, slot);
4006 if (comp_keys(&disk_key, max_key) >= 0) {
4007 ret = 1;
4008 goto out;
4009 }
4010 }
4011
Chris Mason3f157a22008-06-25 16:01:31 -04004012 tmp = btrfs_find_tree_block(root, blockptr,
4013 btrfs_level_size(root, level - 1));
4014
4015 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4016 free_extent_buffer(tmp);
4017 break;
4018 }
4019 if (tmp)
4020 free_extent_buffer(tmp);
4021 slot++;
4022 }
Chris Masone02119d2008-09-05 16:13:11 -04004023find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004024 /*
4025 * we didn't find a candidate key in this node, walk forward
4026 * and find another one
4027 */
4028 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004029 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004030 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004031 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004032 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004033 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004034 btrfs_release_path(root, path);
4035 goto again;
4036 } else {
4037 goto out;
4038 }
4039 }
4040 /* save our key for returning back */
4041 btrfs_node_key_to_cpu(cur, &found_key, slot);
4042 path->slots[level] = slot;
4043 if (level == path->lowest_level) {
4044 ret = 0;
4045 unlock_up(path, level, 1);
4046 goto out;
4047 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004048 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004049 cur = read_node_slot(root, cur, slot);
4050
4051 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004052
Chris Mason3f157a22008-06-25 16:01:31 -04004053 path->locks[level - 1] = 1;
4054 path->nodes[level - 1] = cur;
4055 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004056 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004057 }
4058out:
4059 if (ret == 0)
4060 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004061 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004062 return ret;
4063}
4064
4065/*
4066 * this is similar to btrfs_next_leaf, but does not try to preserve
4067 * and fixup the path. It looks for and returns the next key in the
4068 * tree based on the current path and the cache_only and min_trans
4069 * parameters.
4070 *
4071 * 0 is returned if another key is found, < 0 if there are any errors
4072 * and 1 is returned if there are no higher keys in the tree
4073 *
4074 * path->keep_locks should be set to 1 on the search made before
4075 * calling this function.
4076 */
Chris Masone7a84562008-06-25 16:01:31 -04004077int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004078 struct btrfs_key *key, int lowest_level,
4079 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004080{
4081 int level = lowest_level;
4082 int slot;
4083 struct extent_buffer *c;
4084
Chris Mason934d3752008-12-08 16:43:10 -05004085 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004086 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004087 if (!path->nodes[level])
4088 return 1;
4089
4090 slot = path->slots[level] + 1;
4091 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004092next:
Chris Masone7a84562008-06-25 16:01:31 -04004093 if (slot >= btrfs_header_nritems(c)) {
4094 level++;
Chris Masond3977122009-01-05 21:25:51 -05004095 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04004096 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04004097 continue;
4098 }
4099 if (level == 0)
4100 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004101 else {
4102 u64 blockptr = btrfs_node_blockptr(c, slot);
4103 u64 gen = btrfs_node_ptr_generation(c, slot);
4104
4105 if (cache_only) {
4106 struct extent_buffer *cur;
4107 cur = btrfs_find_tree_block(root, blockptr,
4108 btrfs_level_size(root, level - 1));
4109 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4110 slot++;
4111 if (cur)
4112 free_extent_buffer(cur);
4113 goto next;
4114 }
4115 free_extent_buffer(cur);
4116 }
4117 if (gen < min_trans) {
4118 slot++;
4119 goto next;
4120 }
Chris Masone7a84562008-06-25 16:01:31 -04004121 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004122 }
Chris Masone7a84562008-06-25 16:01:31 -04004123 return 0;
4124 }
4125 return 1;
4126}
4127
Chris Mason7bb86312007-12-11 09:25:06 -05004128/*
Chris Mason925baed2008-06-25 16:01:30 -04004129 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004130 * returns 0 if it found something or 1 if there are no greater leaves.
4131 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004132 */
Chris Mason234b63a2007-03-13 10:46:10 -04004133int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004134{
4135 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004136 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004137 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004138 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004139 struct btrfs_key key;
4140 u32 nritems;
4141 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004142 int old_spinning = path->leave_spinning;
4143 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004144
4145 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004146 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004147 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004148
Chris Mason8e73f272009-04-03 10:14:18 -04004149 /*
4150 * we take the blocks in an order that upsets lockdep. Using
4151 * blocking mode is the only way around it.
4152 */
4153#ifdef CONFIG_DEBUG_LOCK_ALLOC
4154 force_blocking = 1;
4155#endif
Chris Mason925baed2008-06-25 16:01:30 -04004156
Chris Mason8e73f272009-04-03 10:14:18 -04004157 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4158again:
4159 level = 1;
4160 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004161 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004162
Chris Masona2135012008-06-25 16:01:30 -04004163 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004164
4165 if (!force_blocking)
4166 path->leave_spinning = 1;
4167
Chris Mason925baed2008-06-25 16:01:30 -04004168 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4169 path->keep_locks = 0;
4170
4171 if (ret < 0)
4172 return ret;
4173
Chris Masona2135012008-06-25 16:01:30 -04004174 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004175 /*
4176 * by releasing the path above we dropped all our locks. A balance
4177 * could have added more items next to the key that used to be
4178 * at the very end of the block. So, check again here and
4179 * advance the path if there are now more items available.
4180 */
Chris Masona2135012008-06-25 16:01:30 -04004181 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004182 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004183 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004184 goto done;
4185 }
Chris Masond97e63b2007-02-20 16:40:44 -05004186
Chris Masond3977122009-01-05 21:25:51 -05004187 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004188 if (!path->nodes[level]) {
4189 ret = 1;
4190 goto done;
4191 }
Chris Mason5f39d392007-10-15 16:14:19 -04004192
Chris Masond97e63b2007-02-20 16:40:44 -05004193 slot = path->slots[level] + 1;
4194 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004195 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004196 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004197 if (level == BTRFS_MAX_LEVEL) {
4198 ret = 1;
4199 goto done;
4200 }
Chris Masond97e63b2007-02-20 16:40:44 -05004201 continue;
4202 }
Chris Mason5f39d392007-10-15 16:14:19 -04004203
Chris Mason925baed2008-06-25 16:01:30 -04004204 if (next) {
4205 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004206 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004207 }
Chris Mason5f39d392007-10-15 16:14:19 -04004208
Chris Mason8e73f272009-04-03 10:14:18 -04004209 next = c;
4210 ret = read_block_for_search(NULL, root, path, &next, level,
4211 slot, &key);
4212 if (ret == -EAGAIN)
4213 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004214
Chris Mason5cd57b22008-06-25 16:01:30 -04004215 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004216 ret = btrfs_try_spin_lock(next);
4217 if (!ret) {
4218 btrfs_set_path_blocking(path);
4219 btrfs_tree_lock(next);
4220 if (!force_blocking)
4221 btrfs_clear_path_blocking(path, next);
4222 }
4223 if (force_blocking)
4224 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004225 }
Chris Masond97e63b2007-02-20 16:40:44 -05004226 break;
4227 }
4228 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004229 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004230 level--;
4231 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004232 if (path->locks[level])
4233 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004234
Chris Mason5f39d392007-10-15 16:14:19 -04004235 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004236 path->nodes[level] = next;
4237 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004238 if (!path->skip_locking)
4239 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004240
Chris Masond97e63b2007-02-20 16:40:44 -05004241 if (!level)
4242 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004243
Chris Mason8e73f272009-04-03 10:14:18 -04004244 ret = read_block_for_search(NULL, root, path, &next, level,
4245 0, &key);
4246 if (ret == -EAGAIN)
4247 goto again;
4248
Chris Mason5cd57b22008-06-25 16:01:30 -04004249 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004250 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004251 ret = btrfs_try_spin_lock(next);
4252 if (!ret) {
4253 btrfs_set_path_blocking(path);
4254 btrfs_tree_lock(next);
4255 if (!force_blocking)
4256 btrfs_clear_path_blocking(path, next);
4257 }
4258 if (force_blocking)
4259 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004260 }
Chris Masond97e63b2007-02-20 16:40:44 -05004261 }
Chris Mason8e73f272009-04-03 10:14:18 -04004262 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004263done:
4264 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004265 path->leave_spinning = old_spinning;
4266 if (!old_spinning)
4267 btrfs_set_path_blocking(path);
4268
4269 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004270}
Chris Mason0b86a832008-03-24 15:01:56 -04004271
Chris Mason3f157a22008-06-25 16:01:31 -04004272/*
4273 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4274 * searching until it gets past min_objectid or finds an item of 'type'
4275 *
4276 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4277 */
Chris Mason0b86a832008-03-24 15:01:56 -04004278int btrfs_previous_item(struct btrfs_root *root,
4279 struct btrfs_path *path, u64 min_objectid,
4280 int type)
4281{
4282 struct btrfs_key found_key;
4283 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004284 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004285 int ret;
4286
Chris Masond3977122009-01-05 21:25:51 -05004287 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004288 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004289 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004290 ret = btrfs_prev_leaf(root, path);
4291 if (ret != 0)
4292 return ret;
4293 } else {
4294 path->slots[0]--;
4295 }
4296 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004297 nritems = btrfs_header_nritems(leaf);
4298 if (nritems == 0)
4299 return 1;
4300 if (path->slots[0] == nritems)
4301 path->slots[0]--;
4302
Chris Mason0b86a832008-03-24 15:01:56 -04004303 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4304 if (found_key.type == type)
4305 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004306 if (found_key.objectid < min_objectid)
4307 break;
4308 if (found_key.objectid == min_objectid &&
4309 found_key.type < type)
4310 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004311 }
4312 return 1;
4313}