blob: 551177c0011ae90d3e2c0460340071ea97158b50 [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 Masondf24a2b2007-04-04 09:36:31 -040041inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040051 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
53 }
Chris Masondf24a2b2007-04-04 09:36:31 -040054 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040055}
56
Chris Masonb4ce94d2009-02-04 09:25:08 -050057/*
58 * set all locked nodes in the path to blocking locks. This should
59 * be done before scheduling
60 */
61noinline void btrfs_set_path_blocking(struct btrfs_path *p)
62{
63 int i;
64 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
65 if (p->nodes[i] && p->locks[i])
66 btrfs_set_lock_blocking(p->nodes[i]);
67 }
68}
69
70/*
71 * reset all the locked nodes in the patch to spinning locks.
72 */
73noinline void btrfs_clear_path_blocking(struct btrfs_path *p)
74{
75 int i;
76 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
77 if (p->nodes[i] && p->locks[i])
78 btrfs_clear_lock_blocking(p->nodes[i]);
79 }
80}
81
Chris Masond352ac62008-09-29 15:18:18 -040082/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040083void btrfs_free_path(struct btrfs_path *p)
84{
Chris Masondf24a2b2007-04-04 09:36:31 -040085 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040086 kmem_cache_free(btrfs_path_cachep, p);
87}
88
Chris Masond352ac62008-09-29 15:18:18 -040089/*
90 * path release drops references on the extent buffers in the path
91 * and it drops any locks held by this path
92 *
93 * It is safe to call this on paths that no locks or extent buffers held.
94 */
Chris Masond3977122009-01-05 21:25:51 -050095noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050096{
97 int i;
Chris Masona2135012008-06-25 16:01:30 -040098
Chris Mason234b63a2007-03-13 10:46:10 -040099 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400100 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500101 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400102 continue;
103 if (p->locks[i]) {
104 btrfs_tree_unlock(p->nodes[i]);
105 p->locks[i] = 0;
106 }
Chris Mason5f39d392007-10-15 16:14:19 -0400107 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400108 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500109 }
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
113 * safely gets a reference on the root node of a tree. A lock
114 * is not taken, so a concurrent writer may put a different node
115 * at the root of the tree. See btrfs_lock_root_node for the
116 * looping required.
117 *
118 * The extent buffer returned by this has a reference taken, so
119 * it won't disappear. It may stop being the root of the tree
120 * at any time because there are no locks held.
121 */
Chris Mason925baed2008-06-25 16:01:30 -0400122struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
123{
124 struct extent_buffer *eb;
125 spin_lock(&root->node_lock);
126 eb = root->node;
127 extent_buffer_get(eb);
128 spin_unlock(&root->node_lock);
129 return eb;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/* loop around taking references on and locking the root node of the
133 * tree until you end up with a lock on the root. A locked buffer
134 * is returned, with a reference held.
135 */
Chris Mason925baed2008-06-25 16:01:30 -0400136struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
137{
138 struct extent_buffer *eb;
139
Chris Masond3977122009-01-05 21:25:51 -0500140 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400141 eb = btrfs_root_node(root);
142 btrfs_tree_lock(eb);
143
144 spin_lock(&root->node_lock);
145 if (eb == root->node) {
146 spin_unlock(&root->node_lock);
147 break;
148 }
149 spin_unlock(&root->node_lock);
150
151 btrfs_tree_unlock(eb);
152 free_extent_buffer(eb);
153 }
154 return eb;
155}
156
Chris Masond352ac62008-09-29 15:18:18 -0400157/* cowonly root (everything not a reference counted cow subvolume), just get
158 * put onto a simple dirty list. transaction.c walks this to make sure they
159 * get properly updated on disk.
160 */
Chris Mason0b86a832008-03-24 15:01:56 -0400161static void add_root_to_dirty_list(struct btrfs_root *root)
162{
163 if (root->track_dirty && list_empty(&root->dirty_list)) {
164 list_add(&root->dirty_list,
165 &root->fs_info->dirty_cowonly_roots);
166 }
167}
168
Chris Masond352ac62008-09-29 15:18:18 -0400169/*
170 * used by snapshot creation to make a copy of a root for a tree with
171 * a given objectid. The buffer with the new root node is returned in
172 * cow_ret, and this func returns zero on success or a negative error code.
173 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500174int btrfs_copy_root(struct btrfs_trans_handle *trans,
175 struct btrfs_root *root,
176 struct extent_buffer *buf,
177 struct extent_buffer **cow_ret, u64 new_root_objectid)
178{
179 struct extent_buffer *cow;
180 u32 nritems;
181 int ret = 0;
182 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500183 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500184
Chris Mason4aec2b52007-12-18 16:25:45 -0500185 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
186 if (!new_root)
187 return -ENOMEM;
188
189 memcpy(new_root, root, sizeof(*new_root));
190 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500191
192 WARN_ON(root->ref_cows && trans->transid !=
193 root->fs_info->running_transaction->transid);
194 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
195
196 level = btrfs_header_level(buf);
197 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400198
199 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
200 new_root_objectid, trans->transid,
201 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500202 if (IS_ERR(cow)) {
203 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500205 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500206
207 copy_extent_buffer(cow, buf, 0, 0, cow->len);
208 btrfs_set_header_bytenr(cow, cow->start);
209 btrfs_set_header_generation(cow, trans->transid);
210 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400211 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500212
Yan Zheng2b820322008-11-17 21:11:30 -0500213 write_extent_buffer(cow, root->fs_info->fsid,
214 (unsigned long)btrfs_header_fsid(cow),
215 BTRFS_FSID_SIZE);
216
Chris Masonbe20aa92007-12-17 20:14:01 -0500217 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400218 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500219 kfree(new_root);
220
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 if (ret)
222 return ret;
223
224 btrfs_mark_buffer_dirty(cow);
225 *cow_ret = cow;
226 return 0;
227}
228
Chris Masond352ac62008-09-29 15:18:18 -0400229/*
Chris Masond3977122009-01-05 21:25:51 -0500230 * does the dirty work in cow of a single block. The parent block (if
231 * supplied) is updated to point to the new cow copy. The new buffer is marked
232 * dirty and returned locked. If you modify the block it needs to be marked
233 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400234 *
235 * search_start -- an allocation hint for the new block
236 *
Chris Masond3977122009-01-05 21:25:51 -0500237 * empty_size -- a hint that you plan on doing more cow. This is the size in
238 * bytes the allocator should try to find free next to the block it returns.
239 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400240 *
241 * prealloc_dest -- if you have already reserved a destination for the cow,
Chris Masond3977122009-01-05 21:25:51 -0500242 * this uses that block instead of allocating a new one.
243 * btrfs_alloc_reserved_extent is used to finish the allocation.
Chris Masond352ac62008-09-29 15:18:18 -0400244 */
Chris Masond3977122009-01-05 21:25:51 -0500245static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400246 struct btrfs_root *root,
247 struct extent_buffer *buf,
248 struct extent_buffer *parent, int parent_slot,
249 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400250 u64 search_start, u64 empty_size,
251 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400252{
Zheng Yan31840ae2008-09-23 13:14:14 -0400253 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400254 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500255 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400256 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500257 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400258 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400259
Chris Mason925baed2008-06-25 16:01:30 -0400260 if (*cow_ret == buf)
261 unlock_orig = 1;
262
263 WARN_ON(!btrfs_tree_locked(buf));
264
Zheng Yan31840ae2008-09-23 13:14:14 -0400265 if (parent)
266 parent_start = parent->start;
267 else
268 parent_start = 0;
269
Chris Mason7bb86312007-12-11 09:25:06 -0500270 WARN_ON(root->ref_cows && trans->transid !=
271 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400272 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400273
Chris Mason7bb86312007-12-11 09:25:06 -0500274 level = btrfs_header_level(buf);
275 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400276
Chris Mason65b51a02008-08-01 15:11:20 -0400277 if (prealloc_dest) {
278 struct btrfs_key ins;
279
280 ins.objectid = prealloc_dest;
281 ins.offset = buf->len;
282 ins.type = BTRFS_EXTENT_ITEM_KEY;
283
Zheng Yan31840ae2008-09-23 13:14:14 -0400284 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400285 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400286 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400287 BUG_ON(ret);
288 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
289 buf->len);
290 } else {
291 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400292 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400293 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400294 trans->transid, level,
295 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400296 }
Chris Mason6702ed42007-08-07 16:15:09 -0400297 if (IS_ERR(cow))
298 return PTR_ERR(cow);
299
Chris Masonb4ce94d2009-02-04 09:25:08 -0500300 /* cow is set to blocking by btrfs_init_new_buffer */
301
Chris Mason5f39d392007-10-15 16:14:19 -0400302 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400303 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400304 btrfs_set_header_generation(cow, trans->transid);
305 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400306 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400307
Yan Zheng2b820322008-11-17 21:11:30 -0500308 write_extent_buffer(cow, root->fs_info->fsid,
309 (unsigned long)btrfs_header_fsid(cow),
310 BTRFS_FSID_SIZE);
311
Chris Mason5f39d392007-10-15 16:14:19 -0400312 WARN_ON(btrfs_header_generation(buf) > trans->transid);
313 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400314 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400315 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400316 if (ret)
317 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400318
319 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
320 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400321 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
322 /*
323 * There are only two places that can drop reference to
324 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400325 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400326 * we check reference count while tree block is locked.
327 * Furthermore, if reference count is one, it won't get
328 * increased by someone else.
329 */
330 u32 refs;
331 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
332 buf->len, &refs);
333 BUG_ON(ret);
334 if (refs == 1) {
335 ret = btrfs_update_ref(trans, root, buf, cow,
336 0, nritems);
337 clean_tree_block(trans, root, buf);
338 } else {
339 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
340 }
341 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400342 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400343 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
344 if (ret)
345 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400346 clean_tree_block(trans, root, buf);
347 }
348
Zheng Yan1a40e232008-09-26 10:09:34 -0400349 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400350 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
351 WARN_ON(ret);
352 }
353
Chris Mason6702ed42007-08-07 16:15:09 -0400354 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400355 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400356
357 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400358 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400359 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400360 spin_unlock(&root->node_lock);
361
Chris Mason6702ed42007-08-07 16:15:09 -0400362 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400363 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400364 buf->len, buf->start,
365 root->root_key.objectid,
366 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400367 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400368 }
Chris Mason5f39d392007-10-15 16:14:19 -0400369 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400370 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400371 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400372 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400373 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500374 WARN_ON(trans->transid == 0);
375 btrfs_set_node_ptr_generation(parent, parent_slot,
376 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400377 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400378 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500379 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400380 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400381 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400382 }
Chris Mason925baed2008-06-25 16:01:30 -0400383 if (unlock_orig)
384 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400385 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400386 btrfs_mark_buffer_dirty(cow);
387 *cow_ret = cow;
388 return 0;
389}
390
Chris Masond352ac62008-09-29 15:18:18 -0400391/*
392 * cows a single block, see __btrfs_cow_block for the real work.
393 * This version of it has extra checks so that a block isn't cow'd more than
394 * once per transaction, as long as it hasn't been written yet
395 */
Chris Masond3977122009-01-05 21:25:51 -0500396noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400397 struct btrfs_root *root, struct extent_buffer *buf,
398 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400399 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500400{
Chris Mason6702ed42007-08-07 16:15:09 -0400401 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400402 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500403
Chris Masonccd467d2007-06-28 15:57:36 -0400404 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500405 printk(KERN_CRIT "trans %llu running %llu\n",
406 (unsigned long long)trans->transid,
407 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400408 root->fs_info->running_transaction->transid);
409 WARN_ON(1);
410 }
411 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500412 printk(KERN_CRIT "trans %llu running %llu\n",
413 (unsigned long long)trans->transid,
414 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400415 WARN_ON(1);
416 }
Chris Masondc17ff82008-01-08 15:46:30 -0500417
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400418 if (btrfs_header_generation(buf) == trans->transid &&
419 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400420 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500421 *cow_ret = buf;
Chris Mason65b51a02008-08-01 15:11:20 -0400422 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500423 return 0;
424 }
Chris Masonc4876852009-02-04 09:24:25 -0500425
Chris Mason0b86a832008-03-24 15:01:56 -0400426 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500427
428 if (parent)
429 btrfs_set_lock_blocking(parent);
430 btrfs_set_lock_blocking(buf);
431
Chris Masonf510cfe2007-10-15 16:14:48 -0400432 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400433 parent_slot, cow_ret, search_start, 0,
434 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400435 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400436}
437
Chris Masond352ac62008-09-29 15:18:18 -0400438/*
439 * helper function for defrag to decide if two blocks pointed to by a
440 * node are actually close by
441 */
Chris Mason6b800532007-10-15 16:17:34 -0400442static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400443{
Chris Mason6b800532007-10-15 16:17:34 -0400444 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400445 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400446 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400447 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500448 return 0;
449}
450
Chris Mason081e9572007-11-06 10:26:24 -0500451/*
452 * compare two keys in a memcmp fashion
453 */
454static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
455{
456 struct btrfs_key k1;
457
458 btrfs_disk_key_to_cpu(&k1, disk);
459
460 if (k1.objectid > k2->objectid)
461 return 1;
462 if (k1.objectid < k2->objectid)
463 return -1;
464 if (k1.type > k2->type)
465 return 1;
466 if (k1.type < k2->type)
467 return -1;
468 if (k1.offset > k2->offset)
469 return 1;
470 if (k1.offset < k2->offset)
471 return -1;
472 return 0;
473}
474
Josef Bacikf3465ca2008-11-12 14:19:50 -0500475/*
476 * same as comp_keys only with two btrfs_key's
477 */
478static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
479{
480 if (k1->objectid > k2->objectid)
481 return 1;
482 if (k1->objectid < k2->objectid)
483 return -1;
484 if (k1->type > k2->type)
485 return 1;
486 if (k1->type < k2->type)
487 return -1;
488 if (k1->offset > k2->offset)
489 return 1;
490 if (k1->offset < k2->offset)
491 return -1;
492 return 0;
493}
Chris Mason081e9572007-11-06 10:26:24 -0500494
Chris Masond352ac62008-09-29 15:18:18 -0400495/*
496 * this is used by the defrag code to go through all the
497 * leaves pointed to by a node and reallocate them so that
498 * disk order is close to key order
499 */
Chris Mason6702ed42007-08-07 16:15:09 -0400500int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400501 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400502 int start_slot, int cache_only, u64 *last_ret,
503 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400504{
Chris Mason6b800532007-10-15 16:17:34 -0400505 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400506 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400507 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400508 u64 search_start = *last_ret;
509 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400510 u64 other;
511 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400512 int end_slot;
513 int i;
514 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400515 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400516 int uptodate;
517 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500518 int progress_passed = 0;
519 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400520
Chris Mason5708b952007-10-25 15:43:18 -0400521 parent_level = btrfs_header_level(parent);
522 if (cache_only && parent_level != 1)
523 return 0;
524
Chris Masond3977122009-01-05 21:25:51 -0500525 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400526 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500527 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400528 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400529
Chris Mason6b800532007-10-15 16:17:34 -0400530 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400531 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400532 end_slot = parent_nritems;
533
534 if (parent_nritems == 1)
535 return 0;
536
Chris Masonb4ce94d2009-02-04 09:25:08 -0500537 btrfs_set_lock_blocking(parent);
538
Chris Mason6702ed42007-08-07 16:15:09 -0400539 for (i = start_slot; i < end_slot; i++) {
540 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400541
Chris Mason5708b952007-10-25 15:43:18 -0400542 if (!parent->map_token) {
543 map_extent_buffer(parent,
544 btrfs_node_key_ptr_offset(i),
545 sizeof(struct btrfs_key_ptr),
546 &parent->map_token, &parent->kaddr,
547 &parent->map_start, &parent->map_len,
548 KM_USER1);
549 }
Chris Mason081e9572007-11-06 10:26:24 -0500550 btrfs_node_key(parent, &disk_key, i);
551 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
552 continue;
553
554 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400555 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400556 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400557 if (last_block == 0)
558 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400559
Chris Mason6702ed42007-08-07 16:15:09 -0400560 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400561 other = btrfs_node_blockptr(parent, i - 1);
562 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400563 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400564 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400565 other = btrfs_node_blockptr(parent, i + 1);
566 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400567 }
Chris Masone9d0b132007-08-10 14:06:19 -0400568 if (close) {
569 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400570 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400571 }
Chris Mason5708b952007-10-25 15:43:18 -0400572 if (parent->map_token) {
573 unmap_extent_buffer(parent, parent->map_token,
574 KM_USER1);
575 parent->map_token = NULL;
576 }
Chris Mason6702ed42007-08-07 16:15:09 -0400577
Chris Mason6b800532007-10-15 16:17:34 -0400578 cur = btrfs_find_tree_block(root, blocknr, blocksize);
579 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400580 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400581 else
582 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400583 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400584 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400585 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400586 continue;
587 }
Chris Mason6b800532007-10-15 16:17:34 -0400588 if (!cur) {
589 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400590 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400591 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400592 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400593 }
Chris Mason6702ed42007-08-07 16:15:09 -0400594 }
Chris Masone9d0b132007-08-10 14:06:19 -0400595 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400596 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400597
Chris Masone7a84562008-06-25 16:01:31 -0400598 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500599 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400600 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400601 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400602 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400603 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400604 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400605 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400606 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400607 break;
Yan252c38f2007-08-29 09:11:44 -0400608 }
Chris Masone7a84562008-06-25 16:01:31 -0400609 search_start = cur->start;
610 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400611 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400612 btrfs_tree_unlock(cur);
613 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400614 }
Chris Mason5708b952007-10-25 15:43:18 -0400615 if (parent->map_token) {
616 unmap_extent_buffer(parent, parent->map_token,
617 KM_USER1);
618 parent->map_token = NULL;
619 }
Chris Mason6702ed42007-08-07 16:15:09 -0400620 return err;
621}
622
Chris Mason74123bd2007-02-02 11:05:29 -0500623/*
624 * The leaf data grows from end-to-front in the node.
625 * this returns the address of the start of the last item,
626 * which is the stop of the leaf data stack
627 */
Chris Mason123abc82007-03-14 14:14:43 -0400628static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400629 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500630{
Chris Mason5f39d392007-10-15 16:14:19 -0400631 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500632 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400633 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400634 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500635}
636
Chris Masond352ac62008-09-29 15:18:18 -0400637/*
638 * extra debugging checks to make sure all the items in a key are
639 * well formed and in the proper order
640 */
Chris Mason123abc82007-03-14 14:14:43 -0400641static int check_node(struct btrfs_root *root, struct btrfs_path *path,
642 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500643{
Chris Mason5f39d392007-10-15 16:14:19 -0400644 struct extent_buffer *parent = NULL;
645 struct extent_buffer *node = path->nodes[level];
646 struct btrfs_disk_key parent_key;
647 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500648 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400649 int slot;
650 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400651 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500652
653 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400654 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400655
Chris Mason8d7be552007-05-10 11:24:42 -0400656 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400657 BUG_ON(nritems == 0);
658 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400659 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400660 btrfs_node_key(parent, &parent_key, parent_slot);
661 btrfs_node_key(node, &node_key, 0);
662 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400663 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400664 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400665 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500666 }
Chris Mason123abc82007-03-14 14:14:43 -0400667 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400668 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400669 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
670 btrfs_node_key(node, &node_key, slot);
671 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400672 }
673 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400674 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
675 btrfs_node_key(node, &node_key, slot);
676 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500677 }
678 return 0;
679}
680
Chris Masond352ac62008-09-29 15:18:18 -0400681/*
682 * extra checking to make sure all the items in a leaf are
683 * well formed and in the proper order
684 */
Chris Mason123abc82007-03-14 14:14:43 -0400685static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
686 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500687{
Chris Mason5f39d392007-10-15 16:14:19 -0400688 struct extent_buffer *leaf = path->nodes[level];
689 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500690 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400691 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400692 struct btrfs_disk_key parent_key;
693 struct btrfs_disk_key leaf_key;
694 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400695
Chris Mason5f39d392007-10-15 16:14:19 -0400696 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500697
698 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400699 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400700
701 if (nritems == 0)
702 return 0;
703
704 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400705 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400706 btrfs_node_key(parent, &parent_key, parent_slot);
707 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400708
Chris Mason5f39d392007-10-15 16:14:19 -0400709 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400710 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400711 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400712 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500713 }
Chris Mason5f39d392007-10-15 16:14:19 -0400714 if (slot != 0 && slot < nritems - 1) {
715 btrfs_item_key(leaf, &leaf_key, slot);
716 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
717 if (comp_keys(&leaf_key, &cpukey) <= 0) {
718 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500719 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400720 BUG_ON(1);
721 }
722 if (btrfs_item_offset_nr(leaf, slot - 1) !=
723 btrfs_item_end_nr(leaf, slot)) {
724 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500725 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400726 BUG_ON(1);
727 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500728 }
Chris Mason8d7be552007-05-10 11:24:42 -0400729 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400730 btrfs_item_key(leaf, &leaf_key, slot);
731 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
732 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
733 if (btrfs_item_offset_nr(leaf, slot) !=
734 btrfs_item_end_nr(leaf, slot + 1)) {
735 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500736 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400737 BUG_ON(1);
738 }
Chris Mason8d7be552007-05-10 11:24:42 -0400739 }
Chris Mason5f39d392007-10-15 16:14:19 -0400740 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
741 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500742 return 0;
743}
744
Chris Masond3977122009-01-05 21:25:51 -0500745static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500746 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500747{
Chris Mason85d824c2008-04-10 10:23:19 -0400748 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500749 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400750 return check_leaf(root, path, level);
751 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500752}
753
Chris Mason74123bd2007-02-02 11:05:29 -0500754/*
Chris Mason5f39d392007-10-15 16:14:19 -0400755 * search for key in the extent_buffer. The items start at offset p,
756 * and they are item_size apart. There are 'max' items in p.
757 *
Chris Mason74123bd2007-02-02 11:05:29 -0500758 * the slot in the array is returned via slot, and it points to
759 * the place where you would insert key if it is not found in
760 * the array.
761 *
762 * slot may point to max if the key is bigger than all of the keys
763 */
Chris Masone02119d2008-09-05 16:13:11 -0400764static noinline int generic_bin_search(struct extent_buffer *eb,
765 unsigned long p,
766 int item_size, struct btrfs_key *key,
767 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500768{
769 int low = 0;
770 int high = max;
771 int mid;
772 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400773 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct btrfs_disk_key unaligned;
775 unsigned long offset;
776 char *map_token = NULL;
777 char *kaddr = NULL;
778 unsigned long map_start = 0;
779 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400780 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500781
Chris Masond3977122009-01-05 21:25:51 -0500782 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500783 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400784 offset = p + mid * item_size;
785
786 if (!map_token || offset < map_start ||
787 (offset + sizeof(struct btrfs_disk_key)) >
788 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400789 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400790 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400791 map_token = NULL;
792 }
Chris Mason934d3752008-12-08 16:43:10 -0500793
794 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400795 sizeof(struct btrfs_disk_key),
796 &map_token, &kaddr,
797 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400798
Chris Mason479965d2007-10-15 16:14:27 -0400799 if (!err) {
800 tmp = (struct btrfs_disk_key *)(kaddr + offset -
801 map_start);
802 } else {
803 read_extent_buffer(eb, &unaligned,
804 offset, sizeof(unaligned));
805 tmp = &unaligned;
806 }
807
Chris Mason5f39d392007-10-15 16:14:19 -0400808 } else {
809 tmp = (struct btrfs_disk_key *)(kaddr + offset -
810 map_start);
811 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500812 ret = comp_keys(tmp, key);
813
814 if (ret < 0)
815 low = mid + 1;
816 else if (ret > 0)
817 high = mid;
818 else {
819 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400820 if (map_token)
821 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500822 return 0;
823 }
824 }
825 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400826 if (map_token)
827 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500828 return 1;
829}
830
Chris Mason97571fd2007-02-24 13:39:08 -0500831/*
832 * simple bin_search frontend that does the right thing for
833 * leaves vs nodes
834 */
Chris Mason5f39d392007-10-15 16:14:19 -0400835static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
836 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500837{
Chris Mason5f39d392007-10-15 16:14:19 -0400838 if (level == 0) {
839 return generic_bin_search(eb,
840 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400841 sizeof(struct btrfs_item),
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 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400845 return generic_bin_search(eb,
846 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400847 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400848 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400849 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500850 }
851 return -1;
852}
853
Chris Masond352ac62008-09-29 15:18:18 -0400854/* given a node and slot number, this reads the blocks it points to. The
855 * extent buffer is returned with a reference taken (but unlocked).
856 * NULL is returned on error.
857 */
Chris Masone02119d2008-09-05 16:13:11 -0400858static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400859 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500860{
Chris Masonca7a79a2008-05-12 12:59:19 -0400861 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500862 if (slot < 0)
863 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400864 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500865 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400866
867 BUG_ON(level == 0);
868
Chris Masondb945352007-10-15 16:15:53 -0400869 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400870 btrfs_level_size(root, level - 1),
871 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500872}
873
Chris Masond352ac62008-09-29 15:18:18 -0400874/*
875 * node level balancing, used to make sure nodes are in proper order for
876 * item deletion. We balance from the top down, so we have to make sure
877 * that a deletion won't leave an node completely empty later on.
878 */
Chris Masone02119d2008-09-05 16:13:11 -0400879static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500880 struct btrfs_root *root,
881 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500882{
Chris Mason5f39d392007-10-15 16:14:19 -0400883 struct extent_buffer *right = NULL;
884 struct extent_buffer *mid;
885 struct extent_buffer *left = NULL;
886 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500887 int ret = 0;
888 int wret;
889 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500890 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400891 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500892 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500893
894 if (level == 0)
895 return 0;
896
Chris Mason5f39d392007-10-15 16:14:19 -0400897 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500898
Chris Mason925baed2008-06-25 16:01:30 -0400899 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500900 WARN_ON(btrfs_header_generation(mid) != trans->transid);
901
Chris Mason1d4f8a02007-03-13 09:28:32 -0400902 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500903
Chris Mason234b63a2007-03-13 10:46:10 -0400904 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400905 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500906 pslot = path->slots[level + 1];
907
Chris Mason40689472007-03-17 14:29:23 -0400908 /*
909 * deal with the case where there is only one pointer in the root
910 * by promoting the node below to a root
911 */
Chris Mason5f39d392007-10-15 16:14:19 -0400912 if (!parent) {
913 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500914
Chris Mason5f39d392007-10-15 16:14:19 -0400915 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500916 return 0;
917
918 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400919 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400920 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500921 btrfs_set_lock_blocking(child);
Chris Masonbb803952007-03-01 12:04:21 -0500922 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400923 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500924 BUG_ON(ret);
925
Chris Mason925baed2008-06-25 16:01:30 -0400926 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500927 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400928 spin_unlock(&root->node_lock);
929
Zheng Yan31840ae2008-09-23 13:14:14 -0400930 ret = btrfs_update_extent_ref(trans, root, child->start,
931 mid->start, child->start,
932 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400933 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400934 BUG_ON(ret);
935
Chris Mason0b86a832008-03-24 15:01:56 -0400936 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400937 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500938
Chris Mason925baed2008-06-25 16:01:30 -0400939 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500940 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400941 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400942 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500943 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400944 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500945 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400946 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400947 btrfs_header_generation(mid),
948 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500949 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400950 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400951 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500952 }
Chris Mason5f39d392007-10-15 16:14:19 -0400953 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400954 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500955 return 0;
956
Chris Mason5f39d392007-10-15 16:14:19 -0400957 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400958 err_on_enospc = 1;
959
Chris Mason5f39d392007-10-15 16:14:19 -0400960 left = read_node_slot(root, parent, pslot - 1);
961 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400962 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500963 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400964 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400965 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400966 if (wret) {
967 ret = wret;
968 goto enospc;
969 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400970 }
Chris Mason5f39d392007-10-15 16:14:19 -0400971 right = read_node_slot(root, parent, pslot + 1);
972 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400973 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500974 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400975 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400976 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400977 if (wret) {
978 ret = wret;
979 goto enospc;
980 }
981 }
982
983 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400984 if (left) {
985 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400986 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500987 if (wret < 0)
988 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400989 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500991 }
Chris Mason79f95c82007-03-01 15:16:26 -0500992
993 /*
994 * then try to empty the right most buffer into the middle
995 */
Chris Mason5f39d392007-10-15 16:14:19 -0400996 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400997 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400998 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500999 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001001 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -05001002 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001003 u32 blocksize = right->len;
1004
Chris Mason5f39d392007-10-15 16:14:19 -04001005 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001006 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001007 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001008 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001009 wret = del_ptr(trans, root, path, level + 1, pslot +
1010 1);
Chris Masonbb803952007-03-01 12:04:21 -05001011 if (wret)
1012 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001013 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001014 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001015 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001016 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001017 if (wret)
1018 ret = wret;
1019 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001020 struct btrfs_disk_key right_key;
1021 btrfs_node_key(right, &right_key, 0);
1022 btrfs_set_node_key(parent, &right_key, pslot + 1);
1023 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001024 }
1025 }
Chris Mason5f39d392007-10-15 16:14:19 -04001026 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001027 /*
1028 * we're not allowed to leave a node with one item in the
1029 * tree during a delete. A deletion from lower in the tree
1030 * could try to delete the only pointer in this node.
1031 * So, pull some keys from the left.
1032 * There has to be a left pointer at this point because
1033 * otherwise we would have pulled some pointers from the
1034 * right
1035 */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 BUG_ON(!left);
1037 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001038 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001039 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001040 goto enospc;
1041 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001042 if (wret == 1) {
1043 wret = push_node_left(trans, root, left, mid, 1);
1044 if (wret < 0)
1045 ret = wret;
1046 }
Chris Mason79f95c82007-03-01 15:16:26 -05001047 BUG_ON(wret == 1);
1048 }
Chris Mason5f39d392007-10-15 16:14:19 -04001049 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001050 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001051 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001052 u64 bytenr = mid->start;
1053 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001054
Chris Mason5f39d392007-10-15 16:14:19 -04001055 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001056 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001057 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001058 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001059 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001060 if (wret)
1061 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001062 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001063 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001064 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001065 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001066 if (wret)
1067 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001068 } else {
1069 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001070 struct btrfs_disk_key mid_key;
1071 btrfs_node_key(mid, &mid_key, 0);
1072 btrfs_set_node_key(parent, &mid_key, pslot);
1073 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001074 }
Chris Masonbb803952007-03-01 12:04:21 -05001075
Chris Mason79f95c82007-03-01 15:16:26 -05001076 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001077 if (left) {
1078 if (btrfs_header_nritems(left) > orig_slot) {
1079 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001080 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001081 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001082 path->slots[level + 1] -= 1;
1083 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001084 if (mid) {
1085 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001086 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001087 }
Chris Masonbb803952007-03-01 12:04:21 -05001088 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001089 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001090 path->slots[level] = orig_slot;
1091 }
1092 }
Chris Mason79f95c82007-03-01 15:16:26 -05001093 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001094 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001095 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001096 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001097 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001098enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001099 if (right) {
1100 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001101 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001102 }
1103 if (left) {
1104 if (path->nodes[level] != left)
1105 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001106 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001107 }
Chris Masonbb803952007-03-01 12:04:21 -05001108 return ret;
1109}
1110
Chris Masond352ac62008-09-29 15:18:18 -04001111/* Node balancing for insertion. Here we only split or push nodes around
1112 * when they are completely full. This is also done top down, so we
1113 * have to be pessimistic.
1114 */
Chris Masond3977122009-01-05 21:25:51 -05001115static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001116 struct btrfs_root *root,
1117 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001118{
Chris Mason5f39d392007-10-15 16:14:19 -04001119 struct extent_buffer *right = NULL;
1120 struct extent_buffer *mid;
1121 struct extent_buffer *left = NULL;
1122 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001123 int ret = 0;
1124 int wret;
1125 int pslot;
1126 int orig_slot = path->slots[level];
1127 u64 orig_ptr;
1128
1129 if (level == 0)
1130 return 1;
1131
Chris Mason5f39d392007-10-15 16:14:19 -04001132 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001133 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001134 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1135
1136 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001137 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001138 pslot = path->slots[level + 1];
1139
Chris Mason5f39d392007-10-15 16:14:19 -04001140 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001141 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001142
Chris Mason5f39d392007-10-15 16:14:19 -04001143 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001144
1145 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001146 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001147 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001148
1149 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001150 btrfs_set_lock_blocking(left);
1151
Chris Mason5f39d392007-10-15 16:14:19 -04001152 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001153 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1154 wret = 1;
1155 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001156 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001157 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001158 if (ret)
1159 wret = 1;
1160 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001161 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001162 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001163 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001164 }
Chris Masone66f7092007-04-20 13:16:02 -04001165 if (wret < 0)
1166 ret = wret;
1167 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001168 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001169 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001170 btrfs_node_key(mid, &disk_key, 0);
1171 btrfs_set_node_key(parent, &disk_key, pslot);
1172 btrfs_mark_buffer_dirty(parent);
1173 if (btrfs_header_nritems(left) > orig_slot) {
1174 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001175 path->slots[level + 1] -= 1;
1176 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001177 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001178 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001179 } else {
1180 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001181 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001182 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001183 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001185 }
Chris Masone66f7092007-04-20 13:16:02 -04001186 return 0;
1187 }
Chris Mason925baed2008-06-25 16:01:30 -04001188 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001189 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001190 }
Chris Mason925baed2008-06-25 16:01:30 -04001191 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001192
1193 /*
1194 * then try to empty the right most buffer into the middle
1195 */
Chris Mason5f39d392007-10-15 16:14:19 -04001196 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001197 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001198
Chris Mason925baed2008-06-25 16:01:30 -04001199 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001200 btrfs_set_lock_blocking(right);
1201
Chris Mason5f39d392007-10-15 16:14:19 -04001202 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001203 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1204 wret = 1;
1205 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001206 ret = btrfs_cow_block(trans, root, right,
1207 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001208 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001209 if (ret)
1210 wret = 1;
1211 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001212 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001213 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001214 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001215 }
Chris Masone66f7092007-04-20 13:16:02 -04001216 if (wret < 0)
1217 ret = wret;
1218 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001219 struct btrfs_disk_key disk_key;
1220
1221 btrfs_node_key(right, &disk_key, 0);
1222 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1223 btrfs_mark_buffer_dirty(parent);
1224
1225 if (btrfs_header_nritems(mid) <= orig_slot) {
1226 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001227 path->slots[level + 1] += 1;
1228 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001229 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001230 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001231 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001232 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001233 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001234 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001235 }
Chris Masone66f7092007-04-20 13:16:02 -04001236 return 0;
1237 }
Chris Mason925baed2008-06-25 16:01:30 -04001238 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001239 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001240 }
Chris Masone66f7092007-04-20 13:16:02 -04001241 return 1;
1242}
1243
Chris Mason74123bd2007-02-02 11:05:29 -05001244/*
Chris Masond352ac62008-09-29 15:18:18 -04001245 * readahead one full node of leaves, finding things that are close
1246 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001247 */
Chris Masone02119d2008-09-05 16:13:11 -04001248static noinline void reada_for_search(struct btrfs_root *root,
1249 struct btrfs_path *path,
1250 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001251{
Chris Mason5f39d392007-10-15 16:14:19 -04001252 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001253 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001254 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001255 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001256 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001257 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001258 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001259 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001260 u32 nr;
1261 u32 blocksize;
1262 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001263
Chris Masona6b6e752007-10-15 16:22:39 -04001264 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001265 return;
1266
Chris Mason6702ed42007-08-07 16:15:09 -04001267 if (!path->nodes[level])
1268 return;
1269
Chris Mason5f39d392007-10-15 16:14:19 -04001270 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001271
Chris Mason3c69fae2007-08-07 15:52:22 -04001272 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001273 blocksize = btrfs_level_size(root, level - 1);
1274 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001275 if (eb) {
1276 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 return;
1278 }
1279
Chris Masona7175312009-01-22 09:23:10 -05001280 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001281
Chris Mason5f39d392007-10-15 16:14:19 -04001282 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001283 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001284 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001285 if (direction < 0) {
1286 if (nr == 0)
1287 break;
1288 nr--;
1289 } else if (direction > 0) {
1290 nr++;
1291 if (nr >= nritems)
1292 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001293 }
Chris Mason01f46652007-12-21 16:24:26 -05001294 if (path->reada < 0 && objectid) {
1295 btrfs_node_key(node, &disk_key, nr);
1296 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1297 break;
1298 }
Chris Mason6b800532007-10-15 16:17:34 -04001299 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001300 if ((search <= target && target - search <= 65536) ||
1301 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001302 readahead_tree_block(root, search, blocksize,
1303 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001304 nread += blocksize;
1305 }
1306 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001307 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001308 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001309 }
1310}
Chris Mason925baed2008-06-25 16:01:30 -04001311
Chris Masond352ac62008-09-29 15:18:18 -04001312/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001313 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1314 * cache
1315 */
1316static noinline int reada_for_balance(struct btrfs_root *root,
1317 struct btrfs_path *path, int level)
1318{
1319 int slot;
1320 int nritems;
1321 struct extent_buffer *parent;
1322 struct extent_buffer *eb;
1323 u64 gen;
1324 u64 block1 = 0;
1325 u64 block2 = 0;
1326 int ret = 0;
1327 int blocksize;
1328
1329 parent = path->nodes[level - 1];
1330 if (!parent)
1331 return 0;
1332
1333 nritems = btrfs_header_nritems(parent);
1334 slot = path->slots[level];
1335 blocksize = btrfs_level_size(root, level);
1336
1337 if (slot > 0) {
1338 block1 = btrfs_node_blockptr(parent, slot - 1);
1339 gen = btrfs_node_ptr_generation(parent, slot - 1);
1340 eb = btrfs_find_tree_block(root, block1, blocksize);
1341 if (eb && btrfs_buffer_uptodate(eb, gen))
1342 block1 = 0;
1343 free_extent_buffer(eb);
1344 }
1345 if (slot < nritems) {
1346 block2 = btrfs_node_blockptr(parent, slot + 1);
1347 gen = btrfs_node_ptr_generation(parent, slot + 1);
1348 eb = btrfs_find_tree_block(root, block2, blocksize);
1349 if (eb && btrfs_buffer_uptodate(eb, gen))
1350 block2 = 0;
1351 free_extent_buffer(eb);
1352 }
1353 if (block1 || block2) {
1354 ret = -EAGAIN;
1355 btrfs_release_path(root, path);
1356 if (block1)
1357 readahead_tree_block(root, block1, blocksize, 0);
1358 if (block2)
1359 readahead_tree_block(root, block2, blocksize, 0);
1360
1361 if (block1) {
1362 eb = read_tree_block(root, block1, blocksize, 0);
1363 free_extent_buffer(eb);
1364 }
1365 if (block1) {
1366 eb = read_tree_block(root, block2, blocksize, 0);
1367 free_extent_buffer(eb);
1368 }
1369 }
1370 return ret;
1371}
1372
1373
1374/*
Chris Masond3977122009-01-05 21:25:51 -05001375 * when we walk down the tree, it is usually safe to unlock the higher layers
1376 * in the tree. The exceptions are when our path goes through slot 0, because
1377 * operations on the tree might require changing key pointers higher up in the
1378 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001379 *
Chris Masond3977122009-01-05 21:25:51 -05001380 * callers might also have set path->keep_locks, which tells this code to keep
1381 * the lock if the path points to the last slot in the block. This is part of
1382 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001383 *
Chris Masond3977122009-01-05 21:25:51 -05001384 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1385 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001386 */
Chris Masone02119d2008-09-05 16:13:11 -04001387static noinline void unlock_up(struct btrfs_path *path, int level,
1388 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001389{
1390 int i;
1391 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001392 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001393 struct extent_buffer *t;
1394
1395 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1396 if (!path->nodes[i])
1397 break;
1398 if (!path->locks[i])
1399 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001400 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001401 skip_level = i + 1;
1402 continue;
1403 }
Chris Mason051e1b92008-06-25 16:01:30 -04001404 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001405 u32 nritems;
1406 t = path->nodes[i];
1407 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001408 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001409 skip_level = i + 1;
1410 continue;
1411 }
1412 }
Chris Mason051e1b92008-06-25 16:01:30 -04001413 if (skip_level < i && i >= lowest_unlock)
1414 no_skips = 1;
1415
Chris Mason925baed2008-06-25 16:01:30 -04001416 t = path->nodes[i];
1417 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1418 btrfs_tree_unlock(t);
1419 path->locks[i] = 0;
1420 }
1421 }
1422}
1423
Chris Mason3c69fae2007-08-07 15:52:22 -04001424/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001425 * This releases any locks held in the path starting at level and
1426 * going all the way up to the root.
1427 *
1428 * btrfs_search_slot will keep the lock held on higher nodes in a few
1429 * corner cases, such as COW of the block at slot zero in the node. This
1430 * ignores those rules, and it should only be called when there are no
1431 * more updates to be done higher up in the tree.
1432 */
1433noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1434{
1435 int i;
1436
1437 if (path->keep_locks || path->lowest_level)
1438 return;
1439
1440 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1441 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001442 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001443 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001444 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001445 btrfs_tree_unlock(path->nodes[i]);
1446 path->locks[i] = 0;
1447 }
1448}
1449
1450/*
Chris Mason74123bd2007-02-02 11:05:29 -05001451 * look for key in the tree. path is filled in with nodes along the way
1452 * if key is found, we return zero and you can find the item in the leaf
1453 * level of the path (level 0)
1454 *
1455 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001456 * be inserted, and 1 is returned. If there are other errors during the
1457 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001458 *
1459 * if ins_len > 0, nodes and leaves will be split as we walk down the
1460 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1461 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001462 */
Chris Masone089f052007-03-16 16:20:31 -04001463int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1464 *root, struct btrfs_key *key, struct btrfs_path *p, int
1465 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001466{
Chris Mason5f39d392007-10-15 16:14:19 -04001467 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001468 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001469 int slot;
1470 int ret;
1471 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001472 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001473 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001474 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001475 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001476 u64 blocknr;
1477 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001478 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001479
Chris Mason6702ed42007-08-07 16:15:09 -04001480 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001481 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001482 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001483
Chris Mason925baed2008-06-25 16:01:30 -04001484 if (ins_len < 0)
1485 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001486
1487 prealloc_block.objectid = 0;
1488
Chris Masonbb803952007-03-01 12:04:21 -05001489again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001490 if (p->skip_locking)
1491 b = btrfs_root_node(root);
1492 else
1493 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001494
Chris Masoneb60cea2007-02-02 09:18:22 -05001495 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001496 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001497
1498 /*
1499 * setup the path here so we can release it under lock
1500 * contention with the cow code
1501 */
1502 p->nodes[level] = b;
1503 if (!p->skip_locking)
1504 p->locks[level] = 1;
1505
Chris Mason02217ed2007-03-02 16:08:05 -05001506 if (cow) {
1507 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001508
1509 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001510 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001511 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001512 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001513 goto cow_done;
1514 }
Chris Mason65b51a02008-08-01 15:11:20 -04001515
1516 /* ok, we have to cow, is our old prealloc the right
1517 * size?
1518 */
1519 if (prealloc_block.objectid &&
1520 prealloc_block.offset != b->len) {
Chris Mason7b78c172009-02-04 09:12:46 -05001521 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001522 btrfs_free_reserved_extent(root,
1523 prealloc_block.objectid,
1524 prealloc_block.offset);
1525 prealloc_block.objectid = 0;
Chris Mason7b78c172009-02-04 09:12:46 -05001526 goto again;
Chris Mason65b51a02008-08-01 15:11:20 -04001527 }
1528
1529 /*
1530 * for higher level blocks, try not to allocate blocks
1531 * with the block and the parent locks held.
1532 */
Chris Mason7b78c172009-02-04 09:12:46 -05001533 if (level > 0 && !prealloc_block.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001534 btrfs_path_lock_waiting(p, level)) {
1535 u32 size = b->len;
1536 u64 hint = b->start;
1537
1538 btrfs_release_path(root, p);
1539 ret = btrfs_reserve_extent(trans, root,
1540 size, size, 0,
1541 hint, (u64)-1,
1542 &prealloc_block, 0);
1543 BUG_ON(ret);
1544 goto again;
1545 }
1546
Chris Masonb4ce94d2009-02-04 09:25:08 -05001547 btrfs_set_path_blocking(p);
1548
Chris Masone20d96d2007-03-22 12:13:20 -04001549 wret = btrfs_cow_block(trans, root, b,
1550 p->nodes[level + 1],
1551 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001552 &b, prealloc_block.objectid);
1553 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001554 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001555 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001556 ret = wret;
1557 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001558 }
Chris Mason02217ed2007-03-02 16:08:05 -05001559 }
Chris Mason65b51a02008-08-01 15:11:20 -04001560cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001561 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001562 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001563 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001564 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001565
Chris Masoneb60cea2007-02-02 09:18:22 -05001566 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001567 if (!p->skip_locking)
1568 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001569
Chris Masonb4ce94d2009-02-04 09:25:08 -05001570 btrfs_clear_path_blocking(p);
1571
1572 /*
1573 * we have a lock on b and as long as we aren't changing
1574 * the tree, there is no way to for the items in b to change.
1575 * It is safe to drop the lock on our parent before we
1576 * go through the expensive btree search on b.
1577 *
1578 * If cow is true, then we might be changing slot zero,
1579 * which may require changing the parent. So, we can't
1580 * drop the lock until after we know which slot we're
1581 * operating on.
1582 */
1583 if (!cow)
1584 btrfs_unlock_up_safe(p, level + 1);
1585
Chris Mason123abc82007-03-14 14:14:43 -04001586 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001587 if (ret) {
1588 ret = -1;
1589 goto done;
1590 }
Chris Mason925baed2008-06-25 16:01:30 -04001591
Chris Mason5f39d392007-10-15 16:14:19 -04001592 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001593
Chris Mason5f39d392007-10-15 16:14:19 -04001594 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001595 if (ret && slot > 0)
1596 slot -= 1;
1597 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001598 if ((p->search_for_split || ins_len > 0) &&
1599 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001600 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001601 int sret;
1602
1603 sret = reada_for_balance(root, p, level);
1604 if (sret)
1605 goto again;
1606
1607 btrfs_set_path_blocking(p);
1608 sret = split_node(trans, root, p, level);
1609 btrfs_clear_path_blocking(p);
1610
Chris Mason5c680ed2007-02-22 11:39:13 -05001611 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001612 if (sret) {
1613 ret = sret;
1614 goto done;
1615 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001616 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001617 slot = p->slots[level];
Chris Mason7b78c172009-02-04 09:12:46 -05001618 } else if (ins_len < 0 &&
1619 btrfs_header_nritems(b) <
1620 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001621 int sret;
1622
1623 sret = reada_for_balance(root, p, level);
1624 if (sret)
1625 goto again;
1626
1627 btrfs_set_path_blocking(p);
1628 sret = balance_level(trans, root, p, level);
1629 btrfs_clear_path_blocking(p);
1630
Chris Mason65b51a02008-08-01 15:11:20 -04001631 if (sret) {
1632 ret = sret;
1633 goto done;
1634 }
Chris Masonbb803952007-03-01 12:04:21 -05001635 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001636 if (!b) {
1637 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001638 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001639 }
Chris Masonbb803952007-03-01 12:04:21 -05001640 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001641 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001642 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001643 unlock_up(p, level, lowest_unlock);
1644
Chris Mason9f3a7422007-08-07 15:52:19 -04001645 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001646 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001647 ret = 0;
1648 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001649 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001650
Chris Mason594a24e2008-06-25 16:01:30 -04001651 blocknr = btrfs_node_blockptr(b, slot);
1652 gen = btrfs_node_ptr_generation(b, slot);
1653 blocksize = btrfs_level_size(root, level - 1);
1654
1655 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1656 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001657 b = tmp;
1658 } else {
1659 /*
1660 * reduce lock contention at high levels
1661 * of the btree by dropping locks before
1662 * we read.
1663 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001664 if (level > 0) {
Chris Mason051e1b92008-06-25 16:01:30 -04001665 btrfs_release_path(NULL, p);
1666 if (tmp)
1667 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001668 if (should_reada)
1669 reada_for_search(root, p,
1670 level, slot,
1671 key->objectid);
1672
Chris Mason594a24e2008-06-25 16:01:30 -04001673 tmp = read_tree_block(root, blocknr,
1674 blocksize, gen);
1675 if (tmp)
1676 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001677 goto again;
1678 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001679 btrfs_set_path_blocking(p);
Chris Masona74a4b92008-06-25 16:01:31 -04001680 if (tmp)
1681 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001682 if (should_reada)
1683 reada_for_search(root, p,
1684 level, slot,
1685 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001686 b = read_node_slot(root, b, slot);
1687 }
1688 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001689 if (!p->skip_locking) {
1690 int lret;
1691
1692 btrfs_clear_path_blocking(p);
1693 lret = btrfs_try_spin_lock(b);
1694
1695 if (!lret) {
1696 btrfs_set_path_blocking(p);
1697 btrfs_tree_lock(b);
1698 btrfs_clear_path_blocking(p);
1699 }
1700 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001701 } else {
1702 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001703 if (ins_len > 0 &&
1704 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001705 int sret;
1706
1707 btrfs_set_path_blocking(p);
1708 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001709 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001710 btrfs_clear_path_blocking(p);
1711
Chris Mason5c680ed2007-02-22 11:39:13 -05001712 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001713 if (sret) {
1714 ret = sret;
1715 goto done;
1716 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001717 }
Chris Mason459931e2008-12-10 09:10:46 -05001718 if (!p->search_for_split)
1719 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001720 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001721 }
1722 }
Chris Mason65b51a02008-08-01 15:11:20 -04001723 ret = 1;
1724done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001725 /*
1726 * we don't really know what they plan on doing with the path
1727 * from here on, so for now just mark it as blocking
1728 */
1729 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001730 if (prealloc_block.objectid) {
1731 btrfs_free_reserved_extent(root,
1732 prealloc_block.objectid,
1733 prealloc_block.offset);
1734 }
Chris Mason65b51a02008-08-01 15:11:20 -04001735 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736}
1737
Zheng Yan1a40e232008-09-26 10:09:34 -04001738int btrfs_merge_path(struct btrfs_trans_handle *trans,
1739 struct btrfs_root *root,
1740 struct btrfs_key *node_keys,
1741 u64 *nodes, int lowest_level)
1742{
1743 struct extent_buffer *eb;
1744 struct extent_buffer *parent;
1745 struct btrfs_key key;
1746 u64 bytenr;
1747 u64 generation;
1748 u32 blocksize;
1749 int level;
1750 int slot;
1751 int key_match;
1752 int ret;
1753
1754 eb = btrfs_lock_root_node(root);
1755 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1756 BUG_ON(ret);
1757
Chris Masonb4ce94d2009-02-04 09:25:08 -05001758 btrfs_set_lock_blocking(eb);
1759
Zheng Yan1a40e232008-09-26 10:09:34 -04001760 parent = eb;
1761 while (1) {
1762 level = btrfs_header_level(parent);
1763 if (level == 0 || level <= lowest_level)
1764 break;
1765
1766 ret = bin_search(parent, &node_keys[lowest_level], level,
1767 &slot);
1768 if (ret && slot > 0)
1769 slot--;
1770
1771 bytenr = btrfs_node_blockptr(parent, slot);
1772 if (nodes[level - 1] == bytenr)
1773 break;
1774
1775 blocksize = btrfs_level_size(root, level - 1);
1776 generation = btrfs_node_ptr_generation(parent, slot);
1777 btrfs_node_key_to_cpu(eb, &key, slot);
1778 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1779
Yan Zhengf82d02d2008-10-29 14:49:05 -04001780 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001781 eb = read_tree_block(root, bytenr, blocksize,
1782 generation);
1783 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001784 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001785 }
1786
1787 /*
1788 * if node keys match and node pointer hasn't been modified
1789 * in the running transaction, we can merge the path. for
1790 * blocks owened by reloc trees, the node pointer check is
1791 * skipped, this is because these blocks are fully controlled
1792 * by the space balance code, no one else can modify them.
1793 */
1794 if (!nodes[level - 1] || !key_match ||
1795 (generation == trans->transid &&
1796 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1797 if (level == 1 || level == lowest_level + 1) {
1798 if (generation == trans->transid) {
1799 btrfs_tree_unlock(eb);
1800 free_extent_buffer(eb);
1801 }
1802 break;
1803 }
1804
1805 if (generation != trans->transid) {
1806 eb = read_tree_block(root, bytenr, blocksize,
1807 generation);
1808 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001809 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001810 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001811
1812 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1813 &eb, 0);
1814 BUG_ON(ret);
1815
Yan Zhengf82d02d2008-10-29 14:49:05 -04001816 if (root->root_key.objectid ==
1817 BTRFS_TREE_RELOC_OBJECTID) {
1818 if (!nodes[level - 1]) {
1819 nodes[level - 1] = eb->start;
1820 memcpy(&node_keys[level - 1], &key,
1821 sizeof(node_keys[0]));
1822 } else {
1823 WARN_ON(1);
1824 }
1825 }
1826
Zheng Yan1a40e232008-09-26 10:09:34 -04001827 btrfs_tree_unlock(parent);
1828 free_extent_buffer(parent);
1829 parent = eb;
1830 continue;
1831 }
1832
Zheng Yan1a40e232008-09-26 10:09:34 -04001833 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1834 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1835 btrfs_mark_buffer_dirty(parent);
1836
1837 ret = btrfs_inc_extent_ref(trans, root,
1838 nodes[level - 1],
1839 blocksize, parent->start,
1840 btrfs_header_owner(parent),
1841 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001842 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001843 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001844
1845 /*
1846 * If the block was created in the running transaction,
1847 * it's possible this is the last reference to it, so we
1848 * should drop the subtree.
1849 */
1850 if (generation == trans->transid) {
1851 ret = btrfs_drop_subtree(trans, root, eb, parent);
1852 BUG_ON(ret);
1853 btrfs_tree_unlock(eb);
1854 free_extent_buffer(eb);
1855 } else {
1856 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001857 blocksize, parent->start,
1858 btrfs_header_owner(parent),
1859 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001860 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001861 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001862 }
1863 break;
1864 }
1865 btrfs_tree_unlock(parent);
1866 free_extent_buffer(parent);
1867 return 0;
1868}
1869
Chris Mason74123bd2007-02-02 11:05:29 -05001870/*
1871 * adjust the pointers going up the tree, starting at level
1872 * making sure the right key of each node is points to 'key'.
1873 * This is used after shifting pointers to the left, so it stops
1874 * fixing up pointers when a given leaf/node is not in slot 0 of the
1875 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001876 *
1877 * If this fails to write a tree block, it returns -1, but continues
1878 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001879 */
Chris Mason5f39d392007-10-15 16:14:19 -04001880static int fixup_low_keys(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *root, struct btrfs_path *path,
1882 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883{
1884 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001885 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001886 struct extent_buffer *t;
1887
Chris Mason234b63a2007-03-13 10:46:10 -04001888 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001889 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001890 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001892 t = path->nodes[i];
1893 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001894 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001895 if (tslot != 0)
1896 break;
1897 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001898 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001899}
1900
Chris Mason74123bd2007-02-02 11:05:29 -05001901/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001902 * update item key.
1903 *
1904 * This function isn't completely safe. It's the caller's responsibility
1905 * that the new key won't break the order
1906 */
1907int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1908 struct btrfs_root *root, struct btrfs_path *path,
1909 struct btrfs_key *new_key)
1910{
1911 struct btrfs_disk_key disk_key;
1912 struct extent_buffer *eb;
1913 int slot;
1914
1915 eb = path->nodes[0];
1916 slot = path->slots[0];
1917 if (slot > 0) {
1918 btrfs_item_key(eb, &disk_key, slot - 1);
1919 if (comp_keys(&disk_key, new_key) >= 0)
1920 return -1;
1921 }
1922 if (slot < btrfs_header_nritems(eb) - 1) {
1923 btrfs_item_key(eb, &disk_key, slot + 1);
1924 if (comp_keys(&disk_key, new_key) <= 0)
1925 return -1;
1926 }
1927
1928 btrfs_cpu_key_to_disk(&disk_key, new_key);
1929 btrfs_set_item_key(eb, &disk_key, slot);
1930 btrfs_mark_buffer_dirty(eb);
1931 if (slot == 0)
1932 fixup_low_keys(trans, root, path, &disk_key, 1);
1933 return 0;
1934}
1935
1936/*
Chris Mason74123bd2007-02-02 11:05:29 -05001937 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001938 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001939 *
1940 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1941 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001942 */
Chris Mason98ed5172008-01-03 10:01:48 -05001943static int push_node_left(struct btrfs_trans_handle *trans,
1944 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001945 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001947 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001948 int src_nritems;
1949 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001950 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001951
Chris Mason5f39d392007-10-15 16:14:19 -04001952 src_nritems = btrfs_header_nritems(src);
1953 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001954 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001955 WARN_ON(btrfs_header_generation(src) != trans->transid);
1956 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001957
Chris Masonbce4eae2008-04-24 14:42:46 -04001958 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001959 return 1;
1960
Chris Masond3977122009-01-05 21:25:51 -05001961 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962 return 1;
1963
Chris Masonbce4eae2008-04-24 14:42:46 -04001964 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001965 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001966 if (push_items < src_nritems) {
1967 /* leave at least 8 pointers in the node if
1968 * we aren't going to empty it
1969 */
1970 if (src_nritems - push_items < 8) {
1971 if (push_items <= 8)
1972 return 1;
1973 push_items -= 8;
1974 }
1975 }
1976 } else
1977 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001978
Chris Mason5f39d392007-10-15 16:14:19 -04001979 copy_extent_buffer(dst, src,
1980 btrfs_node_key_ptr_offset(dst_nritems),
1981 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001982 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001983
Chris Masonbb803952007-03-01 12:04:21 -05001984 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001985 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1986 btrfs_node_key_ptr_offset(push_items),
1987 (src_nritems - push_items) *
1988 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001989 }
Chris Mason5f39d392007-10-15 16:14:19 -04001990 btrfs_set_header_nritems(src, src_nritems - push_items);
1991 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1992 btrfs_mark_buffer_dirty(src);
1993 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001994
1995 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1996 BUG_ON(ret);
1997
Chris Masonbb803952007-03-01 12:04:21 -05001998 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001999}
2000
Chris Mason97571fd2007-02-24 13:39:08 -05002001/*
Chris Mason79f95c82007-03-01 15:16:26 -05002002 * try to push data from one node into the next node right in the
2003 * tree.
2004 *
2005 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2006 * error, and > 0 if there was no room in the right hand block.
2007 *
2008 * this will only push up to 1/2 the contents of the left node over
2009 */
Chris Mason5f39d392007-10-15 16:14:19 -04002010static int balance_node_right(struct btrfs_trans_handle *trans,
2011 struct btrfs_root *root,
2012 struct extent_buffer *dst,
2013 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002014{
Chris Mason79f95c82007-03-01 15:16:26 -05002015 int push_items = 0;
2016 int max_push;
2017 int src_nritems;
2018 int dst_nritems;
2019 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002020
Chris Mason7bb86312007-12-11 09:25:06 -05002021 WARN_ON(btrfs_header_generation(src) != trans->transid);
2022 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2023
Chris Mason5f39d392007-10-15 16:14:19 -04002024 src_nritems = btrfs_header_nritems(src);
2025 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002026 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002027 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002028 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002029
Chris Masond3977122009-01-05 21:25:51 -05002030 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002031 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002032
2033 max_push = src_nritems / 2 + 1;
2034 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002035 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002036 return 1;
Yan252c38f2007-08-29 09:11:44 -04002037
Chris Mason79f95c82007-03-01 15:16:26 -05002038 if (max_push < push_items)
2039 push_items = max_push;
2040
Chris Mason5f39d392007-10-15 16:14:19 -04002041 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2042 btrfs_node_key_ptr_offset(0),
2043 (dst_nritems) *
2044 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 copy_extent_buffer(dst, src,
2047 btrfs_node_key_ptr_offset(0),
2048 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002049 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002050
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_set_header_nritems(src, src_nritems - push_items);
2052 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002053
Chris Mason5f39d392007-10-15 16:14:19 -04002054 btrfs_mark_buffer_dirty(src);
2055 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002056
2057 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2058 BUG_ON(ret);
2059
Chris Mason79f95c82007-03-01 15:16:26 -05002060 return ret;
2061}
2062
2063/*
Chris Mason97571fd2007-02-24 13:39:08 -05002064 * helper function to insert a new root level in the tree.
2065 * A new node is allocated, and a single item is inserted to
2066 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002067 *
2068 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002069 */
Chris Masond3977122009-01-05 21:25:51 -05002070static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002071 struct btrfs_root *root,
2072 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002073{
Chris Mason7bb86312007-12-11 09:25:06 -05002074 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002075 struct extent_buffer *lower;
2076 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002077 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002078 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002079 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002080
2081 BUG_ON(path->nodes[level]);
2082 BUG_ON(path->nodes[level-1] != root->node);
2083
Chris Mason7bb86312007-12-11 09:25:06 -05002084 lower = path->nodes[level-1];
2085 if (level == 1)
2086 btrfs_item_key(lower, &lower_key, 0);
2087 else
2088 btrfs_node_key(lower, &lower_key, 0);
2089
Zheng Yan31840ae2008-09-23 13:14:14 -04002090 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2091 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002092 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002093 if (IS_ERR(c))
2094 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002095
Chris Mason5f39d392007-10-15 16:14:19 -04002096 memset_extent_buffer(c, 0, 0, root->nodesize);
2097 btrfs_set_header_nritems(c, 1);
2098 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002099 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 btrfs_set_header_generation(c, trans->transid);
2101 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002102
Chris Mason5f39d392007-10-15 16:14:19 -04002103 write_extent_buffer(c, root->fs_info->fsid,
2104 (unsigned long)btrfs_header_fsid(c),
2105 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002106
2107 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2108 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2109 BTRFS_UUID_SIZE);
2110
Chris Mason5f39d392007-10-15 16:14:19 -04002111 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002112 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002113 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002114 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002115
2116 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002117
2118 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002119
Chris Mason925baed2008-06-25 16:01:30 -04002120 spin_lock(&root->node_lock);
2121 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002122 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002123 spin_unlock(&root->node_lock);
2124
Zheng Yan31840ae2008-09-23 13:14:14 -04002125 ret = btrfs_update_extent_ref(trans, root, lower->start,
2126 lower->start, c->start,
2127 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002128 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002129 BUG_ON(ret);
2130
Chris Mason925baed2008-06-25 16:01:30 -04002131 /* the super has an extra ref to root->node */
2132 free_extent_buffer(old);
2133
Chris Mason0b86a832008-03-24 15:01:56 -04002134 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002135 extent_buffer_get(c);
2136 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002137 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002138 path->slots[level] = 0;
2139 return 0;
2140}
2141
Chris Mason74123bd2007-02-02 11:05:29 -05002142/*
2143 * worker function to insert a single pointer in a node.
2144 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002145 *
Chris Mason74123bd2007-02-02 11:05:29 -05002146 * slot and level indicate where you want the key to go, and
2147 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002148 *
2149 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002150 */
Chris Masone089f052007-03-16 16:20:31 -04002151static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2152 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002153 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002154{
Chris Mason5f39d392007-10-15 16:14:19 -04002155 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002156 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002157
2158 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002159 lower = path->nodes[level];
2160 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002161 if (slot > nritems)
2162 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002163 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002164 BUG();
2165 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002166 memmove_extent_buffer(lower,
2167 btrfs_node_key_ptr_offset(slot + 1),
2168 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002169 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002170 }
Chris Mason5f39d392007-10-15 16:14:19 -04002171 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002172 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002173 WARN_ON(trans->transid == 0);
2174 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002175 btrfs_set_header_nritems(lower, nritems + 1);
2176 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002177 return 0;
2178}
2179
Chris Mason97571fd2007-02-24 13:39:08 -05002180/*
2181 * split the node at the specified level in path in two.
2182 * The path is corrected to point to the appropriate node after the split
2183 *
2184 * Before splitting this tries to make some room in the node by pushing
2185 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002186 *
2187 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002188 */
Chris Masone02119d2008-09-05 16:13:11 -04002189static noinline int split_node(struct btrfs_trans_handle *trans,
2190 struct btrfs_root *root,
2191 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002192{
Chris Mason5f39d392007-10-15 16:14:19 -04002193 struct extent_buffer *c;
2194 struct extent_buffer *split;
2195 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002196 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002197 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002198 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002199 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002200
Chris Mason5f39d392007-10-15 16:14:19 -04002201 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002202 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002203 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002204 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002205 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002206 if (ret)
2207 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002208 } else {
2209 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002210 c = path->nodes[level];
2211 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002212 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002213 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002214 if (ret < 0)
2215 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002216 }
Chris Masone66f7092007-04-20 13:16:02 -04002217
Chris Mason5f39d392007-10-15 16:14:19 -04002218 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002219
Chris Mason925baed2008-06-25 16:01:30 -04002220 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002221 path->nodes[level + 1]->start,
2222 root->root_key.objectid,
2223 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002224 if (IS_ERR(split))
2225 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002226
Chris Mason5f39d392007-10-15 16:14:19 -04002227 btrfs_set_header_flags(split, btrfs_header_flags(c));
2228 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002229 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002230 btrfs_set_header_generation(split, trans->transid);
2231 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002232 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002233 write_extent_buffer(split, root->fs_info->fsid,
2234 (unsigned long)btrfs_header_fsid(split),
2235 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002236 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2237 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2238 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002239
Chris Mason7518a232007-03-12 12:01:18 -04002240 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002241
2242 copy_extent_buffer(split, c,
2243 btrfs_node_key_ptr_offset(0),
2244 btrfs_node_key_ptr_offset(mid),
2245 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2246 btrfs_set_header_nritems(split, c_nritems - mid);
2247 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002248 ret = 0;
2249
Chris Mason5f39d392007-10-15 16:14:19 -04002250 btrfs_mark_buffer_dirty(c);
2251 btrfs_mark_buffer_dirty(split);
2252
2253 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002254 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002255 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002256 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002257 if (wret)
2258 ret = wret;
2259
Zheng Yan31840ae2008-09-23 13:14:14 -04002260 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2261 BUG_ON(ret);
2262
Chris Mason5de08d72007-02-24 06:24:44 -05002263 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002264 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002265 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002266 free_extent_buffer(c);
2267 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002268 path->slots[level + 1] += 1;
2269 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002270 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002271 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002272 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002273 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002274}
2275
Chris Mason74123bd2007-02-02 11:05:29 -05002276/*
2277 * how many bytes are required to store the items in a leaf. start
2278 * and nr indicate which items in the leaf to check. This totals up the
2279 * space used both by the item structs and the item data
2280 */
Chris Mason5f39d392007-10-15 16:14:19 -04002281static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002282{
2283 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002284 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002285 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002286
2287 if (!nr)
2288 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002289 data_len = btrfs_item_end_nr(l, start);
2290 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002291 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002292 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002293 return data_len;
2294}
2295
Chris Mason74123bd2007-02-02 11:05:29 -05002296/*
Chris Masond4dbff92007-04-04 14:08:15 -04002297 * The space between the end of the leaf items and
2298 * the start of the leaf data. IOW, how much room
2299 * the leaf has left for both items and data
2300 */
Chris Masond3977122009-01-05 21:25:51 -05002301noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002302 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002303{
Chris Mason5f39d392007-10-15 16:14:19 -04002304 int nritems = btrfs_header_nritems(leaf);
2305 int ret;
2306 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2307 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002308 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2309 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002310 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002311 leaf_space_used(leaf, 0, nritems), nritems);
2312 }
2313 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002314}
2315
2316/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002317 * push some data in the path leaf to the right, trying to free up at
2318 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002319 *
2320 * returns 1 if the push failed because the other node didn't have enough
2321 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002322 */
Chris Masone089f052007-03-16 16:20:31 -04002323static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002324 *root, struct btrfs_path *path, int data_size,
2325 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002326{
Chris Mason5f39d392007-10-15 16:14:19 -04002327 struct extent_buffer *left = path->nodes[0];
2328 struct extent_buffer *right;
2329 struct extent_buffer *upper;
2330 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002331 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002332 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002333 int free_space;
2334 int push_space = 0;
2335 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002336 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002337 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002338 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002339 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002340 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002341 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002342 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002343
2344 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002345 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002346 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002347
Chris Mason00ec4c52007-02-24 12:47:20 -05002348 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002349 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002351
Chris Masona2135012008-06-25 16:01:30 -04002352 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2353
Chris Masonca7a79a2008-05-12 12:59:19 -04002354 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002355 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002356 btrfs_set_lock_blocking(right);
2357
Chris Mason123abc82007-03-14 14:14:43 -04002358 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002359 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002360 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002361
Chris Mason5f39d392007-10-15 16:14:19 -04002362 /* cow and double check */
2363 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002364 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002365 if (ret)
2366 goto out_unlock;
2367
Chris Mason5f39d392007-10-15 16:14:19 -04002368 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002369 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002370 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002371
2372 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002373 if (left_nritems == 0)
2374 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002375
Chris Mason34a38212007-11-07 13:31:03 -05002376 if (empty)
2377 nr = 0;
2378 else
2379 nr = 1;
2380
Zheng Yan31840ae2008-09-23 13:14:14 -04002381 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002382 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002383
Chris Mason34a38212007-11-07 13:31:03 -05002384 i = left_nritems - 1;
2385 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002386 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002387
Zheng Yan31840ae2008-09-23 13:14:14 -04002388 if (!empty && push_items > 0) {
2389 if (path->slots[0] > i)
2390 break;
2391 if (path->slots[0] == i) {
2392 int space = btrfs_leaf_free_space(root, left);
2393 if (space + push_space * 2 > free_space)
2394 break;
2395 }
2396 }
2397
Chris Mason00ec4c52007-02-24 12:47:20 -05002398 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002399 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002400
2401 if (!left->map_token) {
2402 map_extent_buffer(left, (unsigned long)item,
2403 sizeof(struct btrfs_item),
2404 &left->map_token, &left->kaddr,
2405 &left->map_start, &left->map_len,
2406 KM_USER1);
2407 }
2408
2409 this_item_size = btrfs_item_size(left, item);
2410 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002411 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002412
Chris Mason00ec4c52007-02-24 12:47:20 -05002413 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002414 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002415 if (i == 0)
2416 break;
2417 i--;
Chris Masondb945352007-10-15 16:15:53 -04002418 }
2419 if (left->map_token) {
2420 unmap_extent_buffer(left, left->map_token, KM_USER1);
2421 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002422 }
Chris Mason5f39d392007-10-15 16:14:19 -04002423
Chris Mason925baed2008-06-25 16:01:30 -04002424 if (push_items == 0)
2425 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002426
Chris Mason34a38212007-11-07 13:31:03 -05002427 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002428 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002429
Chris Mason00ec4c52007-02-24 12:47:20 -05002430 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002431 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002434 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002435
Chris Mason00ec4c52007-02-24 12:47:20 -05002436 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002437 data_end = leaf_data_end(root, right);
2438 memmove_extent_buffer(right,
2439 btrfs_leaf_data(right) + data_end - push_space,
2440 btrfs_leaf_data(right) + data_end,
2441 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2442
Chris Mason00ec4c52007-02-24 12:47:20 -05002443 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002444 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002445 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2446 btrfs_leaf_data(left) + leaf_data_end(root, left),
2447 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002448
2449 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2450 btrfs_item_nr_offset(0),
2451 right_nritems * sizeof(struct btrfs_item));
2452
Chris Mason00ec4c52007-02-24 12:47:20 -05002453 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002454 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2455 btrfs_item_nr_offset(left_nritems - push_items),
2456 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002457
2458 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002459 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002460 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002461 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002462 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002463 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002464 if (!right->map_token) {
2465 map_extent_buffer(right, (unsigned long)item,
2466 sizeof(struct btrfs_item),
2467 &right->map_token, &right->kaddr,
2468 &right->map_start, &right->map_len,
2469 KM_USER1);
2470 }
2471 push_space -= btrfs_item_size(right, item);
2472 btrfs_set_item_offset(right, item, push_space);
2473 }
2474
2475 if (right->map_token) {
2476 unmap_extent_buffer(right, right->map_token, KM_USER1);
2477 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002478 }
Chris Mason7518a232007-03-12 12:01:18 -04002479 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002480 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002481
Chris Mason34a38212007-11-07 13:31:03 -05002482 if (left_nritems)
2483 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002484 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002485
Zheng Yan31840ae2008-09-23 13:14:14 -04002486 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2487 BUG_ON(ret);
2488
Chris Mason5f39d392007-10-15 16:14:19 -04002489 btrfs_item_key(right, &disk_key, 0);
2490 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002491 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002492
Chris Mason00ec4c52007-02-24 12:47:20 -05002493 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002494 if (path->slots[0] >= left_nritems) {
2495 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002496 if (btrfs_header_nritems(path->nodes[0]) == 0)
2497 clean_tree_block(trans, root, path->nodes[0]);
2498 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002499 free_extent_buffer(path->nodes[0]);
2500 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002501 path->slots[1] += 1;
2502 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002503 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002504 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002505 }
2506 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002507
2508out_unlock:
2509 btrfs_tree_unlock(right);
2510 free_extent_buffer(right);
2511 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002512}
Chris Mason925baed2008-06-25 16:01:30 -04002513
Chris Mason00ec4c52007-02-24 12:47:20 -05002514/*
Chris Mason74123bd2007-02-02 11:05:29 -05002515 * push some data in the path leaf to the left, trying to free up at
2516 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2517 */
Chris Masone089f052007-03-16 16:20:31 -04002518static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002519 *root, struct btrfs_path *path, int data_size,
2520 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002521{
Chris Mason5f39d392007-10-15 16:14:19 -04002522 struct btrfs_disk_key disk_key;
2523 struct extent_buffer *right = path->nodes[0];
2524 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002525 int slot;
2526 int i;
2527 int free_space;
2528 int push_space = 0;
2529 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002530 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002531 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002532 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002533 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002534 int ret = 0;
2535 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002536 u32 this_item_size;
2537 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002538
2539 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002540 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002541 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002542 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002543 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002544
Chris Mason3685f792007-10-19 09:23:27 -04002545 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002546 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002547 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002548
Chris Masona2135012008-06-25 16:01:30 -04002549 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2550
Chris Masonca7a79a2008-05-12 12:59:19 -04002551 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002552 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002553 btrfs_set_lock_blocking(left);
2554
Chris Mason123abc82007-03-14 14:14:43 -04002555 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002556 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002557 ret = 1;
2558 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 }
Chris Mason02217ed2007-03-02 16:08:05 -05002560
2561 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002562 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002563 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002564 if (ret) {
2565 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002566 ret = 1;
2567 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002568 }
Chris Mason3685f792007-10-19 09:23:27 -04002569
Chris Mason123abc82007-03-14 14:14:43 -04002570 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002571 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002572 ret = 1;
2573 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002574 }
2575
Chris Mason34a38212007-11-07 13:31:03 -05002576 if (empty)
2577 nr = right_nritems;
2578 else
2579 nr = right_nritems - 1;
2580
2581 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002582 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002583 if (!right->map_token) {
2584 map_extent_buffer(right, (unsigned long)item,
2585 sizeof(struct btrfs_item),
2586 &right->map_token, &right->kaddr,
2587 &right->map_start, &right->map_len,
2588 KM_USER1);
2589 }
2590
Zheng Yan31840ae2008-09-23 13:14:14 -04002591 if (!empty && push_items > 0) {
2592 if (path->slots[0] < i)
2593 break;
2594 if (path->slots[0] == i) {
2595 int space = btrfs_leaf_free_space(root, right);
2596 if (space + push_space * 2 > free_space)
2597 break;
2598 }
2599 }
2600
Chris Masonbe0e5c02007-01-26 15:51:26 -05002601 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002602 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002603
2604 this_item_size = btrfs_item_size(right, item);
2605 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002606 break;
Chris Masondb945352007-10-15 16:15:53 -04002607
Chris Masonbe0e5c02007-01-26 15:51:26 -05002608 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002609 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 }
Chris Masondb945352007-10-15 16:15:53 -04002611
2612 if (right->map_token) {
2613 unmap_extent_buffer(right, right->map_token, KM_USER1);
2614 right->map_token = NULL;
2615 }
2616
Chris Masonbe0e5c02007-01-26 15:51:26 -05002617 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002618 ret = 1;
2619 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002620 }
Chris Mason34a38212007-11-07 13:31:03 -05002621 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002622 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002623
Chris Masonbe0e5c02007-01-26 15:51:26 -05002624 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002625 copy_extent_buffer(left, right,
2626 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2627 btrfs_item_nr_offset(0),
2628 push_items * sizeof(struct btrfs_item));
2629
Chris Mason123abc82007-03-14 14:14:43 -04002630 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002631 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002632
2633 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002634 leaf_data_end(root, left) - push_space,
2635 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002636 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002637 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002638 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002639 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002640
Chris Masondb945352007-10-15 16:15:53 -04002641 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002642 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002643 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002644
Chris Mason5f39d392007-10-15 16:14:19 -04002645 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002646 if (!left->map_token) {
2647 map_extent_buffer(left, (unsigned long)item,
2648 sizeof(struct btrfs_item),
2649 &left->map_token, &left->kaddr,
2650 &left->map_start, &left->map_len,
2651 KM_USER1);
2652 }
2653
Chris Mason5f39d392007-10-15 16:14:19 -04002654 ioff = btrfs_item_offset(left, item);
2655 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002656 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002657 }
Chris Mason5f39d392007-10-15 16:14:19 -04002658 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002659 if (left->map_token) {
2660 unmap_extent_buffer(left, left->map_token, KM_USER1);
2661 left->map_token = NULL;
2662 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002663
2664 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002665 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002666 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2667 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002668 WARN_ON(1);
2669 }
Chris Mason5f39d392007-10-15 16:14:19 -04002670
Chris Mason34a38212007-11-07 13:31:03 -05002671 if (push_items < right_nritems) {
2672 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2673 leaf_data_end(root, right);
2674 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2675 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2676 btrfs_leaf_data(right) +
2677 leaf_data_end(root, right), push_space);
2678
2679 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002680 btrfs_item_nr_offset(push_items),
2681 (btrfs_header_nritems(right) - push_items) *
2682 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002683 }
Yaneef1c492007-11-26 10:58:13 -05002684 right_nritems -= push_items;
2685 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002686 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002687 for (i = 0; i < right_nritems; i++) {
2688 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002689
2690 if (!right->map_token) {
2691 map_extent_buffer(right, (unsigned long)item,
2692 sizeof(struct btrfs_item),
2693 &right->map_token, &right->kaddr,
2694 &right->map_start, &right->map_len,
2695 KM_USER1);
2696 }
2697
2698 push_space = push_space - btrfs_item_size(right, item);
2699 btrfs_set_item_offset(right, item, push_space);
2700 }
2701 if (right->map_token) {
2702 unmap_extent_buffer(right, right->map_token, KM_USER1);
2703 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002705
Chris Mason5f39d392007-10-15 16:14:19 -04002706 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002707 if (right_nritems)
2708 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002709
Zheng Yan31840ae2008-09-23 13:14:14 -04002710 ret = btrfs_update_ref(trans, root, right, left,
2711 old_left_nritems, push_items);
2712 BUG_ON(ret);
2713
Chris Mason5f39d392007-10-15 16:14:19 -04002714 btrfs_item_key(right, &disk_key, 0);
2715 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002716 if (wret)
2717 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002718
2719 /* then fixup the leaf pointer in the path */
2720 if (path->slots[0] < push_items) {
2721 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002722 if (btrfs_header_nritems(path->nodes[0]) == 0)
2723 clean_tree_block(trans, root, path->nodes[0]);
2724 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 free_extent_buffer(path->nodes[0]);
2726 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002727 path->slots[1] -= 1;
2728 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002729 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002730 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002731 path->slots[0] -= push_items;
2732 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002733 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002734 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002735out:
2736 btrfs_tree_unlock(left);
2737 free_extent_buffer(left);
2738 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002739}
2740
Chris Mason74123bd2007-02-02 11:05:29 -05002741/*
2742 * split the path's leaf in two, making sure there is at least data_size
2743 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002744 *
2745 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002746 */
Chris Masone02119d2008-09-05 16:13:11 -04002747static noinline int split_leaf(struct btrfs_trans_handle *trans,
2748 struct btrfs_root *root,
2749 struct btrfs_key *ins_key,
2750 struct btrfs_path *path, int data_size,
2751 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002752{
Chris Mason5f39d392007-10-15 16:14:19 -04002753 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002754 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002755 int mid;
2756 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002757 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002758 int data_copy_size;
2759 int rt_data_off;
2760 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002761 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002762 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002763 int double_split;
2764 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002765 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002766
Chris Mason40689472007-03-17 14:29:23 -04002767 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002768 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002769 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002770 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002771 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002772 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002773 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002774 if (wret < 0)
2775 return wret;
2776 }
2777 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002778
Chris Mason3685f792007-10-19 09:23:27 -04002779 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002780 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002781 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002782 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002783
Chris Mason5c680ed2007-02-22 11:39:13 -05002784 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002785 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002786 if (ret)
2787 return ret;
2788 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002789again:
2790 double_split = 0;
2791 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002792 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002793 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002794 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002795
Chris Mason925baed2008-06-25 16:01:30 -04002796 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002797 path->nodes[1]->start,
2798 root->root_key.objectid,
2799 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002800 if (IS_ERR(right)) {
2801 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002802 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002803 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002804
Chris Mason5f39d392007-10-15 16:14:19 -04002805 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002806 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002807 btrfs_set_header_generation(right, trans->transid);
2808 btrfs_set_header_owner(right, root->root_key.objectid);
2809 btrfs_set_header_level(right, 0);
2810 write_extent_buffer(right, root->fs_info->fsid,
2811 (unsigned long)btrfs_header_fsid(right),
2812 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002813
2814 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2815 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2816 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002817 if (mid <= slot) {
2818 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002819 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002820 BTRFS_LEAF_DATA_SIZE(root)) {
2821 if (slot >= nritems) {
2822 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002824 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002825 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002826 path->slots[1] + 1, 1);
2827 if (wret)
2828 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002829
2830 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002831 free_extent_buffer(path->nodes[0]);
2832 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002833 path->slots[0] = 0;
2834 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002835 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002836 return ret;
2837 }
2838 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002839 if (mid != nritems &&
2840 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002841 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002842 double_split = 1;
2843 }
Chris Masond4dbff92007-04-04 14:08:15 -04002844 }
2845 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002846 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002847 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002848 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002849 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002850 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002851 wret = insert_ptr(trans, root, path,
2852 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002853 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002854 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002855 if (wret)
2856 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002857 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002858 free_extent_buffer(path->nodes[0]);
2859 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002860 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002861 if (path->slots[1] == 0) {
2862 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002863 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002864 if (wret)
2865 ret = wret;
2866 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002867 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002868 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002869 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002870 mid = 1;
2871 } else {
2872 mid = slot;
2873 if (mid != nritems &&
2874 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002875 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002876 double_split = 1;
2877 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002878 }
Chris Masond4dbff92007-04-04 14:08:15 -04002879 }
2880 }
Chris Mason5f39d392007-10-15 16:14:19 -04002881 nritems = nritems - mid;
2882 btrfs_set_header_nritems(right, nritems);
2883 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2884
2885 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2886 btrfs_item_nr_offset(mid),
2887 nritems * sizeof(struct btrfs_item));
2888
2889 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002890 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2891 data_copy_size, btrfs_leaf_data(l) +
2892 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002893
Chris Mason5f39d392007-10-15 16:14:19 -04002894 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2895 btrfs_item_end_nr(l, mid);
2896
2897 for (i = 0; i < nritems; i++) {
2898 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002899 u32 ioff;
2900
2901 if (!right->map_token) {
2902 map_extent_buffer(right, (unsigned long)item,
2903 sizeof(struct btrfs_item),
2904 &right->map_token, &right->kaddr,
2905 &right->map_start, &right->map_len,
2906 KM_USER1);
2907 }
2908
2909 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002910 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002911 }
Chris Mason74123bd2007-02-02 11:05:29 -05002912
Chris Masondb945352007-10-15 16:15:53 -04002913 if (right->map_token) {
2914 unmap_extent_buffer(right, right->map_token, KM_USER1);
2915 right->map_token = NULL;
2916 }
2917
Chris Mason5f39d392007-10-15 16:14:19 -04002918 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002919 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002920 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002921 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2922 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002923 if (wret)
2924 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002925
2926 btrfs_mark_buffer_dirty(right);
2927 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002928 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002929
Zheng Yan31840ae2008-09-23 13:14:14 -04002930 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2931 BUG_ON(ret);
2932
Chris Masonbe0e5c02007-01-26 15:51:26 -05002933 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002934 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002935 free_extent_buffer(path->nodes[0]);
2936 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002937 path->slots[0] -= mid;
2938 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002939 } else {
2940 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002941 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002942 }
Chris Mason5f39d392007-10-15 16:14:19 -04002943
Chris Masoneb60cea2007-02-02 09:18:22 -05002944 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002945
Chris Masoncc0c5532007-10-25 15:42:57 -04002946 if (double_split) {
2947 BUG_ON(num_doubles != 0);
2948 num_doubles++;
2949 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002950 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002951 return ret;
2952}
2953
Chris Masond352ac62008-09-29 15:18:18 -04002954/*
Chris Mason459931e2008-12-10 09:10:46 -05002955 * This function splits a single item into two items,
2956 * giving 'new_key' to the new item and splitting the
2957 * old one at split_offset (from the start of the item).
2958 *
2959 * The path may be released by this operation. After
2960 * the split, the path is pointing to the old item. The
2961 * new item is going to be in the same node as the old one.
2962 *
2963 * Note, the item being split must be smaller enough to live alone on
2964 * a tree block with room for one extra struct btrfs_item
2965 *
2966 * This allows us to split the item in place, keeping a lock on the
2967 * leaf the entire time.
2968 */
2969int btrfs_split_item(struct btrfs_trans_handle *trans,
2970 struct btrfs_root *root,
2971 struct btrfs_path *path,
2972 struct btrfs_key *new_key,
2973 unsigned long split_offset)
2974{
2975 u32 item_size;
2976 struct extent_buffer *leaf;
2977 struct btrfs_key orig_key;
2978 struct btrfs_item *item;
2979 struct btrfs_item *new_item;
2980 int ret = 0;
2981 int slot;
2982 u32 nritems;
2983 u32 orig_offset;
2984 struct btrfs_disk_key disk_key;
2985 char *buf;
2986
2987 leaf = path->nodes[0];
2988 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2989 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2990 goto split;
2991
2992 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2993 btrfs_release_path(root, path);
2994
2995 path->search_for_split = 1;
2996 path->keep_locks = 1;
2997
2998 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2999 path->search_for_split = 0;
3000
3001 /* if our item isn't there or got smaller, return now */
3002 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3003 path->slots[0])) {
3004 path->keep_locks = 0;
3005 return -EAGAIN;
3006 }
3007
Yan Zheng87b29b22008-12-17 10:21:48 -05003008 ret = split_leaf(trans, root, &orig_key, path,
3009 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003010 path->keep_locks = 0;
3011 BUG_ON(ret);
3012
Chris Masonb4ce94d2009-02-04 09:25:08 -05003013 /*
3014 * make sure any changes to the path from split_leaf leave it
3015 * in a blocking state
3016 */
3017 btrfs_set_path_blocking(path);
3018
Chris Mason459931e2008-12-10 09:10:46 -05003019 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05003020 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003021
3022split:
3023 item = btrfs_item_nr(leaf, path->slots[0]);
3024 orig_offset = btrfs_item_offset(leaf, item);
3025 item_size = btrfs_item_size(leaf, item);
3026
3027
3028 buf = kmalloc(item_size, GFP_NOFS);
3029 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3030 path->slots[0]), item_size);
3031 slot = path->slots[0] + 1;
3032 leaf = path->nodes[0];
3033
3034 nritems = btrfs_header_nritems(leaf);
3035
3036 if (slot != nritems) {
3037 /* shift the items */
3038 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3039 btrfs_item_nr_offset(slot),
3040 (nritems - slot) * sizeof(struct btrfs_item));
3041
3042 }
3043
3044 btrfs_cpu_key_to_disk(&disk_key, new_key);
3045 btrfs_set_item_key(leaf, &disk_key, slot);
3046
3047 new_item = btrfs_item_nr(leaf, slot);
3048
3049 btrfs_set_item_offset(leaf, new_item, orig_offset);
3050 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3051
3052 btrfs_set_item_offset(leaf, item,
3053 orig_offset + item_size - split_offset);
3054 btrfs_set_item_size(leaf, item, split_offset);
3055
3056 btrfs_set_header_nritems(leaf, nritems + 1);
3057
3058 /* write the data for the start of the original item */
3059 write_extent_buffer(leaf, buf,
3060 btrfs_item_ptr_offset(leaf, path->slots[0]),
3061 split_offset);
3062
3063 /* write the data for the new item */
3064 write_extent_buffer(leaf, buf + split_offset,
3065 btrfs_item_ptr_offset(leaf, slot),
3066 item_size - split_offset);
3067 btrfs_mark_buffer_dirty(leaf);
3068
3069 ret = 0;
3070 if (btrfs_leaf_free_space(root, leaf) < 0) {
3071 btrfs_print_leaf(root, leaf);
3072 BUG();
3073 }
3074 kfree(buf);
3075 return ret;
3076}
3077
3078/*
Chris Masond352ac62008-09-29 15:18:18 -04003079 * make the item pointed to by the path smaller. new_size indicates
3080 * how small to make it, and from_end tells us if we just chop bytes
3081 * off the end of the item or if we shift the item to chop bytes off
3082 * the front.
3083 */
Chris Masonb18c6682007-04-17 13:26:50 -04003084int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3085 struct btrfs_root *root,
3086 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003087 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003088{
3089 int ret = 0;
3090 int slot;
3091 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003092 struct extent_buffer *leaf;
3093 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003094 u32 nritems;
3095 unsigned int data_end;
3096 unsigned int old_data_start;
3097 unsigned int old_size;
3098 unsigned int size_diff;
3099 int i;
3100
3101 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003102 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003103 slot = path->slots[0];
3104
3105 old_size = btrfs_item_size_nr(leaf, slot);
3106 if (old_size == new_size)
3107 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003108
Chris Mason5f39d392007-10-15 16:14:19 -04003109 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003110 data_end = leaf_data_end(root, leaf);
3111
Chris Mason5f39d392007-10-15 16:14:19 -04003112 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003113
Chris Masonb18c6682007-04-17 13:26:50 -04003114 size_diff = old_size - new_size;
3115
3116 BUG_ON(slot < 0);
3117 BUG_ON(slot >= nritems);
3118
3119 /*
3120 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3121 */
3122 /* first correct the data pointers */
3123 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003124 u32 ioff;
3125 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003126
3127 if (!leaf->map_token) {
3128 map_extent_buffer(leaf, (unsigned long)item,
3129 sizeof(struct btrfs_item),
3130 &leaf->map_token, &leaf->kaddr,
3131 &leaf->map_start, &leaf->map_len,
3132 KM_USER1);
3133 }
3134
Chris Mason5f39d392007-10-15 16:14:19 -04003135 ioff = btrfs_item_offset(leaf, item);
3136 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003137 }
Chris Masondb945352007-10-15 16:15:53 -04003138
3139 if (leaf->map_token) {
3140 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3141 leaf->map_token = NULL;
3142 }
3143
Chris Masonb18c6682007-04-17 13:26:50 -04003144 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003145 if (from_end) {
3146 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3147 data_end + size_diff, btrfs_leaf_data(leaf) +
3148 data_end, old_data_start + new_size - data_end);
3149 } else {
3150 struct btrfs_disk_key disk_key;
3151 u64 offset;
3152
3153 btrfs_item_key(leaf, &disk_key, slot);
3154
3155 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3156 unsigned long ptr;
3157 struct btrfs_file_extent_item *fi;
3158
3159 fi = btrfs_item_ptr(leaf, slot,
3160 struct btrfs_file_extent_item);
3161 fi = (struct btrfs_file_extent_item *)(
3162 (unsigned long)fi - size_diff);
3163
3164 if (btrfs_file_extent_type(leaf, fi) ==
3165 BTRFS_FILE_EXTENT_INLINE) {
3166 ptr = btrfs_item_ptr_offset(leaf, slot);
3167 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003168 (unsigned long)fi,
3169 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003170 disk_bytenr));
3171 }
3172 }
3173
3174 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3175 data_end + size_diff, btrfs_leaf_data(leaf) +
3176 data_end, old_data_start - data_end);
3177
3178 offset = btrfs_disk_key_offset(&disk_key);
3179 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3180 btrfs_set_item_key(leaf, &disk_key, slot);
3181 if (slot == 0)
3182 fixup_low_keys(trans, root, path, &disk_key, 1);
3183 }
Chris Mason5f39d392007-10-15 16:14:19 -04003184
3185 item = btrfs_item_nr(leaf, slot);
3186 btrfs_set_item_size(leaf, item, new_size);
3187 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003188
3189 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003190 if (btrfs_leaf_free_space(root, leaf) < 0) {
3191 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003192 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003193 }
Chris Masonb18c6682007-04-17 13:26:50 -04003194 return ret;
3195}
3196
Chris Masond352ac62008-09-29 15:18:18 -04003197/*
3198 * make the item pointed to by the path bigger, data_size is the new size.
3199 */
Chris Mason5f39d392007-10-15 16:14:19 -04003200int btrfs_extend_item(struct btrfs_trans_handle *trans,
3201 struct btrfs_root *root, struct btrfs_path *path,
3202 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003203{
3204 int ret = 0;
3205 int slot;
3206 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003207 struct extent_buffer *leaf;
3208 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003209 u32 nritems;
3210 unsigned int data_end;
3211 unsigned int old_data;
3212 unsigned int old_size;
3213 int i;
3214
3215 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003216 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003217
Chris Mason5f39d392007-10-15 16:14:19 -04003218 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003219 data_end = leaf_data_end(root, leaf);
3220
Chris Mason5f39d392007-10-15 16:14:19 -04003221 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3222 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003223 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003224 }
Chris Mason6567e832007-04-16 09:22:45 -04003225 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003226 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003227
3228 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003229 if (slot >= nritems) {
3230 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003231 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3232 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003233 BUG_ON(1);
3234 }
Chris Mason6567e832007-04-16 09:22:45 -04003235
3236 /*
3237 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3238 */
3239 /* first correct the data pointers */
3240 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003241 u32 ioff;
3242 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003243
3244 if (!leaf->map_token) {
3245 map_extent_buffer(leaf, (unsigned long)item,
3246 sizeof(struct btrfs_item),
3247 &leaf->map_token, &leaf->kaddr,
3248 &leaf->map_start, &leaf->map_len,
3249 KM_USER1);
3250 }
Chris Mason5f39d392007-10-15 16:14:19 -04003251 ioff = btrfs_item_offset(leaf, item);
3252 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003253 }
Chris Mason5f39d392007-10-15 16:14:19 -04003254
Chris Masondb945352007-10-15 16:15:53 -04003255 if (leaf->map_token) {
3256 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3257 leaf->map_token = NULL;
3258 }
3259
Chris Mason6567e832007-04-16 09:22:45 -04003260 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003261 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003262 data_end - data_size, btrfs_leaf_data(leaf) +
3263 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003264
Chris Mason6567e832007-04-16 09:22:45 -04003265 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003266 old_size = btrfs_item_size_nr(leaf, slot);
3267 item = btrfs_item_nr(leaf, slot);
3268 btrfs_set_item_size(leaf, item, old_size + data_size);
3269 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003270
3271 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003272 if (btrfs_leaf_free_space(root, leaf) < 0) {
3273 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003274 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003275 }
Chris Mason6567e832007-04-16 09:22:45 -04003276 return ret;
3277}
3278
Chris Mason74123bd2007-02-02 11:05:29 -05003279/*
Chris Masond352ac62008-09-29 15:18:18 -04003280 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003281 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003282 * Returns the number of keys that were inserted.
3283 */
3284int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3285 struct btrfs_root *root,
3286 struct btrfs_path *path,
3287 struct btrfs_key *cpu_key, u32 *data_size,
3288 int nr)
3289{
3290 struct extent_buffer *leaf;
3291 struct btrfs_item *item;
3292 int ret = 0;
3293 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003294 int i;
3295 u32 nritems;
3296 u32 total_data = 0;
3297 u32 total_size = 0;
3298 unsigned int data_end;
3299 struct btrfs_disk_key disk_key;
3300 struct btrfs_key found_key;
3301
Yan Zheng87b29b22008-12-17 10:21:48 -05003302 for (i = 0; i < nr; i++) {
3303 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3304 BTRFS_LEAF_DATA_SIZE(root)) {
3305 break;
3306 nr = i;
3307 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003308 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003309 total_size += data_size[i] + sizeof(struct btrfs_item);
3310 }
3311 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003312
Josef Bacikf3465ca2008-11-12 14:19:50 -05003313 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3314 if (ret == 0)
3315 return -EEXIST;
3316 if (ret < 0)
3317 goto out;
3318
Josef Bacikf3465ca2008-11-12 14:19:50 -05003319 leaf = path->nodes[0];
3320
3321 nritems = btrfs_header_nritems(leaf);
3322 data_end = leaf_data_end(root, leaf);
3323
3324 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3325 for (i = nr; i >= 0; i--) {
3326 total_data -= data_size[i];
3327 total_size -= data_size[i] + sizeof(struct btrfs_item);
3328 if (total_size < btrfs_leaf_free_space(root, leaf))
3329 break;
3330 }
3331 nr = i;
3332 }
3333
3334 slot = path->slots[0];
3335 BUG_ON(slot < 0);
3336
3337 if (slot != nritems) {
3338 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3339
3340 item = btrfs_item_nr(leaf, slot);
3341 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3342
3343 /* figure out how many keys we can insert in here */
3344 total_data = data_size[0];
3345 for (i = 1; i < nr; i++) {
3346 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3347 break;
3348 total_data += data_size[i];
3349 }
3350 nr = i;
3351
3352 if (old_data < data_end) {
3353 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003354 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003355 slot, old_data, data_end);
3356 BUG_ON(1);
3357 }
3358 /*
3359 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3360 */
3361 /* first correct the data pointers */
3362 WARN_ON(leaf->map_token);
3363 for (i = slot; i < nritems; i++) {
3364 u32 ioff;
3365
3366 item = btrfs_item_nr(leaf, i);
3367 if (!leaf->map_token) {
3368 map_extent_buffer(leaf, (unsigned long)item,
3369 sizeof(struct btrfs_item),
3370 &leaf->map_token, &leaf->kaddr,
3371 &leaf->map_start, &leaf->map_len,
3372 KM_USER1);
3373 }
3374
3375 ioff = btrfs_item_offset(leaf, item);
3376 btrfs_set_item_offset(leaf, item, ioff - total_data);
3377 }
3378 if (leaf->map_token) {
3379 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3380 leaf->map_token = NULL;
3381 }
3382
3383 /* shift the items */
3384 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3385 btrfs_item_nr_offset(slot),
3386 (nritems - slot) * sizeof(struct btrfs_item));
3387
3388 /* shift the data */
3389 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3390 data_end - total_data, btrfs_leaf_data(leaf) +
3391 data_end, old_data - data_end);
3392 data_end = old_data;
3393 } else {
3394 /*
3395 * this sucks but it has to be done, if we are inserting at
3396 * the end of the leaf only insert 1 of the items, since we
3397 * have no way of knowing whats on the next leaf and we'd have
3398 * to drop our current locks to figure it out
3399 */
3400 nr = 1;
3401 }
3402
3403 /* setup the item for the new data */
3404 for (i = 0; i < nr; i++) {
3405 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3406 btrfs_set_item_key(leaf, &disk_key, slot + i);
3407 item = btrfs_item_nr(leaf, slot + i);
3408 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3409 data_end -= data_size[i];
3410 btrfs_set_item_size(leaf, item, data_size[i]);
3411 }
3412 btrfs_set_header_nritems(leaf, nritems + nr);
3413 btrfs_mark_buffer_dirty(leaf);
3414
3415 ret = 0;
3416 if (slot == 0) {
3417 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3418 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3419 }
3420
3421 if (btrfs_leaf_free_space(root, leaf) < 0) {
3422 btrfs_print_leaf(root, leaf);
3423 BUG();
3424 }
3425out:
3426 if (!ret)
3427 ret = nr;
3428 return ret;
3429}
3430
3431/*
3432 * Given a key and some data, insert items into the tree.
3433 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003434 */
Chris Mason9c583092008-01-29 15:15:18 -05003435int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003436 struct btrfs_root *root,
3437 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003438 struct btrfs_key *cpu_key, u32 *data_size,
3439 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003440{
Chris Mason5f39d392007-10-15 16:14:19 -04003441 struct extent_buffer *leaf;
3442 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003443 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003444 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003445 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003446 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003447 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003448 u32 total_size = 0;
3449 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003450 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003451 struct btrfs_disk_key disk_key;
3452
Chris Masond3977122009-01-05 21:25:51 -05003453 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003454 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003455
Josef Bacik7b128762008-07-24 12:17:14 -04003456 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003457 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003458 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003459 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003460 if (ret < 0)
3461 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003462
Chris Mason62e27492007-03-15 12:56:47 -04003463 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003464 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003465
Chris Mason5f39d392007-10-15 16:14:19 -04003466 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003467 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003468
Chris Masonf25956c2008-09-12 15:32:53 -04003469 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003470 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003471 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003472 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003473 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003474 }
Chris Mason5f39d392007-10-15 16:14:19 -04003475
Chris Mason62e27492007-03-15 12:56:47 -04003476 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003477 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003478
Chris Masonbe0e5c02007-01-26 15:51:26 -05003479 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003480 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 if (old_data < data_end) {
3483 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003484 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003485 slot, old_data, data_end);
3486 BUG_ON(1);
3487 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003488 /*
3489 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3490 */
3491 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003492 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003493 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003494 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003495
Chris Mason5f39d392007-10-15 16:14:19 -04003496 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003497 if (!leaf->map_token) {
3498 map_extent_buffer(leaf, (unsigned long)item,
3499 sizeof(struct btrfs_item),
3500 &leaf->map_token, &leaf->kaddr,
3501 &leaf->map_start, &leaf->map_len,
3502 KM_USER1);
3503 }
3504
Chris Mason5f39d392007-10-15 16:14:19 -04003505 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003506 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003507 }
Chris Masondb945352007-10-15 16:15:53 -04003508 if (leaf->map_token) {
3509 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3510 leaf->map_token = NULL;
3511 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003512
3513 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003514 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003515 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003516 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003517
3518 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003519 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003520 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003521 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003522 data_end = old_data;
3523 }
Chris Mason5f39d392007-10-15 16:14:19 -04003524
Chris Mason62e27492007-03-15 12:56:47 -04003525 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003526 for (i = 0; i < nr; i++) {
3527 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3528 btrfs_set_item_key(leaf, &disk_key, slot + i);
3529 item = btrfs_item_nr(leaf, slot + i);
3530 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3531 data_end -= data_size[i];
3532 btrfs_set_item_size(leaf, item, data_size[i]);
3533 }
3534 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003535 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003536
3537 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003538 if (slot == 0) {
3539 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003540 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003541 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003542
Chris Mason5f39d392007-10-15 16:14:19 -04003543 if (btrfs_leaf_free_space(root, leaf) < 0) {
3544 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003545 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003546 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003547out:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003548 btrfs_unlock_up_safe(path, 1);
Chris Mason62e27492007-03-15 12:56:47 -04003549 return ret;
3550}
3551
3552/*
3553 * Given a key and some data, insert an item into the tree.
3554 * This does all the path init required, making room in the tree if needed.
3555 */
Chris Masone089f052007-03-16 16:20:31 -04003556int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3557 *root, struct btrfs_key *cpu_key, void *data, u32
3558 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003559{
3560 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003561 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003562 struct extent_buffer *leaf;
3563 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003564
Chris Mason2c90e5d2007-04-02 10:50:19 -04003565 path = btrfs_alloc_path();
3566 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003567 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003568 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003569 leaf = path->nodes[0];
3570 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3571 write_extent_buffer(leaf, data, ptr, data_size);
3572 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003573 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003574 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003575 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003576}
3577
Chris Mason74123bd2007-02-02 11:05:29 -05003578/*
Chris Mason5de08d72007-02-24 06:24:44 -05003579 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003580 *
Chris Masond352ac62008-09-29 15:18:18 -04003581 * the tree should have been previously balanced so the deletion does not
3582 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003583 */
Chris Masone089f052007-03-16 16:20:31 -04003584static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3585 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586{
Chris Mason5f39d392007-10-15 16:14:19 -04003587 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003588 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003589 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003590 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003591
Chris Mason5f39d392007-10-15 16:14:19 -04003592 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003593 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003594 memmove_extent_buffer(parent,
3595 btrfs_node_key_ptr_offset(slot),
3596 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003597 sizeof(struct btrfs_key_ptr) *
3598 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003599 }
Chris Mason7518a232007-03-12 12:01:18 -04003600 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003601 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003602 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003603 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003604 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003605 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003606 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003607 struct btrfs_disk_key disk_key;
3608
3609 btrfs_node_key(parent, &disk_key, 0);
3610 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003611 if (wret)
3612 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003613 }
Chris Masond6025572007-03-30 14:27:56 -04003614 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003615 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003616}
3617
Chris Mason74123bd2007-02-02 11:05:29 -05003618/*
Chris Mason323ac952008-10-01 19:05:46 -04003619 * a helper function to delete the leaf pointed to by path->slots[1] and
3620 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3621 * already know it, it is faster to have them pass it down than to
3622 * read it out of the node again.
3623 *
3624 * This deletes the pointer in path->nodes[1] and frees the leaf
3625 * block extent. zero is returned if it all worked out, < 0 otherwise.
3626 *
3627 * The path must have already been setup for deleting the leaf, including
3628 * all the proper balancing. path->nodes[1] must be locked.
3629 */
3630noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3631 struct btrfs_root *root,
3632 struct btrfs_path *path, u64 bytenr)
3633{
3634 int ret;
3635 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003636 u64 parent_start = path->nodes[1]->start;
3637 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003638
3639 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3640 if (ret)
3641 return ret;
3642
Chris Mason4d081c42009-02-04 09:31:28 -05003643 /*
3644 * btrfs_free_extent is expensive, we want to make sure we
3645 * aren't holding any locks when we call it
3646 */
3647 btrfs_unlock_up_safe(path, 0);
3648
Chris Mason323ac952008-10-01 19:05:46 -04003649 ret = btrfs_free_extent(trans, root, bytenr,
3650 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003651 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003652 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003653 return ret;
3654}
3655/*
Chris Mason74123bd2007-02-02 11:05:29 -05003656 * delete the item at the leaf level in path. If that empties
3657 * the leaf, remove it from the tree
3658 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003659int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3660 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003661{
Chris Mason5f39d392007-10-15 16:14:19 -04003662 struct extent_buffer *leaf;
3663 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003664 int last_off;
3665 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003666 int ret = 0;
3667 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003668 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003669 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670
Chris Mason5f39d392007-10-15 16:14:19 -04003671 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003672 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3673
3674 for (i = 0; i < nr; i++)
3675 dsize += btrfs_item_size_nr(leaf, slot + i);
3676
Chris Mason5f39d392007-10-15 16:14:19 -04003677 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678
Chris Mason85e21ba2008-01-29 15:11:36 -05003679 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003680 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003681
3682 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003683 data_end + dsize,
3684 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003685 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003686
Chris Mason85e21ba2008-01-29 15:11:36 -05003687 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003688 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003689
Chris Mason5f39d392007-10-15 16:14:19 -04003690 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003691 if (!leaf->map_token) {
3692 map_extent_buffer(leaf, (unsigned long)item,
3693 sizeof(struct btrfs_item),
3694 &leaf->map_token, &leaf->kaddr,
3695 &leaf->map_start, &leaf->map_len,
3696 KM_USER1);
3697 }
Chris Mason5f39d392007-10-15 16:14:19 -04003698 ioff = btrfs_item_offset(leaf, item);
3699 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003700 }
Chris Masondb945352007-10-15 16:15:53 -04003701
3702 if (leaf->map_token) {
3703 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3704 leaf->map_token = NULL;
3705 }
3706
Chris Mason5f39d392007-10-15 16:14:19 -04003707 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003708 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003709 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003710 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003711 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003712 btrfs_set_header_nritems(leaf, nritems - nr);
3713 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003714
Chris Mason74123bd2007-02-02 11:05:29 -05003715 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003716 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003717 if (leaf == root->node) {
3718 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003719 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003720 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3721 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003722 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003723 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003724 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003725 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003726 struct btrfs_disk_key disk_key;
3727
3728 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003729 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003730 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003731 if (wret)
3732 ret = wret;
3733 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003734
Chris Mason74123bd2007-02-02 11:05:29 -05003735 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003736 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003737 /* push_leaf_left fixes the path.
3738 * make sure the path still points to our leaf
3739 * for possible call to del_ptr below
3740 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003741 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003742 extent_buffer_get(leaf);
3743
Chris Mason85e21ba2008-01-29 15:11:36 -05003744 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003745 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003746 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003747
3748 if (path->nodes[0] == leaf &&
3749 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003750 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003751 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003752 ret = wret;
3753 }
Chris Mason5f39d392007-10-15 16:14:19 -04003754
3755 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003756 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003757 ret = btrfs_del_leaf(trans, root, path,
3758 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003759 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003760 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003761 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003762 /* if we're still in the path, make sure
3763 * we're dirty. Otherwise, one of the
3764 * push_leaf functions must have already
3765 * dirtied this buffer
3766 */
3767 if (path->nodes[0] == leaf)
3768 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003769 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003770 }
Chris Masond5719762007-03-23 10:01:08 -04003771 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003772 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003773 }
3774 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003775 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003776}
3777
Chris Mason97571fd2007-02-24 13:39:08 -05003778/*
Chris Mason925baed2008-06-25 16:01:30 -04003779 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003780 * returns 0 if it found something or 1 if there are no lesser leaves.
3781 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003782 *
3783 * This may release the path, and so you may lose any locks held at the
3784 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003785 */
3786int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3787{
Chris Mason925baed2008-06-25 16:01:30 -04003788 struct btrfs_key key;
3789 struct btrfs_disk_key found_key;
3790 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003791
Chris Mason925baed2008-06-25 16:01:30 -04003792 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003793
Chris Mason925baed2008-06-25 16:01:30 -04003794 if (key.offset > 0)
3795 key.offset--;
3796 else if (key.type > 0)
3797 key.type--;
3798 else if (key.objectid > 0)
3799 key.objectid--;
3800 else
3801 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003802
Chris Mason925baed2008-06-25 16:01:30 -04003803 btrfs_release_path(root, path);
3804 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3805 if (ret < 0)
3806 return ret;
3807 btrfs_item_key(path->nodes[0], &found_key, 0);
3808 ret = comp_keys(&found_key, &key);
3809 if (ret < 0)
3810 return 0;
3811 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003812}
3813
Chris Mason3f157a22008-06-25 16:01:31 -04003814/*
3815 * A helper function to walk down the tree starting at min_key, and looking
3816 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003817 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003818 *
3819 * This does not cow, but it does stuff the starting key it finds back
3820 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3821 * key and get a writable path.
3822 *
3823 * This does lock as it descends, and path->keep_locks should be set
3824 * to 1 by the caller.
3825 *
3826 * This honors path->lowest_level to prevent descent past a given level
3827 * of the tree.
3828 *
Chris Masond352ac62008-09-29 15:18:18 -04003829 * min_trans indicates the oldest transaction that you are interested
3830 * in walking through. Any nodes or leaves older than min_trans are
3831 * skipped over (without reading them).
3832 *
Chris Mason3f157a22008-06-25 16:01:31 -04003833 * returns zero if something useful was found, < 0 on error and 1 if there
3834 * was nothing in the tree that matched the search criteria.
3835 */
3836int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003837 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003838 struct btrfs_path *path, int cache_only,
3839 u64 min_trans)
3840{
3841 struct extent_buffer *cur;
3842 struct btrfs_key found_key;
3843 int slot;
Yan96524802008-07-24 12:19:49 -04003844 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003845 u32 nritems;
3846 int level;
3847 int ret = 1;
3848
Chris Mason934d3752008-12-08 16:43:10 -05003849 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003850again:
3851 cur = btrfs_lock_root_node(root);
3852 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003853 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003854 path->nodes[level] = cur;
3855 path->locks[level] = 1;
3856
3857 if (btrfs_header_generation(cur) < min_trans) {
3858 ret = 1;
3859 goto out;
3860 }
Chris Masond3977122009-01-05 21:25:51 -05003861 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003862 nritems = btrfs_header_nritems(cur);
3863 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003864 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003865
Chris Mason323ac952008-10-01 19:05:46 -04003866 /* at the lowest level, we're done, setup the path and exit */
3867 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003868 if (slot >= nritems)
3869 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003870 ret = 0;
3871 path->slots[level] = slot;
3872 btrfs_item_key_to_cpu(cur, &found_key, slot);
3873 goto out;
3874 }
Yan96524802008-07-24 12:19:49 -04003875 if (sret && slot > 0)
3876 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003877 /*
3878 * check this node pointer against the cache_only and
3879 * min_trans parameters. If it isn't in cache or is too
3880 * old, skip to the next one.
3881 */
Chris Masond3977122009-01-05 21:25:51 -05003882 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003883 u64 blockptr;
3884 u64 gen;
3885 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003886 struct btrfs_disk_key disk_key;
3887
Chris Mason3f157a22008-06-25 16:01:31 -04003888 blockptr = btrfs_node_blockptr(cur, slot);
3889 gen = btrfs_node_ptr_generation(cur, slot);
3890 if (gen < min_trans) {
3891 slot++;
3892 continue;
3893 }
3894 if (!cache_only)
3895 break;
3896
Chris Masone02119d2008-09-05 16:13:11 -04003897 if (max_key) {
3898 btrfs_node_key(cur, &disk_key, slot);
3899 if (comp_keys(&disk_key, max_key) >= 0) {
3900 ret = 1;
3901 goto out;
3902 }
3903 }
3904
Chris Mason3f157a22008-06-25 16:01:31 -04003905 tmp = btrfs_find_tree_block(root, blockptr,
3906 btrfs_level_size(root, level - 1));
3907
3908 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3909 free_extent_buffer(tmp);
3910 break;
3911 }
3912 if (tmp)
3913 free_extent_buffer(tmp);
3914 slot++;
3915 }
Chris Masone02119d2008-09-05 16:13:11 -04003916find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003917 /*
3918 * we didn't find a candidate key in this node, walk forward
3919 * and find another one
3920 */
3921 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003922 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003923 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003924 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003925 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003926 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003927 btrfs_release_path(root, path);
3928 goto again;
3929 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05003930 btrfs_clear_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003931 goto out;
3932 }
3933 }
3934 /* save our key for returning back */
3935 btrfs_node_key_to_cpu(cur, &found_key, slot);
3936 path->slots[level] = slot;
3937 if (level == path->lowest_level) {
3938 ret = 0;
3939 unlock_up(path, level, 1);
3940 goto out;
3941 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003942 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003943 cur = read_node_slot(root, cur, slot);
3944
3945 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003946
Chris Mason3f157a22008-06-25 16:01:31 -04003947 path->locks[level - 1] = 1;
3948 path->nodes[level - 1] = cur;
3949 unlock_up(path, level, 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003950 btrfs_clear_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003951 }
3952out:
3953 if (ret == 0)
3954 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05003955 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003956 return ret;
3957}
3958
3959/*
3960 * this is similar to btrfs_next_leaf, but does not try to preserve
3961 * and fixup the path. It looks for and returns the next key in the
3962 * tree based on the current path and the cache_only and min_trans
3963 * parameters.
3964 *
3965 * 0 is returned if another key is found, < 0 if there are any errors
3966 * and 1 is returned if there are no higher keys in the tree
3967 *
3968 * path->keep_locks should be set to 1 on the search made before
3969 * calling this function.
3970 */
Chris Masone7a84562008-06-25 16:01:31 -04003971int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003972 struct btrfs_key *key, int lowest_level,
3973 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003974{
3975 int level = lowest_level;
3976 int slot;
3977 struct extent_buffer *c;
3978
Chris Mason934d3752008-12-08 16:43:10 -05003979 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003980 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003981 if (!path->nodes[level])
3982 return 1;
3983
3984 slot = path->slots[level] + 1;
3985 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003986next:
Chris Masone7a84562008-06-25 16:01:31 -04003987 if (slot >= btrfs_header_nritems(c)) {
3988 level++;
Chris Masond3977122009-01-05 21:25:51 -05003989 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04003990 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04003991 continue;
3992 }
3993 if (level == 0)
3994 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003995 else {
3996 u64 blockptr = btrfs_node_blockptr(c, slot);
3997 u64 gen = btrfs_node_ptr_generation(c, slot);
3998
3999 if (cache_only) {
4000 struct extent_buffer *cur;
4001 cur = btrfs_find_tree_block(root, blockptr,
4002 btrfs_level_size(root, level - 1));
4003 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4004 slot++;
4005 if (cur)
4006 free_extent_buffer(cur);
4007 goto next;
4008 }
4009 free_extent_buffer(cur);
4010 }
4011 if (gen < min_trans) {
4012 slot++;
4013 goto next;
4014 }
Chris Masone7a84562008-06-25 16:01:31 -04004015 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004016 }
Chris Masone7a84562008-06-25 16:01:31 -04004017 return 0;
4018 }
4019 return 1;
4020}
4021
Chris Mason7bb86312007-12-11 09:25:06 -05004022/*
Chris Mason925baed2008-06-25 16:01:30 -04004023 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004024 * returns 0 if it found something or 1 if there are no greater leaves.
4025 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004026 */
Chris Mason234b63a2007-03-13 10:46:10 -04004027int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004028{
4029 int slot;
4030 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004031 struct extent_buffer *c;
4032 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004033 struct btrfs_key key;
4034 u32 nritems;
4035 int ret;
4036
4037 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004038 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004039 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004040
4041 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4042
Chris Mason925baed2008-06-25 16:01:30 -04004043 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04004044 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04004045 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4046 path->keep_locks = 0;
4047
4048 if (ret < 0)
4049 return ret;
4050
Chris Masonb4ce94d2009-02-04 09:25:08 -05004051 btrfs_set_path_blocking(path);
Chris Masona2135012008-06-25 16:01:30 -04004052 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004053 /*
4054 * by releasing the path above we dropped all our locks. A balance
4055 * could have added more items next to the key that used to be
4056 * at the very end of the block. So, check again here and
4057 * advance the path if there are now more items available.
4058 */
Chris Masona2135012008-06-25 16:01:30 -04004059 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004060 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04004061 goto done;
4062 }
Chris Masond97e63b2007-02-20 16:40:44 -05004063
Chris Masond3977122009-01-05 21:25:51 -05004064 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05004065 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05004066 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004067
Chris Masond97e63b2007-02-20 16:40:44 -05004068 slot = path->slots[level] + 1;
4069 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004070 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004071 level++;
Chris Masond3977122009-01-05 21:25:51 -05004072 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05004073 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004074 continue;
4075 }
Chris Mason5f39d392007-10-15 16:14:19 -04004076
Chris Mason925baed2008-06-25 16:01:30 -04004077 if (next) {
4078 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004079 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004080 }
Chris Mason5f39d392007-10-15 16:14:19 -04004081
Chris Masonb4ce94d2009-02-04 09:25:08 -05004082 /* the path was set to blocking above */
Chris Mason0bd40a72008-07-17 12:54:43 -04004083 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4084 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05004085 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004086
Chris Masonca7a79a2008-05-12 12:59:19 -04004087 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04004088 if (!path->skip_locking) {
4089 WARN_ON(!btrfs_tree_locked(c));
4090 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004091 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004092 }
Chris Masond97e63b2007-02-20 16:40:44 -05004093 break;
4094 }
4095 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004096 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004097 level--;
4098 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004099 if (path->locks[level])
4100 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04004101 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004102 path->nodes[level] = next;
4103 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004104 if (!path->skip_locking)
4105 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004106 if (!level)
4107 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004108
4109 btrfs_set_path_blocking(path);
Chris Mason925baed2008-06-25 16:01:30 -04004110 if (level == 1 && path->locks[1] && path->reada)
4111 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04004112 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04004113 if (!path->skip_locking) {
4114 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
4115 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004116 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004117 }
Chris Masond97e63b2007-02-20 16:40:44 -05004118 }
Chris Mason925baed2008-06-25 16:01:30 -04004119done:
4120 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05004121 return 0;
4122}
Chris Mason0b86a832008-03-24 15:01:56 -04004123
Chris Mason3f157a22008-06-25 16:01:31 -04004124/*
4125 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4126 * searching until it gets past min_objectid or finds an item of 'type'
4127 *
4128 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4129 */
Chris Mason0b86a832008-03-24 15:01:56 -04004130int btrfs_previous_item(struct btrfs_root *root,
4131 struct btrfs_path *path, u64 min_objectid,
4132 int type)
4133{
4134 struct btrfs_key found_key;
4135 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004136 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004137 int ret;
4138
Chris Masond3977122009-01-05 21:25:51 -05004139 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004140 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004141 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004142 ret = btrfs_prev_leaf(root, path);
4143 if (ret != 0)
4144 return ret;
4145 } else {
4146 path->slots[0]--;
4147 }
4148 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004149 nritems = btrfs_header_nritems(leaf);
4150 if (nritems == 0)
4151 return 1;
4152 if (path->slots[0] == nritems)
4153 path->slots[0]--;
4154
Chris Mason0b86a832008-03-24 15:01:56 -04004155 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4156 if (found_key.type == type)
4157 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004158 if (found_key.objectid < min_objectid)
4159 break;
4160 if (found_key.objectid == min_objectid &&
4161 found_key.type < type)
4162 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004163 }
4164 return 1;
4165}