blob: 87c90387283b21a8b8202cceac69838b8c5280a5 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Mason2c90e5d2007-04-02 10:50:19 -040041struct btrfs_path *btrfs_alloc_path(void)
42{
Chris Masondf24a2b2007-04-04 09:36:31 -040043 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050044 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
45 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040046 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040047 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040048}
49
Chris Masonb4ce94d2009-02-04 09:25:08 -050050/*
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
53 */
54noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55{
56 int i;
57 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
58 if (p->nodes[i] && p->locks[i])
59 btrfs_set_lock_blocking(p->nodes[i]);
60 }
61}
62
63/*
64 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050065 *
66 * held is used to keep lockdep happy, when lockdep is enabled
67 * we set held to a blocking lock before we go around and
68 * retake all the spinlocks in the path. You can safely use NULL
69 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050070 */
Chris Mason4008c042009-02-12 14:09:45 -050071noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
72 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050073{
74 int i;
Chris Mason4008c042009-02-12 14:09:45 -050075
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 /* lockdep really cares that we take all of these spinlocks
78 * in the right order. If any of the locks in the path are not
79 * currently blocking, it is going to complain. So, make really
80 * really sure by forcing the path to blocking before we clear
81 * the path blocking.
82 */
83 if (held)
84 btrfs_set_lock_blocking(held);
85 btrfs_set_path_blocking(p);
86#endif
87
88 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050089 if (p->nodes[i] && p->locks[i])
90 btrfs_clear_lock_blocking(p->nodes[i]);
91 }
Chris Mason4008c042009-02-12 14:09:45 -050092
93#ifdef CONFIG_DEBUG_LOCK_ALLOC
94 if (held)
95 btrfs_clear_lock_blocking(held);
96#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050097}
98
Chris Masond352ac62008-09-29 15:18:18 -040099/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400100void btrfs_free_path(struct btrfs_path *p)
101{
Chris Masondf24a2b2007-04-04 09:36:31 -0400102 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400103 kmem_cache_free(btrfs_path_cachep, p);
104}
105
Chris Masond352ac62008-09-29 15:18:18 -0400106/*
107 * path release drops references on the extent buffers in the path
108 * and it drops any locks held by this path
109 *
110 * It is safe to call this on paths that no locks or extent buffers held.
111 */
Chris Masond3977122009-01-05 21:25:51 -0500112noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500113{
114 int i;
Chris Masona2135012008-06-25 16:01:30 -0400115
Chris Mason234b63a2007-03-13 10:46:10 -0400116 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400117 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500118 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400119 continue;
120 if (p->locks[i]) {
121 btrfs_tree_unlock(p->nodes[i]);
122 p->locks[i] = 0;
123 }
Chris Mason5f39d392007-10-15 16:14:19 -0400124 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 }
127}
128
Chris Masond352ac62008-09-29 15:18:18 -0400129/*
130 * safely gets a reference on the root node of a tree. A lock
131 * is not taken, so a concurrent writer may put a different node
132 * at the root of the tree. See btrfs_lock_root_node for the
133 * looping required.
134 *
135 * The extent buffer returned by this has a reference taken, so
136 * it won't disappear. It may stop being the root of the tree
137 * at any time because there are no locks held.
138 */
Chris Mason925baed2008-06-25 16:01:30 -0400139struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
140{
141 struct extent_buffer *eb;
142 spin_lock(&root->node_lock);
143 eb = root->node;
144 extent_buffer_get(eb);
145 spin_unlock(&root->node_lock);
146 return eb;
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/* loop around taking references on and locking the root node of the
150 * tree until you end up with a lock on the root. A locked buffer
151 * is returned, with a reference held.
152 */
Chris Mason925baed2008-06-25 16:01:30 -0400153struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
154{
155 struct extent_buffer *eb;
156
Chris Masond3977122009-01-05 21:25:51 -0500157 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400158 eb = btrfs_root_node(root);
159 btrfs_tree_lock(eb);
160
161 spin_lock(&root->node_lock);
162 if (eb == root->node) {
163 spin_unlock(&root->node_lock);
164 break;
165 }
166 spin_unlock(&root->node_lock);
167
168 btrfs_tree_unlock(eb);
169 free_extent_buffer(eb);
170 }
171 return eb;
172}
173
Chris Masond352ac62008-09-29 15:18:18 -0400174/* cowonly root (everything not a reference counted cow subvolume), just get
175 * put onto a simple dirty list. transaction.c walks this to make sure they
176 * get properly updated on disk.
177 */
Chris Mason0b86a832008-03-24 15:01:56 -0400178static void add_root_to_dirty_list(struct btrfs_root *root)
179{
180 if (root->track_dirty && list_empty(&root->dirty_list)) {
181 list_add(&root->dirty_list,
182 &root->fs_info->dirty_cowonly_roots);
183 }
184}
185
Chris Masond352ac62008-09-29 15:18:18 -0400186/*
187 * used by snapshot creation to make a copy of a root for a tree with
188 * a given objectid. The buffer with the new root node is returned in
189 * cow_ret, and this func returns zero on success or a negative error code.
190 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500191int btrfs_copy_root(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root,
193 struct extent_buffer *buf,
194 struct extent_buffer **cow_ret, u64 new_root_objectid)
195{
196 struct extent_buffer *cow;
197 u32 nritems;
198 int ret = 0;
199 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500200 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201
Chris Mason4aec2b52007-12-18 16:25:45 -0500202 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
203 if (!new_root)
204 return -ENOMEM;
205
206 memcpy(new_root, root, sizeof(*new_root));
207 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208
209 WARN_ON(root->ref_cows && trans->transid !=
210 root->fs_info->running_transaction->transid);
211 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
212
213 level = btrfs_header_level(buf);
214 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400215
216 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
217 new_root_objectid, trans->transid,
218 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500219 if (IS_ERR(cow)) {
220 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500222 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
224 copy_extent_buffer(cow, buf, 0, 0, cow->len);
225 btrfs_set_header_bytenr(cow, cow->start);
226 btrfs_set_header_generation(cow, trans->transid);
227 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400228 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229
Yan Zheng2b820322008-11-17 21:11:30 -0500230 write_extent_buffer(cow, root->fs_info->fsid,
231 (unsigned long)btrfs_header_fsid(cow),
232 BTRFS_FSID_SIZE);
233
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400235 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500236 kfree(new_root);
237
Chris Masonbe20aa92007-12-17 20:14:01 -0500238 if (ret)
239 return ret;
240
241 btrfs_mark_buffer_dirty(cow);
242 *cow_ret = cow;
243 return 0;
244}
245
Chris Masond352ac62008-09-29 15:18:18 -0400246/*
Chris Masond3977122009-01-05 21:25:51 -0500247 * does the dirty work in cow of a single block. The parent block (if
248 * supplied) is updated to point to the new cow copy. The new buffer is marked
249 * dirty and returned locked. If you modify the block it needs to be marked
250 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400251 *
252 * search_start -- an allocation hint for the new block
253 *
Chris Masond3977122009-01-05 21:25:51 -0500254 * empty_size -- a hint that you plan on doing more cow. This is the size in
255 * bytes the allocator should try to find free next to the block it returns.
256 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400257 */
Chris Masond3977122009-01-05 21:25:51 -0500258static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400259 struct btrfs_root *root,
260 struct extent_buffer *buf,
261 struct extent_buffer *parent, int parent_slot,
262 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400263 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400264{
Zheng Yan31840ae2008-09-23 13:14:14 -0400265 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400266 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500267 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400268 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500269 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400270 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400271
Chris Mason925baed2008-06-25 16:01:30 -0400272 if (*cow_ret == buf)
273 unlock_orig = 1;
274
Chris Masonb9447ef82009-03-09 11:45:38 -0400275 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400276
Zheng Yan31840ae2008-09-23 13:14:14 -0400277 if (parent)
278 parent_start = parent->start;
279 else
280 parent_start = 0;
281
Chris Mason7bb86312007-12-11 09:25:06 -0500282 WARN_ON(root->ref_cows && trans->transid !=
283 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400284 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400285
Chris Mason7bb86312007-12-11 09:25:06 -0500286 level = btrfs_header_level(buf);
287 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400288
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400289 cow = btrfs_alloc_free_block(trans, root, buf->len,
290 parent_start, root->root_key.objectid,
291 trans->transid, level,
292 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400293 if (IS_ERR(cow))
294 return PTR_ERR(cow);
295
Chris Masonb4ce94d2009-02-04 09:25:08 -0500296 /* cow is set to blocking by btrfs_init_new_buffer */
297
Chris Mason5f39d392007-10-15 16:14:19 -0400298 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400299 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400300 btrfs_set_header_generation(cow, trans->transid);
301 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400302 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400303
Yan Zheng2b820322008-11-17 21:11:30 -0500304 write_extent_buffer(cow, root->fs_info->fsid,
305 (unsigned long)btrfs_header_fsid(cow),
306 BTRFS_FSID_SIZE);
307
Chris Mason5f39d392007-10-15 16:14:19 -0400308 WARN_ON(btrfs_header_generation(buf) > trans->transid);
309 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400310 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400311 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400312 if (ret)
313 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400314
315 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
316 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400317 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
318 /*
319 * There are only two places that can drop reference to
320 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400321 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400322 * we check reference count while tree block is locked.
323 * Furthermore, if reference count is one, it won't get
324 * increased by someone else.
325 */
326 u32 refs;
327 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
328 buf->len, &refs);
329 BUG_ON(ret);
330 if (refs == 1) {
331 ret = btrfs_update_ref(trans, root, buf, cow,
332 0, nritems);
333 clean_tree_block(trans, root, buf);
334 } else {
335 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
336 }
337 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400338 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400339 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
340 if (ret)
341 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400342 clean_tree_block(trans, root, buf);
343 }
344
Zheng Yan1a40e232008-09-26 10:09:34 -0400345 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400346 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
347 WARN_ON(ret);
348 }
349
Chris Mason6702ed42007-08-07 16:15:09 -0400350 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400351 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400352
353 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400354 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400355 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400356 spin_unlock(&root->node_lock);
357
Chris Mason6702ed42007-08-07 16:15:09 -0400358 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400359 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400360 buf->len, buf->start,
361 root->root_key.objectid,
362 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400363 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400364 }
Chris Mason5f39d392007-10-15 16:14:19 -0400365 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400366 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400367 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400368 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400369 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500370 WARN_ON(trans->transid == 0);
371 btrfs_set_node_ptr_generation(parent, parent_slot,
372 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400373 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400374 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500375 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400376 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400377 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400378 }
Chris Mason925baed2008-06-25 16:01:30 -0400379 if (unlock_orig)
380 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400381 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400382 btrfs_mark_buffer_dirty(cow);
383 *cow_ret = cow;
384 return 0;
385}
386
Chris Masond352ac62008-09-29 15:18:18 -0400387/*
388 * cows a single block, see __btrfs_cow_block for the real work.
389 * This version of it has extra checks so that a block isn't cow'd more than
390 * once per transaction, as long as it hasn't been written yet
391 */
Chris Masond3977122009-01-05 21:25:51 -0500392noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400393 struct btrfs_root *root, struct extent_buffer *buf,
394 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400395 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500396{
Chris Mason6702ed42007-08-07 16:15:09 -0400397 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400398 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500399
Chris Masonccd467d2007-06-28 15:57:36 -0400400 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500401 printk(KERN_CRIT "trans %llu running %llu\n",
402 (unsigned long long)trans->transid,
403 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400404 root->fs_info->running_transaction->transid);
405 WARN_ON(1);
406 }
407 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500408 printk(KERN_CRIT "trans %llu running %llu\n",
409 (unsigned long long)trans->transid,
410 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400411 WARN_ON(1);
412 }
Chris Masondc17ff82008-01-08 15:46:30 -0500413
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400414 if (btrfs_header_generation(buf) == trans->transid &&
415 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400416 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500417 *cow_ret = buf;
418 return 0;
419 }
Chris Masonc4876852009-02-04 09:24:25 -0500420
Chris Mason0b86a832008-03-24 15:01:56 -0400421 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500422
423 if (parent)
424 btrfs_set_lock_blocking(parent);
425 btrfs_set_lock_blocking(buf);
426
Chris Masonf510cfe2007-10-15 16:14:48 -0400427 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400428 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400429 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400430}
431
Chris Masond352ac62008-09-29 15:18:18 -0400432/*
433 * helper function for defrag to decide if two blocks pointed to by a
434 * node are actually close by
435 */
Chris Mason6b800532007-10-15 16:17:34 -0400436static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400437{
Chris Mason6b800532007-10-15 16:17:34 -0400438 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400439 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400440 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400441 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500442 return 0;
443}
444
Chris Mason081e9572007-11-06 10:26:24 -0500445/*
446 * compare two keys in a memcmp fashion
447 */
448static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
449{
450 struct btrfs_key k1;
451
452 btrfs_disk_key_to_cpu(&k1, disk);
453
454 if (k1.objectid > k2->objectid)
455 return 1;
456 if (k1.objectid < k2->objectid)
457 return -1;
458 if (k1.type > k2->type)
459 return 1;
460 if (k1.type < k2->type)
461 return -1;
462 if (k1.offset > k2->offset)
463 return 1;
464 if (k1.offset < k2->offset)
465 return -1;
466 return 0;
467}
468
Josef Bacikf3465ca2008-11-12 14:19:50 -0500469/*
470 * same as comp_keys only with two btrfs_key's
471 */
472static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
473{
474 if (k1->objectid > k2->objectid)
475 return 1;
476 if (k1->objectid < k2->objectid)
477 return -1;
478 if (k1->type > k2->type)
479 return 1;
480 if (k1->type < k2->type)
481 return -1;
482 if (k1->offset > k2->offset)
483 return 1;
484 if (k1->offset < k2->offset)
485 return -1;
486 return 0;
487}
Chris Mason081e9572007-11-06 10:26:24 -0500488
Chris Masond352ac62008-09-29 15:18:18 -0400489/*
490 * this is used by the defrag code to go through all the
491 * leaves pointed to by a node and reallocate them so that
492 * disk order is close to key order
493 */
Chris Mason6702ed42007-08-07 16:15:09 -0400494int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400495 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400496 int start_slot, int cache_only, u64 *last_ret,
497 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400498{
Chris Mason6b800532007-10-15 16:17:34 -0400499 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400500 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400501 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400502 u64 search_start = *last_ret;
503 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400504 u64 other;
505 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400506 int end_slot;
507 int i;
508 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400509 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400510 int uptodate;
511 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500512 int progress_passed = 0;
513 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400514
Chris Mason5708b952007-10-25 15:43:18 -0400515 parent_level = btrfs_header_level(parent);
516 if (cache_only && parent_level != 1)
517 return 0;
518
Chris Masond3977122009-01-05 21:25:51 -0500519 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400520 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500521 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400522 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400523
Chris Mason6b800532007-10-15 16:17:34 -0400524 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400525 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400526 end_slot = parent_nritems;
527
528 if (parent_nritems == 1)
529 return 0;
530
Chris Masonb4ce94d2009-02-04 09:25:08 -0500531 btrfs_set_lock_blocking(parent);
532
Chris Mason6702ed42007-08-07 16:15:09 -0400533 for (i = start_slot; i < end_slot; i++) {
534 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400535
Chris Mason5708b952007-10-25 15:43:18 -0400536 if (!parent->map_token) {
537 map_extent_buffer(parent,
538 btrfs_node_key_ptr_offset(i),
539 sizeof(struct btrfs_key_ptr),
540 &parent->map_token, &parent->kaddr,
541 &parent->map_start, &parent->map_len,
542 KM_USER1);
543 }
Chris Mason081e9572007-11-06 10:26:24 -0500544 btrfs_node_key(parent, &disk_key, i);
545 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
546 continue;
547
548 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400549 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400550 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400551 if (last_block == 0)
552 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400553
Chris Mason6702ed42007-08-07 16:15:09 -0400554 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400555 other = btrfs_node_blockptr(parent, i - 1);
556 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400557 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400558 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400559 other = btrfs_node_blockptr(parent, i + 1);
560 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400561 }
Chris Masone9d0b132007-08-10 14:06:19 -0400562 if (close) {
563 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400564 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400565 }
Chris Mason5708b952007-10-25 15:43:18 -0400566 if (parent->map_token) {
567 unmap_extent_buffer(parent, parent->map_token,
568 KM_USER1);
569 parent->map_token = NULL;
570 }
Chris Mason6702ed42007-08-07 16:15:09 -0400571
Chris Mason6b800532007-10-15 16:17:34 -0400572 cur = btrfs_find_tree_block(root, blocknr, blocksize);
573 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400574 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400575 else
576 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400577 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400578 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400579 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400580 continue;
581 }
Chris Mason6b800532007-10-15 16:17:34 -0400582 if (!cur) {
583 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400584 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400585 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400586 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400587 }
Chris Mason6702ed42007-08-07 16:15:09 -0400588 }
Chris Masone9d0b132007-08-10 14:06:19 -0400589 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400590 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400591
Chris Masone7a84562008-06-25 16:01:31 -0400592 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500593 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400594 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400595 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400596 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400597 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400598 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400599 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400600 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400601 break;
Yan252c38f2007-08-29 09:11:44 -0400602 }
Chris Masone7a84562008-06-25 16:01:31 -0400603 search_start = cur->start;
604 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400605 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400606 btrfs_tree_unlock(cur);
607 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400608 }
Chris Mason5708b952007-10-25 15:43:18 -0400609 if (parent->map_token) {
610 unmap_extent_buffer(parent, parent->map_token,
611 KM_USER1);
612 parent->map_token = NULL;
613 }
Chris Mason6702ed42007-08-07 16:15:09 -0400614 return err;
615}
616
Chris Mason74123bd2007-02-02 11:05:29 -0500617/*
618 * The leaf data grows from end-to-front in the node.
619 * this returns the address of the start of the last item,
620 * which is the stop of the leaf data stack
621 */
Chris Mason123abc82007-03-14 14:14:43 -0400622static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400623 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500624{
Chris Mason5f39d392007-10-15 16:14:19 -0400625 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500626 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400627 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400628 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500629}
630
Chris Masond352ac62008-09-29 15:18:18 -0400631/*
632 * extra debugging checks to make sure all the items in a key are
633 * well formed and in the proper order
634 */
Chris Mason123abc82007-03-14 14:14:43 -0400635static int check_node(struct btrfs_root *root, struct btrfs_path *path,
636 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500637{
Chris Mason5f39d392007-10-15 16:14:19 -0400638 struct extent_buffer *parent = NULL;
639 struct extent_buffer *node = path->nodes[level];
640 struct btrfs_disk_key parent_key;
641 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500642 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400643 int slot;
644 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400645 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500646
647 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400648 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400649
Chris Mason8d7be552007-05-10 11:24:42 -0400650 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400651 BUG_ON(nritems == 0);
652 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400653 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400654 btrfs_node_key(parent, &parent_key, parent_slot);
655 btrfs_node_key(node, &node_key, 0);
656 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400657 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400658 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400659 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500660 }
Chris Mason123abc82007-03-14 14:14:43 -0400661 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400662 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400663 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
664 btrfs_node_key(node, &node_key, slot);
665 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400666 }
667 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400668 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
669 btrfs_node_key(node, &node_key, slot);
670 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500671 }
672 return 0;
673}
674
Chris Masond352ac62008-09-29 15:18:18 -0400675/*
676 * extra checking to make sure all the items in a leaf are
677 * well formed and in the proper order
678 */
Chris Mason123abc82007-03-14 14:14:43 -0400679static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
680 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500681{
Chris Mason5f39d392007-10-15 16:14:19 -0400682 struct extent_buffer *leaf = path->nodes[level];
683 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500684 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400685 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400686 struct btrfs_disk_key parent_key;
687 struct btrfs_disk_key leaf_key;
688 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400689
Chris Mason5f39d392007-10-15 16:14:19 -0400690 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500691
692 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400693 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400694
695 if (nritems == 0)
696 return 0;
697
698 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400699 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400700 btrfs_node_key(parent, &parent_key, parent_slot);
701 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400702
Chris Mason5f39d392007-10-15 16:14:19 -0400703 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400704 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400705 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400706 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500707 }
Chris Mason5f39d392007-10-15 16:14:19 -0400708 if (slot != 0 && slot < nritems - 1) {
709 btrfs_item_key(leaf, &leaf_key, slot);
710 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
711 if (comp_keys(&leaf_key, &cpukey) <= 0) {
712 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500713 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400714 BUG_ON(1);
715 }
716 if (btrfs_item_offset_nr(leaf, slot - 1) !=
717 btrfs_item_end_nr(leaf, slot)) {
718 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500719 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400720 BUG_ON(1);
721 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500722 }
Chris Mason8d7be552007-05-10 11:24:42 -0400723 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400724 btrfs_item_key(leaf, &leaf_key, slot);
725 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
726 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
727 if (btrfs_item_offset_nr(leaf, slot) !=
728 btrfs_item_end_nr(leaf, slot + 1)) {
729 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500730 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400731 BUG_ON(1);
732 }
Chris Mason8d7be552007-05-10 11:24:42 -0400733 }
Chris Mason5f39d392007-10-15 16:14:19 -0400734 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
735 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500736 return 0;
737}
738
Chris Masond3977122009-01-05 21:25:51 -0500739static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500740 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741{
Chris Mason85d824c2008-04-10 10:23:19 -0400742 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500743 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400744 return check_leaf(root, path, level);
745 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500746}
747
Chris Mason74123bd2007-02-02 11:05:29 -0500748/*
Chris Mason5f39d392007-10-15 16:14:19 -0400749 * search for key in the extent_buffer. The items start at offset p,
750 * and they are item_size apart. There are 'max' items in p.
751 *
Chris Mason74123bd2007-02-02 11:05:29 -0500752 * the slot in the array is returned via slot, and it points to
753 * the place where you would insert key if it is not found in
754 * the array.
755 *
756 * slot may point to max if the key is bigger than all of the keys
757 */
Chris Masone02119d2008-09-05 16:13:11 -0400758static noinline int generic_bin_search(struct extent_buffer *eb,
759 unsigned long p,
760 int item_size, struct btrfs_key *key,
761 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500762{
763 int low = 0;
764 int high = max;
765 int mid;
766 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400767 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400768 struct btrfs_disk_key unaligned;
769 unsigned long offset;
770 char *map_token = NULL;
771 char *kaddr = NULL;
772 unsigned long map_start = 0;
773 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400774 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775
Chris Masond3977122009-01-05 21:25:51 -0500776 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400778 offset = p + mid * item_size;
779
780 if (!map_token || offset < map_start ||
781 (offset + sizeof(struct btrfs_disk_key)) >
782 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400783 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400784 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400785 map_token = NULL;
786 }
Chris Mason934d3752008-12-08 16:43:10 -0500787
788 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400789 sizeof(struct btrfs_disk_key),
790 &map_token, &kaddr,
791 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400792
Chris Mason479965d2007-10-15 16:14:27 -0400793 if (!err) {
794 tmp = (struct btrfs_disk_key *)(kaddr + offset -
795 map_start);
796 } else {
797 read_extent_buffer(eb, &unaligned,
798 offset, sizeof(unaligned));
799 tmp = &unaligned;
800 }
801
Chris Mason5f39d392007-10-15 16:14:19 -0400802 } else {
803 tmp = (struct btrfs_disk_key *)(kaddr + offset -
804 map_start);
805 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500806 ret = comp_keys(tmp, key);
807
808 if (ret < 0)
809 low = mid + 1;
810 else if (ret > 0)
811 high = mid;
812 else {
813 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400814 if (map_token)
815 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500816 return 0;
817 }
818 }
819 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400820 if (map_token)
821 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500822 return 1;
823}
824
Chris Mason97571fd2007-02-24 13:39:08 -0500825/*
826 * simple bin_search frontend that does the right thing for
827 * leaves vs nodes
828 */
Chris Mason5f39d392007-10-15 16:14:19 -0400829static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
830 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500831{
Chris Mason5f39d392007-10-15 16:14:19 -0400832 if (level == 0) {
833 return generic_bin_search(eb,
834 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400835 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400836 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400837 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500838 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400839 return generic_bin_search(eb,
840 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400841 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400842 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400843 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500844 }
845 return -1;
846}
847
Chris Masond352ac62008-09-29 15:18:18 -0400848/* given a node and slot number, this reads the blocks it points to. The
849 * extent buffer is returned with a reference taken (but unlocked).
850 * NULL is returned on error.
851 */
Chris Masone02119d2008-09-05 16:13:11 -0400852static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400853 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500854{
Chris Masonca7a79a2008-05-12 12:59:19 -0400855 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500856 if (slot < 0)
857 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400858 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500859 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400860
861 BUG_ON(level == 0);
862
Chris Masondb945352007-10-15 16:15:53 -0400863 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400864 btrfs_level_size(root, level - 1),
865 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500866}
867
Chris Masond352ac62008-09-29 15:18:18 -0400868/*
869 * node level balancing, used to make sure nodes are in proper order for
870 * item deletion. We balance from the top down, so we have to make sure
871 * that a deletion won't leave an node completely empty later on.
872 */
Chris Masone02119d2008-09-05 16:13:11 -0400873static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500874 struct btrfs_root *root,
875 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500876{
Chris Mason5f39d392007-10-15 16:14:19 -0400877 struct extent_buffer *right = NULL;
878 struct extent_buffer *mid;
879 struct extent_buffer *left = NULL;
880 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500881 int ret = 0;
882 int wret;
883 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500884 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400885 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500886 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500887
888 if (level == 0)
889 return 0;
890
Chris Mason5f39d392007-10-15 16:14:19 -0400891 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500892
Chris Mason925baed2008-06-25 16:01:30 -0400893 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500894 WARN_ON(btrfs_header_generation(mid) != trans->transid);
895
Chris Mason1d4f8a02007-03-13 09:28:32 -0400896 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500897
Chris Mason234b63a2007-03-13 10:46:10 -0400898 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400899 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500900 pslot = path->slots[level + 1];
901
Chris Mason40689472007-03-17 14:29:23 -0400902 /*
903 * deal with the case where there is only one pointer in the root
904 * by promoting the node below to a root
905 */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 if (!parent) {
907 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500908
Chris Mason5f39d392007-10-15 16:14:19 -0400909 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500910 return 0;
911
912 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500914 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400915 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500916 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400917 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -0500918 BUG_ON(ret);
919
Chris Mason925baed2008-06-25 16:01:30 -0400920 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500921 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400922 spin_unlock(&root->node_lock);
923
Zheng Yan31840ae2008-09-23 13:14:14 -0400924 ret = btrfs_update_extent_ref(trans, root, child->start,
925 mid->start, child->start,
926 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400927 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400928 BUG_ON(ret);
929
Chris Mason0b86a832008-03-24 15:01:56 -0400930 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400931 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500932
Chris Mason925baed2008-06-25 16:01:30 -0400933 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500934 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400935 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400936 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500937 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400938 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500939 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400940 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400941 btrfs_header_generation(mid),
942 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500943 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400944 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400945 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500946 }
Chris Mason5f39d392007-10-15 16:14:19 -0400947 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400948 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500949 return 0;
950
Chris Mason5f39d392007-10-15 16:14:19 -0400951 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400952 err_on_enospc = 1;
953
Chris Mason5f39d392007-10-15 16:14:19 -0400954 left = read_node_slot(root, parent, pslot - 1);
955 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400956 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500957 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400958 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400959 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400960 if (wret) {
961 ret = wret;
962 goto enospc;
963 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400964 }
Chris Mason5f39d392007-10-15 16:14:19 -0400965 right = read_node_slot(root, parent, pslot + 1);
966 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400967 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500968 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400969 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400970 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400971 if (wret) {
972 ret = wret;
973 goto enospc;
974 }
975 }
976
977 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400978 if (left) {
979 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400980 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500981 if (wret < 0)
982 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400983 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400984 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500985 }
Chris Mason79f95c82007-03-01 15:16:26 -0500986
987 /*
988 * then try to empty the right most buffer into the middle
989 */
Chris Mason5f39d392007-10-15 16:14:19 -0400990 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400991 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500993 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400994 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400995 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500996 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400997 u32 blocksize = right->len;
998
Chris Mason5f39d392007-10-15 16:14:19 -0400999 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001000 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001001 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001002 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001003 wret = del_ptr(trans, root, path, level + 1, pslot +
1004 1);
Chris Masonbb803952007-03-01 12:04:21 -05001005 if (wret)
1006 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001007 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001008 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001009 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001010 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001011 if (wret)
1012 ret = wret;
1013 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001014 struct btrfs_disk_key right_key;
1015 btrfs_node_key(right, &right_key, 0);
1016 btrfs_set_node_key(parent, &right_key, pslot + 1);
1017 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001018 }
1019 }
Chris Mason5f39d392007-10-15 16:14:19 -04001020 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001021 /*
1022 * we're not allowed to leave a node with one item in the
1023 * tree during a delete. A deletion from lower in the tree
1024 * could try to delete the only pointer in this node.
1025 * So, pull some keys from the left.
1026 * There has to be a left pointer at this point because
1027 * otherwise we would have pulled some pointers from the
1028 * right
1029 */
Chris Mason5f39d392007-10-15 16:14:19 -04001030 BUG_ON(!left);
1031 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001032 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001033 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001034 goto enospc;
1035 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001036 if (wret == 1) {
1037 wret = push_node_left(trans, root, left, mid, 1);
1038 if (wret < 0)
1039 ret = wret;
1040 }
Chris Mason79f95c82007-03-01 15:16:26 -05001041 BUG_ON(wret == 1);
1042 }
Chris Mason5f39d392007-10-15 16:14:19 -04001043 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001044 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001045 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001046 u64 bytenr = mid->start;
1047 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001048
Chris Mason5f39d392007-10-15 16:14:19 -04001049 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001050 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001051 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001052 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001053 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001054 if (wret)
1055 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001056 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001057 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001058 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001059 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001060 if (wret)
1061 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001062 } else {
1063 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001064 struct btrfs_disk_key mid_key;
1065 btrfs_node_key(mid, &mid_key, 0);
1066 btrfs_set_node_key(parent, &mid_key, pslot);
1067 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001068 }
Chris Masonbb803952007-03-01 12:04:21 -05001069
Chris Mason79f95c82007-03-01 15:16:26 -05001070 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001071 if (left) {
1072 if (btrfs_header_nritems(left) > orig_slot) {
1073 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001074 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001075 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001076 path->slots[level + 1] -= 1;
1077 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001078 if (mid) {
1079 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001080 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001081 }
Chris Masonbb803952007-03-01 12:04:21 -05001082 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001083 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001084 path->slots[level] = orig_slot;
1085 }
1086 }
Chris Mason79f95c82007-03-01 15:16:26 -05001087 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001088 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001089 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001090 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001091 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001092enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001093 if (right) {
1094 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001095 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001096 }
1097 if (left) {
1098 if (path->nodes[level] != left)
1099 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001100 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001101 }
Chris Masonbb803952007-03-01 12:04:21 -05001102 return ret;
1103}
1104
Chris Masond352ac62008-09-29 15:18:18 -04001105/* Node balancing for insertion. Here we only split or push nodes around
1106 * when they are completely full. This is also done top down, so we
1107 * have to be pessimistic.
1108 */
Chris Masond3977122009-01-05 21:25:51 -05001109static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001110 struct btrfs_root *root,
1111 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001112{
Chris Mason5f39d392007-10-15 16:14:19 -04001113 struct extent_buffer *right = NULL;
1114 struct extent_buffer *mid;
1115 struct extent_buffer *left = NULL;
1116 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001117 int ret = 0;
1118 int wret;
1119 int pslot;
1120 int orig_slot = path->slots[level];
1121 u64 orig_ptr;
1122
1123 if (level == 0)
1124 return 1;
1125
Chris Mason5f39d392007-10-15 16:14:19 -04001126 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001127 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001128 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1129
1130 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001131 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001132 pslot = path->slots[level + 1];
1133
Chris Mason5f39d392007-10-15 16:14:19 -04001134 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001135 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001136
Chris Mason5f39d392007-10-15 16:14:19 -04001137 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001138
1139 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001140 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001141 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001142
1143 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001144 btrfs_set_lock_blocking(left);
1145
Chris Mason5f39d392007-10-15 16:14:19 -04001146 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001147 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1148 wret = 1;
1149 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001150 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001151 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001152 if (ret)
1153 wret = 1;
1154 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001155 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001156 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001157 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001158 }
Chris Masone66f7092007-04-20 13:16:02 -04001159 if (wret < 0)
1160 ret = wret;
1161 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001162 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001163 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001164 btrfs_node_key(mid, &disk_key, 0);
1165 btrfs_set_node_key(parent, &disk_key, pslot);
1166 btrfs_mark_buffer_dirty(parent);
1167 if (btrfs_header_nritems(left) > orig_slot) {
1168 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001169 path->slots[level + 1] -= 1;
1170 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001171 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001172 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001173 } else {
1174 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001175 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001176 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001177 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001178 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001179 }
Chris Masone66f7092007-04-20 13:16:02 -04001180 return 0;
1181 }
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 Mason925baed2008-06-25 16:01:30 -04001185 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001186
1187 /*
1188 * then try to empty the right most buffer into the middle
1189 */
Chris Mason5f39d392007-10-15 16:14:19 -04001190 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001191 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001192
Chris Mason925baed2008-06-25 16:01:30 -04001193 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001194 btrfs_set_lock_blocking(right);
1195
Chris Mason5f39d392007-10-15 16:14:19 -04001196 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001197 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1198 wret = 1;
1199 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001200 ret = btrfs_cow_block(trans, root, right,
1201 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001202 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001203 if (ret)
1204 wret = 1;
1205 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001206 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001207 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001208 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001209 }
Chris Masone66f7092007-04-20 13:16:02 -04001210 if (wret < 0)
1211 ret = wret;
1212 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001213 struct btrfs_disk_key disk_key;
1214
1215 btrfs_node_key(right, &disk_key, 0);
1216 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1217 btrfs_mark_buffer_dirty(parent);
1218
1219 if (btrfs_header_nritems(mid) <= orig_slot) {
1220 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001221 path->slots[level + 1] += 1;
1222 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001223 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001224 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001225 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001226 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001227 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001228 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001229 }
Chris Masone66f7092007-04-20 13:16:02 -04001230 return 0;
1231 }
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 1;
1236}
1237
Chris Mason74123bd2007-02-02 11:05:29 -05001238/*
Chris Masond352ac62008-09-29 15:18:18 -04001239 * readahead one full node of leaves, finding things that are close
1240 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001241 */
Chris Masone02119d2008-09-05 16:13:11 -04001242static noinline void reada_for_search(struct btrfs_root *root,
1243 struct btrfs_path *path,
1244 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001245{
Chris Mason5f39d392007-10-15 16:14:19 -04001246 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001247 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001248 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001249 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001250 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001251 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001252 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001253 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001254 u32 nr;
1255 u32 blocksize;
1256 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001257
Chris Masona6b6e752007-10-15 16:22:39 -04001258 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001259 return;
1260
Chris Mason6702ed42007-08-07 16:15:09 -04001261 if (!path->nodes[level])
1262 return;
1263
Chris Mason5f39d392007-10-15 16:14:19 -04001264 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001265
Chris Mason3c69fae2007-08-07 15:52:22 -04001266 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001267 blocksize = btrfs_level_size(root, level - 1);
1268 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001269 if (eb) {
1270 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 return;
1272 }
1273
Chris Masona7175312009-01-22 09:23:10 -05001274 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001275
Chris Mason5f39d392007-10-15 16:14:19 -04001276 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001277 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001278 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001279 if (direction < 0) {
1280 if (nr == 0)
1281 break;
1282 nr--;
1283 } else if (direction > 0) {
1284 nr++;
1285 if (nr >= nritems)
1286 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001287 }
Chris Mason01f46652007-12-21 16:24:26 -05001288 if (path->reada < 0 && objectid) {
1289 btrfs_node_key(node, &disk_key, nr);
1290 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1291 break;
1292 }
Chris Mason6b800532007-10-15 16:17:34 -04001293 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001294 if ((search <= target && target - search <= 65536) ||
1295 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001296 readahead_tree_block(root, search, blocksize,
1297 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001298 nread += blocksize;
1299 }
1300 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001301 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001302 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001303 }
1304}
Chris Mason925baed2008-06-25 16:01:30 -04001305
Chris Masond352ac62008-09-29 15:18:18 -04001306/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001307 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1308 * cache
1309 */
1310static noinline int reada_for_balance(struct btrfs_root *root,
1311 struct btrfs_path *path, int level)
1312{
1313 int slot;
1314 int nritems;
1315 struct extent_buffer *parent;
1316 struct extent_buffer *eb;
1317 u64 gen;
1318 u64 block1 = 0;
1319 u64 block2 = 0;
1320 int ret = 0;
1321 int blocksize;
1322
1323 parent = path->nodes[level - 1];
1324 if (!parent)
1325 return 0;
1326
1327 nritems = btrfs_header_nritems(parent);
1328 slot = path->slots[level];
1329 blocksize = btrfs_level_size(root, level);
1330
1331 if (slot > 0) {
1332 block1 = btrfs_node_blockptr(parent, slot - 1);
1333 gen = btrfs_node_ptr_generation(parent, slot - 1);
1334 eb = btrfs_find_tree_block(root, block1, blocksize);
1335 if (eb && btrfs_buffer_uptodate(eb, gen))
1336 block1 = 0;
1337 free_extent_buffer(eb);
1338 }
1339 if (slot < nritems) {
1340 block2 = btrfs_node_blockptr(parent, slot + 1);
1341 gen = btrfs_node_ptr_generation(parent, slot + 1);
1342 eb = btrfs_find_tree_block(root, block2, blocksize);
1343 if (eb && btrfs_buffer_uptodate(eb, gen))
1344 block2 = 0;
1345 free_extent_buffer(eb);
1346 }
1347 if (block1 || block2) {
1348 ret = -EAGAIN;
1349 btrfs_release_path(root, path);
1350 if (block1)
1351 readahead_tree_block(root, block1, blocksize, 0);
1352 if (block2)
1353 readahead_tree_block(root, block2, blocksize, 0);
1354
1355 if (block1) {
1356 eb = read_tree_block(root, block1, blocksize, 0);
1357 free_extent_buffer(eb);
1358 }
1359 if (block1) {
1360 eb = read_tree_block(root, block2, blocksize, 0);
1361 free_extent_buffer(eb);
1362 }
1363 }
1364 return ret;
1365}
1366
1367
1368/*
Chris Masond3977122009-01-05 21:25:51 -05001369 * when we walk down the tree, it is usually safe to unlock the higher layers
1370 * in the tree. The exceptions are when our path goes through slot 0, because
1371 * operations on the tree might require changing key pointers higher up in the
1372 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001373 *
Chris Masond3977122009-01-05 21:25:51 -05001374 * callers might also have set path->keep_locks, which tells this code to keep
1375 * the lock if the path points to the last slot in the block. This is part of
1376 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001377 *
Chris Masond3977122009-01-05 21:25:51 -05001378 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1379 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001380 */
Chris Masone02119d2008-09-05 16:13:11 -04001381static noinline void unlock_up(struct btrfs_path *path, int level,
1382 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001383{
1384 int i;
1385 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001386 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001387 struct extent_buffer *t;
1388
1389 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1390 if (!path->nodes[i])
1391 break;
1392 if (!path->locks[i])
1393 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001394 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001395 skip_level = i + 1;
1396 continue;
1397 }
Chris Mason051e1b92008-06-25 16:01:30 -04001398 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001399 u32 nritems;
1400 t = path->nodes[i];
1401 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001402 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001403 skip_level = i + 1;
1404 continue;
1405 }
1406 }
Chris Mason051e1b92008-06-25 16:01:30 -04001407 if (skip_level < i && i >= lowest_unlock)
1408 no_skips = 1;
1409
Chris Mason925baed2008-06-25 16:01:30 -04001410 t = path->nodes[i];
1411 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1412 btrfs_tree_unlock(t);
1413 path->locks[i] = 0;
1414 }
1415 }
1416}
1417
Chris Mason3c69fae2007-08-07 15:52:22 -04001418/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001419 * This releases any locks held in the path starting at level and
1420 * going all the way up to the root.
1421 *
1422 * btrfs_search_slot will keep the lock held on higher nodes in a few
1423 * corner cases, such as COW of the block at slot zero in the node. This
1424 * ignores those rules, and it should only be called when there are no
1425 * more updates to be done higher up in the tree.
1426 */
1427noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1428{
1429 int i;
1430
1431 if (path->keep_locks || path->lowest_level)
1432 return;
1433
1434 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1435 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001436 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001437 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001438 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001439 btrfs_tree_unlock(path->nodes[i]);
1440 path->locks[i] = 0;
1441 }
1442}
1443
1444/*
Chris Mason74123bd2007-02-02 11:05:29 -05001445 * look for key in the tree. path is filled in with nodes along the way
1446 * if key is found, we return zero and you can find the item in the leaf
1447 * level of the path (level 0)
1448 *
1449 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001450 * be inserted, and 1 is returned. If there are other errors during the
1451 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001452 *
1453 * if ins_len > 0, nodes and leaves will be split as we walk down the
1454 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1455 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001456 */
Chris Masone089f052007-03-16 16:20:31 -04001457int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1458 *root, struct btrfs_key *key, struct btrfs_path *p, int
1459 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001460{
Chris Mason5f39d392007-10-15 16:14:19 -04001461 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001462 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001463 int slot;
1464 int ret;
1465 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001466 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001467 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001468 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001469 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001470 u64 blocknr;
1471 u64 gen;
Chris Mason9f3a7422007-08-07 15:52:19 -04001472
Chris Mason6702ed42007-08-07 16:15:09 -04001473 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001474 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001475 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001476
Chris Mason925baed2008-06-25 16:01:30 -04001477 if (ins_len < 0)
1478 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001479
Chris Masonbb803952007-03-01 12:04:21 -05001480again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001481 if (p->skip_locking)
1482 b = btrfs_root_node(root);
1483 else
1484 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001485
Chris Masoneb60cea2007-02-02 09:18:22 -05001486 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001487 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001488
1489 /*
1490 * setup the path here so we can release it under lock
1491 * contention with the cow code
1492 */
1493 p->nodes[level] = b;
1494 if (!p->skip_locking)
1495 p->locks[level] = 1;
1496
Chris Mason02217ed2007-03-02 16:08:05 -05001497 if (cow) {
1498 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001499
1500 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001501 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001502 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001503 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001504 goto cow_done;
1505 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001506 btrfs_set_path_blocking(p);
1507
Chris Masone20d96d2007-03-22 12:13:20 -04001508 wret = btrfs_cow_block(trans, root, b,
1509 p->nodes[level + 1],
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001510 p->slots[level + 1], &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001511 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001512 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001513 ret = wret;
1514 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001515 }
Chris Mason02217ed2007-03-02 16:08:05 -05001516 }
Chris Mason65b51a02008-08-01 15:11:20 -04001517cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001518 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001519 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001520 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001521 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001522
Chris Masoneb60cea2007-02-02 09:18:22 -05001523 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001524 if (!p->skip_locking)
1525 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001526
Chris Mason4008c042009-02-12 14:09:45 -05001527 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001528
1529 /*
1530 * we have a lock on b and as long as we aren't changing
1531 * the tree, there is no way to for the items in b to change.
1532 * It is safe to drop the lock on our parent before we
1533 * go through the expensive btree search on b.
1534 *
1535 * If cow is true, then we might be changing slot zero,
1536 * which may require changing the parent. So, we can't
1537 * drop the lock until after we know which slot we're
1538 * operating on.
1539 */
1540 if (!cow)
1541 btrfs_unlock_up_safe(p, level + 1);
1542
Chris Mason123abc82007-03-14 14:14:43 -04001543 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001544 if (ret) {
1545 ret = -1;
1546 goto done;
1547 }
Chris Mason925baed2008-06-25 16:01:30 -04001548
Chris Mason5f39d392007-10-15 16:14:19 -04001549 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001550
Chris Mason5f39d392007-10-15 16:14:19 -04001551 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001552 if (ret && slot > 0)
1553 slot -= 1;
1554 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001555 if ((p->search_for_split || ins_len > 0) &&
1556 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001557 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001558 int sret;
1559
1560 sret = reada_for_balance(root, p, level);
1561 if (sret)
1562 goto again;
1563
1564 btrfs_set_path_blocking(p);
1565 sret = split_node(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001566 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001567
Chris Mason5c680ed2007-02-22 11:39:13 -05001568 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001569 if (sret) {
1570 ret = sret;
1571 goto done;
1572 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001573 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001574 slot = p->slots[level];
Chris Mason7b78c172009-02-04 09:12:46 -05001575 } else if (ins_len < 0 &&
1576 btrfs_header_nritems(b) <
1577 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001578 int sret;
1579
1580 sret = reada_for_balance(root, p, level);
1581 if (sret)
1582 goto again;
1583
1584 btrfs_set_path_blocking(p);
1585 sret = balance_level(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001586 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001587
Chris Mason65b51a02008-08-01 15:11:20 -04001588 if (sret) {
1589 ret = sret;
1590 goto done;
1591 }
Chris Masonbb803952007-03-01 12:04:21 -05001592 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001593 if (!b) {
1594 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001595 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001596 }
Chris Masonbb803952007-03-01 12:04:21 -05001597 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001598 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001599 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001600 unlock_up(p, level, lowest_unlock);
1601
Chris Mason9f3a7422007-08-07 15:52:19 -04001602 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001603 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001604 ret = 0;
1605 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001606 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001607
Chris Mason594a24e2008-06-25 16:01:30 -04001608 blocknr = btrfs_node_blockptr(b, slot);
1609 gen = btrfs_node_ptr_generation(b, slot);
1610 blocksize = btrfs_level_size(root, level - 1);
1611
1612 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1613 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001614 b = tmp;
1615 } else {
1616 /*
1617 * reduce lock contention at high levels
1618 * of the btree by dropping locks before
1619 * we read.
1620 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001621 if (level > 0) {
Chris Mason051e1b92008-06-25 16:01:30 -04001622 btrfs_release_path(NULL, p);
1623 if (tmp)
1624 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001625 if (should_reada)
1626 reada_for_search(root, p,
1627 level, slot,
1628 key->objectid);
1629
Chris Mason594a24e2008-06-25 16:01:30 -04001630 tmp = read_tree_block(root, blocknr,
1631 blocksize, gen);
1632 if (tmp)
1633 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001634 goto again;
1635 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001636 btrfs_set_path_blocking(p);
Chris Masona74a4b92008-06-25 16:01:31 -04001637 if (tmp)
1638 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001639 if (should_reada)
1640 reada_for_search(root, p,
1641 level, slot,
1642 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001643 b = read_node_slot(root, b, slot);
1644 }
1645 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001646 if (!p->skip_locking) {
1647 int lret;
1648
Chris Mason4008c042009-02-12 14:09:45 -05001649 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001650 lret = btrfs_try_spin_lock(b);
1651
1652 if (!lret) {
1653 btrfs_set_path_blocking(p);
1654 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001655 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001656 }
1657 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001658 } else {
1659 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001660 if (ins_len > 0 &&
1661 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001662 int sret;
1663
1664 btrfs_set_path_blocking(p);
1665 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001666 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001667 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001668
Chris Mason5c680ed2007-02-22 11:39:13 -05001669 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001670 if (sret) {
1671 ret = sret;
1672 goto done;
1673 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001674 }
Chris Mason459931e2008-12-10 09:10:46 -05001675 if (!p->search_for_split)
1676 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001677 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001678 }
1679 }
Chris Mason65b51a02008-08-01 15:11:20 -04001680 ret = 1;
1681done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001682 /*
1683 * we don't really know what they plan on doing with the path
1684 * from here on, so for now just mark it as blocking
1685 */
1686 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001687 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001688}
1689
Zheng Yan1a40e232008-09-26 10:09:34 -04001690int btrfs_merge_path(struct btrfs_trans_handle *trans,
1691 struct btrfs_root *root,
1692 struct btrfs_key *node_keys,
1693 u64 *nodes, int lowest_level)
1694{
1695 struct extent_buffer *eb;
1696 struct extent_buffer *parent;
1697 struct btrfs_key key;
1698 u64 bytenr;
1699 u64 generation;
1700 u32 blocksize;
1701 int level;
1702 int slot;
1703 int key_match;
1704 int ret;
1705
1706 eb = btrfs_lock_root_node(root);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001707 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001708 BUG_ON(ret);
1709
Chris Masonb4ce94d2009-02-04 09:25:08 -05001710 btrfs_set_lock_blocking(eb);
1711
Zheng Yan1a40e232008-09-26 10:09:34 -04001712 parent = eb;
1713 while (1) {
1714 level = btrfs_header_level(parent);
1715 if (level == 0 || level <= lowest_level)
1716 break;
1717
1718 ret = bin_search(parent, &node_keys[lowest_level], level,
1719 &slot);
1720 if (ret && slot > 0)
1721 slot--;
1722
1723 bytenr = btrfs_node_blockptr(parent, slot);
1724 if (nodes[level - 1] == bytenr)
1725 break;
1726
1727 blocksize = btrfs_level_size(root, level - 1);
1728 generation = btrfs_node_ptr_generation(parent, slot);
1729 btrfs_node_key_to_cpu(eb, &key, slot);
1730 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1731
Yan Zhengf82d02d2008-10-29 14:49:05 -04001732 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001733 eb = read_tree_block(root, bytenr, blocksize,
1734 generation);
1735 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001736 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001737 }
1738
1739 /*
1740 * if node keys match and node pointer hasn't been modified
1741 * in the running transaction, we can merge the path. for
1742 * blocks owened by reloc trees, the node pointer check is
1743 * skipped, this is because these blocks are fully controlled
1744 * by the space balance code, no one else can modify them.
1745 */
1746 if (!nodes[level - 1] || !key_match ||
1747 (generation == trans->transid &&
1748 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1749 if (level == 1 || level == lowest_level + 1) {
1750 if (generation == trans->transid) {
1751 btrfs_tree_unlock(eb);
1752 free_extent_buffer(eb);
1753 }
1754 break;
1755 }
1756
1757 if (generation != trans->transid) {
1758 eb = read_tree_block(root, bytenr, blocksize,
1759 generation);
1760 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001761 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001762 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001763
1764 ret = btrfs_cow_block(trans, root, eb, parent, slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001765 &eb);
Zheng Yan1a40e232008-09-26 10:09:34 -04001766 BUG_ON(ret);
1767
Yan Zhengf82d02d2008-10-29 14:49:05 -04001768 if (root->root_key.objectid ==
1769 BTRFS_TREE_RELOC_OBJECTID) {
1770 if (!nodes[level - 1]) {
1771 nodes[level - 1] = eb->start;
1772 memcpy(&node_keys[level - 1], &key,
1773 sizeof(node_keys[0]));
1774 } else {
1775 WARN_ON(1);
1776 }
1777 }
1778
Zheng Yan1a40e232008-09-26 10:09:34 -04001779 btrfs_tree_unlock(parent);
1780 free_extent_buffer(parent);
1781 parent = eb;
1782 continue;
1783 }
1784
Zheng Yan1a40e232008-09-26 10:09:34 -04001785 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1786 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1787 btrfs_mark_buffer_dirty(parent);
1788
1789 ret = btrfs_inc_extent_ref(trans, root,
1790 nodes[level - 1],
1791 blocksize, parent->start,
1792 btrfs_header_owner(parent),
1793 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001794 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001795 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001796
1797 /*
1798 * If the block was created in the running transaction,
1799 * it's possible this is the last reference to it, so we
1800 * should drop the subtree.
1801 */
1802 if (generation == trans->transid) {
1803 ret = btrfs_drop_subtree(trans, root, eb, parent);
1804 BUG_ON(ret);
1805 btrfs_tree_unlock(eb);
1806 free_extent_buffer(eb);
1807 } else {
1808 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001809 blocksize, parent->start,
1810 btrfs_header_owner(parent),
1811 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001812 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001813 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001814 }
1815 break;
1816 }
1817 btrfs_tree_unlock(parent);
1818 free_extent_buffer(parent);
1819 return 0;
1820}
1821
Chris Mason74123bd2007-02-02 11:05:29 -05001822/*
1823 * adjust the pointers going up the tree, starting at level
1824 * making sure the right key of each node is points to 'key'.
1825 * This is used after shifting pointers to the left, so it stops
1826 * fixing up pointers when a given leaf/node is not in slot 0 of the
1827 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001828 *
1829 * If this fails to write a tree block, it returns -1, but continues
1830 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001831 */
Chris Mason5f39d392007-10-15 16:14:19 -04001832static int fixup_low_keys(struct btrfs_trans_handle *trans,
1833 struct btrfs_root *root, struct btrfs_path *path,
1834 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001835{
1836 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001837 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001838 struct extent_buffer *t;
1839
Chris Mason234b63a2007-03-13 10:46:10 -04001840 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001841 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001842 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001843 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001844 t = path->nodes[i];
1845 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001846 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001847 if (tslot != 0)
1848 break;
1849 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001850 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001851}
1852
Chris Mason74123bd2007-02-02 11:05:29 -05001853/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001854 * update item key.
1855 *
1856 * This function isn't completely safe. It's the caller's responsibility
1857 * that the new key won't break the order
1858 */
1859int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root, struct btrfs_path *path,
1861 struct btrfs_key *new_key)
1862{
1863 struct btrfs_disk_key disk_key;
1864 struct extent_buffer *eb;
1865 int slot;
1866
1867 eb = path->nodes[0];
1868 slot = path->slots[0];
1869 if (slot > 0) {
1870 btrfs_item_key(eb, &disk_key, slot - 1);
1871 if (comp_keys(&disk_key, new_key) >= 0)
1872 return -1;
1873 }
1874 if (slot < btrfs_header_nritems(eb) - 1) {
1875 btrfs_item_key(eb, &disk_key, slot + 1);
1876 if (comp_keys(&disk_key, new_key) <= 0)
1877 return -1;
1878 }
1879
1880 btrfs_cpu_key_to_disk(&disk_key, new_key);
1881 btrfs_set_item_key(eb, &disk_key, slot);
1882 btrfs_mark_buffer_dirty(eb);
1883 if (slot == 0)
1884 fixup_low_keys(trans, root, path, &disk_key, 1);
1885 return 0;
1886}
1887
1888/*
Chris Mason74123bd2007-02-02 11:05:29 -05001889 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001890 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001891 *
1892 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1893 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001894 */
Chris Mason98ed5172008-01-03 10:01:48 -05001895static int push_node_left(struct btrfs_trans_handle *trans,
1896 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001897 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001898{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001899 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001900 int src_nritems;
1901 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001902 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001903
Chris Mason5f39d392007-10-15 16:14:19 -04001904 src_nritems = btrfs_header_nritems(src);
1905 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001906 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001907 WARN_ON(btrfs_header_generation(src) != trans->transid);
1908 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001909
Chris Masonbce4eae2008-04-24 14:42:46 -04001910 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001911 return 1;
1912
Chris Masond3977122009-01-05 21:25:51 -05001913 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001914 return 1;
1915
Chris Masonbce4eae2008-04-24 14:42:46 -04001916 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001917 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001918 if (push_items < src_nritems) {
1919 /* leave at least 8 pointers in the node if
1920 * we aren't going to empty it
1921 */
1922 if (src_nritems - push_items < 8) {
1923 if (push_items <= 8)
1924 return 1;
1925 push_items -= 8;
1926 }
1927 }
1928 } else
1929 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001930
Chris Mason5f39d392007-10-15 16:14:19 -04001931 copy_extent_buffer(dst, src,
1932 btrfs_node_key_ptr_offset(dst_nritems),
1933 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001934 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001935
Chris Masonbb803952007-03-01 12:04:21 -05001936 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001937 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1938 btrfs_node_key_ptr_offset(push_items),
1939 (src_nritems - push_items) *
1940 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001941 }
Chris Mason5f39d392007-10-15 16:14:19 -04001942 btrfs_set_header_nritems(src, src_nritems - push_items);
1943 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1944 btrfs_mark_buffer_dirty(src);
1945 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001946
1947 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1948 BUG_ON(ret);
1949
Chris Masonbb803952007-03-01 12:04:21 -05001950 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001951}
1952
Chris Mason97571fd2007-02-24 13:39:08 -05001953/*
Chris Mason79f95c82007-03-01 15:16:26 -05001954 * try to push data from one node into the next node right in the
1955 * tree.
1956 *
1957 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1958 * error, and > 0 if there was no room in the right hand block.
1959 *
1960 * this will only push up to 1/2 the contents of the left node over
1961 */
Chris Mason5f39d392007-10-15 16:14:19 -04001962static int balance_node_right(struct btrfs_trans_handle *trans,
1963 struct btrfs_root *root,
1964 struct extent_buffer *dst,
1965 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001966{
Chris Mason79f95c82007-03-01 15:16:26 -05001967 int push_items = 0;
1968 int max_push;
1969 int src_nritems;
1970 int dst_nritems;
1971 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001972
Chris Mason7bb86312007-12-11 09:25:06 -05001973 WARN_ON(btrfs_header_generation(src) != trans->transid);
1974 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1975
Chris Mason5f39d392007-10-15 16:14:19 -04001976 src_nritems = btrfs_header_nritems(src);
1977 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001978 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001979 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001980 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001981
Chris Masond3977122009-01-05 21:25:51 -05001982 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001983 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001984
1985 max_push = src_nritems / 2 + 1;
1986 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001987 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001988 return 1;
Yan252c38f2007-08-29 09:11:44 -04001989
Chris Mason79f95c82007-03-01 15:16:26 -05001990 if (max_push < push_items)
1991 push_items = max_push;
1992
Chris Mason5f39d392007-10-15 16:14:19 -04001993 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1994 btrfs_node_key_ptr_offset(0),
1995 (dst_nritems) *
1996 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001997
Chris Mason5f39d392007-10-15 16:14:19 -04001998 copy_extent_buffer(dst, src,
1999 btrfs_node_key_ptr_offset(0),
2000 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002001 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002002
Chris Mason5f39d392007-10-15 16:14:19 -04002003 btrfs_set_header_nritems(src, src_nritems - push_items);
2004 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002005
Chris Mason5f39d392007-10-15 16:14:19 -04002006 btrfs_mark_buffer_dirty(src);
2007 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002008
2009 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2010 BUG_ON(ret);
2011
Chris Mason79f95c82007-03-01 15:16:26 -05002012 return ret;
2013}
2014
2015/*
Chris Mason97571fd2007-02-24 13:39:08 -05002016 * helper function to insert a new root level in the tree.
2017 * A new node is allocated, and a single item is inserted to
2018 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002019 *
2020 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002021 */
Chris Masond3977122009-01-05 21:25:51 -05002022static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002023 struct btrfs_root *root,
2024 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002025{
Chris Mason7bb86312007-12-11 09:25:06 -05002026 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002027 struct extent_buffer *lower;
2028 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002029 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002030 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002031 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002032
2033 BUG_ON(path->nodes[level]);
2034 BUG_ON(path->nodes[level-1] != root->node);
2035
Chris Mason7bb86312007-12-11 09:25:06 -05002036 lower = path->nodes[level-1];
2037 if (level == 1)
2038 btrfs_item_key(lower, &lower_key, 0);
2039 else
2040 btrfs_node_key(lower, &lower_key, 0);
2041
Zheng Yan31840ae2008-09-23 13:14:14 -04002042 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2043 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002044 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002045 if (IS_ERR(c))
2046 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002047
Chris Mason5f39d392007-10-15 16:14:19 -04002048 memset_extent_buffer(c, 0, 0, root->nodesize);
2049 btrfs_set_header_nritems(c, 1);
2050 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002051 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 btrfs_set_header_generation(c, trans->transid);
2053 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002054
Chris Mason5f39d392007-10-15 16:14:19 -04002055 write_extent_buffer(c, root->fs_info->fsid,
2056 (unsigned long)btrfs_header_fsid(c),
2057 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002058
2059 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2060 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2061 BTRFS_UUID_SIZE);
2062
Chris Mason5f39d392007-10-15 16:14:19 -04002063 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002064 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002065 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002066 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002067
2068 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002069
2070 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002071
Chris Mason925baed2008-06-25 16:01:30 -04002072 spin_lock(&root->node_lock);
2073 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002074 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002075 spin_unlock(&root->node_lock);
2076
Zheng Yan31840ae2008-09-23 13:14:14 -04002077 ret = btrfs_update_extent_ref(trans, root, lower->start,
2078 lower->start, c->start,
2079 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002080 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002081 BUG_ON(ret);
2082
Chris Mason925baed2008-06-25 16:01:30 -04002083 /* the super has an extra ref to root->node */
2084 free_extent_buffer(old);
2085
Chris Mason0b86a832008-03-24 15:01:56 -04002086 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002087 extent_buffer_get(c);
2088 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002089 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002090 path->slots[level] = 0;
2091 return 0;
2092}
2093
Chris Mason74123bd2007-02-02 11:05:29 -05002094/*
2095 * worker function to insert a single pointer in a node.
2096 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002097 *
Chris Mason74123bd2007-02-02 11:05:29 -05002098 * slot and level indicate where you want the key to go, and
2099 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002100 *
2101 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002102 */
Chris Masone089f052007-03-16 16:20:31 -04002103static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2104 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002105 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002106{
Chris Mason5f39d392007-10-15 16:14:19 -04002107 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002108 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002109
2110 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 lower = path->nodes[level];
2112 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002113 if (slot > nritems)
2114 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002115 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002116 BUG();
2117 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002118 memmove_extent_buffer(lower,
2119 btrfs_node_key_ptr_offset(slot + 1),
2120 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002121 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002122 }
Chris Mason5f39d392007-10-15 16:14:19 -04002123 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002124 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002125 WARN_ON(trans->transid == 0);
2126 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002127 btrfs_set_header_nritems(lower, nritems + 1);
2128 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002129 return 0;
2130}
2131
Chris Mason97571fd2007-02-24 13:39:08 -05002132/*
2133 * split the node at the specified level in path in two.
2134 * The path is corrected to point to the appropriate node after the split
2135 *
2136 * Before splitting this tries to make some room in the node by pushing
2137 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002138 *
2139 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002140 */
Chris Masone02119d2008-09-05 16:13:11 -04002141static noinline int split_node(struct btrfs_trans_handle *trans,
2142 struct btrfs_root *root,
2143 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002144{
Chris Mason5f39d392007-10-15 16:14:19 -04002145 struct extent_buffer *c;
2146 struct extent_buffer *split;
2147 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002148 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002149 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002150 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002151 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002152
Chris Mason5f39d392007-10-15 16:14:19 -04002153 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002154 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002155 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002156 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002157 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002158 if (ret)
2159 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002160 } else {
2161 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002162 c = path->nodes[level];
2163 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002164 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002165 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002166 if (ret < 0)
2167 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168 }
Chris Masone66f7092007-04-20 13:16:02 -04002169
Chris Mason5f39d392007-10-15 16:14:19 -04002170 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002171
Chris Mason925baed2008-06-25 16:01:30 -04002172 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002173 path->nodes[level + 1]->start,
2174 root->root_key.objectid,
2175 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002176 if (IS_ERR(split))
2177 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002178
Chris Mason5f39d392007-10-15 16:14:19 -04002179 btrfs_set_header_flags(split, btrfs_header_flags(c));
2180 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002181 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 btrfs_set_header_generation(split, trans->transid);
2183 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002184 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 write_extent_buffer(split, root->fs_info->fsid,
2186 (unsigned long)btrfs_header_fsid(split),
2187 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002188 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2189 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2190 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002191
Chris Mason7518a232007-03-12 12:01:18 -04002192 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002193
2194 copy_extent_buffer(split, c,
2195 btrfs_node_key_ptr_offset(0),
2196 btrfs_node_key_ptr_offset(mid),
2197 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2198 btrfs_set_header_nritems(split, c_nritems - mid);
2199 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002200 ret = 0;
2201
Chris Mason5f39d392007-10-15 16:14:19 -04002202 btrfs_mark_buffer_dirty(c);
2203 btrfs_mark_buffer_dirty(split);
2204
2205 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002206 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002207 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002208 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002209 if (wret)
2210 ret = wret;
2211
Zheng Yan31840ae2008-09-23 13:14:14 -04002212 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2213 BUG_ON(ret);
2214
Chris Mason5de08d72007-02-24 06:24:44 -05002215 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002216 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002217 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002218 free_extent_buffer(c);
2219 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002220 path->slots[level + 1] += 1;
2221 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002222 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002223 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002224 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002225 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002226}
2227
Chris Mason74123bd2007-02-02 11:05:29 -05002228/*
2229 * how many bytes are required to store the items in a leaf. start
2230 * and nr indicate which items in the leaf to check. This totals up the
2231 * space used both by the item structs and the item data
2232 */
Chris Mason5f39d392007-10-15 16:14:19 -04002233static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002234{
2235 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002236 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002237 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002238
2239 if (!nr)
2240 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002241 data_len = btrfs_item_end_nr(l, start);
2242 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002243 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002244 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002245 return data_len;
2246}
2247
Chris Mason74123bd2007-02-02 11:05:29 -05002248/*
Chris Masond4dbff92007-04-04 14:08:15 -04002249 * The space between the end of the leaf items and
2250 * the start of the leaf data. IOW, how much room
2251 * the leaf has left for both items and data
2252 */
Chris Masond3977122009-01-05 21:25:51 -05002253noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002254 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002255{
Chris Mason5f39d392007-10-15 16:14:19 -04002256 int nritems = btrfs_header_nritems(leaf);
2257 int ret;
2258 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2259 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002260 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2261 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002262 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002263 leaf_space_used(leaf, 0, nritems), nritems);
2264 }
2265 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002266}
2267
2268/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002269 * push some data in the path leaf to the right, trying to free up at
2270 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002271 *
2272 * returns 1 if the push failed because the other node didn't have enough
2273 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002274 */
Chris Masone089f052007-03-16 16:20:31 -04002275static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002276 *root, struct btrfs_path *path, int data_size,
2277 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002278{
Chris Mason5f39d392007-10-15 16:14:19 -04002279 struct extent_buffer *left = path->nodes[0];
2280 struct extent_buffer *right;
2281 struct extent_buffer *upper;
2282 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002283 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002284 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002285 int free_space;
2286 int push_space = 0;
2287 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002288 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002289 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002290 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002291 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002292 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002293 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002294 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002295
2296 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002297 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002298 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002299
Chris Mason00ec4c52007-02-24 12:47:20 -05002300 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002301 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002302 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002303
Chris Masonb9447ef82009-03-09 11:45:38 -04002304 btrfs_assert_tree_locked(path->nodes[1]);
Chris Masona2135012008-06-25 16:01:30 -04002305
Chris Masonca7a79a2008-05-12 12:59:19 -04002306 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002307 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002308 btrfs_set_lock_blocking(right);
2309
Chris Mason123abc82007-03-14 14:14:43 -04002310 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002311 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002312 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002313
Chris Mason5f39d392007-10-15 16:14:19 -04002314 /* cow and double check */
2315 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002316 slot + 1, &right);
Chris Mason925baed2008-06-25 16:01:30 -04002317 if (ret)
2318 goto out_unlock;
2319
Chris Mason5f39d392007-10-15 16:14:19 -04002320 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002321 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002322 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002323
2324 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002325 if (left_nritems == 0)
2326 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002327
Chris Mason34a38212007-11-07 13:31:03 -05002328 if (empty)
2329 nr = 0;
2330 else
2331 nr = 1;
2332
Zheng Yan31840ae2008-09-23 13:14:14 -04002333 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002334 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002335
Chris Mason34a38212007-11-07 13:31:03 -05002336 i = left_nritems - 1;
2337 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002338 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002339
Zheng Yan31840ae2008-09-23 13:14:14 -04002340 if (!empty && push_items > 0) {
2341 if (path->slots[0] > i)
2342 break;
2343 if (path->slots[0] == i) {
2344 int space = btrfs_leaf_free_space(root, left);
2345 if (space + push_space * 2 > free_space)
2346 break;
2347 }
2348 }
2349
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002351 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002352
2353 if (!left->map_token) {
2354 map_extent_buffer(left, (unsigned long)item,
2355 sizeof(struct btrfs_item),
2356 &left->map_token, &left->kaddr,
2357 &left->map_start, &left->map_len,
2358 KM_USER1);
2359 }
2360
2361 this_item_size = btrfs_item_size(left, item);
2362 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002363 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002364
Chris Mason00ec4c52007-02-24 12:47:20 -05002365 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002366 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002367 if (i == 0)
2368 break;
2369 i--;
Chris Masondb945352007-10-15 16:15:53 -04002370 }
2371 if (left->map_token) {
2372 unmap_extent_buffer(left, left->map_token, KM_USER1);
2373 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002374 }
Chris Mason5f39d392007-10-15 16:14:19 -04002375
Chris Mason925baed2008-06-25 16:01:30 -04002376 if (push_items == 0)
2377 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002378
Chris Mason34a38212007-11-07 13:31:03 -05002379 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002380 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002381
Chris Mason00ec4c52007-02-24 12:47:20 -05002382 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002383 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002384
Chris Mason5f39d392007-10-15 16:14:19 -04002385 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002386 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002387
Chris Mason00ec4c52007-02-24 12:47:20 -05002388 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002389 data_end = leaf_data_end(root, right);
2390 memmove_extent_buffer(right,
2391 btrfs_leaf_data(right) + data_end - push_space,
2392 btrfs_leaf_data(right) + data_end,
2393 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2394
Chris Mason00ec4c52007-02-24 12:47:20 -05002395 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002396 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002397 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2398 btrfs_leaf_data(left) + leaf_data_end(root, left),
2399 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002400
2401 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2402 btrfs_item_nr_offset(0),
2403 right_nritems * sizeof(struct btrfs_item));
2404
Chris Mason00ec4c52007-02-24 12:47:20 -05002405 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002406 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2407 btrfs_item_nr_offset(left_nritems - push_items),
2408 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002409
2410 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002411 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002412 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002413 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002414 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002415 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002416 if (!right->map_token) {
2417 map_extent_buffer(right, (unsigned long)item,
2418 sizeof(struct btrfs_item),
2419 &right->map_token, &right->kaddr,
2420 &right->map_start, &right->map_len,
2421 KM_USER1);
2422 }
2423 push_space -= btrfs_item_size(right, item);
2424 btrfs_set_item_offset(right, item, push_space);
2425 }
2426
2427 if (right->map_token) {
2428 unmap_extent_buffer(right, right->map_token, KM_USER1);
2429 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002430 }
Chris Mason7518a232007-03-12 12:01:18 -04002431 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002432 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002433
Chris Mason34a38212007-11-07 13:31:03 -05002434 if (left_nritems)
2435 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002436 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002437
Zheng Yan31840ae2008-09-23 13:14:14 -04002438 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2439 BUG_ON(ret);
2440
Chris Mason5f39d392007-10-15 16:14:19 -04002441 btrfs_item_key(right, &disk_key, 0);
2442 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002443 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002444
Chris Mason00ec4c52007-02-24 12:47:20 -05002445 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002446 if (path->slots[0] >= left_nritems) {
2447 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002448 if (btrfs_header_nritems(path->nodes[0]) == 0)
2449 clean_tree_block(trans, root, path->nodes[0]);
2450 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002451 free_extent_buffer(path->nodes[0]);
2452 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002453 path->slots[1] += 1;
2454 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002455 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002456 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002457 }
2458 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002459
2460out_unlock:
2461 btrfs_tree_unlock(right);
2462 free_extent_buffer(right);
2463 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002464}
Chris Mason925baed2008-06-25 16:01:30 -04002465
Chris Mason00ec4c52007-02-24 12:47:20 -05002466/*
Chris Mason74123bd2007-02-02 11:05:29 -05002467 * push some data in the path leaf to the left, trying to free up at
2468 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2469 */
Chris Masone089f052007-03-16 16:20:31 -04002470static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002471 *root, struct btrfs_path *path, int data_size,
2472 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473{
Chris Mason5f39d392007-10-15 16:14:19 -04002474 struct btrfs_disk_key disk_key;
2475 struct extent_buffer *right = path->nodes[0];
2476 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002477 int slot;
2478 int i;
2479 int free_space;
2480 int push_space = 0;
2481 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002482 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002483 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002484 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002485 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002486 int ret = 0;
2487 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002488 u32 this_item_size;
2489 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002490
2491 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002492 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002493 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002494 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002495 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002496
Chris Mason3685f792007-10-19 09:23:27 -04002497 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002498 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002499 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002500
Chris Masonb9447ef82009-03-09 11:45:38 -04002501 btrfs_assert_tree_locked(path->nodes[1]);
Chris Masona2135012008-06-25 16:01:30 -04002502
Chris Masonca7a79a2008-05-12 12:59:19 -04002503 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002504 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002505 btrfs_set_lock_blocking(left);
2506
Chris Mason123abc82007-03-14 14:14:43 -04002507 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002508 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002509 ret = 1;
2510 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002511 }
Chris Mason02217ed2007-03-02 16:08:05 -05002512
2513 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002514 ret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002515 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002516 if (ret) {
2517 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002518 ret = 1;
2519 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002520 }
Chris Mason3685f792007-10-19 09:23:27 -04002521
Chris Mason123abc82007-03-14 14:14:43 -04002522 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002523 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002524 ret = 1;
2525 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002526 }
2527
Chris Mason34a38212007-11-07 13:31:03 -05002528 if (empty)
2529 nr = right_nritems;
2530 else
2531 nr = right_nritems - 1;
2532
2533 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002534 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002535 if (!right->map_token) {
2536 map_extent_buffer(right, (unsigned long)item,
2537 sizeof(struct btrfs_item),
2538 &right->map_token, &right->kaddr,
2539 &right->map_start, &right->map_len,
2540 KM_USER1);
2541 }
2542
Zheng Yan31840ae2008-09-23 13:14:14 -04002543 if (!empty && push_items > 0) {
2544 if (path->slots[0] < i)
2545 break;
2546 if (path->slots[0] == i) {
2547 int space = btrfs_leaf_free_space(root, right);
2548 if (space + push_space * 2 > free_space)
2549 break;
2550 }
2551 }
2552
Chris Masonbe0e5c02007-01-26 15:51:26 -05002553 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002554 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002555
2556 this_item_size = btrfs_item_size(right, item);
2557 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002558 break;
Chris Masondb945352007-10-15 16:15:53 -04002559
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002561 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562 }
Chris Masondb945352007-10-15 16:15:53 -04002563
2564 if (right->map_token) {
2565 unmap_extent_buffer(right, right->map_token, KM_USER1);
2566 right->map_token = NULL;
2567 }
2568
Chris Masonbe0e5c02007-01-26 15:51:26 -05002569 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002570 ret = 1;
2571 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002572 }
Chris Mason34a38212007-11-07 13:31:03 -05002573 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002574 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002575
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002577 copy_extent_buffer(left, right,
2578 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2579 btrfs_item_nr_offset(0),
2580 push_items * sizeof(struct btrfs_item));
2581
Chris Mason123abc82007-03-14 14:14:43 -04002582 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002583 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002584
2585 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002586 leaf_data_end(root, left) - push_space,
2587 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002588 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002589 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002590 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002591 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002592
Chris Masondb945352007-10-15 16:15:53 -04002593 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002594 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002595 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002596
Chris Mason5f39d392007-10-15 16:14:19 -04002597 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002598 if (!left->map_token) {
2599 map_extent_buffer(left, (unsigned long)item,
2600 sizeof(struct btrfs_item),
2601 &left->map_token, &left->kaddr,
2602 &left->map_start, &left->map_len,
2603 KM_USER1);
2604 }
2605
Chris Mason5f39d392007-10-15 16:14:19 -04002606 ioff = btrfs_item_offset(left, item);
2607 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002608 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 }
Chris Mason5f39d392007-10-15 16:14:19 -04002610 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002611 if (left->map_token) {
2612 unmap_extent_buffer(left, left->map_token, KM_USER1);
2613 left->map_token = NULL;
2614 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002615
2616 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002617 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002618 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2619 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002620 WARN_ON(1);
2621 }
Chris Mason5f39d392007-10-15 16:14:19 -04002622
Chris Mason34a38212007-11-07 13:31:03 -05002623 if (push_items < right_nritems) {
2624 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2625 leaf_data_end(root, right);
2626 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2627 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2628 btrfs_leaf_data(right) +
2629 leaf_data_end(root, right), push_space);
2630
2631 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002632 btrfs_item_nr_offset(push_items),
2633 (btrfs_header_nritems(right) - push_items) *
2634 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002635 }
Yaneef1c492007-11-26 10:58:13 -05002636 right_nritems -= push_items;
2637 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002638 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002639 for (i = 0; i < right_nritems; i++) {
2640 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002641
2642 if (!right->map_token) {
2643 map_extent_buffer(right, (unsigned long)item,
2644 sizeof(struct btrfs_item),
2645 &right->map_token, &right->kaddr,
2646 &right->map_start, &right->map_len,
2647 KM_USER1);
2648 }
2649
2650 push_space = push_space - btrfs_item_size(right, item);
2651 btrfs_set_item_offset(right, item, push_space);
2652 }
2653 if (right->map_token) {
2654 unmap_extent_buffer(right, right->map_token, KM_USER1);
2655 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002656 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002657
Chris Mason5f39d392007-10-15 16:14:19 -04002658 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002659 if (right_nritems)
2660 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002661
Zheng Yan31840ae2008-09-23 13:14:14 -04002662 ret = btrfs_update_ref(trans, root, right, left,
2663 old_left_nritems, push_items);
2664 BUG_ON(ret);
2665
Chris Mason5f39d392007-10-15 16:14:19 -04002666 btrfs_item_key(right, &disk_key, 0);
2667 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002668 if (wret)
2669 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670
2671 /* then fixup the leaf pointer in the path */
2672 if (path->slots[0] < push_items) {
2673 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002674 if (btrfs_header_nritems(path->nodes[0]) == 0)
2675 clean_tree_block(trans, root, path->nodes[0]);
2676 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 free_extent_buffer(path->nodes[0]);
2678 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002679 path->slots[1] -= 1;
2680 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002681 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002682 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002683 path->slots[0] -= push_items;
2684 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002685 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002686 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002687out:
2688 btrfs_tree_unlock(left);
2689 free_extent_buffer(left);
2690 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002691}
2692
Chris Mason74123bd2007-02-02 11:05:29 -05002693/*
2694 * split the path's leaf in two, making sure there is at least data_size
2695 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002696 *
2697 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002698 */
Chris Masone02119d2008-09-05 16:13:11 -04002699static noinline int split_leaf(struct btrfs_trans_handle *trans,
2700 struct btrfs_root *root,
2701 struct btrfs_key *ins_key,
2702 struct btrfs_path *path, int data_size,
2703 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704{
Chris Mason5f39d392007-10-15 16:14:19 -04002705 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002706 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002707 int mid;
2708 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002709 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002710 int data_copy_size;
2711 int rt_data_off;
2712 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002713 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002714 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002715 int double_split;
2716 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002717 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002718
Chris Mason40689472007-03-17 14:29:23 -04002719 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002720 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002721 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002722 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002723 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002724 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002725 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002726 if (wret < 0)
2727 return wret;
2728 }
2729 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002730
Chris Mason3685f792007-10-19 09:23:27 -04002731 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002732 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002733 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002734 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002735
Chris Mason5c680ed2007-02-22 11:39:13 -05002736 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002737 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002738 if (ret)
2739 return ret;
2740 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002741again:
2742 double_split = 0;
2743 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002744 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002745 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002746 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002747
Chris Mason925baed2008-06-25 16:01:30 -04002748 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002749 path->nodes[1]->start,
2750 root->root_key.objectid,
2751 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002752 if (IS_ERR(right)) {
2753 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002754 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002755 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002756
Chris Mason5f39d392007-10-15 16:14:19 -04002757 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002758 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002759 btrfs_set_header_generation(right, trans->transid);
2760 btrfs_set_header_owner(right, root->root_key.objectid);
2761 btrfs_set_header_level(right, 0);
2762 write_extent_buffer(right, root->fs_info->fsid,
2763 (unsigned long)btrfs_header_fsid(right),
2764 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002765
2766 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2767 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2768 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002769 if (mid <= slot) {
2770 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002771 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002772 BTRFS_LEAF_DATA_SIZE(root)) {
2773 if (slot >= nritems) {
2774 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002775 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002776 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002777 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002778 path->slots[1] + 1, 1);
2779 if (wret)
2780 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002781
2782 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002783 free_extent_buffer(path->nodes[0]);
2784 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002785 path->slots[0] = 0;
2786 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002787 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002788 return ret;
2789 }
2790 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002791 if (mid != nritems &&
2792 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002793 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002794 double_split = 1;
2795 }
Chris Masond4dbff92007-04-04 14:08:15 -04002796 }
2797 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002798 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002799 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002800 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002801 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002802 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002803 wret = insert_ptr(trans, root, path,
2804 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002805 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002806 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002807 if (wret)
2808 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002809 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002810 free_extent_buffer(path->nodes[0]);
2811 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002812 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002813 if (path->slots[1] == 0) {
2814 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002815 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002816 if (wret)
2817 ret = wret;
2818 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002819 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002820 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002821 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002822 mid = 1;
2823 } else {
2824 mid = slot;
2825 if (mid != nritems &&
2826 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002827 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002828 double_split = 1;
2829 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002830 }
Chris Masond4dbff92007-04-04 14:08:15 -04002831 }
2832 }
Chris Mason5f39d392007-10-15 16:14:19 -04002833 nritems = nritems - mid;
2834 btrfs_set_header_nritems(right, nritems);
2835 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2836
2837 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2838 btrfs_item_nr_offset(mid),
2839 nritems * sizeof(struct btrfs_item));
2840
2841 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002842 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2843 data_copy_size, btrfs_leaf_data(l) +
2844 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002845
Chris Mason5f39d392007-10-15 16:14:19 -04002846 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2847 btrfs_item_end_nr(l, mid);
2848
2849 for (i = 0; i < nritems; i++) {
2850 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002851 u32 ioff;
2852
2853 if (!right->map_token) {
2854 map_extent_buffer(right, (unsigned long)item,
2855 sizeof(struct btrfs_item),
2856 &right->map_token, &right->kaddr,
2857 &right->map_start, &right->map_len,
2858 KM_USER1);
2859 }
2860
2861 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002862 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002863 }
Chris Mason74123bd2007-02-02 11:05:29 -05002864
Chris Masondb945352007-10-15 16:15:53 -04002865 if (right->map_token) {
2866 unmap_extent_buffer(right, right->map_token, KM_USER1);
2867 right->map_token = NULL;
2868 }
2869
Chris Mason5f39d392007-10-15 16:14:19 -04002870 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002871 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002872 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002873 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2874 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002875 if (wret)
2876 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002877
2878 btrfs_mark_buffer_dirty(right);
2879 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002880 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002881
Zheng Yan31840ae2008-09-23 13:14:14 -04002882 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2883 BUG_ON(ret);
2884
Chris Masonbe0e5c02007-01-26 15:51:26 -05002885 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002886 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002887 free_extent_buffer(path->nodes[0]);
2888 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002889 path->slots[0] -= mid;
2890 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002891 } else {
2892 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002893 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002894 }
Chris Mason5f39d392007-10-15 16:14:19 -04002895
Chris Masoneb60cea2007-02-02 09:18:22 -05002896 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002897
Chris Masoncc0c5532007-10-25 15:42:57 -04002898 if (double_split) {
2899 BUG_ON(num_doubles != 0);
2900 num_doubles++;
2901 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002902 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002903 return ret;
2904}
2905
Chris Masond352ac62008-09-29 15:18:18 -04002906/*
Chris Mason459931e2008-12-10 09:10:46 -05002907 * This function splits a single item into two items,
2908 * giving 'new_key' to the new item and splitting the
2909 * old one at split_offset (from the start of the item).
2910 *
2911 * The path may be released by this operation. After
2912 * the split, the path is pointing to the old item. The
2913 * new item is going to be in the same node as the old one.
2914 *
2915 * Note, the item being split must be smaller enough to live alone on
2916 * a tree block with room for one extra struct btrfs_item
2917 *
2918 * This allows us to split the item in place, keeping a lock on the
2919 * leaf the entire time.
2920 */
2921int btrfs_split_item(struct btrfs_trans_handle *trans,
2922 struct btrfs_root *root,
2923 struct btrfs_path *path,
2924 struct btrfs_key *new_key,
2925 unsigned long split_offset)
2926{
2927 u32 item_size;
2928 struct extent_buffer *leaf;
2929 struct btrfs_key orig_key;
2930 struct btrfs_item *item;
2931 struct btrfs_item *new_item;
2932 int ret = 0;
2933 int slot;
2934 u32 nritems;
2935 u32 orig_offset;
2936 struct btrfs_disk_key disk_key;
2937 char *buf;
2938
2939 leaf = path->nodes[0];
2940 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2941 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2942 goto split;
2943
2944 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2945 btrfs_release_path(root, path);
2946
2947 path->search_for_split = 1;
2948 path->keep_locks = 1;
2949
2950 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2951 path->search_for_split = 0;
2952
2953 /* if our item isn't there or got smaller, return now */
2954 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2955 path->slots[0])) {
2956 path->keep_locks = 0;
2957 return -EAGAIN;
2958 }
2959
Yan Zheng87b29b22008-12-17 10:21:48 -05002960 ret = split_leaf(trans, root, &orig_key, path,
2961 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05002962 path->keep_locks = 0;
2963 BUG_ON(ret);
2964
Chris Masonb4ce94d2009-02-04 09:25:08 -05002965 /*
2966 * make sure any changes to the path from split_leaf leave it
2967 * in a blocking state
2968 */
2969 btrfs_set_path_blocking(path);
2970
Chris Mason459931e2008-12-10 09:10:46 -05002971 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05002972 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05002973
2974split:
2975 item = btrfs_item_nr(leaf, path->slots[0]);
2976 orig_offset = btrfs_item_offset(leaf, item);
2977 item_size = btrfs_item_size(leaf, item);
2978
2979
2980 buf = kmalloc(item_size, GFP_NOFS);
2981 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2982 path->slots[0]), item_size);
2983 slot = path->slots[0] + 1;
2984 leaf = path->nodes[0];
2985
2986 nritems = btrfs_header_nritems(leaf);
2987
2988 if (slot != nritems) {
2989 /* shift the items */
2990 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2991 btrfs_item_nr_offset(slot),
2992 (nritems - slot) * sizeof(struct btrfs_item));
2993
2994 }
2995
2996 btrfs_cpu_key_to_disk(&disk_key, new_key);
2997 btrfs_set_item_key(leaf, &disk_key, slot);
2998
2999 new_item = btrfs_item_nr(leaf, slot);
3000
3001 btrfs_set_item_offset(leaf, new_item, orig_offset);
3002 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3003
3004 btrfs_set_item_offset(leaf, item,
3005 orig_offset + item_size - split_offset);
3006 btrfs_set_item_size(leaf, item, split_offset);
3007
3008 btrfs_set_header_nritems(leaf, nritems + 1);
3009
3010 /* write the data for the start of the original item */
3011 write_extent_buffer(leaf, buf,
3012 btrfs_item_ptr_offset(leaf, path->slots[0]),
3013 split_offset);
3014
3015 /* write the data for the new item */
3016 write_extent_buffer(leaf, buf + split_offset,
3017 btrfs_item_ptr_offset(leaf, slot),
3018 item_size - split_offset);
3019 btrfs_mark_buffer_dirty(leaf);
3020
3021 ret = 0;
3022 if (btrfs_leaf_free_space(root, leaf) < 0) {
3023 btrfs_print_leaf(root, leaf);
3024 BUG();
3025 }
3026 kfree(buf);
3027 return ret;
3028}
3029
3030/*
Chris Masond352ac62008-09-29 15:18:18 -04003031 * make the item pointed to by the path smaller. new_size indicates
3032 * how small to make it, and from_end tells us if we just chop bytes
3033 * off the end of the item or if we shift the item to chop bytes off
3034 * the front.
3035 */
Chris Masonb18c6682007-04-17 13:26:50 -04003036int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3037 struct btrfs_root *root,
3038 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003039 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003040{
3041 int ret = 0;
3042 int slot;
3043 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003044 struct extent_buffer *leaf;
3045 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003046 u32 nritems;
3047 unsigned int data_end;
3048 unsigned int old_data_start;
3049 unsigned int old_size;
3050 unsigned int size_diff;
3051 int i;
3052
3053 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003054 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003055 slot = path->slots[0];
3056
3057 old_size = btrfs_item_size_nr(leaf, slot);
3058 if (old_size == new_size)
3059 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003060
Chris Mason5f39d392007-10-15 16:14:19 -04003061 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003062 data_end = leaf_data_end(root, leaf);
3063
Chris Mason5f39d392007-10-15 16:14:19 -04003064 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003065
Chris Masonb18c6682007-04-17 13:26:50 -04003066 size_diff = old_size - new_size;
3067
3068 BUG_ON(slot < 0);
3069 BUG_ON(slot >= nritems);
3070
3071 /*
3072 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3073 */
3074 /* first correct the data pointers */
3075 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003076 u32 ioff;
3077 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003078
3079 if (!leaf->map_token) {
3080 map_extent_buffer(leaf, (unsigned long)item,
3081 sizeof(struct btrfs_item),
3082 &leaf->map_token, &leaf->kaddr,
3083 &leaf->map_start, &leaf->map_len,
3084 KM_USER1);
3085 }
3086
Chris Mason5f39d392007-10-15 16:14:19 -04003087 ioff = btrfs_item_offset(leaf, item);
3088 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003089 }
Chris Masondb945352007-10-15 16:15:53 -04003090
3091 if (leaf->map_token) {
3092 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3093 leaf->map_token = NULL;
3094 }
3095
Chris Masonb18c6682007-04-17 13:26:50 -04003096 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003097 if (from_end) {
3098 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3099 data_end + size_diff, btrfs_leaf_data(leaf) +
3100 data_end, old_data_start + new_size - data_end);
3101 } else {
3102 struct btrfs_disk_key disk_key;
3103 u64 offset;
3104
3105 btrfs_item_key(leaf, &disk_key, slot);
3106
3107 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3108 unsigned long ptr;
3109 struct btrfs_file_extent_item *fi;
3110
3111 fi = btrfs_item_ptr(leaf, slot,
3112 struct btrfs_file_extent_item);
3113 fi = (struct btrfs_file_extent_item *)(
3114 (unsigned long)fi - size_diff);
3115
3116 if (btrfs_file_extent_type(leaf, fi) ==
3117 BTRFS_FILE_EXTENT_INLINE) {
3118 ptr = btrfs_item_ptr_offset(leaf, slot);
3119 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003120 (unsigned long)fi,
3121 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003122 disk_bytenr));
3123 }
3124 }
3125
3126 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3127 data_end + size_diff, btrfs_leaf_data(leaf) +
3128 data_end, old_data_start - data_end);
3129
3130 offset = btrfs_disk_key_offset(&disk_key);
3131 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3132 btrfs_set_item_key(leaf, &disk_key, slot);
3133 if (slot == 0)
3134 fixup_low_keys(trans, root, path, &disk_key, 1);
3135 }
Chris Mason5f39d392007-10-15 16:14:19 -04003136
3137 item = btrfs_item_nr(leaf, slot);
3138 btrfs_set_item_size(leaf, item, new_size);
3139 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003140
3141 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003142 if (btrfs_leaf_free_space(root, leaf) < 0) {
3143 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003144 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003145 }
Chris Masonb18c6682007-04-17 13:26:50 -04003146 return ret;
3147}
3148
Chris Masond352ac62008-09-29 15:18:18 -04003149/*
3150 * make the item pointed to by the path bigger, data_size is the new size.
3151 */
Chris Mason5f39d392007-10-15 16:14:19 -04003152int btrfs_extend_item(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root, struct btrfs_path *path,
3154 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003155{
3156 int ret = 0;
3157 int slot;
3158 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003159 struct extent_buffer *leaf;
3160 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003161 u32 nritems;
3162 unsigned int data_end;
3163 unsigned int old_data;
3164 unsigned int old_size;
3165 int i;
3166
3167 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003168 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003169
Chris Mason5f39d392007-10-15 16:14:19 -04003170 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003171 data_end = leaf_data_end(root, leaf);
3172
Chris Mason5f39d392007-10-15 16:14:19 -04003173 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3174 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003175 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003176 }
Chris Mason6567e832007-04-16 09:22:45 -04003177 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003178 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003179
3180 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003181 if (slot >= nritems) {
3182 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003183 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3184 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003185 BUG_ON(1);
3186 }
Chris Mason6567e832007-04-16 09:22:45 -04003187
3188 /*
3189 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3190 */
3191 /* first correct the data pointers */
3192 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003193 u32 ioff;
3194 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003195
3196 if (!leaf->map_token) {
3197 map_extent_buffer(leaf, (unsigned long)item,
3198 sizeof(struct btrfs_item),
3199 &leaf->map_token, &leaf->kaddr,
3200 &leaf->map_start, &leaf->map_len,
3201 KM_USER1);
3202 }
Chris Mason5f39d392007-10-15 16:14:19 -04003203 ioff = btrfs_item_offset(leaf, item);
3204 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003205 }
Chris Mason5f39d392007-10-15 16:14:19 -04003206
Chris Masondb945352007-10-15 16:15:53 -04003207 if (leaf->map_token) {
3208 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3209 leaf->map_token = NULL;
3210 }
3211
Chris Mason6567e832007-04-16 09:22:45 -04003212 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003213 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003214 data_end - data_size, btrfs_leaf_data(leaf) +
3215 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003216
Chris Mason6567e832007-04-16 09:22:45 -04003217 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003218 old_size = btrfs_item_size_nr(leaf, slot);
3219 item = btrfs_item_nr(leaf, slot);
3220 btrfs_set_item_size(leaf, item, old_size + data_size);
3221 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003222
3223 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003224 if (btrfs_leaf_free_space(root, leaf) < 0) {
3225 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003226 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003227 }
Chris Mason6567e832007-04-16 09:22:45 -04003228 return ret;
3229}
3230
Chris Mason74123bd2007-02-02 11:05:29 -05003231/*
Chris Masond352ac62008-09-29 15:18:18 -04003232 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003233 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003234 * Returns the number of keys that were inserted.
3235 */
3236int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3237 struct btrfs_root *root,
3238 struct btrfs_path *path,
3239 struct btrfs_key *cpu_key, u32 *data_size,
3240 int nr)
3241{
3242 struct extent_buffer *leaf;
3243 struct btrfs_item *item;
3244 int ret = 0;
3245 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003246 int i;
3247 u32 nritems;
3248 u32 total_data = 0;
3249 u32 total_size = 0;
3250 unsigned int data_end;
3251 struct btrfs_disk_key disk_key;
3252 struct btrfs_key found_key;
3253
Yan Zheng87b29b22008-12-17 10:21:48 -05003254 for (i = 0; i < nr; i++) {
3255 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3256 BTRFS_LEAF_DATA_SIZE(root)) {
3257 break;
3258 nr = i;
3259 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003260 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003261 total_size += data_size[i] + sizeof(struct btrfs_item);
3262 }
3263 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003264
Josef Bacikf3465ca2008-11-12 14:19:50 -05003265 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3266 if (ret == 0)
3267 return -EEXIST;
3268 if (ret < 0)
3269 goto out;
3270
Josef Bacikf3465ca2008-11-12 14:19:50 -05003271 leaf = path->nodes[0];
3272
3273 nritems = btrfs_header_nritems(leaf);
3274 data_end = leaf_data_end(root, leaf);
3275
3276 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3277 for (i = nr; i >= 0; i--) {
3278 total_data -= data_size[i];
3279 total_size -= data_size[i] + sizeof(struct btrfs_item);
3280 if (total_size < btrfs_leaf_free_space(root, leaf))
3281 break;
3282 }
3283 nr = i;
3284 }
3285
3286 slot = path->slots[0];
3287 BUG_ON(slot < 0);
3288
3289 if (slot != nritems) {
3290 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3291
3292 item = btrfs_item_nr(leaf, slot);
3293 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3294
3295 /* figure out how many keys we can insert in here */
3296 total_data = data_size[0];
3297 for (i = 1; i < nr; i++) {
3298 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3299 break;
3300 total_data += data_size[i];
3301 }
3302 nr = i;
3303
3304 if (old_data < data_end) {
3305 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003306 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003307 slot, old_data, data_end);
3308 BUG_ON(1);
3309 }
3310 /*
3311 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3312 */
3313 /* first correct the data pointers */
3314 WARN_ON(leaf->map_token);
3315 for (i = slot; i < nritems; i++) {
3316 u32 ioff;
3317
3318 item = btrfs_item_nr(leaf, i);
3319 if (!leaf->map_token) {
3320 map_extent_buffer(leaf, (unsigned long)item,
3321 sizeof(struct btrfs_item),
3322 &leaf->map_token, &leaf->kaddr,
3323 &leaf->map_start, &leaf->map_len,
3324 KM_USER1);
3325 }
3326
3327 ioff = btrfs_item_offset(leaf, item);
3328 btrfs_set_item_offset(leaf, item, ioff - total_data);
3329 }
3330 if (leaf->map_token) {
3331 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3332 leaf->map_token = NULL;
3333 }
3334
3335 /* shift the items */
3336 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3337 btrfs_item_nr_offset(slot),
3338 (nritems - slot) * sizeof(struct btrfs_item));
3339
3340 /* shift the data */
3341 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3342 data_end - total_data, btrfs_leaf_data(leaf) +
3343 data_end, old_data - data_end);
3344 data_end = old_data;
3345 } else {
3346 /*
3347 * this sucks but it has to be done, if we are inserting at
3348 * the end of the leaf only insert 1 of the items, since we
3349 * have no way of knowing whats on the next leaf and we'd have
3350 * to drop our current locks to figure it out
3351 */
3352 nr = 1;
3353 }
3354
3355 /* setup the item for the new data */
3356 for (i = 0; i < nr; i++) {
3357 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3358 btrfs_set_item_key(leaf, &disk_key, slot + i);
3359 item = btrfs_item_nr(leaf, slot + i);
3360 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3361 data_end -= data_size[i];
3362 btrfs_set_item_size(leaf, item, data_size[i]);
3363 }
3364 btrfs_set_header_nritems(leaf, nritems + nr);
3365 btrfs_mark_buffer_dirty(leaf);
3366
3367 ret = 0;
3368 if (slot == 0) {
3369 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3370 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3371 }
3372
3373 if (btrfs_leaf_free_space(root, leaf) < 0) {
3374 btrfs_print_leaf(root, leaf);
3375 BUG();
3376 }
3377out:
3378 if (!ret)
3379 ret = nr;
3380 return ret;
3381}
3382
3383/*
3384 * Given a key and some data, insert items into the tree.
3385 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003386 */
Chris Mason9c583092008-01-29 15:15:18 -05003387int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003388 struct btrfs_root *root,
3389 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003390 struct btrfs_key *cpu_key, u32 *data_size,
3391 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003392{
Chris Mason5f39d392007-10-15 16:14:19 -04003393 struct extent_buffer *leaf;
3394 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003395 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003396 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003397 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003398 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003399 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003400 u32 total_size = 0;
3401 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003402 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003403 struct btrfs_disk_key disk_key;
3404
Chris Masond3977122009-01-05 21:25:51 -05003405 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003406 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003407
Josef Bacik7b128762008-07-24 12:17:14 -04003408 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003409 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003410 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003411 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003412 if (ret < 0)
3413 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003414
Chris Mason62e27492007-03-15 12:56:47 -04003415 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003416 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003417
Chris Mason5f39d392007-10-15 16:14:19 -04003418 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003419 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003420
Chris Masonf25956c2008-09-12 15:32:53 -04003421 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003422 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003423 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003424 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003425 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003426 }
Chris Mason5f39d392007-10-15 16:14:19 -04003427
Chris Mason62e27492007-03-15 12:56:47 -04003428 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003429 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003430
Chris Masonbe0e5c02007-01-26 15:51:26 -05003431 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003432 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003433
Chris Mason5f39d392007-10-15 16:14:19 -04003434 if (old_data < data_end) {
3435 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003436 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003437 slot, old_data, data_end);
3438 BUG_ON(1);
3439 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003440 /*
3441 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3442 */
3443 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003444 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003445 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003446 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003447
Chris Mason5f39d392007-10-15 16:14:19 -04003448 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003449 if (!leaf->map_token) {
3450 map_extent_buffer(leaf, (unsigned long)item,
3451 sizeof(struct btrfs_item),
3452 &leaf->map_token, &leaf->kaddr,
3453 &leaf->map_start, &leaf->map_len,
3454 KM_USER1);
3455 }
3456
Chris Mason5f39d392007-10-15 16:14:19 -04003457 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003458 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003459 }
Chris Masondb945352007-10-15 16:15:53 -04003460 if (leaf->map_token) {
3461 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3462 leaf->map_token = NULL;
3463 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003464
3465 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003466 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003467 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003468 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003469
3470 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003471 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003472 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003473 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003474 data_end = old_data;
3475 }
Chris Mason5f39d392007-10-15 16:14:19 -04003476
Chris Mason62e27492007-03-15 12:56:47 -04003477 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003478 for (i = 0; i < nr; i++) {
3479 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3480 btrfs_set_item_key(leaf, &disk_key, slot + i);
3481 item = btrfs_item_nr(leaf, slot + i);
3482 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3483 data_end -= data_size[i];
3484 btrfs_set_item_size(leaf, item, data_size[i]);
3485 }
3486 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003487 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003488
3489 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003490 if (slot == 0) {
3491 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003492 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003493 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003494
Chris Mason5f39d392007-10-15 16:14:19 -04003495 if (btrfs_leaf_free_space(root, leaf) < 0) {
3496 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003497 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003498 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003499out:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003500 btrfs_unlock_up_safe(path, 1);
Chris Mason62e27492007-03-15 12:56:47 -04003501 return ret;
3502}
3503
3504/*
3505 * Given a key and some data, insert an item into the tree.
3506 * This does all the path init required, making room in the tree if needed.
3507 */
Chris Masone089f052007-03-16 16:20:31 -04003508int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3509 *root, struct btrfs_key *cpu_key, void *data, u32
3510 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003511{
3512 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003513 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003514 struct extent_buffer *leaf;
3515 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003516
Chris Mason2c90e5d2007-04-02 10:50:19 -04003517 path = btrfs_alloc_path();
3518 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003519 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003520 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003521 leaf = path->nodes[0];
3522 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3523 write_extent_buffer(leaf, data, ptr, data_size);
3524 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003525 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003526 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003527 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003528}
3529
Chris Mason74123bd2007-02-02 11:05:29 -05003530/*
Chris Mason5de08d72007-02-24 06:24:44 -05003531 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003532 *
Chris Masond352ac62008-09-29 15:18:18 -04003533 * the tree should have been previously balanced so the deletion does not
3534 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003535 */
Chris Masone089f052007-03-16 16:20:31 -04003536static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3537 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538{
Chris Mason5f39d392007-10-15 16:14:19 -04003539 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003540 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003541 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003542 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003543
Chris Mason5f39d392007-10-15 16:14:19 -04003544 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003545 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003546 memmove_extent_buffer(parent,
3547 btrfs_node_key_ptr_offset(slot),
3548 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003549 sizeof(struct btrfs_key_ptr) *
3550 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003551 }
Chris Mason7518a232007-03-12 12:01:18 -04003552 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003553 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003554 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003555 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003556 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003557 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003558 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003559 struct btrfs_disk_key disk_key;
3560
3561 btrfs_node_key(parent, &disk_key, 0);
3562 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003563 if (wret)
3564 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003565 }
Chris Masond6025572007-03-30 14:27:56 -04003566 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003567 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003568}
3569
Chris Mason74123bd2007-02-02 11:05:29 -05003570/*
Chris Mason323ac952008-10-01 19:05:46 -04003571 * a helper function to delete the leaf pointed to by path->slots[1] and
3572 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3573 * already know it, it is faster to have them pass it down than to
3574 * read it out of the node again.
3575 *
3576 * This deletes the pointer in path->nodes[1] and frees the leaf
3577 * block extent. zero is returned if it all worked out, < 0 otherwise.
3578 *
3579 * The path must have already been setup for deleting the leaf, including
3580 * all the proper balancing. path->nodes[1] must be locked.
3581 */
3582noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3583 struct btrfs_root *root,
3584 struct btrfs_path *path, u64 bytenr)
3585{
3586 int ret;
3587 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003588 u64 parent_start = path->nodes[1]->start;
3589 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003590
3591 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3592 if (ret)
3593 return ret;
3594
Chris Mason4d081c42009-02-04 09:31:28 -05003595 /*
3596 * btrfs_free_extent is expensive, we want to make sure we
3597 * aren't holding any locks when we call it
3598 */
3599 btrfs_unlock_up_safe(path, 0);
3600
Chris Mason323ac952008-10-01 19:05:46 -04003601 ret = btrfs_free_extent(trans, root, bytenr,
3602 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003603 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003604 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003605 return ret;
3606}
3607/*
Chris Mason74123bd2007-02-02 11:05:29 -05003608 * delete the item at the leaf level in path. If that empties
3609 * the leaf, remove it from the tree
3610 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003611int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3612 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003613{
Chris Mason5f39d392007-10-15 16:14:19 -04003614 struct extent_buffer *leaf;
3615 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003616 int last_off;
3617 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003618 int ret = 0;
3619 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003620 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003621 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003622
Chris Mason5f39d392007-10-15 16:14:19 -04003623 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003624 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3625
3626 for (i = 0; i < nr; i++)
3627 dsize += btrfs_item_size_nr(leaf, slot + i);
3628
Chris Mason5f39d392007-10-15 16:14:19 -04003629 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003630
Chris Mason85e21ba2008-01-29 15:11:36 -05003631 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003632 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
3634 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003635 data_end + dsize,
3636 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003637 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003638
Chris Mason85e21ba2008-01-29 15:11:36 -05003639 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003640 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003641
Chris Mason5f39d392007-10-15 16:14:19 -04003642 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003643 if (!leaf->map_token) {
3644 map_extent_buffer(leaf, (unsigned long)item,
3645 sizeof(struct btrfs_item),
3646 &leaf->map_token, &leaf->kaddr,
3647 &leaf->map_start, &leaf->map_len,
3648 KM_USER1);
3649 }
Chris Mason5f39d392007-10-15 16:14:19 -04003650 ioff = btrfs_item_offset(leaf, item);
3651 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003652 }
Chris Masondb945352007-10-15 16:15:53 -04003653
3654 if (leaf->map_token) {
3655 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3656 leaf->map_token = NULL;
3657 }
3658
Chris Mason5f39d392007-10-15 16:14:19 -04003659 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003660 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003661 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003662 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003663 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003664 btrfs_set_header_nritems(leaf, nritems - nr);
3665 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003666
Chris Mason74123bd2007-02-02 11:05:29 -05003667 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003668 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003669 if (leaf == root->node) {
3670 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003671 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003672 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3673 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003674 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003675 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003676 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003677 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003678 struct btrfs_disk_key disk_key;
3679
3680 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003681 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003682 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003683 if (wret)
3684 ret = wret;
3685 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003686
Chris Mason74123bd2007-02-02 11:05:29 -05003687 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003688 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003689 /* push_leaf_left fixes the path.
3690 * make sure the path still points to our leaf
3691 * for possible call to del_ptr below
3692 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003693 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003694 extent_buffer_get(leaf);
3695
Chris Mason85e21ba2008-01-29 15:11:36 -05003696 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003697 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003698 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003699
3700 if (path->nodes[0] == leaf &&
3701 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003702 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003703 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003704 ret = wret;
3705 }
Chris Mason5f39d392007-10-15 16:14:19 -04003706
3707 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003708 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003709 ret = btrfs_del_leaf(trans, root, path,
3710 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003711 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003712 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003713 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003714 /* if we're still in the path, make sure
3715 * we're dirty. Otherwise, one of the
3716 * push_leaf functions must have already
3717 * dirtied this buffer
3718 */
3719 if (path->nodes[0] == leaf)
3720 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003721 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003722 }
Chris Masond5719762007-03-23 10:01:08 -04003723 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003724 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003725 }
3726 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003727 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003728}
3729
Chris Mason97571fd2007-02-24 13:39:08 -05003730/*
Chris Mason925baed2008-06-25 16:01:30 -04003731 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003732 * returns 0 if it found something or 1 if there are no lesser leaves.
3733 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003734 *
3735 * This may release the path, and so you may lose any locks held at the
3736 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003737 */
3738int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3739{
Chris Mason925baed2008-06-25 16:01:30 -04003740 struct btrfs_key key;
3741 struct btrfs_disk_key found_key;
3742 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003743
Chris Mason925baed2008-06-25 16:01:30 -04003744 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003745
Chris Mason925baed2008-06-25 16:01:30 -04003746 if (key.offset > 0)
3747 key.offset--;
3748 else if (key.type > 0)
3749 key.type--;
3750 else if (key.objectid > 0)
3751 key.objectid--;
3752 else
3753 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003754
Chris Mason925baed2008-06-25 16:01:30 -04003755 btrfs_release_path(root, path);
3756 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3757 if (ret < 0)
3758 return ret;
3759 btrfs_item_key(path->nodes[0], &found_key, 0);
3760 ret = comp_keys(&found_key, &key);
3761 if (ret < 0)
3762 return 0;
3763 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003764}
3765
Chris Mason3f157a22008-06-25 16:01:31 -04003766/*
3767 * A helper function to walk down the tree starting at min_key, and looking
3768 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003769 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003770 *
3771 * This does not cow, but it does stuff the starting key it finds back
3772 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3773 * key and get a writable path.
3774 *
3775 * This does lock as it descends, and path->keep_locks should be set
3776 * to 1 by the caller.
3777 *
3778 * This honors path->lowest_level to prevent descent past a given level
3779 * of the tree.
3780 *
Chris Masond352ac62008-09-29 15:18:18 -04003781 * min_trans indicates the oldest transaction that you are interested
3782 * in walking through. Any nodes or leaves older than min_trans are
3783 * skipped over (without reading them).
3784 *
Chris Mason3f157a22008-06-25 16:01:31 -04003785 * returns zero if something useful was found, < 0 on error and 1 if there
3786 * was nothing in the tree that matched the search criteria.
3787 */
3788int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003789 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003790 struct btrfs_path *path, int cache_only,
3791 u64 min_trans)
3792{
3793 struct extent_buffer *cur;
3794 struct btrfs_key found_key;
3795 int slot;
Yan96524802008-07-24 12:19:49 -04003796 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003797 u32 nritems;
3798 int level;
3799 int ret = 1;
3800
Chris Mason934d3752008-12-08 16:43:10 -05003801 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003802again:
3803 cur = btrfs_lock_root_node(root);
3804 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003805 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003806 path->nodes[level] = cur;
3807 path->locks[level] = 1;
3808
3809 if (btrfs_header_generation(cur) < min_trans) {
3810 ret = 1;
3811 goto out;
3812 }
Chris Masond3977122009-01-05 21:25:51 -05003813 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003814 nritems = btrfs_header_nritems(cur);
3815 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003816 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003817
Chris Mason323ac952008-10-01 19:05:46 -04003818 /* at the lowest level, we're done, setup the path and exit */
3819 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003820 if (slot >= nritems)
3821 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003822 ret = 0;
3823 path->slots[level] = slot;
3824 btrfs_item_key_to_cpu(cur, &found_key, slot);
3825 goto out;
3826 }
Yan96524802008-07-24 12:19:49 -04003827 if (sret && slot > 0)
3828 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003829 /*
3830 * check this node pointer against the cache_only and
3831 * min_trans parameters. If it isn't in cache or is too
3832 * old, skip to the next one.
3833 */
Chris Masond3977122009-01-05 21:25:51 -05003834 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003835 u64 blockptr;
3836 u64 gen;
3837 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003838 struct btrfs_disk_key disk_key;
3839
Chris Mason3f157a22008-06-25 16:01:31 -04003840 blockptr = btrfs_node_blockptr(cur, slot);
3841 gen = btrfs_node_ptr_generation(cur, slot);
3842 if (gen < min_trans) {
3843 slot++;
3844 continue;
3845 }
3846 if (!cache_only)
3847 break;
3848
Chris Masone02119d2008-09-05 16:13:11 -04003849 if (max_key) {
3850 btrfs_node_key(cur, &disk_key, slot);
3851 if (comp_keys(&disk_key, max_key) >= 0) {
3852 ret = 1;
3853 goto out;
3854 }
3855 }
3856
Chris Mason3f157a22008-06-25 16:01:31 -04003857 tmp = btrfs_find_tree_block(root, blockptr,
3858 btrfs_level_size(root, level - 1));
3859
3860 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3861 free_extent_buffer(tmp);
3862 break;
3863 }
3864 if (tmp)
3865 free_extent_buffer(tmp);
3866 slot++;
3867 }
Chris Masone02119d2008-09-05 16:13:11 -04003868find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003869 /*
3870 * we didn't find a candidate key in this node, walk forward
3871 * and find another one
3872 */
3873 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003874 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003875 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003876 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003877 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003878 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003879 btrfs_release_path(root, path);
3880 goto again;
3881 } else {
3882 goto out;
3883 }
3884 }
3885 /* save our key for returning back */
3886 btrfs_node_key_to_cpu(cur, &found_key, slot);
3887 path->slots[level] = slot;
3888 if (level == path->lowest_level) {
3889 ret = 0;
3890 unlock_up(path, level, 1);
3891 goto out;
3892 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003893 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003894 cur = read_node_slot(root, cur, slot);
3895
3896 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003897
Chris Mason3f157a22008-06-25 16:01:31 -04003898 path->locks[level - 1] = 1;
3899 path->nodes[level - 1] = cur;
3900 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05003901 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04003902 }
3903out:
3904 if (ret == 0)
3905 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05003906 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003907 return ret;
3908}
3909
3910/*
3911 * this is similar to btrfs_next_leaf, but does not try to preserve
3912 * and fixup the path. It looks for and returns the next key in the
3913 * tree based on the current path and the cache_only and min_trans
3914 * parameters.
3915 *
3916 * 0 is returned if another key is found, < 0 if there are any errors
3917 * and 1 is returned if there are no higher keys in the tree
3918 *
3919 * path->keep_locks should be set to 1 on the search made before
3920 * calling this function.
3921 */
Chris Masone7a84562008-06-25 16:01:31 -04003922int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003923 struct btrfs_key *key, int lowest_level,
3924 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003925{
3926 int level = lowest_level;
3927 int slot;
3928 struct extent_buffer *c;
3929
Chris Mason934d3752008-12-08 16:43:10 -05003930 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003931 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003932 if (!path->nodes[level])
3933 return 1;
3934
3935 slot = path->slots[level] + 1;
3936 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003937next:
Chris Masone7a84562008-06-25 16:01:31 -04003938 if (slot >= btrfs_header_nritems(c)) {
3939 level++;
Chris Masond3977122009-01-05 21:25:51 -05003940 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04003941 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04003942 continue;
3943 }
3944 if (level == 0)
3945 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003946 else {
3947 u64 blockptr = btrfs_node_blockptr(c, slot);
3948 u64 gen = btrfs_node_ptr_generation(c, slot);
3949
3950 if (cache_only) {
3951 struct extent_buffer *cur;
3952 cur = btrfs_find_tree_block(root, blockptr,
3953 btrfs_level_size(root, level - 1));
3954 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3955 slot++;
3956 if (cur)
3957 free_extent_buffer(cur);
3958 goto next;
3959 }
3960 free_extent_buffer(cur);
3961 }
3962 if (gen < min_trans) {
3963 slot++;
3964 goto next;
3965 }
Chris Masone7a84562008-06-25 16:01:31 -04003966 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003967 }
Chris Masone7a84562008-06-25 16:01:31 -04003968 return 0;
3969 }
3970 return 1;
3971}
3972
Chris Mason7bb86312007-12-11 09:25:06 -05003973/*
Chris Mason925baed2008-06-25 16:01:30 -04003974 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003975 * returns 0 if it found something or 1 if there are no greater leaves.
3976 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003977 */
Chris Mason234b63a2007-03-13 10:46:10 -04003978int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003979{
3980 int slot;
3981 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003982 struct extent_buffer *c;
3983 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003984 struct btrfs_key key;
3985 u32 nritems;
3986 int ret;
3987
3988 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05003989 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04003990 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003991
3992 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3993
Chris Mason925baed2008-06-25 16:01:30 -04003994 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003995 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003996 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3997 path->keep_locks = 0;
3998
3999 if (ret < 0)
4000 return ret;
4001
Chris Masonb4ce94d2009-02-04 09:25:08 -05004002 btrfs_set_path_blocking(path);
Chris Masona2135012008-06-25 16:01:30 -04004003 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004004 /*
4005 * by releasing the path above we dropped all our locks. A balance
4006 * could have added more items next to the key that used to be
4007 * at the very end of the block. So, check again here and
4008 * advance the path if there are now more items available.
4009 */
Chris Masona2135012008-06-25 16:01:30 -04004010 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004011 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04004012 goto done;
4013 }
Chris Masond97e63b2007-02-20 16:40:44 -05004014
Chris Masond3977122009-01-05 21:25:51 -05004015 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05004016 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05004017 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004018
Chris Masond97e63b2007-02-20 16:40:44 -05004019 slot = path->slots[level] + 1;
4020 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004021 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004022 level++;
Chris Masond3977122009-01-05 21:25:51 -05004023 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05004024 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004025 continue;
4026 }
Chris Mason5f39d392007-10-15 16:14:19 -04004027
Chris Mason925baed2008-06-25 16:01:30 -04004028 if (next) {
4029 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004030 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004031 }
Chris Mason5f39d392007-10-15 16:14:19 -04004032
Chris Masonb4ce94d2009-02-04 09:25:08 -05004033 /* the path was set to blocking above */
Chris Mason0bd40a72008-07-17 12:54:43 -04004034 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4035 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05004036 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004037
Chris Masonca7a79a2008-05-12 12:59:19 -04004038 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04004039 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004040 btrfs_assert_tree_locked(c);
Chris Mason5cd57b22008-06-25 16:01:30 -04004041 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004042 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004043 }
Chris Masond97e63b2007-02-20 16:40:44 -05004044 break;
4045 }
4046 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004047 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004048 level--;
4049 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004050 if (path->locks[level])
4051 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04004052 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004053 path->nodes[level] = next;
4054 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004055 if (!path->skip_locking)
4056 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004057 if (!level)
4058 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004059
4060 btrfs_set_path_blocking(path);
Chris Mason925baed2008-06-25 16:01:30 -04004061 if (level == 1 && path->locks[1] && path->reada)
4062 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04004063 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04004064 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004065 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5cd57b22008-06-25 16:01:30 -04004066 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004067 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004068 }
Chris Masond97e63b2007-02-20 16:40:44 -05004069 }
Chris Mason925baed2008-06-25 16:01:30 -04004070done:
4071 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05004072 return 0;
4073}
Chris Mason0b86a832008-03-24 15:01:56 -04004074
Chris Mason3f157a22008-06-25 16:01:31 -04004075/*
4076 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4077 * searching until it gets past min_objectid or finds an item of 'type'
4078 *
4079 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4080 */
Chris Mason0b86a832008-03-24 15:01:56 -04004081int btrfs_previous_item(struct btrfs_root *root,
4082 struct btrfs_path *path, u64 min_objectid,
4083 int type)
4084{
4085 struct btrfs_key found_key;
4086 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004087 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004088 int ret;
4089
Chris Masond3977122009-01-05 21:25:51 -05004090 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004091 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004092 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004093 ret = btrfs_prev_leaf(root, path);
4094 if (ret != 0)
4095 return ret;
4096 } else {
4097 path->slots[0]--;
4098 }
4099 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004100 nritems = btrfs_header_nritems(leaf);
4101 if (nritems == 0)
4102 return 1;
4103 if (path->slots[0] == nritems)
4104 path->slots[0]--;
4105
Chris Mason0b86a832008-03-24 15:01:56 -04004106 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4107 if (found_key.type == type)
4108 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004109 if (found_key.objectid < min_objectid)
4110 break;
4111 if (found_key.objectid == min_objectid &&
4112 found_key.type < type)
4113 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004114 }
4115 return 1;
4116}