blob: 9e46c07768167f53f9ae0c9b6ffdb5ce8641f56e [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 Masond352ac62008-09-29 15:18:18 -040057/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040058void btrfs_free_path(struct btrfs_path *p)
59{
Chris Masondf24a2b2007-04-04 09:36:31 -040060 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040061 kmem_cache_free(btrfs_path_cachep, p);
62}
63
Chris Masond352ac62008-09-29 15:18:18 -040064/*
65 * path release drops references on the extent buffers in the path
66 * and it drops any locks held by this path
67 *
68 * It is safe to call this on paths that no locks or extent buffers held.
69 */
Chris Masond3977122009-01-05 21:25:51 -050070noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050071{
72 int i;
Chris Masona2135012008-06-25 16:01:30 -040073
Chris Mason234b63a2007-03-13 10:46:10 -040074 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040075 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050076 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040077 continue;
78 if (p->locks[i]) {
79 btrfs_tree_unlock(p->nodes[i]);
80 p->locks[i] = 0;
81 }
Chris Mason5f39d392007-10-15 16:14:19 -040082 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -040083 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -050084 }
85}
86
Chris Masond352ac62008-09-29 15:18:18 -040087/*
88 * safely gets a reference on the root node of a tree. A lock
89 * is not taken, so a concurrent writer may put a different node
90 * at the root of the tree. See btrfs_lock_root_node for the
91 * looping required.
92 *
93 * The extent buffer returned by this has a reference taken, so
94 * it won't disappear. It may stop being the root of the tree
95 * at any time because there are no locks held.
96 */
Chris Mason925baed2008-06-25 16:01:30 -040097struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
98{
99 struct extent_buffer *eb;
100 spin_lock(&root->node_lock);
101 eb = root->node;
102 extent_buffer_get(eb);
103 spin_unlock(&root->node_lock);
104 return eb;
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/* loop around taking references on and locking the root node of the
108 * tree until you end up with a lock on the root. A locked buffer
109 * is returned, with a reference held.
110 */
Chris Mason925baed2008-06-25 16:01:30 -0400111struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
112{
113 struct extent_buffer *eb;
114
Chris Masond3977122009-01-05 21:25:51 -0500115 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400116 eb = btrfs_root_node(root);
117 btrfs_tree_lock(eb);
118
119 spin_lock(&root->node_lock);
120 if (eb == root->node) {
121 spin_unlock(&root->node_lock);
122 break;
123 }
124 spin_unlock(&root->node_lock);
125
126 btrfs_tree_unlock(eb);
127 free_extent_buffer(eb);
128 }
129 return eb;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/* cowonly root (everything not a reference counted cow subvolume), just get
133 * put onto a simple dirty list. transaction.c walks this to make sure they
134 * get properly updated on disk.
135 */
Chris Mason0b86a832008-03-24 15:01:56 -0400136static void add_root_to_dirty_list(struct btrfs_root *root)
137{
138 if (root->track_dirty && list_empty(&root->dirty_list)) {
139 list_add(&root->dirty_list,
140 &root->fs_info->dirty_cowonly_roots);
141 }
142}
143
Chris Masond352ac62008-09-29 15:18:18 -0400144/*
145 * used by snapshot creation to make a copy of a root for a tree with
146 * a given objectid. The buffer with the new root node is returned in
147 * cow_ret, and this func returns zero on success or a negative error code.
148 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500149int btrfs_copy_root(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct extent_buffer *buf,
152 struct extent_buffer **cow_ret, u64 new_root_objectid)
153{
154 struct extent_buffer *cow;
155 u32 nritems;
156 int ret = 0;
157 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500158 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500159
Chris Mason4aec2b52007-12-18 16:25:45 -0500160 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
161 if (!new_root)
162 return -ENOMEM;
163
164 memcpy(new_root, root, sizeof(*new_root));
165 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500166
167 WARN_ON(root->ref_cows && trans->transid !=
168 root->fs_info->running_transaction->transid);
169 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
170
171 level = btrfs_header_level(buf);
172 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400173
174 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
175 new_root_objectid, trans->transid,
176 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500177 if (IS_ERR(cow)) {
178 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500179 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500180 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500181
182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
183 btrfs_set_header_bytenr(cow, cow->start);
184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500187
Yan Zheng2b820322008-11-17 21:11:30 -0500188 write_extent_buffer(cow, root->fs_info->fsid,
189 (unsigned long)btrfs_header_fsid(cow),
190 BTRFS_FSID_SIZE);
191
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400193 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500194 kfree(new_root);
195
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 if (ret)
197 return ret;
198
199 btrfs_mark_buffer_dirty(cow);
200 *cow_ret = cow;
201 return 0;
202}
203
Chris Masond352ac62008-09-29 15:18:18 -0400204/*
Chris Masond3977122009-01-05 21:25:51 -0500205 * does the dirty work in cow of a single block. The parent block (if
206 * supplied) is updated to point to the new cow copy. The new buffer is marked
207 * dirty and returned locked. If you modify the block it needs to be marked
208 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400209 *
210 * search_start -- an allocation hint for the new block
211 *
Chris Masond3977122009-01-05 21:25:51 -0500212 * empty_size -- a hint that you plan on doing more cow. This is the size in
213 * bytes the allocator should try to find free next to the block it returns.
214 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400215 *
216 * prealloc_dest -- if you have already reserved a destination for the cow,
Chris Masond3977122009-01-05 21:25:51 -0500217 * this uses that block instead of allocating a new one.
218 * btrfs_alloc_reserved_extent is used to finish the allocation.
Chris Masond352ac62008-09-29 15:18:18 -0400219 */
Chris Masond3977122009-01-05 21:25:51 -0500220static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400221 struct btrfs_root *root,
222 struct extent_buffer *buf,
223 struct extent_buffer *parent, int parent_slot,
224 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400225 u64 search_start, u64 empty_size,
226 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400227{
Zheng Yan31840ae2008-09-23 13:14:14 -0400228 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400229 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500230 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400231 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500232 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400233 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400234
Chris Mason925baed2008-06-25 16:01:30 -0400235 if (*cow_ret == buf)
236 unlock_orig = 1;
237
238 WARN_ON(!btrfs_tree_locked(buf));
239
Zheng Yan31840ae2008-09-23 13:14:14 -0400240 if (parent)
241 parent_start = parent->start;
242 else
243 parent_start = 0;
244
Chris Mason7bb86312007-12-11 09:25:06 -0500245 WARN_ON(root->ref_cows && trans->transid !=
246 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400247 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400248
Chris Mason7bb86312007-12-11 09:25:06 -0500249 level = btrfs_header_level(buf);
250 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400251
Chris Mason65b51a02008-08-01 15:11:20 -0400252 if (prealloc_dest) {
253 struct btrfs_key ins;
254
255 ins.objectid = prealloc_dest;
256 ins.offset = buf->len;
257 ins.type = BTRFS_EXTENT_ITEM_KEY;
258
Zheng Yan31840ae2008-09-23 13:14:14 -0400259 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400260 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400261 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400262 BUG_ON(ret);
263 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
264 buf->len);
265 } else {
266 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400267 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400268 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400269 trans->transid, level,
270 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400271 }
Chris Mason6702ed42007-08-07 16:15:09 -0400272 if (IS_ERR(cow))
273 return PTR_ERR(cow);
274
Chris Mason5f39d392007-10-15 16:14:19 -0400275 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400276 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400277 btrfs_set_header_generation(cow, trans->transid);
278 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400279 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400280
Yan Zheng2b820322008-11-17 21:11:30 -0500281 write_extent_buffer(cow, root->fs_info->fsid,
282 (unsigned long)btrfs_header_fsid(cow),
283 BTRFS_FSID_SIZE);
284
Chris Mason5f39d392007-10-15 16:14:19 -0400285 WARN_ON(btrfs_header_generation(buf) > trans->transid);
286 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400287 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400288 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400289 if (ret)
290 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400291
292 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
293 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400294 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
295 /*
296 * There are only two places that can drop reference to
297 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400298 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400299 * we check reference count while tree block is locked.
300 * Furthermore, if reference count is one, it won't get
301 * increased by someone else.
302 */
303 u32 refs;
304 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
305 buf->len, &refs);
306 BUG_ON(ret);
307 if (refs == 1) {
308 ret = btrfs_update_ref(trans, root, buf, cow,
309 0, nritems);
310 clean_tree_block(trans, root, buf);
311 } else {
312 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
313 }
314 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400315 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400316 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
317 if (ret)
318 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400319 clean_tree_block(trans, root, buf);
320 }
321
Zheng Yan1a40e232008-09-26 10:09:34 -0400322 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400323 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
324 WARN_ON(ret);
325 }
326
Chris Mason6702ed42007-08-07 16:15:09 -0400327 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400328 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400329
330 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400331 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400332 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400333 spin_unlock(&root->node_lock);
334
Chris Mason6702ed42007-08-07 16:15:09 -0400335 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400336 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400337 buf->len, buf->start,
338 root->root_key.objectid,
339 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400340 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400341 }
Chris Mason5f39d392007-10-15 16:14:19 -0400342 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400343 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400344 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400345 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400346 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500347 WARN_ON(trans->transid == 0);
348 btrfs_set_node_ptr_generation(parent, parent_slot,
349 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400350 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400351 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500352 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400353 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400354 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400355 }
Chris Mason925baed2008-06-25 16:01:30 -0400356 if (unlock_orig)
357 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400358 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400359 btrfs_mark_buffer_dirty(cow);
360 *cow_ret = cow;
361 return 0;
362}
363
Chris Masond352ac62008-09-29 15:18:18 -0400364/*
365 * cows a single block, see __btrfs_cow_block for the real work.
366 * This version of it has extra checks so that a block isn't cow'd more than
367 * once per transaction, as long as it hasn't been written yet
368 */
Chris Masond3977122009-01-05 21:25:51 -0500369noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400370 struct btrfs_root *root, struct extent_buffer *buf,
371 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400372 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500373{
Chris Mason6702ed42007-08-07 16:15:09 -0400374 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400375 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500376
Chris Masonccd467d2007-06-28 15:57:36 -0400377 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500378 printk(KERN_CRIT "trans %llu running %llu\n",
379 (unsigned long long)trans->transid,
380 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400381 root->fs_info->running_transaction->transid);
382 WARN_ON(1);
383 }
384 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500385 printk(KERN_CRIT "trans %llu running %llu\n",
386 (unsigned long long)trans->transid,
387 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400388 WARN_ON(1);
389 }
Chris Masondc17ff82008-01-08 15:46:30 -0500390
Chris Mason63b10fc2008-04-01 11:21:32 -0400391 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400392 if (btrfs_header_generation(buf) == trans->transid &&
393 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400394 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500395 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400396 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400397 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500398 return 0;
399 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400400 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400401 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400402 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400403 parent_slot, cow_ret, search_start, 0,
404 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400405 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400406}
407
Chris Masond352ac62008-09-29 15:18:18 -0400408/*
409 * helper function for defrag to decide if two blocks pointed to by a
410 * node are actually close by
411 */
Chris Mason6b800532007-10-15 16:17:34 -0400412static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400413{
Chris Mason6b800532007-10-15 16:17:34 -0400414 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400415 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400416 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400417 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500418 return 0;
419}
420
Chris Mason081e9572007-11-06 10:26:24 -0500421/*
422 * compare two keys in a memcmp fashion
423 */
424static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
425{
426 struct btrfs_key k1;
427
428 btrfs_disk_key_to_cpu(&k1, disk);
429
430 if (k1.objectid > k2->objectid)
431 return 1;
432 if (k1.objectid < k2->objectid)
433 return -1;
434 if (k1.type > k2->type)
435 return 1;
436 if (k1.type < k2->type)
437 return -1;
438 if (k1.offset > k2->offset)
439 return 1;
440 if (k1.offset < k2->offset)
441 return -1;
442 return 0;
443}
444
Josef Bacikf3465ca2008-11-12 14:19:50 -0500445/*
446 * same as comp_keys only with two btrfs_key's
447 */
448static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
449{
450 if (k1->objectid > k2->objectid)
451 return 1;
452 if (k1->objectid < k2->objectid)
453 return -1;
454 if (k1->type > k2->type)
455 return 1;
456 if (k1->type < k2->type)
457 return -1;
458 if (k1->offset > k2->offset)
459 return 1;
460 if (k1->offset < k2->offset)
461 return -1;
462 return 0;
463}
Chris Mason081e9572007-11-06 10:26:24 -0500464
Chris Masond352ac62008-09-29 15:18:18 -0400465/*
466 * this is used by the defrag code to go through all the
467 * leaves pointed to by a node and reallocate them so that
468 * disk order is close to key order
469 */
Chris Mason6702ed42007-08-07 16:15:09 -0400470int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400471 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400472 int start_slot, int cache_only, u64 *last_ret,
473 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400474{
Chris Mason6b800532007-10-15 16:17:34 -0400475 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400476 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400477 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400478 u64 search_start = *last_ret;
479 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400480 u64 other;
481 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400482 int end_slot;
483 int i;
484 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400485 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400486 int uptodate;
487 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500488 int progress_passed = 0;
489 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400490
Chris Mason5708b952007-10-25 15:43:18 -0400491 parent_level = btrfs_header_level(parent);
492 if (cache_only && parent_level != 1)
493 return 0;
494
Chris Masond3977122009-01-05 21:25:51 -0500495 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400496 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500497 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400498 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400499
Chris Mason6b800532007-10-15 16:17:34 -0400500 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400501 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400502 end_slot = parent_nritems;
503
504 if (parent_nritems == 1)
505 return 0;
506
507 for (i = start_slot; i < end_slot; i++) {
508 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400509
Chris Mason5708b952007-10-25 15:43:18 -0400510 if (!parent->map_token) {
511 map_extent_buffer(parent,
512 btrfs_node_key_ptr_offset(i),
513 sizeof(struct btrfs_key_ptr),
514 &parent->map_token, &parent->kaddr,
515 &parent->map_start, &parent->map_len,
516 KM_USER1);
517 }
Chris Mason081e9572007-11-06 10:26:24 -0500518 btrfs_node_key(parent, &disk_key, i);
519 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
520 continue;
521
522 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400523 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400524 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400525 if (last_block == 0)
526 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400527
Chris Mason6702ed42007-08-07 16:15:09 -0400528 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400529 other = btrfs_node_blockptr(parent, i - 1);
530 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400531 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400532 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400533 other = btrfs_node_blockptr(parent, i + 1);
534 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400535 }
Chris Masone9d0b132007-08-10 14:06:19 -0400536 if (close) {
537 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400538 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400539 }
Chris Mason5708b952007-10-25 15:43:18 -0400540 if (parent->map_token) {
541 unmap_extent_buffer(parent, parent->map_token,
542 KM_USER1);
543 parent->map_token = NULL;
544 }
Chris Mason6702ed42007-08-07 16:15:09 -0400545
Chris Mason6b800532007-10-15 16:17:34 -0400546 cur = btrfs_find_tree_block(root, blocknr, blocksize);
547 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400548 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400549 else
550 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400551 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400552 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400553 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400554 continue;
555 }
Chris Mason6b800532007-10-15 16:17:34 -0400556 if (!cur) {
557 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400558 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400559 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400560 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400561 }
Chris Mason6702ed42007-08-07 16:15:09 -0400562 }
Chris Masone9d0b132007-08-10 14:06:19 -0400563 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400564 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400565
Chris Masone7a84562008-06-25 16:01:31 -0400566 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400567 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400568 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400569 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400570 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400571 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400572 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400573 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400574 break;
Yan252c38f2007-08-29 09:11:44 -0400575 }
Chris Masone7a84562008-06-25 16:01:31 -0400576 search_start = cur->start;
577 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400578 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400579 btrfs_tree_unlock(cur);
580 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400581 }
Chris Mason5708b952007-10-25 15:43:18 -0400582 if (parent->map_token) {
583 unmap_extent_buffer(parent, parent->map_token,
584 KM_USER1);
585 parent->map_token = NULL;
586 }
Chris Mason6702ed42007-08-07 16:15:09 -0400587 return err;
588}
589
Chris Mason74123bd2007-02-02 11:05:29 -0500590/*
591 * The leaf data grows from end-to-front in the node.
592 * this returns the address of the start of the last item,
593 * which is the stop of the leaf data stack
594 */
Chris Mason123abc82007-03-14 14:14:43 -0400595static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400596 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500597{
Chris Mason5f39d392007-10-15 16:14:19 -0400598 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500599 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400600 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400601 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500602}
603
Chris Masond352ac62008-09-29 15:18:18 -0400604/*
605 * extra debugging checks to make sure all the items in a key are
606 * well formed and in the proper order
607 */
Chris Mason123abc82007-03-14 14:14:43 -0400608static int check_node(struct btrfs_root *root, struct btrfs_path *path,
609 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500610{
Chris Mason5f39d392007-10-15 16:14:19 -0400611 struct extent_buffer *parent = NULL;
612 struct extent_buffer *node = path->nodes[level];
613 struct btrfs_disk_key parent_key;
614 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500615 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400616 int slot;
617 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400618 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500619
620 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400621 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400622
Chris Mason8d7be552007-05-10 11:24:42 -0400623 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400624 BUG_ON(nritems == 0);
625 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400626 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400627 btrfs_node_key(parent, &parent_key, parent_slot);
628 btrfs_node_key(node, &node_key, 0);
629 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400630 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400631 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400632 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500633 }
Chris Mason123abc82007-03-14 14:14:43 -0400634 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400635 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400636 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
637 btrfs_node_key(node, &node_key, slot);
638 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400639 }
640 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400641 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
642 btrfs_node_key(node, &node_key, slot);
643 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500644 }
645 return 0;
646}
647
Chris Masond352ac62008-09-29 15:18:18 -0400648/*
649 * extra checking to make sure all the items in a leaf are
650 * well formed and in the proper order
651 */
Chris Mason123abc82007-03-14 14:14:43 -0400652static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
653 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500654{
Chris Mason5f39d392007-10-15 16:14:19 -0400655 struct extent_buffer *leaf = path->nodes[level];
656 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500657 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400658 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400659 struct btrfs_disk_key parent_key;
660 struct btrfs_disk_key leaf_key;
661 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400662
Chris Mason5f39d392007-10-15 16:14:19 -0400663 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500664
665 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400666 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400667
668 if (nritems == 0)
669 return 0;
670
671 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400672 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400673 btrfs_node_key(parent, &parent_key, parent_slot);
674 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400675
Chris Mason5f39d392007-10-15 16:14:19 -0400676 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400677 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400678 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400679 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500680 }
Chris Mason5f39d392007-10-15 16:14:19 -0400681 if (slot != 0 && slot < nritems - 1) {
682 btrfs_item_key(leaf, &leaf_key, slot);
683 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
684 if (comp_keys(&leaf_key, &cpukey) <= 0) {
685 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500686 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400687 BUG_ON(1);
688 }
689 if (btrfs_item_offset_nr(leaf, slot - 1) !=
690 btrfs_item_end_nr(leaf, slot)) {
691 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500692 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400693 BUG_ON(1);
694 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500695 }
Chris Mason8d7be552007-05-10 11:24:42 -0400696 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400697 btrfs_item_key(leaf, &leaf_key, slot);
698 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
699 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
700 if (btrfs_item_offset_nr(leaf, slot) !=
701 btrfs_item_end_nr(leaf, slot + 1)) {
702 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500703 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400704 BUG_ON(1);
705 }
Chris Mason8d7be552007-05-10 11:24:42 -0400706 }
Chris Mason5f39d392007-10-15 16:14:19 -0400707 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
708 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500709 return 0;
710}
711
Chris Masond3977122009-01-05 21:25:51 -0500712static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500713 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500714{
Chris Mason85d824c2008-04-10 10:23:19 -0400715 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500716 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400717 return check_leaf(root, path, level);
718 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500719}
720
Chris Mason74123bd2007-02-02 11:05:29 -0500721/*
Chris Mason5f39d392007-10-15 16:14:19 -0400722 * search for key in the extent_buffer. The items start at offset p,
723 * and they are item_size apart. There are 'max' items in p.
724 *
Chris Mason74123bd2007-02-02 11:05:29 -0500725 * the slot in the array is returned via slot, and it points to
726 * the place where you would insert key if it is not found in
727 * the array.
728 *
729 * slot may point to max if the key is bigger than all of the keys
730 */
Chris Masone02119d2008-09-05 16:13:11 -0400731static noinline int generic_bin_search(struct extent_buffer *eb,
732 unsigned long p,
733 int item_size, struct btrfs_key *key,
734 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735{
736 int low = 0;
737 int high = max;
738 int mid;
739 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400740 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400741 struct btrfs_disk_key unaligned;
742 unsigned long offset;
743 char *map_token = NULL;
744 char *kaddr = NULL;
745 unsigned long map_start = 0;
746 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400747 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500748
Chris Masond3977122009-01-05 21:25:51 -0500749 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500750 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400751 offset = p + mid * item_size;
752
753 if (!map_token || offset < map_start ||
754 (offset + sizeof(struct btrfs_disk_key)) >
755 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400756 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400757 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400758 map_token = NULL;
759 }
Chris Mason934d3752008-12-08 16:43:10 -0500760
761 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400762 sizeof(struct btrfs_disk_key),
763 &map_token, &kaddr,
764 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400765
Chris Mason479965d2007-10-15 16:14:27 -0400766 if (!err) {
767 tmp = (struct btrfs_disk_key *)(kaddr + offset -
768 map_start);
769 } else {
770 read_extent_buffer(eb, &unaligned,
771 offset, sizeof(unaligned));
772 tmp = &unaligned;
773 }
774
Chris Mason5f39d392007-10-15 16:14:19 -0400775 } else {
776 tmp = (struct btrfs_disk_key *)(kaddr + offset -
777 map_start);
778 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500779 ret = comp_keys(tmp, key);
780
781 if (ret < 0)
782 low = mid + 1;
783 else if (ret > 0)
784 high = mid;
785 else {
786 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400787 if (map_token)
788 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500789 return 0;
790 }
791 }
792 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400793 if (map_token)
794 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500795 return 1;
796}
797
Chris Mason97571fd2007-02-24 13:39:08 -0500798/*
799 * simple bin_search frontend that does the right thing for
800 * leaves vs nodes
801 */
Chris Mason5f39d392007-10-15 16:14:19 -0400802static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
803 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500804{
Chris Mason5f39d392007-10-15 16:14:19 -0400805 if (level == 0) {
806 return generic_bin_search(eb,
807 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400808 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400809 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400810 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500811 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400812 return generic_bin_search(eb,
813 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400814 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400815 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400816 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817 }
818 return -1;
819}
820
Chris Masond352ac62008-09-29 15:18:18 -0400821/* given a node and slot number, this reads the blocks it points to. The
822 * extent buffer is returned with a reference taken (but unlocked).
823 * NULL is returned on error.
824 */
Chris Masone02119d2008-09-05 16:13:11 -0400825static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400826 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500827{
Chris Masonca7a79a2008-05-12 12:59:19 -0400828 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500829 if (slot < 0)
830 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500832 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400833
834 BUG_ON(level == 0);
835
Chris Masondb945352007-10-15 16:15:53 -0400836 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400837 btrfs_level_size(root, level - 1),
838 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500839}
840
Chris Masond352ac62008-09-29 15:18:18 -0400841/*
842 * node level balancing, used to make sure nodes are in proper order for
843 * item deletion. We balance from the top down, so we have to make sure
844 * that a deletion won't leave an node completely empty later on.
845 */
Chris Masone02119d2008-09-05 16:13:11 -0400846static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500847 struct btrfs_root *root,
848 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500849{
Chris Mason5f39d392007-10-15 16:14:19 -0400850 struct extent_buffer *right = NULL;
851 struct extent_buffer *mid;
852 struct extent_buffer *left = NULL;
853 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500854 int ret = 0;
855 int wret;
856 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500857 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400858 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500859 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500860
861 if (level == 0)
862 return 0;
863
Chris Mason5f39d392007-10-15 16:14:19 -0400864 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400865 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500866 WARN_ON(btrfs_header_generation(mid) != trans->transid);
867
Chris Mason1d4f8a02007-03-13 09:28:32 -0400868 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500869
Chris Mason234b63a2007-03-13 10:46:10 -0400870 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400871 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500872 pslot = path->slots[level + 1];
873
Chris Mason40689472007-03-17 14:29:23 -0400874 /*
875 * deal with the case where there is only one pointer in the root
876 * by promoting the node below to a root
877 */
Chris Mason5f39d392007-10-15 16:14:19 -0400878 if (!parent) {
879 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500880
Chris Mason5f39d392007-10-15 16:14:19 -0400881 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500882 return 0;
883
884 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400885 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400886 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500887 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400888 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500889 BUG_ON(ret);
890
Chris Mason925baed2008-06-25 16:01:30 -0400891 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500892 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400893 spin_unlock(&root->node_lock);
894
Zheng Yan31840ae2008-09-23 13:14:14 -0400895 ret = btrfs_update_extent_ref(trans, root, child->start,
896 mid->start, child->start,
897 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400898 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400899 BUG_ON(ret);
900
Chris Mason0b86a832008-03-24 15:01:56 -0400901 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400902 btrfs_tree_unlock(child);
903 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500904 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400905 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400906 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500907 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400908 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500909 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400910 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400911 btrfs_header_generation(mid),
912 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500913 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400914 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400915 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500916 }
Chris Mason5f39d392007-10-15 16:14:19 -0400917 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400918 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500919 return 0;
920
Chris Mason5f39d392007-10-15 16:14:19 -0400921 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400922 err_on_enospc = 1;
923
Chris Mason5f39d392007-10-15 16:14:19 -0400924 left = read_node_slot(root, parent, pslot - 1);
925 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400926 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400927 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400928 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400929 if (wret) {
930 ret = wret;
931 goto enospc;
932 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400933 }
Chris Mason5f39d392007-10-15 16:14:19 -0400934 right = read_node_slot(root, parent, pslot + 1);
935 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400936 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400937 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400938 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400939 if (wret) {
940 ret = wret;
941 goto enospc;
942 }
943 }
944
945 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400946 if (left) {
947 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400948 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500949 if (wret < 0)
950 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400951 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400952 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500953 }
Chris Mason79f95c82007-03-01 15:16:26 -0500954
955 /*
956 * then try to empty the right most buffer into the middle
957 */
Chris Mason5f39d392007-10-15 16:14:19 -0400958 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400959 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400960 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500961 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400962 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400963 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500964 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400965 u32 blocksize = right->len;
966
Chris Mason5f39d392007-10-15 16:14:19 -0400967 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400969 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500970 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400971 wret = del_ptr(trans, root, path, level + 1, pslot +
972 1);
Chris Masonbb803952007-03-01 12:04:21 -0500973 if (wret)
974 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400975 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -0400976 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500977 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400978 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500979 if (wret)
980 ret = wret;
981 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400982 struct btrfs_disk_key right_key;
983 btrfs_node_key(right, &right_key, 0);
984 btrfs_set_node_key(parent, &right_key, pslot + 1);
985 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500986 }
987 }
Chris Mason5f39d392007-10-15 16:14:19 -0400988 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500989 /*
990 * we're not allowed to leave a node with one item in the
991 * tree during a delete. A deletion from lower in the tree
992 * could try to delete the only pointer in this node.
993 * So, pull some keys from the left.
994 * There has to be a left pointer at this point because
995 * otherwise we would have pulled some pointers from the
996 * right
997 */
Chris Mason5f39d392007-10-15 16:14:19 -0400998 BUG_ON(!left);
999 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001000 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001001 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001002 goto enospc;
1003 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001004 if (wret == 1) {
1005 wret = push_node_left(trans, root, left, mid, 1);
1006 if (wret < 0)
1007 ret = wret;
1008 }
Chris Mason79f95c82007-03-01 15:16:26 -05001009 BUG_ON(wret == 1);
1010 }
Chris Mason5f39d392007-10-15 16:14:19 -04001011 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001012 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001013 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001014 u64 bytenr = mid->start;
1015 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001016
Chris Mason5f39d392007-10-15 16:14:19 -04001017 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001018 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001019 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001020 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001021 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001022 if (wret)
1023 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001024 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001025 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001026 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001027 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001028 if (wret)
1029 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001030 } else {
1031 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001032 struct btrfs_disk_key mid_key;
1033 btrfs_node_key(mid, &mid_key, 0);
1034 btrfs_set_node_key(parent, &mid_key, pslot);
1035 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001036 }
Chris Masonbb803952007-03-01 12:04:21 -05001037
Chris Mason79f95c82007-03-01 15:16:26 -05001038 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001039 if (left) {
1040 if (btrfs_header_nritems(left) > orig_slot) {
1041 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001042 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001043 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001044 path->slots[level + 1] -= 1;
1045 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001046 if (mid) {
1047 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001048 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001049 }
Chris Masonbb803952007-03-01 12:04:21 -05001050 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001051 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001052 path->slots[level] = orig_slot;
1053 }
1054 }
Chris Mason79f95c82007-03-01 15:16:26 -05001055 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001056 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001057 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001058 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001059 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001060enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001061 if (right) {
1062 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001063 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001064 }
1065 if (left) {
1066 if (path->nodes[level] != left)
1067 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001068 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001069 }
Chris Masonbb803952007-03-01 12:04:21 -05001070 return ret;
1071}
1072
Chris Masond352ac62008-09-29 15:18:18 -04001073/* Node balancing for insertion. Here we only split or push nodes around
1074 * when they are completely full. This is also done top down, so we
1075 * have to be pessimistic.
1076 */
Chris Masond3977122009-01-05 21:25:51 -05001077static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001078 struct btrfs_root *root,
1079 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001080{
Chris Mason5f39d392007-10-15 16:14:19 -04001081 struct extent_buffer *right = NULL;
1082 struct extent_buffer *mid;
1083 struct extent_buffer *left = NULL;
1084 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001085 int ret = 0;
1086 int wret;
1087 int pslot;
1088 int orig_slot = path->slots[level];
1089 u64 orig_ptr;
1090
1091 if (level == 0)
1092 return 1;
1093
Chris Mason5f39d392007-10-15 16:14:19 -04001094 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001095 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001096 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1097
1098 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001099 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001100 pslot = path->slots[level + 1];
1101
Chris Mason5f39d392007-10-15 16:14:19 -04001102 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001103 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001104
Chris Mason5f39d392007-10-15 16:14:19 -04001105 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001106
1107 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001108 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001109 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001110
1111 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001112 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001113 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1114 wret = 1;
1115 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001116 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001117 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001118 if (ret)
1119 wret = 1;
1120 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001121 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001122 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001123 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001124 }
Chris Masone66f7092007-04-20 13:16:02 -04001125 if (wret < 0)
1126 ret = wret;
1127 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001128 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001129 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001130 btrfs_node_key(mid, &disk_key, 0);
1131 btrfs_set_node_key(parent, &disk_key, pslot);
1132 btrfs_mark_buffer_dirty(parent);
1133 if (btrfs_header_nritems(left) > orig_slot) {
1134 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001135 path->slots[level + 1] -= 1;
1136 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001137 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001138 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001139 } else {
1140 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001141 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001142 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001143 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001144 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001145 }
Chris Masone66f7092007-04-20 13:16:02 -04001146 return 0;
1147 }
Chris Mason925baed2008-06-25 16:01:30 -04001148 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001149 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001150 }
Chris Mason925baed2008-06-25 16:01:30 -04001151 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001152
1153 /*
1154 * then try to empty the right most buffer into the middle
1155 */
Chris Mason5f39d392007-10-15 16:14:19 -04001156 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001157 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001158 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001159 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001160 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1161 wret = 1;
1162 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001163 ret = btrfs_cow_block(trans, root, right,
1164 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001165 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001166 if (ret)
1167 wret = 1;
1168 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001169 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001170 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001171 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001172 }
Chris Masone66f7092007-04-20 13:16:02 -04001173 if (wret < 0)
1174 ret = wret;
1175 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001176 struct btrfs_disk_key disk_key;
1177
1178 btrfs_node_key(right, &disk_key, 0);
1179 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1180 btrfs_mark_buffer_dirty(parent);
1181
1182 if (btrfs_header_nritems(mid) <= orig_slot) {
1183 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001184 path->slots[level + 1] += 1;
1185 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001186 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001187 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001188 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001189 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001190 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001191 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001192 }
Chris Masone66f7092007-04-20 13:16:02 -04001193 return 0;
1194 }
Chris Mason925baed2008-06-25 16:01:30 -04001195 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001196 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001197 }
Chris Masone66f7092007-04-20 13:16:02 -04001198 return 1;
1199}
1200
Chris Mason74123bd2007-02-02 11:05:29 -05001201/*
Chris Masond352ac62008-09-29 15:18:18 -04001202 * readahead one full node of leaves, finding things that are close
1203 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001204 */
Chris Masone02119d2008-09-05 16:13:11 -04001205static noinline void reada_for_search(struct btrfs_root *root,
1206 struct btrfs_path *path,
1207 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001208{
Chris Mason5f39d392007-10-15 16:14:19 -04001209 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001210 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001211 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001212 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001213 u64 lowest_read;
1214 u64 highest_read;
1215 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001216 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001217 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001218 u32 nr;
1219 u32 blocksize;
1220 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001221
Chris Masona6b6e752007-10-15 16:22:39 -04001222 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001223 return;
1224
Chris Mason6702ed42007-08-07 16:15:09 -04001225 if (!path->nodes[level])
1226 return;
1227
Chris Mason5f39d392007-10-15 16:14:19 -04001228 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001229
Chris Mason3c69fae2007-08-07 15:52:22 -04001230 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001231 blocksize = btrfs_level_size(root, level - 1);
1232 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001233 if (eb) {
1234 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001235 return;
1236 }
1237
Chris Mason6b800532007-10-15 16:17:34 -04001238 highest_read = search;
1239 lowest_read = search;
1240
Chris Mason5f39d392007-10-15 16:14:19 -04001241 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001242 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001243 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001244 if (direction < 0) {
1245 if (nr == 0)
1246 break;
1247 nr--;
1248 } else if (direction > 0) {
1249 nr++;
1250 if (nr >= nritems)
1251 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001252 }
Chris Mason01f46652007-12-21 16:24:26 -05001253 if (path->reada < 0 && objectid) {
1254 btrfs_node_key(node, &disk_key, nr);
1255 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1256 break;
1257 }
Chris Mason6b800532007-10-15 16:17:34 -04001258 search = btrfs_node_blockptr(node, nr);
1259 if ((search >= lowest_read && search <= highest_read) ||
Chris Mason6f3577b2008-11-13 09:59:36 -05001260 (search < lowest_read && lowest_read - search <= 16384) ||
1261 (search > highest_read && search - highest_read <= 16384)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001262 readahead_tree_block(root, search, blocksize,
1263 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001264 nread += blocksize;
1265 }
1266 nscan++;
Chris Mason6f3577b2008-11-13 09:59:36 -05001267 if (path->reada < 2 && (nread > (64 * 1024) || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001268 break;
Chris Masond3977122009-01-05 21:25:51 -05001269
1270 if (nread > (256 * 1024) || nscan > 128)
Chris Mason6b800532007-10-15 16:17:34 -04001271 break;
1272
1273 if (search < lowest_read)
1274 lowest_read = search;
1275 if (search > highest_read)
1276 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 }
1278}
Chris Mason925baed2008-06-25 16:01:30 -04001279
Chris Masond352ac62008-09-29 15:18:18 -04001280/*
Chris Masond3977122009-01-05 21:25:51 -05001281 * when we walk down the tree, it is usually safe to unlock the higher layers
1282 * in the tree. The exceptions are when our path goes through slot 0, because
1283 * operations on the tree might require changing key pointers higher up in the
1284 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001285 *
Chris Masond3977122009-01-05 21:25:51 -05001286 * callers might also have set path->keep_locks, which tells this code to keep
1287 * the lock if the path points to the last slot in the block. This is part of
1288 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001289 *
Chris Masond3977122009-01-05 21:25:51 -05001290 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1291 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001292 */
Chris Masone02119d2008-09-05 16:13:11 -04001293static noinline void unlock_up(struct btrfs_path *path, int level,
1294 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001295{
1296 int i;
1297 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001298 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001299 struct extent_buffer *t;
1300
1301 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1302 if (!path->nodes[i])
1303 break;
1304 if (!path->locks[i])
1305 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001306 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001307 skip_level = i + 1;
1308 continue;
1309 }
Chris Mason051e1b92008-06-25 16:01:30 -04001310 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001311 u32 nritems;
1312 t = path->nodes[i];
1313 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001314 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001315 skip_level = i + 1;
1316 continue;
1317 }
1318 }
Chris Mason051e1b92008-06-25 16:01:30 -04001319 if (skip_level < i && i >= lowest_unlock)
1320 no_skips = 1;
1321
Chris Mason925baed2008-06-25 16:01:30 -04001322 t = path->nodes[i];
1323 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1324 btrfs_tree_unlock(t);
1325 path->locks[i] = 0;
1326 }
1327 }
1328}
1329
Chris Mason3c69fae2007-08-07 15:52:22 -04001330/*
Chris Mason74123bd2007-02-02 11:05:29 -05001331 * look for key in the tree. path is filled in with nodes along the way
1332 * if key is found, we return zero and you can find the item in the leaf
1333 * level of the path (level 0)
1334 *
1335 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001336 * be inserted, and 1 is returned. If there are other errors during the
1337 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001338 *
1339 * if ins_len > 0, nodes and leaves will be split as we walk down the
1340 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1341 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001342 */
Chris Masone089f052007-03-16 16:20:31 -04001343int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1344 *root, struct btrfs_key *key, struct btrfs_path *p, int
1345 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001346{
Chris Mason5f39d392007-10-15 16:14:19 -04001347 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001348 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001349 int slot;
1350 int ret;
1351 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001352 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001353 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001354 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001355 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001356 u64 blocknr;
1357 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001358 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001359
Chris Mason6702ed42007-08-07 16:15:09 -04001360 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001361 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001362 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001363
Chris Mason925baed2008-06-25 16:01:30 -04001364 if (ins_len < 0)
1365 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001366
1367 prealloc_block.objectid = 0;
1368
Chris Masonbb803952007-03-01 12:04:21 -05001369again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001370 if (p->skip_locking)
1371 b = btrfs_root_node(root);
1372 else
1373 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001374
Chris Masoneb60cea2007-02-02 09:18:22 -05001375 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001376 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001377
1378 /*
1379 * setup the path here so we can release it under lock
1380 * contention with the cow code
1381 */
1382 p->nodes[level] = b;
1383 if (!p->skip_locking)
1384 p->locks[level] = 1;
1385
Chris Mason02217ed2007-03-02 16:08:05 -05001386 if (cow) {
1387 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001388
1389 /* is a cow on this block not required */
1390 spin_lock(&root->fs_info->hash_lock);
1391 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001392 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001393 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1394 spin_unlock(&root->fs_info->hash_lock);
1395 goto cow_done;
1396 }
1397 spin_unlock(&root->fs_info->hash_lock);
1398
1399 /* ok, we have to cow, is our old prealloc the right
1400 * size?
1401 */
1402 if (prealloc_block.objectid &&
1403 prealloc_block.offset != b->len) {
1404 btrfs_free_reserved_extent(root,
1405 prealloc_block.objectid,
1406 prealloc_block.offset);
1407 prealloc_block.objectid = 0;
1408 }
1409
1410 /*
1411 * for higher level blocks, try not to allocate blocks
1412 * with the block and the parent locks held.
1413 */
1414 if (level > 1 && !prealloc_block.objectid &&
1415 btrfs_path_lock_waiting(p, level)) {
1416 u32 size = b->len;
1417 u64 hint = b->start;
1418
1419 btrfs_release_path(root, p);
1420 ret = btrfs_reserve_extent(trans, root,
1421 size, size, 0,
1422 hint, (u64)-1,
1423 &prealloc_block, 0);
1424 BUG_ON(ret);
1425 goto again;
1426 }
1427
Chris Masone20d96d2007-03-22 12:13:20 -04001428 wret = btrfs_cow_block(trans, root, b,
1429 p->nodes[level + 1],
1430 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001431 &b, prealloc_block.objectid);
1432 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001433 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001434 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001435 ret = wret;
1436 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001437 }
Chris Mason02217ed2007-03-02 16:08:05 -05001438 }
Chris Mason65b51a02008-08-01 15:11:20 -04001439cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001440 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001441 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001442 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001443 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001444
Chris Masoneb60cea2007-02-02 09:18:22 -05001445 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001446 if (!p->skip_locking)
1447 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001448
Chris Mason123abc82007-03-14 14:14:43 -04001449 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001450 if (ret) {
1451 ret = -1;
1452 goto done;
1453 }
Chris Mason925baed2008-06-25 16:01:30 -04001454
Chris Mason5f39d392007-10-15 16:14:19 -04001455 ret = bin_search(b, key, level, &slot);
1456 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001457 if (ret && slot > 0)
1458 slot -= 1;
1459 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001460 if ((p->search_for_split || ins_len > 0) &&
1461 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001462 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001463 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001464 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001465 if (sret) {
1466 ret = sret;
1467 goto done;
1468 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001469 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001470 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001471 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001472 int sret = balance_level(trans, root, p,
1473 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001474 if (sret) {
1475 ret = sret;
1476 goto done;
1477 }
Chris Masonbb803952007-03-01 12:04:21 -05001478 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001479 if (!b) {
1480 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001481 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001482 }
Chris Masonbb803952007-03-01 12:04:21 -05001483 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001484 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001485 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001486 unlock_up(p, level, lowest_unlock);
1487
Chris Mason9f3a7422007-08-07 15:52:19 -04001488 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001489 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001490 ret = 0;
1491 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001492 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001493
Chris Mason594a24e2008-06-25 16:01:30 -04001494 blocknr = btrfs_node_blockptr(b, slot);
1495 gen = btrfs_node_ptr_generation(b, slot);
1496 blocksize = btrfs_level_size(root, level - 1);
1497
1498 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1499 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001500 b = tmp;
1501 } else {
1502 /*
1503 * reduce lock contention at high levels
1504 * of the btree by dropping locks before
1505 * we read.
1506 */
1507 if (level > 1) {
1508 btrfs_release_path(NULL, p);
1509 if (tmp)
1510 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001511 if (should_reada)
1512 reada_for_search(root, p,
1513 level, slot,
1514 key->objectid);
1515
Chris Mason594a24e2008-06-25 16:01:30 -04001516 tmp = read_tree_block(root, blocknr,
1517 blocksize, gen);
1518 if (tmp)
1519 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001520 goto again;
1521 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001522 if (tmp)
1523 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001524 if (should_reada)
1525 reada_for_search(root, p,
1526 level, slot,
1527 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001528 b = read_node_slot(root, b, slot);
1529 }
1530 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001531 if (!p->skip_locking)
1532 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001533 } else {
1534 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001535 if (ins_len > 0 &&
1536 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001537 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001538 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001539 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001540 if (sret) {
1541 ret = sret;
1542 goto done;
1543 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001544 }
Chris Mason459931e2008-12-10 09:10:46 -05001545 if (!p->search_for_split)
1546 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001547 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001548 }
1549 }
Chris Mason65b51a02008-08-01 15:11:20 -04001550 ret = 1;
1551done:
1552 if (prealloc_block.objectid) {
1553 btrfs_free_reserved_extent(root,
1554 prealloc_block.objectid,
1555 prealloc_block.offset);
1556 }
1557
1558 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001559}
1560
Zheng Yan1a40e232008-09-26 10:09:34 -04001561int btrfs_merge_path(struct btrfs_trans_handle *trans,
1562 struct btrfs_root *root,
1563 struct btrfs_key *node_keys,
1564 u64 *nodes, int lowest_level)
1565{
1566 struct extent_buffer *eb;
1567 struct extent_buffer *parent;
1568 struct btrfs_key key;
1569 u64 bytenr;
1570 u64 generation;
1571 u32 blocksize;
1572 int level;
1573 int slot;
1574 int key_match;
1575 int ret;
1576
1577 eb = btrfs_lock_root_node(root);
1578 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1579 BUG_ON(ret);
1580
1581 parent = eb;
1582 while (1) {
1583 level = btrfs_header_level(parent);
1584 if (level == 0 || level <= lowest_level)
1585 break;
1586
1587 ret = bin_search(parent, &node_keys[lowest_level], level,
1588 &slot);
1589 if (ret && slot > 0)
1590 slot--;
1591
1592 bytenr = btrfs_node_blockptr(parent, slot);
1593 if (nodes[level - 1] == bytenr)
1594 break;
1595
1596 blocksize = btrfs_level_size(root, level - 1);
1597 generation = btrfs_node_ptr_generation(parent, slot);
1598 btrfs_node_key_to_cpu(eb, &key, slot);
1599 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1600
Yan Zhengf82d02d2008-10-29 14:49:05 -04001601 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001602 eb = read_tree_block(root, bytenr, blocksize,
1603 generation);
1604 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001605 }
1606
1607 /*
1608 * if node keys match and node pointer hasn't been modified
1609 * in the running transaction, we can merge the path. for
1610 * blocks owened by reloc trees, the node pointer check is
1611 * skipped, this is because these blocks are fully controlled
1612 * by the space balance code, no one else can modify them.
1613 */
1614 if (!nodes[level - 1] || !key_match ||
1615 (generation == trans->transid &&
1616 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1617 if (level == 1 || level == lowest_level + 1) {
1618 if (generation == trans->transid) {
1619 btrfs_tree_unlock(eb);
1620 free_extent_buffer(eb);
1621 }
1622 break;
1623 }
1624
1625 if (generation != trans->transid) {
1626 eb = read_tree_block(root, bytenr, blocksize,
1627 generation);
1628 btrfs_tree_lock(eb);
1629 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001630
1631 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1632 &eb, 0);
1633 BUG_ON(ret);
1634
Yan Zhengf82d02d2008-10-29 14:49:05 -04001635 if (root->root_key.objectid ==
1636 BTRFS_TREE_RELOC_OBJECTID) {
1637 if (!nodes[level - 1]) {
1638 nodes[level - 1] = eb->start;
1639 memcpy(&node_keys[level - 1], &key,
1640 sizeof(node_keys[0]));
1641 } else {
1642 WARN_ON(1);
1643 }
1644 }
1645
Zheng Yan1a40e232008-09-26 10:09:34 -04001646 btrfs_tree_unlock(parent);
1647 free_extent_buffer(parent);
1648 parent = eb;
1649 continue;
1650 }
1651
Zheng Yan1a40e232008-09-26 10:09:34 -04001652 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1653 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1654 btrfs_mark_buffer_dirty(parent);
1655
1656 ret = btrfs_inc_extent_ref(trans, root,
1657 nodes[level - 1],
1658 blocksize, parent->start,
1659 btrfs_header_owner(parent),
1660 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001661 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001662 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001663
1664 /*
1665 * If the block was created in the running transaction,
1666 * it's possible this is the last reference to it, so we
1667 * should drop the subtree.
1668 */
1669 if (generation == trans->transid) {
1670 ret = btrfs_drop_subtree(trans, root, eb, parent);
1671 BUG_ON(ret);
1672 btrfs_tree_unlock(eb);
1673 free_extent_buffer(eb);
1674 } else {
1675 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001676 blocksize, parent->start,
1677 btrfs_header_owner(parent),
1678 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001679 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001680 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001681 }
1682 break;
1683 }
1684 btrfs_tree_unlock(parent);
1685 free_extent_buffer(parent);
1686 return 0;
1687}
1688
Chris Mason74123bd2007-02-02 11:05:29 -05001689/*
1690 * adjust the pointers going up the tree, starting at level
1691 * making sure the right key of each node is points to 'key'.
1692 * This is used after shifting pointers to the left, so it stops
1693 * fixing up pointers when a given leaf/node is not in slot 0 of the
1694 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001695 *
1696 * If this fails to write a tree block, it returns -1, but continues
1697 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001698 */
Chris Mason5f39d392007-10-15 16:14:19 -04001699static int fixup_low_keys(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root, struct btrfs_path *path,
1701 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001702{
1703 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001704 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001705 struct extent_buffer *t;
1706
Chris Mason234b63a2007-03-13 10:46:10 -04001707 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001708 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001709 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001710 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001711 t = path->nodes[i];
1712 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001713 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001714 if (tslot != 0)
1715 break;
1716 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001717 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001718}
1719
Chris Mason74123bd2007-02-02 11:05:29 -05001720/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001721 * update item key.
1722 *
1723 * This function isn't completely safe. It's the caller's responsibility
1724 * that the new key won't break the order
1725 */
1726int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root, struct btrfs_path *path,
1728 struct btrfs_key *new_key)
1729{
1730 struct btrfs_disk_key disk_key;
1731 struct extent_buffer *eb;
1732 int slot;
1733
1734 eb = path->nodes[0];
1735 slot = path->slots[0];
1736 if (slot > 0) {
1737 btrfs_item_key(eb, &disk_key, slot - 1);
1738 if (comp_keys(&disk_key, new_key) >= 0)
1739 return -1;
1740 }
1741 if (slot < btrfs_header_nritems(eb) - 1) {
1742 btrfs_item_key(eb, &disk_key, slot + 1);
1743 if (comp_keys(&disk_key, new_key) <= 0)
1744 return -1;
1745 }
1746
1747 btrfs_cpu_key_to_disk(&disk_key, new_key);
1748 btrfs_set_item_key(eb, &disk_key, slot);
1749 btrfs_mark_buffer_dirty(eb);
1750 if (slot == 0)
1751 fixup_low_keys(trans, root, path, &disk_key, 1);
1752 return 0;
1753}
1754
1755/*
Chris Mason74123bd2007-02-02 11:05:29 -05001756 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001757 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001758 *
1759 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1760 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001761 */
Chris Mason98ed5172008-01-03 10:01:48 -05001762static int push_node_left(struct btrfs_trans_handle *trans,
1763 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001764 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001765{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001766 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001767 int src_nritems;
1768 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001769 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001770
Chris Mason5f39d392007-10-15 16:14:19 -04001771 src_nritems = btrfs_header_nritems(src);
1772 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001773 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001774 WARN_ON(btrfs_header_generation(src) != trans->transid);
1775 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001776
Chris Masonbce4eae2008-04-24 14:42:46 -04001777 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001778 return 1;
1779
Chris Masond3977122009-01-05 21:25:51 -05001780 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001781 return 1;
1782
Chris Masonbce4eae2008-04-24 14:42:46 -04001783 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001784 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001785 if (push_items < src_nritems) {
1786 /* leave at least 8 pointers in the node if
1787 * we aren't going to empty it
1788 */
1789 if (src_nritems - push_items < 8) {
1790 if (push_items <= 8)
1791 return 1;
1792 push_items -= 8;
1793 }
1794 }
1795 } else
1796 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001797
Chris Mason5f39d392007-10-15 16:14:19 -04001798 copy_extent_buffer(dst, src,
1799 btrfs_node_key_ptr_offset(dst_nritems),
1800 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001801 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001802
Chris Masonbb803952007-03-01 12:04:21 -05001803 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001804 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1805 btrfs_node_key_ptr_offset(push_items),
1806 (src_nritems - push_items) *
1807 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001808 }
Chris Mason5f39d392007-10-15 16:14:19 -04001809 btrfs_set_header_nritems(src, src_nritems - push_items);
1810 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1811 btrfs_mark_buffer_dirty(src);
1812 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001813
1814 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1815 BUG_ON(ret);
1816
Chris Masonbb803952007-03-01 12:04:21 -05001817 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818}
1819
Chris Mason97571fd2007-02-24 13:39:08 -05001820/*
Chris Mason79f95c82007-03-01 15:16:26 -05001821 * try to push data from one node into the next node right in the
1822 * tree.
1823 *
1824 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1825 * error, and > 0 if there was no room in the right hand block.
1826 *
1827 * this will only push up to 1/2 the contents of the left node over
1828 */
Chris Mason5f39d392007-10-15 16:14:19 -04001829static int balance_node_right(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
1831 struct extent_buffer *dst,
1832 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001833{
Chris Mason79f95c82007-03-01 15:16:26 -05001834 int push_items = 0;
1835 int max_push;
1836 int src_nritems;
1837 int dst_nritems;
1838 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001839
Chris Mason7bb86312007-12-11 09:25:06 -05001840 WARN_ON(btrfs_header_generation(src) != trans->transid);
1841 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1842
Chris Mason5f39d392007-10-15 16:14:19 -04001843 src_nritems = btrfs_header_nritems(src);
1844 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001845 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001846 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001847 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001848
Chris Masond3977122009-01-05 21:25:51 -05001849 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001850 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001851
1852 max_push = src_nritems / 2 + 1;
1853 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001854 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001855 return 1;
Yan252c38f2007-08-29 09:11:44 -04001856
Chris Mason79f95c82007-03-01 15:16:26 -05001857 if (max_push < push_items)
1858 push_items = max_push;
1859
Chris Mason5f39d392007-10-15 16:14:19 -04001860 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1861 btrfs_node_key_ptr_offset(0),
1862 (dst_nritems) *
1863 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001864
Chris Mason5f39d392007-10-15 16:14:19 -04001865 copy_extent_buffer(dst, src,
1866 btrfs_node_key_ptr_offset(0),
1867 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001868 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001869
Chris Mason5f39d392007-10-15 16:14:19 -04001870 btrfs_set_header_nritems(src, src_nritems - push_items);
1871 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001872
Chris Mason5f39d392007-10-15 16:14:19 -04001873 btrfs_mark_buffer_dirty(src);
1874 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001875
1876 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1877 BUG_ON(ret);
1878
Chris Mason79f95c82007-03-01 15:16:26 -05001879 return ret;
1880}
1881
1882/*
Chris Mason97571fd2007-02-24 13:39:08 -05001883 * helper function to insert a new root level in the tree.
1884 * A new node is allocated, and a single item is inserted to
1885 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001886 *
1887 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001888 */
Chris Masond3977122009-01-05 21:25:51 -05001889static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001890 struct btrfs_root *root,
1891 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001892{
Chris Mason7bb86312007-12-11 09:25:06 -05001893 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001894 struct extent_buffer *lower;
1895 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001896 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001897 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001898 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001899
1900 BUG_ON(path->nodes[level]);
1901 BUG_ON(path->nodes[level-1] != root->node);
1902
Chris Mason7bb86312007-12-11 09:25:06 -05001903 lower = path->nodes[level-1];
1904 if (level == 1)
1905 btrfs_item_key(lower, &lower_key, 0);
1906 else
1907 btrfs_node_key(lower, &lower_key, 0);
1908
Zheng Yan31840ae2008-09-23 13:14:14 -04001909 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1910 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001911 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001912 if (IS_ERR(c))
1913 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001914
Chris Mason5f39d392007-10-15 16:14:19 -04001915 memset_extent_buffer(c, 0, 0, root->nodesize);
1916 btrfs_set_header_nritems(c, 1);
1917 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001918 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001919 btrfs_set_header_generation(c, trans->transid);
1920 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001921
Chris Mason5f39d392007-10-15 16:14:19 -04001922 write_extent_buffer(c, root->fs_info->fsid,
1923 (unsigned long)btrfs_header_fsid(c),
1924 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001925
1926 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1927 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1928 BTRFS_UUID_SIZE);
1929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001931 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001932 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001933 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001934
1935 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001936
1937 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001938
Chris Mason925baed2008-06-25 16:01:30 -04001939 spin_lock(&root->node_lock);
1940 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001941 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001942 spin_unlock(&root->node_lock);
1943
Zheng Yan31840ae2008-09-23 13:14:14 -04001944 ret = btrfs_update_extent_ref(trans, root, lower->start,
1945 lower->start, c->start,
1946 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001947 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001948 BUG_ON(ret);
1949
Chris Mason925baed2008-06-25 16:01:30 -04001950 /* the super has an extra ref to root->node */
1951 free_extent_buffer(old);
1952
Chris Mason0b86a832008-03-24 15:01:56 -04001953 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001954 extent_buffer_get(c);
1955 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001956 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001957 path->slots[level] = 0;
1958 return 0;
1959}
1960
Chris Mason74123bd2007-02-02 11:05:29 -05001961/*
1962 * worker function to insert a single pointer in a node.
1963 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001964 *
Chris Mason74123bd2007-02-02 11:05:29 -05001965 * slot and level indicate where you want the key to go, and
1966 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001967 *
1968 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001969 */
Chris Masone089f052007-03-16 16:20:31 -04001970static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1971 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001972 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001973{
Chris Mason5f39d392007-10-15 16:14:19 -04001974 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001975 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001976
1977 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001978 lower = path->nodes[level];
1979 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001980 if (slot > nritems)
1981 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001982 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001983 BUG();
1984 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001985 memmove_extent_buffer(lower,
1986 btrfs_node_key_ptr_offset(slot + 1),
1987 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001988 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001989 }
Chris Mason5f39d392007-10-15 16:14:19 -04001990 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001991 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001992 WARN_ON(trans->transid == 0);
1993 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001994 btrfs_set_header_nritems(lower, nritems + 1);
1995 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001996 return 0;
1997}
1998
Chris Mason97571fd2007-02-24 13:39:08 -05001999/*
2000 * split the node at the specified level in path in two.
2001 * The path is corrected to point to the appropriate node after the split
2002 *
2003 * Before splitting this tries to make some room in the node by pushing
2004 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002005 *
2006 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002007 */
Chris Masone02119d2008-09-05 16:13:11 -04002008static noinline int split_node(struct btrfs_trans_handle *trans,
2009 struct btrfs_root *root,
2010 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002011{
Chris Mason5f39d392007-10-15 16:14:19 -04002012 struct extent_buffer *c;
2013 struct extent_buffer *split;
2014 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002015 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002016 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002017 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002018 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002019
Chris Mason5f39d392007-10-15 16:14:19 -04002020 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002021 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002022 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002023 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002024 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002025 if (ret)
2026 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002027 } else {
2028 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002029 c = path->nodes[level];
2030 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002031 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002032 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002033 if (ret < 0)
2034 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002035 }
Chris Masone66f7092007-04-20 13:16:02 -04002036
Chris Mason5f39d392007-10-15 16:14:19 -04002037 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002038
Chris Mason925baed2008-06-25 16:01:30 -04002039 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002040 path->nodes[level + 1]->start,
2041 root->root_key.objectid,
2042 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002043 if (IS_ERR(split))
2044 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_set_header_flags(split, btrfs_header_flags(c));
2047 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002048 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002049 btrfs_set_header_generation(split, trans->transid);
2050 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002051 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 write_extent_buffer(split, root->fs_info->fsid,
2053 (unsigned long)btrfs_header_fsid(split),
2054 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002055 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2056 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2057 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002058
Chris Mason7518a232007-03-12 12:01:18 -04002059 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002060
2061 copy_extent_buffer(split, c,
2062 btrfs_node_key_ptr_offset(0),
2063 btrfs_node_key_ptr_offset(mid),
2064 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2065 btrfs_set_header_nritems(split, c_nritems - mid);
2066 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002067 ret = 0;
2068
Chris Mason5f39d392007-10-15 16:14:19 -04002069 btrfs_mark_buffer_dirty(c);
2070 btrfs_mark_buffer_dirty(split);
2071
2072 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002073 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002074 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002075 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076 if (wret)
2077 ret = wret;
2078
Zheng Yan31840ae2008-09-23 13:14:14 -04002079 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2080 BUG_ON(ret);
2081
Chris Mason5de08d72007-02-24 06:24:44 -05002082 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002083 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002084 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 free_extent_buffer(c);
2086 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002087 path->slots[level + 1] += 1;
2088 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002089 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002091 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002092 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002093}
2094
Chris Mason74123bd2007-02-02 11:05:29 -05002095/*
2096 * how many bytes are required to store the items in a leaf. start
2097 * and nr indicate which items in the leaf to check. This totals up the
2098 * space used both by the item structs and the item data
2099 */
Chris Mason5f39d392007-10-15 16:14:19 -04002100static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002101{
2102 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002103 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002104 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002105
2106 if (!nr)
2107 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002108 data_len = btrfs_item_end_nr(l, start);
2109 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002110 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002111 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002112 return data_len;
2113}
2114
Chris Mason74123bd2007-02-02 11:05:29 -05002115/*
Chris Masond4dbff92007-04-04 14:08:15 -04002116 * The space between the end of the leaf items and
2117 * the start of the leaf data. IOW, how much room
2118 * the leaf has left for both items and data
2119 */
Chris Masond3977122009-01-05 21:25:51 -05002120noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002121 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002122{
Chris Mason5f39d392007-10-15 16:14:19 -04002123 int nritems = btrfs_header_nritems(leaf);
2124 int ret;
2125 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2126 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002127 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2128 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002129 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002130 leaf_space_used(leaf, 0, nritems), nritems);
2131 }
2132 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002133}
2134
2135/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002136 * push some data in the path leaf to the right, trying to free up at
2137 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002138 *
2139 * returns 1 if the push failed because the other node didn't have enough
2140 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002141 */
Chris Masone089f052007-03-16 16:20:31 -04002142static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002143 *root, struct btrfs_path *path, int data_size,
2144 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002145{
Chris Mason5f39d392007-10-15 16:14:19 -04002146 struct extent_buffer *left = path->nodes[0];
2147 struct extent_buffer *right;
2148 struct extent_buffer *upper;
2149 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002150 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002151 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002152 int free_space;
2153 int push_space = 0;
2154 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002155 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002156 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002157 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002158 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002159 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002160 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002161 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002162
2163 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002164 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002165 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002166
Chris Mason00ec4c52007-02-24 12:47:20 -05002167 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002168 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002169 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002170
Chris Masona2135012008-06-25 16:01:30 -04002171 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2172
Chris Masonca7a79a2008-05-12 12:59:19 -04002173 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002174 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002175 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002176 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002177 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002178
Chris Mason5f39d392007-10-15 16:14:19 -04002179 /* cow and double check */
2180 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002181 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002182 if (ret)
2183 goto out_unlock;
2184
Chris Mason5f39d392007-10-15 16:14:19 -04002185 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002186 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002187 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002188
2189 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002190 if (left_nritems == 0)
2191 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002192
Chris Mason34a38212007-11-07 13:31:03 -05002193 if (empty)
2194 nr = 0;
2195 else
2196 nr = 1;
2197
Zheng Yan31840ae2008-09-23 13:14:14 -04002198 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002199 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002200
Chris Mason34a38212007-11-07 13:31:03 -05002201 i = left_nritems - 1;
2202 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002203 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002204
Zheng Yan31840ae2008-09-23 13:14:14 -04002205 if (!empty && push_items > 0) {
2206 if (path->slots[0] > i)
2207 break;
2208 if (path->slots[0] == i) {
2209 int space = btrfs_leaf_free_space(root, left);
2210 if (space + push_space * 2 > free_space)
2211 break;
2212 }
2213 }
2214
Chris Mason00ec4c52007-02-24 12:47:20 -05002215 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002216 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002217
2218 if (!left->map_token) {
2219 map_extent_buffer(left, (unsigned long)item,
2220 sizeof(struct btrfs_item),
2221 &left->map_token, &left->kaddr,
2222 &left->map_start, &left->map_len,
2223 KM_USER1);
2224 }
2225
2226 this_item_size = btrfs_item_size(left, item);
2227 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002228 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002229
Chris Mason00ec4c52007-02-24 12:47:20 -05002230 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002231 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002232 if (i == 0)
2233 break;
2234 i--;
Chris Masondb945352007-10-15 16:15:53 -04002235 }
2236 if (left->map_token) {
2237 unmap_extent_buffer(left, left->map_token, KM_USER1);
2238 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002239 }
Chris Mason5f39d392007-10-15 16:14:19 -04002240
Chris Mason925baed2008-06-25 16:01:30 -04002241 if (push_items == 0)
2242 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002243
Chris Mason34a38212007-11-07 13:31:03 -05002244 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002245 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002246
Chris Mason00ec4c52007-02-24 12:47:20 -05002247 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002248 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002249
Chris Mason5f39d392007-10-15 16:14:19 -04002250 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002251 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002252
Chris Mason00ec4c52007-02-24 12:47:20 -05002253 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002254 data_end = leaf_data_end(root, right);
2255 memmove_extent_buffer(right,
2256 btrfs_leaf_data(right) + data_end - push_space,
2257 btrfs_leaf_data(right) + data_end,
2258 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2259
Chris Mason00ec4c52007-02-24 12:47:20 -05002260 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002261 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002262 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2263 btrfs_leaf_data(left) + leaf_data_end(root, left),
2264 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002265
2266 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2267 btrfs_item_nr_offset(0),
2268 right_nritems * sizeof(struct btrfs_item));
2269
Chris Mason00ec4c52007-02-24 12:47:20 -05002270 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002271 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2272 btrfs_item_nr_offset(left_nritems - push_items),
2273 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002274
2275 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002276 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002277 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002278 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002279 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002280 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002281 if (!right->map_token) {
2282 map_extent_buffer(right, (unsigned long)item,
2283 sizeof(struct btrfs_item),
2284 &right->map_token, &right->kaddr,
2285 &right->map_start, &right->map_len,
2286 KM_USER1);
2287 }
2288 push_space -= btrfs_item_size(right, item);
2289 btrfs_set_item_offset(right, item, push_space);
2290 }
2291
2292 if (right->map_token) {
2293 unmap_extent_buffer(right, right->map_token, KM_USER1);
2294 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002295 }
Chris Mason7518a232007-03-12 12:01:18 -04002296 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002297 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002298
Chris Mason34a38212007-11-07 13:31:03 -05002299 if (left_nritems)
2300 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002301 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002302
Zheng Yan31840ae2008-09-23 13:14:14 -04002303 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2304 BUG_ON(ret);
2305
Chris Mason5f39d392007-10-15 16:14:19 -04002306 btrfs_item_key(right, &disk_key, 0);
2307 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002308 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002309
Chris Mason00ec4c52007-02-24 12:47:20 -05002310 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002311 if (path->slots[0] >= left_nritems) {
2312 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002313 if (btrfs_header_nritems(path->nodes[0]) == 0)
2314 clean_tree_block(trans, root, path->nodes[0]);
2315 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002316 free_extent_buffer(path->nodes[0]);
2317 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002318 path->slots[1] += 1;
2319 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002320 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002321 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002322 }
2323 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002324
2325out_unlock:
2326 btrfs_tree_unlock(right);
2327 free_extent_buffer(right);
2328 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002329}
Chris Mason925baed2008-06-25 16:01:30 -04002330
Chris Mason00ec4c52007-02-24 12:47:20 -05002331/*
Chris Mason74123bd2007-02-02 11:05:29 -05002332 * push some data in the path leaf to the left, trying to free up at
2333 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2334 */
Chris Masone089f052007-03-16 16:20:31 -04002335static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002336 *root, struct btrfs_path *path, int data_size,
2337 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002338{
Chris Mason5f39d392007-10-15 16:14:19 -04002339 struct btrfs_disk_key disk_key;
2340 struct extent_buffer *right = path->nodes[0];
2341 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002342 int slot;
2343 int i;
2344 int free_space;
2345 int push_space = 0;
2346 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002347 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002348 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002349 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002350 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002351 int ret = 0;
2352 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002353 u32 this_item_size;
2354 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002355
2356 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002357 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002358 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002359 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002360 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002361
Chris Mason3685f792007-10-19 09:23:27 -04002362 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002363 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002364 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002365
Chris Masona2135012008-06-25 16:01:30 -04002366 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2367
Chris Masonca7a79a2008-05-12 12:59:19 -04002368 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002369 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002370 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002371 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002372 ret = 1;
2373 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002374 }
Chris Mason02217ed2007-03-02 16:08:05 -05002375
2376 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002377 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002378 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002379 if (ret) {
2380 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002381 ret = 1;
2382 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002383 }
Chris Mason3685f792007-10-19 09:23:27 -04002384
Chris Mason123abc82007-03-14 14:14:43 -04002385 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002386 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002387 ret = 1;
2388 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002389 }
2390
Chris Mason34a38212007-11-07 13:31:03 -05002391 if (empty)
2392 nr = right_nritems;
2393 else
2394 nr = right_nritems - 1;
2395
2396 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002397 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002398 if (!right->map_token) {
2399 map_extent_buffer(right, (unsigned long)item,
2400 sizeof(struct btrfs_item),
2401 &right->map_token, &right->kaddr,
2402 &right->map_start, &right->map_len,
2403 KM_USER1);
2404 }
2405
Zheng Yan31840ae2008-09-23 13:14:14 -04002406 if (!empty && push_items > 0) {
2407 if (path->slots[0] < i)
2408 break;
2409 if (path->slots[0] == i) {
2410 int space = btrfs_leaf_free_space(root, right);
2411 if (space + push_space * 2 > free_space)
2412 break;
2413 }
2414 }
2415
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002417 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002418
2419 this_item_size = btrfs_item_size(right, item);
2420 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002421 break;
Chris Masondb945352007-10-15 16:15:53 -04002422
Chris Masonbe0e5c02007-01-26 15:51:26 -05002423 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002424 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002425 }
Chris Masondb945352007-10-15 16:15:53 -04002426
2427 if (right->map_token) {
2428 unmap_extent_buffer(right, right->map_token, KM_USER1);
2429 right->map_token = NULL;
2430 }
2431
Chris Masonbe0e5c02007-01-26 15:51:26 -05002432 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002433 ret = 1;
2434 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002435 }
Chris Mason34a38212007-11-07 13:31:03 -05002436 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002437 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002438
Chris Masonbe0e5c02007-01-26 15:51:26 -05002439 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002440 copy_extent_buffer(left, right,
2441 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2442 btrfs_item_nr_offset(0),
2443 push_items * sizeof(struct btrfs_item));
2444
Chris Mason123abc82007-03-14 14:14:43 -04002445 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002446 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002447
2448 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002449 leaf_data_end(root, left) - push_space,
2450 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002451 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002452 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002453 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002454 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002455
Chris Masondb945352007-10-15 16:15:53 -04002456 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002457 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002458 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002459
Chris Mason5f39d392007-10-15 16:14:19 -04002460 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002461 if (!left->map_token) {
2462 map_extent_buffer(left, (unsigned long)item,
2463 sizeof(struct btrfs_item),
2464 &left->map_token, &left->kaddr,
2465 &left->map_start, &left->map_len,
2466 KM_USER1);
2467 }
2468
Chris Mason5f39d392007-10-15 16:14:19 -04002469 ioff = btrfs_item_offset(left, item);
2470 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002471 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002472 }
Chris Mason5f39d392007-10-15 16:14:19 -04002473 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002474 if (left->map_token) {
2475 unmap_extent_buffer(left, left->map_token, KM_USER1);
2476 left->map_token = NULL;
2477 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002478
2479 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002480 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002481 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2482 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002483 WARN_ON(1);
2484 }
Chris Mason5f39d392007-10-15 16:14:19 -04002485
Chris Mason34a38212007-11-07 13:31:03 -05002486 if (push_items < right_nritems) {
2487 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2488 leaf_data_end(root, right);
2489 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2490 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2491 btrfs_leaf_data(right) +
2492 leaf_data_end(root, right), push_space);
2493
2494 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002495 btrfs_item_nr_offset(push_items),
2496 (btrfs_header_nritems(right) - push_items) *
2497 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002498 }
Yaneef1c492007-11-26 10:58:13 -05002499 right_nritems -= push_items;
2500 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002501 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002502 for (i = 0; i < right_nritems; i++) {
2503 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002504
2505 if (!right->map_token) {
2506 map_extent_buffer(right, (unsigned long)item,
2507 sizeof(struct btrfs_item),
2508 &right->map_token, &right->kaddr,
2509 &right->map_start, &right->map_len,
2510 KM_USER1);
2511 }
2512
2513 push_space = push_space - btrfs_item_size(right, item);
2514 btrfs_set_item_offset(right, item, push_space);
2515 }
2516 if (right->map_token) {
2517 unmap_extent_buffer(right, right->map_token, KM_USER1);
2518 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002519 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002520
Chris Mason5f39d392007-10-15 16:14:19 -04002521 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002522 if (right_nritems)
2523 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002524
Zheng Yan31840ae2008-09-23 13:14:14 -04002525 ret = btrfs_update_ref(trans, root, right, left,
2526 old_left_nritems, push_items);
2527 BUG_ON(ret);
2528
Chris Mason5f39d392007-10-15 16:14:19 -04002529 btrfs_item_key(right, &disk_key, 0);
2530 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002531 if (wret)
2532 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002533
2534 /* then fixup the leaf pointer in the path */
2535 if (path->slots[0] < push_items) {
2536 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002537 if (btrfs_header_nritems(path->nodes[0]) == 0)
2538 clean_tree_block(trans, root, path->nodes[0]);
2539 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002540 free_extent_buffer(path->nodes[0]);
2541 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002542 path->slots[1] -= 1;
2543 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002544 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002545 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002546 path->slots[0] -= push_items;
2547 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002548 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002549 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002550out:
2551 btrfs_tree_unlock(left);
2552 free_extent_buffer(left);
2553 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554}
2555
Chris Mason74123bd2007-02-02 11:05:29 -05002556/*
2557 * split the path's leaf in two, making sure there is at least data_size
2558 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002559 *
2560 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002561 */
Chris Masone02119d2008-09-05 16:13:11 -04002562static noinline int split_leaf(struct btrfs_trans_handle *trans,
2563 struct btrfs_root *root,
2564 struct btrfs_key *ins_key,
2565 struct btrfs_path *path, int data_size,
2566 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567{
Chris Mason5f39d392007-10-15 16:14:19 -04002568 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002569 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002570 int mid;
2571 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002572 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 int data_copy_size;
2574 int rt_data_off;
2575 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002576 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002577 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002578 int double_split;
2579 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002580 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002581
Chris Mason40689472007-03-17 14:29:23 -04002582 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002583 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002584 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002585 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002586 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002587 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002588 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002589 if (wret < 0)
2590 return wret;
2591 }
2592 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002593
Chris Mason3685f792007-10-19 09:23:27 -04002594 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002595 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002596 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002597 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002598
Chris Mason5c680ed2007-02-22 11:39:13 -05002599 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002600 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002601 if (ret)
2602 return ret;
2603 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002604again:
2605 double_split = 0;
2606 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002607 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002608 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002609 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002610
Chris Mason925baed2008-06-25 16:01:30 -04002611 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002612 path->nodes[1]->start,
2613 root->root_key.objectid,
2614 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002615 if (IS_ERR(right)) {
2616 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002617 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002618 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002619
Chris Mason5f39d392007-10-15 16:14:19 -04002620 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002621 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002622 btrfs_set_header_generation(right, trans->transid);
2623 btrfs_set_header_owner(right, root->root_key.objectid);
2624 btrfs_set_header_level(right, 0);
2625 write_extent_buffer(right, root->fs_info->fsid,
2626 (unsigned long)btrfs_header_fsid(right),
2627 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002628
2629 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2630 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2631 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002632 if (mid <= slot) {
2633 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002634 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002635 BTRFS_LEAF_DATA_SIZE(root)) {
2636 if (slot >= nritems) {
2637 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002638 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002639 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002640 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002641 path->slots[1] + 1, 1);
2642 if (wret)
2643 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002644
2645 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002646 free_extent_buffer(path->nodes[0]);
2647 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002648 path->slots[0] = 0;
2649 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002650 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002651 return ret;
2652 }
2653 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002654 if (mid != nritems &&
2655 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002656 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002657 double_split = 1;
2658 }
Chris Masond4dbff92007-04-04 14:08:15 -04002659 }
2660 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002661 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002662 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002663 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002664 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002665 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002666 wret = insert_ptr(trans, root, path,
2667 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002668 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002669 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002670 if (wret)
2671 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002672 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002673 free_extent_buffer(path->nodes[0]);
2674 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002675 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002676 if (path->slots[1] == 0) {
2677 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002678 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002679 if (wret)
2680 ret = wret;
2681 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002682 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002683 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002684 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002685 mid = 1;
2686 } else {
2687 mid = slot;
2688 if (mid != nritems &&
2689 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002690 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002691 double_split = 1;
2692 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002693 }
Chris Masond4dbff92007-04-04 14:08:15 -04002694 }
2695 }
Chris Mason5f39d392007-10-15 16:14:19 -04002696 nritems = nritems - mid;
2697 btrfs_set_header_nritems(right, nritems);
2698 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2699
2700 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2701 btrfs_item_nr_offset(mid),
2702 nritems * sizeof(struct btrfs_item));
2703
2704 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002705 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2706 data_copy_size, btrfs_leaf_data(l) +
2707 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002708
Chris Mason5f39d392007-10-15 16:14:19 -04002709 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2710 btrfs_item_end_nr(l, mid);
2711
2712 for (i = 0; i < nritems; i++) {
2713 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002714 u32 ioff;
2715
2716 if (!right->map_token) {
2717 map_extent_buffer(right, (unsigned long)item,
2718 sizeof(struct btrfs_item),
2719 &right->map_token, &right->kaddr,
2720 &right->map_start, &right->map_len,
2721 KM_USER1);
2722 }
2723
2724 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002726 }
Chris Mason74123bd2007-02-02 11:05:29 -05002727
Chris Masondb945352007-10-15 16:15:53 -04002728 if (right->map_token) {
2729 unmap_extent_buffer(right, right->map_token, KM_USER1);
2730 right->map_token = NULL;
2731 }
2732
Chris Mason5f39d392007-10-15 16:14:19 -04002733 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002734 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002735 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002736 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2737 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002738 if (wret)
2739 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002740
2741 btrfs_mark_buffer_dirty(right);
2742 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002743 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002744
Zheng Yan31840ae2008-09-23 13:14:14 -04002745 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2746 BUG_ON(ret);
2747
Chris Masonbe0e5c02007-01-26 15:51:26 -05002748 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002749 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002750 free_extent_buffer(path->nodes[0]);
2751 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002752 path->slots[0] -= mid;
2753 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002754 } else {
2755 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002756 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002757 }
Chris Mason5f39d392007-10-15 16:14:19 -04002758
Chris Masoneb60cea2007-02-02 09:18:22 -05002759 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002760
Chris Masoncc0c5532007-10-25 15:42:57 -04002761 if (double_split) {
2762 BUG_ON(num_doubles != 0);
2763 num_doubles++;
2764 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002765 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002766 return ret;
2767}
2768
Chris Masond352ac62008-09-29 15:18:18 -04002769/*
Chris Mason459931e2008-12-10 09:10:46 -05002770 * This function splits a single item into two items,
2771 * giving 'new_key' to the new item and splitting the
2772 * old one at split_offset (from the start of the item).
2773 *
2774 * The path may be released by this operation. After
2775 * the split, the path is pointing to the old item. The
2776 * new item is going to be in the same node as the old one.
2777 *
2778 * Note, the item being split must be smaller enough to live alone on
2779 * a tree block with room for one extra struct btrfs_item
2780 *
2781 * This allows us to split the item in place, keeping a lock on the
2782 * leaf the entire time.
2783 */
2784int btrfs_split_item(struct btrfs_trans_handle *trans,
2785 struct btrfs_root *root,
2786 struct btrfs_path *path,
2787 struct btrfs_key *new_key,
2788 unsigned long split_offset)
2789{
2790 u32 item_size;
2791 struct extent_buffer *leaf;
2792 struct btrfs_key orig_key;
2793 struct btrfs_item *item;
2794 struct btrfs_item *new_item;
2795 int ret = 0;
2796 int slot;
2797 u32 nritems;
2798 u32 orig_offset;
2799 struct btrfs_disk_key disk_key;
2800 char *buf;
2801
2802 leaf = path->nodes[0];
2803 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2804 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2805 goto split;
2806
2807 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2808 btrfs_release_path(root, path);
2809
2810 path->search_for_split = 1;
2811 path->keep_locks = 1;
2812
2813 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2814 path->search_for_split = 0;
2815
2816 /* if our item isn't there or got smaller, return now */
2817 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2818 path->slots[0])) {
2819 path->keep_locks = 0;
2820 return -EAGAIN;
2821 }
2822
Yan Zheng87b29b22008-12-17 10:21:48 -05002823 ret = split_leaf(trans, root, &orig_key, path,
2824 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05002825 path->keep_locks = 0;
2826 BUG_ON(ret);
2827
Chris Mason459931e2008-12-10 09:10:46 -05002828 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05002829 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05002830
2831split:
2832 item = btrfs_item_nr(leaf, path->slots[0]);
2833 orig_offset = btrfs_item_offset(leaf, item);
2834 item_size = btrfs_item_size(leaf, item);
2835
2836
2837 buf = kmalloc(item_size, GFP_NOFS);
2838 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2839 path->slots[0]), item_size);
2840 slot = path->slots[0] + 1;
2841 leaf = path->nodes[0];
2842
2843 nritems = btrfs_header_nritems(leaf);
2844
2845 if (slot != nritems) {
2846 /* shift the items */
2847 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2848 btrfs_item_nr_offset(slot),
2849 (nritems - slot) * sizeof(struct btrfs_item));
2850
2851 }
2852
2853 btrfs_cpu_key_to_disk(&disk_key, new_key);
2854 btrfs_set_item_key(leaf, &disk_key, slot);
2855
2856 new_item = btrfs_item_nr(leaf, slot);
2857
2858 btrfs_set_item_offset(leaf, new_item, orig_offset);
2859 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2860
2861 btrfs_set_item_offset(leaf, item,
2862 orig_offset + item_size - split_offset);
2863 btrfs_set_item_size(leaf, item, split_offset);
2864
2865 btrfs_set_header_nritems(leaf, nritems + 1);
2866
2867 /* write the data for the start of the original item */
2868 write_extent_buffer(leaf, buf,
2869 btrfs_item_ptr_offset(leaf, path->slots[0]),
2870 split_offset);
2871
2872 /* write the data for the new item */
2873 write_extent_buffer(leaf, buf + split_offset,
2874 btrfs_item_ptr_offset(leaf, slot),
2875 item_size - split_offset);
2876 btrfs_mark_buffer_dirty(leaf);
2877
2878 ret = 0;
2879 if (btrfs_leaf_free_space(root, leaf) < 0) {
2880 btrfs_print_leaf(root, leaf);
2881 BUG();
2882 }
2883 kfree(buf);
2884 return ret;
2885}
2886
2887/*
Chris Masond352ac62008-09-29 15:18:18 -04002888 * make the item pointed to by the path smaller. new_size indicates
2889 * how small to make it, and from_end tells us if we just chop bytes
2890 * off the end of the item or if we shift the item to chop bytes off
2891 * the front.
2892 */
Chris Masonb18c6682007-04-17 13:26:50 -04002893int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2894 struct btrfs_root *root,
2895 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002896 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002897{
2898 int ret = 0;
2899 int slot;
2900 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002901 struct extent_buffer *leaf;
2902 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002903 u32 nritems;
2904 unsigned int data_end;
2905 unsigned int old_data_start;
2906 unsigned int old_size;
2907 unsigned int size_diff;
2908 int i;
2909
2910 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002911 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002912 slot = path->slots[0];
2913
2914 old_size = btrfs_item_size_nr(leaf, slot);
2915 if (old_size == new_size)
2916 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002917
Chris Mason5f39d392007-10-15 16:14:19 -04002918 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002919 data_end = leaf_data_end(root, leaf);
2920
Chris Mason5f39d392007-10-15 16:14:19 -04002921 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002922
Chris Masonb18c6682007-04-17 13:26:50 -04002923 size_diff = old_size - new_size;
2924
2925 BUG_ON(slot < 0);
2926 BUG_ON(slot >= nritems);
2927
2928 /*
2929 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2930 */
2931 /* first correct the data pointers */
2932 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002933 u32 ioff;
2934 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002935
2936 if (!leaf->map_token) {
2937 map_extent_buffer(leaf, (unsigned long)item,
2938 sizeof(struct btrfs_item),
2939 &leaf->map_token, &leaf->kaddr,
2940 &leaf->map_start, &leaf->map_len,
2941 KM_USER1);
2942 }
2943
Chris Mason5f39d392007-10-15 16:14:19 -04002944 ioff = btrfs_item_offset(leaf, item);
2945 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002946 }
Chris Masondb945352007-10-15 16:15:53 -04002947
2948 if (leaf->map_token) {
2949 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2950 leaf->map_token = NULL;
2951 }
2952
Chris Masonb18c6682007-04-17 13:26:50 -04002953 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002954 if (from_end) {
2955 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2956 data_end + size_diff, btrfs_leaf_data(leaf) +
2957 data_end, old_data_start + new_size - data_end);
2958 } else {
2959 struct btrfs_disk_key disk_key;
2960 u64 offset;
2961
2962 btrfs_item_key(leaf, &disk_key, slot);
2963
2964 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2965 unsigned long ptr;
2966 struct btrfs_file_extent_item *fi;
2967
2968 fi = btrfs_item_ptr(leaf, slot,
2969 struct btrfs_file_extent_item);
2970 fi = (struct btrfs_file_extent_item *)(
2971 (unsigned long)fi - size_diff);
2972
2973 if (btrfs_file_extent_type(leaf, fi) ==
2974 BTRFS_FILE_EXTENT_INLINE) {
2975 ptr = btrfs_item_ptr_offset(leaf, slot);
2976 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05002977 (unsigned long)fi,
2978 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04002979 disk_bytenr));
2980 }
2981 }
2982
2983 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2984 data_end + size_diff, btrfs_leaf_data(leaf) +
2985 data_end, old_data_start - data_end);
2986
2987 offset = btrfs_disk_key_offset(&disk_key);
2988 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2989 btrfs_set_item_key(leaf, &disk_key, slot);
2990 if (slot == 0)
2991 fixup_low_keys(trans, root, path, &disk_key, 1);
2992 }
Chris Mason5f39d392007-10-15 16:14:19 -04002993
2994 item = btrfs_item_nr(leaf, slot);
2995 btrfs_set_item_size(leaf, item, new_size);
2996 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002997
2998 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002999 if (btrfs_leaf_free_space(root, leaf) < 0) {
3000 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003001 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003002 }
Chris Masonb18c6682007-04-17 13:26:50 -04003003 return ret;
3004}
3005
Chris Masond352ac62008-09-29 15:18:18 -04003006/*
3007 * make the item pointed to by the path bigger, data_size is the new size.
3008 */
Chris Mason5f39d392007-10-15 16:14:19 -04003009int btrfs_extend_item(struct btrfs_trans_handle *trans,
3010 struct btrfs_root *root, struct btrfs_path *path,
3011 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003012{
3013 int ret = 0;
3014 int slot;
3015 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003016 struct extent_buffer *leaf;
3017 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003018 u32 nritems;
3019 unsigned int data_end;
3020 unsigned int old_data;
3021 unsigned int old_size;
3022 int i;
3023
3024 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003025 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003026
Chris Mason5f39d392007-10-15 16:14:19 -04003027 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003028 data_end = leaf_data_end(root, leaf);
3029
Chris Mason5f39d392007-10-15 16:14:19 -04003030 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3031 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003032 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003033 }
Chris Mason6567e832007-04-16 09:22:45 -04003034 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003035 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003036
3037 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003038 if (slot >= nritems) {
3039 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003040 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3041 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003042 BUG_ON(1);
3043 }
Chris Mason6567e832007-04-16 09:22:45 -04003044
3045 /*
3046 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3047 */
3048 /* first correct the data pointers */
3049 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003050 u32 ioff;
3051 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003052
3053 if (!leaf->map_token) {
3054 map_extent_buffer(leaf, (unsigned long)item,
3055 sizeof(struct btrfs_item),
3056 &leaf->map_token, &leaf->kaddr,
3057 &leaf->map_start, &leaf->map_len,
3058 KM_USER1);
3059 }
Chris Mason5f39d392007-10-15 16:14:19 -04003060 ioff = btrfs_item_offset(leaf, item);
3061 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003062 }
Chris Mason5f39d392007-10-15 16:14:19 -04003063
Chris Masondb945352007-10-15 16:15:53 -04003064 if (leaf->map_token) {
3065 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3066 leaf->map_token = NULL;
3067 }
3068
Chris Mason6567e832007-04-16 09:22:45 -04003069 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003070 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003071 data_end - data_size, btrfs_leaf_data(leaf) +
3072 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003073
Chris Mason6567e832007-04-16 09:22:45 -04003074 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003075 old_size = btrfs_item_size_nr(leaf, slot);
3076 item = btrfs_item_nr(leaf, slot);
3077 btrfs_set_item_size(leaf, item, old_size + data_size);
3078 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003079
3080 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003081 if (btrfs_leaf_free_space(root, leaf) < 0) {
3082 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003083 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003084 }
Chris Mason6567e832007-04-16 09:22:45 -04003085 return ret;
3086}
3087
Chris Mason74123bd2007-02-02 11:05:29 -05003088/*
Chris Masond352ac62008-09-29 15:18:18 -04003089 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003090 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003091 * Returns the number of keys that were inserted.
3092 */
3093int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3094 struct btrfs_root *root,
3095 struct btrfs_path *path,
3096 struct btrfs_key *cpu_key, u32 *data_size,
3097 int nr)
3098{
3099 struct extent_buffer *leaf;
3100 struct btrfs_item *item;
3101 int ret = 0;
3102 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003103 int i;
3104 u32 nritems;
3105 u32 total_data = 0;
3106 u32 total_size = 0;
3107 unsigned int data_end;
3108 struct btrfs_disk_key disk_key;
3109 struct btrfs_key found_key;
3110
Yan Zheng87b29b22008-12-17 10:21:48 -05003111 for (i = 0; i < nr; i++) {
3112 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3113 BTRFS_LEAF_DATA_SIZE(root)) {
3114 break;
3115 nr = i;
3116 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003117 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003118 total_size += data_size[i] + sizeof(struct btrfs_item);
3119 }
3120 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003121
Josef Bacikf3465ca2008-11-12 14:19:50 -05003122 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3123 if (ret == 0)
3124 return -EEXIST;
3125 if (ret < 0)
3126 goto out;
3127
Josef Bacikf3465ca2008-11-12 14:19:50 -05003128 leaf = path->nodes[0];
3129
3130 nritems = btrfs_header_nritems(leaf);
3131 data_end = leaf_data_end(root, leaf);
3132
3133 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3134 for (i = nr; i >= 0; i--) {
3135 total_data -= data_size[i];
3136 total_size -= data_size[i] + sizeof(struct btrfs_item);
3137 if (total_size < btrfs_leaf_free_space(root, leaf))
3138 break;
3139 }
3140 nr = i;
3141 }
3142
3143 slot = path->slots[0];
3144 BUG_ON(slot < 0);
3145
3146 if (slot != nritems) {
3147 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3148
3149 item = btrfs_item_nr(leaf, slot);
3150 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3151
3152 /* figure out how many keys we can insert in here */
3153 total_data = data_size[0];
3154 for (i = 1; i < nr; i++) {
3155 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3156 break;
3157 total_data += data_size[i];
3158 }
3159 nr = i;
3160
3161 if (old_data < data_end) {
3162 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003163 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003164 slot, old_data, data_end);
3165 BUG_ON(1);
3166 }
3167 /*
3168 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3169 */
3170 /* first correct the data pointers */
3171 WARN_ON(leaf->map_token);
3172 for (i = slot; i < nritems; i++) {
3173 u32 ioff;
3174
3175 item = btrfs_item_nr(leaf, i);
3176 if (!leaf->map_token) {
3177 map_extent_buffer(leaf, (unsigned long)item,
3178 sizeof(struct btrfs_item),
3179 &leaf->map_token, &leaf->kaddr,
3180 &leaf->map_start, &leaf->map_len,
3181 KM_USER1);
3182 }
3183
3184 ioff = btrfs_item_offset(leaf, item);
3185 btrfs_set_item_offset(leaf, item, ioff - total_data);
3186 }
3187 if (leaf->map_token) {
3188 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3189 leaf->map_token = NULL;
3190 }
3191
3192 /* shift the items */
3193 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3194 btrfs_item_nr_offset(slot),
3195 (nritems - slot) * sizeof(struct btrfs_item));
3196
3197 /* shift the data */
3198 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3199 data_end - total_data, btrfs_leaf_data(leaf) +
3200 data_end, old_data - data_end);
3201 data_end = old_data;
3202 } else {
3203 /*
3204 * this sucks but it has to be done, if we are inserting at
3205 * the end of the leaf only insert 1 of the items, since we
3206 * have no way of knowing whats on the next leaf and we'd have
3207 * to drop our current locks to figure it out
3208 */
3209 nr = 1;
3210 }
3211
3212 /* setup the item for the new data */
3213 for (i = 0; i < nr; i++) {
3214 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3215 btrfs_set_item_key(leaf, &disk_key, slot + i);
3216 item = btrfs_item_nr(leaf, slot + i);
3217 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3218 data_end -= data_size[i];
3219 btrfs_set_item_size(leaf, item, data_size[i]);
3220 }
3221 btrfs_set_header_nritems(leaf, nritems + nr);
3222 btrfs_mark_buffer_dirty(leaf);
3223
3224 ret = 0;
3225 if (slot == 0) {
3226 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3227 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3228 }
3229
3230 if (btrfs_leaf_free_space(root, leaf) < 0) {
3231 btrfs_print_leaf(root, leaf);
3232 BUG();
3233 }
3234out:
3235 if (!ret)
3236 ret = nr;
3237 return ret;
3238}
3239
3240/*
3241 * Given a key and some data, insert items into the tree.
3242 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003243 */
Chris Mason9c583092008-01-29 15:15:18 -05003244int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003245 struct btrfs_root *root,
3246 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003247 struct btrfs_key *cpu_key, u32 *data_size,
3248 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003249{
Chris Mason5f39d392007-10-15 16:14:19 -04003250 struct extent_buffer *leaf;
3251 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003252 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003253 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003254 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003255 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003256 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003257 u32 total_size = 0;
3258 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003259 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003260 struct btrfs_disk_key disk_key;
3261
Chris Masond3977122009-01-05 21:25:51 -05003262 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003263 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264
Josef Bacik7b128762008-07-24 12:17:14 -04003265 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003266 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003267 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003268 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003269 if (ret < 0)
3270 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003271
Chris Mason62e27492007-03-15 12:56:47 -04003272 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003273 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003274
Chris Mason5f39d392007-10-15 16:14:19 -04003275 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003276 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003277
Chris Masonf25956c2008-09-12 15:32:53 -04003278 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003279 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003280 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003281 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003282 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003283 }
Chris Mason5f39d392007-10-15 16:14:19 -04003284
Chris Mason62e27492007-03-15 12:56:47 -04003285 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003286 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003287
Chris Masonbe0e5c02007-01-26 15:51:26 -05003288 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003289 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003290
Chris Mason5f39d392007-10-15 16:14:19 -04003291 if (old_data < data_end) {
3292 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003293 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003294 slot, old_data, data_end);
3295 BUG_ON(1);
3296 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003297 /*
3298 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3299 */
3300 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003301 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003302 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003303 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003304
Chris Mason5f39d392007-10-15 16:14:19 -04003305 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003306 if (!leaf->map_token) {
3307 map_extent_buffer(leaf, (unsigned long)item,
3308 sizeof(struct btrfs_item),
3309 &leaf->map_token, &leaf->kaddr,
3310 &leaf->map_start, &leaf->map_len,
3311 KM_USER1);
3312 }
3313
Chris Mason5f39d392007-10-15 16:14:19 -04003314 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003315 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003316 }
Chris Masondb945352007-10-15 16:15:53 -04003317 if (leaf->map_token) {
3318 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3319 leaf->map_token = NULL;
3320 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003321
3322 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003323 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003324 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003325 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003326
3327 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003328 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003329 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003330 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003331 data_end = old_data;
3332 }
Chris Mason5f39d392007-10-15 16:14:19 -04003333
Chris Mason62e27492007-03-15 12:56:47 -04003334 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003335 for (i = 0; i < nr; i++) {
3336 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3337 btrfs_set_item_key(leaf, &disk_key, slot + i);
3338 item = btrfs_item_nr(leaf, slot + i);
3339 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3340 data_end -= data_size[i];
3341 btrfs_set_item_size(leaf, item, data_size[i]);
3342 }
3343 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003344 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003345
3346 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003347 if (slot == 0) {
3348 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003349 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003350 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003351
Chris Mason5f39d392007-10-15 16:14:19 -04003352 if (btrfs_leaf_free_space(root, leaf) < 0) {
3353 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003354 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003355 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003356out:
Chris Mason62e27492007-03-15 12:56:47 -04003357 return ret;
3358}
3359
3360/*
3361 * Given a key and some data, insert an item into the tree.
3362 * This does all the path init required, making room in the tree if needed.
3363 */
Chris Masone089f052007-03-16 16:20:31 -04003364int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3365 *root, struct btrfs_key *cpu_key, void *data, u32
3366 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003367{
3368 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003369 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003370 struct extent_buffer *leaf;
3371 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003372
Chris Mason2c90e5d2007-04-02 10:50:19 -04003373 path = btrfs_alloc_path();
3374 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003375 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003376 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003377 leaf = path->nodes[0];
3378 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3379 write_extent_buffer(leaf, data, ptr, data_size);
3380 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003381 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003382 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003383 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003384}
3385
Chris Mason74123bd2007-02-02 11:05:29 -05003386/*
Chris Mason5de08d72007-02-24 06:24:44 -05003387 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003388 *
Chris Masond352ac62008-09-29 15:18:18 -04003389 * the tree should have been previously balanced so the deletion does not
3390 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003391 */
Chris Masone089f052007-03-16 16:20:31 -04003392static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3393 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003394{
Chris Mason5f39d392007-10-15 16:14:19 -04003395 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003396 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003397 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003398 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003399
Chris Mason5f39d392007-10-15 16:14:19 -04003400 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003401 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003402 memmove_extent_buffer(parent,
3403 btrfs_node_key_ptr_offset(slot),
3404 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003405 sizeof(struct btrfs_key_ptr) *
3406 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003407 }
Chris Mason7518a232007-03-12 12:01:18 -04003408 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003409 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003410 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003411 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003412 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003413 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003414 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003415 struct btrfs_disk_key disk_key;
3416
3417 btrfs_node_key(parent, &disk_key, 0);
3418 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003419 if (wret)
3420 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003421 }
Chris Masond6025572007-03-30 14:27:56 -04003422 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003423 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003424}
3425
Chris Mason74123bd2007-02-02 11:05:29 -05003426/*
Chris Mason323ac952008-10-01 19:05:46 -04003427 * a helper function to delete the leaf pointed to by path->slots[1] and
3428 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3429 * already know it, it is faster to have them pass it down than to
3430 * read it out of the node again.
3431 *
3432 * This deletes the pointer in path->nodes[1] and frees the leaf
3433 * block extent. zero is returned if it all worked out, < 0 otherwise.
3434 *
3435 * The path must have already been setup for deleting the leaf, including
3436 * all the proper balancing. path->nodes[1] must be locked.
3437 */
3438noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 struct btrfs_path *path, u64 bytenr)
3441{
3442 int ret;
3443 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3444
3445 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3446 if (ret)
3447 return ret;
3448
3449 ret = btrfs_free_extent(trans, root, bytenr,
3450 btrfs_level_size(root, 0),
3451 path->nodes[1]->start,
3452 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003453 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003454 return ret;
3455}
3456/*
Chris Mason74123bd2007-02-02 11:05:29 -05003457 * delete the item at the leaf level in path. If that empties
3458 * the leaf, remove it from the tree
3459 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003460int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3461 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003462{
Chris Mason5f39d392007-10-15 16:14:19 -04003463 struct extent_buffer *leaf;
3464 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003465 int last_off;
3466 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003467 int ret = 0;
3468 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003469 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003470 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003471
Chris Mason5f39d392007-10-15 16:14:19 -04003472 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003473 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3474
3475 for (i = 0; i < nr; i++)
3476 dsize += btrfs_item_size_nr(leaf, slot + i);
3477
Chris Mason5f39d392007-10-15 16:14:19 -04003478 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003479
Chris Mason85e21ba2008-01-29 15:11:36 -05003480 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003481 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003482
3483 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003484 data_end + dsize,
3485 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003486 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003487
Chris Mason85e21ba2008-01-29 15:11:36 -05003488 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003489 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003490
Chris Mason5f39d392007-10-15 16:14:19 -04003491 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003492 if (!leaf->map_token) {
3493 map_extent_buffer(leaf, (unsigned long)item,
3494 sizeof(struct btrfs_item),
3495 &leaf->map_token, &leaf->kaddr,
3496 &leaf->map_start, &leaf->map_len,
3497 KM_USER1);
3498 }
Chris Mason5f39d392007-10-15 16:14:19 -04003499 ioff = btrfs_item_offset(leaf, item);
3500 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003501 }
Chris Masondb945352007-10-15 16:15:53 -04003502
3503 if (leaf->map_token) {
3504 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3505 leaf->map_token = NULL;
3506 }
3507
Chris Mason5f39d392007-10-15 16:14:19 -04003508 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003509 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003510 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003511 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003512 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003513 btrfs_set_header_nritems(leaf, nritems - nr);
3514 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003515
Chris Mason74123bd2007-02-02 11:05:29 -05003516 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003517 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003518 if (leaf == root->node) {
3519 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003520 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003521 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3522 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003523 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003524 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003525 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003526 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003527 struct btrfs_disk_key disk_key;
3528
3529 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003530 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003531 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003532 if (wret)
3533 ret = wret;
3534 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003535
Chris Mason74123bd2007-02-02 11:05:29 -05003536 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003537 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538 /* push_leaf_left fixes the path.
3539 * make sure the path still points to our leaf
3540 * for possible call to del_ptr below
3541 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003542 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003543 extent_buffer_get(leaf);
3544
Chris Mason85e21ba2008-01-29 15:11:36 -05003545 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003546 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003547 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003548
3549 if (path->nodes[0] == leaf &&
3550 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003551 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003552 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003553 ret = wret;
3554 }
Chris Mason5f39d392007-10-15 16:14:19 -04003555
3556 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003557 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003558 ret = btrfs_del_leaf(trans, root, path,
3559 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003560 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003561 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003562 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003563 /* if we're still in the path, make sure
3564 * we're dirty. Otherwise, one of the
3565 * push_leaf functions must have already
3566 * dirtied this buffer
3567 */
3568 if (path->nodes[0] == leaf)
3569 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003570 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571 }
Chris Masond5719762007-03-23 10:01:08 -04003572 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003573 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574 }
3575 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003576 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003577}
3578
Chris Mason97571fd2007-02-24 13:39:08 -05003579/*
Chris Mason925baed2008-06-25 16:01:30 -04003580 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003581 * returns 0 if it found something or 1 if there are no lesser leaves.
3582 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003583 *
3584 * This may release the path, and so you may lose any locks held at the
3585 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003586 */
3587int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3588{
Chris Mason925baed2008-06-25 16:01:30 -04003589 struct btrfs_key key;
3590 struct btrfs_disk_key found_key;
3591 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003592
Chris Mason925baed2008-06-25 16:01:30 -04003593 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003594
Chris Mason925baed2008-06-25 16:01:30 -04003595 if (key.offset > 0)
3596 key.offset--;
3597 else if (key.type > 0)
3598 key.type--;
3599 else if (key.objectid > 0)
3600 key.objectid--;
3601 else
3602 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003603
Chris Mason925baed2008-06-25 16:01:30 -04003604 btrfs_release_path(root, path);
3605 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3606 if (ret < 0)
3607 return ret;
3608 btrfs_item_key(path->nodes[0], &found_key, 0);
3609 ret = comp_keys(&found_key, &key);
3610 if (ret < 0)
3611 return 0;
3612 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003613}
3614
Chris Mason3f157a22008-06-25 16:01:31 -04003615/*
3616 * A helper function to walk down the tree starting at min_key, and looking
3617 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003618 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003619 *
3620 * This does not cow, but it does stuff the starting key it finds back
3621 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3622 * key and get a writable path.
3623 *
3624 * This does lock as it descends, and path->keep_locks should be set
3625 * to 1 by the caller.
3626 *
3627 * This honors path->lowest_level to prevent descent past a given level
3628 * of the tree.
3629 *
Chris Masond352ac62008-09-29 15:18:18 -04003630 * min_trans indicates the oldest transaction that you are interested
3631 * in walking through. Any nodes or leaves older than min_trans are
3632 * skipped over (without reading them).
3633 *
Chris Mason3f157a22008-06-25 16:01:31 -04003634 * returns zero if something useful was found, < 0 on error and 1 if there
3635 * was nothing in the tree that matched the search criteria.
3636 */
3637int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003638 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003639 struct btrfs_path *path, int cache_only,
3640 u64 min_trans)
3641{
3642 struct extent_buffer *cur;
3643 struct btrfs_key found_key;
3644 int slot;
Yan96524802008-07-24 12:19:49 -04003645 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003646 u32 nritems;
3647 int level;
3648 int ret = 1;
3649
Chris Mason934d3752008-12-08 16:43:10 -05003650 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003651again:
3652 cur = btrfs_lock_root_node(root);
3653 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003654 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003655 path->nodes[level] = cur;
3656 path->locks[level] = 1;
3657
3658 if (btrfs_header_generation(cur) < min_trans) {
3659 ret = 1;
3660 goto out;
3661 }
Chris Masond3977122009-01-05 21:25:51 -05003662 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003663 nritems = btrfs_header_nritems(cur);
3664 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003665 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003666
Chris Mason323ac952008-10-01 19:05:46 -04003667 /* at the lowest level, we're done, setup the path and exit */
3668 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003669 if (slot >= nritems)
3670 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003671 ret = 0;
3672 path->slots[level] = slot;
3673 btrfs_item_key_to_cpu(cur, &found_key, slot);
3674 goto out;
3675 }
Yan96524802008-07-24 12:19:49 -04003676 if (sret && slot > 0)
3677 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003678 /*
3679 * check this node pointer against the cache_only and
3680 * min_trans parameters. If it isn't in cache or is too
3681 * old, skip to the next one.
3682 */
Chris Masond3977122009-01-05 21:25:51 -05003683 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003684 u64 blockptr;
3685 u64 gen;
3686 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003687 struct btrfs_disk_key disk_key;
3688
Chris Mason3f157a22008-06-25 16:01:31 -04003689 blockptr = btrfs_node_blockptr(cur, slot);
3690 gen = btrfs_node_ptr_generation(cur, slot);
3691 if (gen < min_trans) {
3692 slot++;
3693 continue;
3694 }
3695 if (!cache_only)
3696 break;
3697
Chris Masone02119d2008-09-05 16:13:11 -04003698 if (max_key) {
3699 btrfs_node_key(cur, &disk_key, slot);
3700 if (comp_keys(&disk_key, max_key) >= 0) {
3701 ret = 1;
3702 goto out;
3703 }
3704 }
3705
Chris Mason3f157a22008-06-25 16:01:31 -04003706 tmp = btrfs_find_tree_block(root, blockptr,
3707 btrfs_level_size(root, level - 1));
3708
3709 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3710 free_extent_buffer(tmp);
3711 break;
3712 }
3713 if (tmp)
3714 free_extent_buffer(tmp);
3715 slot++;
3716 }
Chris Masone02119d2008-09-05 16:13:11 -04003717find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003718 /*
3719 * we didn't find a candidate key in this node, walk forward
3720 * and find another one
3721 */
3722 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003723 path->slots[level] = slot;
3724 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003725 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003726 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003727 btrfs_release_path(root, path);
3728 goto again;
3729 } else {
3730 goto out;
3731 }
3732 }
3733 /* save our key for returning back */
3734 btrfs_node_key_to_cpu(cur, &found_key, slot);
3735 path->slots[level] = slot;
3736 if (level == path->lowest_level) {
3737 ret = 0;
3738 unlock_up(path, level, 1);
3739 goto out;
3740 }
3741 cur = read_node_slot(root, cur, slot);
3742
3743 btrfs_tree_lock(cur);
3744 path->locks[level - 1] = 1;
3745 path->nodes[level - 1] = cur;
3746 unlock_up(path, level, 1);
3747 }
3748out:
3749 if (ret == 0)
3750 memcpy(min_key, &found_key, sizeof(found_key));
3751 return ret;
3752}
3753
3754/*
3755 * this is similar to btrfs_next_leaf, but does not try to preserve
3756 * and fixup the path. It looks for and returns the next key in the
3757 * tree based on the current path and the cache_only and min_trans
3758 * parameters.
3759 *
3760 * 0 is returned if another key is found, < 0 if there are any errors
3761 * and 1 is returned if there are no higher keys in the tree
3762 *
3763 * path->keep_locks should be set to 1 on the search made before
3764 * calling this function.
3765 */
Chris Masone7a84562008-06-25 16:01:31 -04003766int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003767 struct btrfs_key *key, int lowest_level,
3768 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003769{
3770 int level = lowest_level;
3771 int slot;
3772 struct extent_buffer *c;
3773
Chris Mason934d3752008-12-08 16:43:10 -05003774 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003775 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003776 if (!path->nodes[level])
3777 return 1;
3778
3779 slot = path->slots[level] + 1;
3780 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003781next:
Chris Masone7a84562008-06-25 16:01:31 -04003782 if (slot >= btrfs_header_nritems(c)) {
3783 level++;
Chris Masond3977122009-01-05 21:25:51 -05003784 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04003785 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04003786 continue;
3787 }
3788 if (level == 0)
3789 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003790 else {
3791 u64 blockptr = btrfs_node_blockptr(c, slot);
3792 u64 gen = btrfs_node_ptr_generation(c, slot);
3793
3794 if (cache_only) {
3795 struct extent_buffer *cur;
3796 cur = btrfs_find_tree_block(root, blockptr,
3797 btrfs_level_size(root, level - 1));
3798 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3799 slot++;
3800 if (cur)
3801 free_extent_buffer(cur);
3802 goto next;
3803 }
3804 free_extent_buffer(cur);
3805 }
3806 if (gen < min_trans) {
3807 slot++;
3808 goto next;
3809 }
Chris Masone7a84562008-06-25 16:01:31 -04003810 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003811 }
Chris Masone7a84562008-06-25 16:01:31 -04003812 return 0;
3813 }
3814 return 1;
3815}
3816
Chris Mason7bb86312007-12-11 09:25:06 -05003817/*
Chris Mason925baed2008-06-25 16:01:30 -04003818 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003819 * returns 0 if it found something or 1 if there are no greater leaves.
3820 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003821 */
Chris Mason234b63a2007-03-13 10:46:10 -04003822int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003823{
3824 int slot;
3825 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003826 struct extent_buffer *c;
3827 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003828 struct btrfs_key key;
3829 u32 nritems;
3830 int ret;
3831
3832 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05003833 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04003834 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003835
3836 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3837
Chris Mason925baed2008-06-25 16:01:30 -04003838 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003839 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3841 path->keep_locks = 0;
3842
3843 if (ret < 0)
3844 return ret;
3845
Chris Masona2135012008-06-25 16:01:30 -04003846 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003847 /*
3848 * by releasing the path above we dropped all our locks. A balance
3849 * could have added more items next to the key that used to be
3850 * at the very end of the block. So, check again here and
3851 * advance the path if there are now more items available.
3852 */
Chris Masona2135012008-06-25 16:01:30 -04003853 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003854 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003855 goto done;
3856 }
Chris Masond97e63b2007-02-20 16:40:44 -05003857
Chris Masond3977122009-01-05 21:25:51 -05003858 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003859 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003860 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003861
Chris Masond97e63b2007-02-20 16:40:44 -05003862 slot = path->slots[level] + 1;
3863 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003864 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003865 level++;
Chris Masond3977122009-01-05 21:25:51 -05003866 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05003867 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003868 continue;
3869 }
Chris Mason5f39d392007-10-15 16:14:19 -04003870
Chris Mason925baed2008-06-25 16:01:30 -04003871 if (next) {
3872 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003873 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003874 }
Chris Mason5f39d392007-10-15 16:14:19 -04003875
Chris Mason0bd40a72008-07-17 12:54:43 -04003876 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3877 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003878 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003879
Chris Masonca7a79a2008-05-12 12:59:19 -04003880 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003881 if (!path->skip_locking) {
3882 WARN_ON(!btrfs_tree_locked(c));
3883 btrfs_tree_lock(next);
3884 }
Chris Masond97e63b2007-02-20 16:40:44 -05003885 break;
3886 }
3887 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003888 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05003889 level--;
3890 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003891 if (path->locks[level])
3892 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003893 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003894 path->nodes[level] = next;
3895 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003896 if (!path->skip_locking)
3897 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003898 if (!level)
3899 break;
Chris Mason925baed2008-06-25 16:01:30 -04003900 if (level == 1 && path->locks[1] && path->reada)
3901 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003902 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003903 if (!path->skip_locking) {
3904 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3905 btrfs_tree_lock(next);
3906 }
Chris Masond97e63b2007-02-20 16:40:44 -05003907 }
Chris Mason925baed2008-06-25 16:01:30 -04003908done:
3909 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003910 return 0;
3911}
Chris Mason0b86a832008-03-24 15:01:56 -04003912
Chris Mason3f157a22008-06-25 16:01:31 -04003913/*
3914 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3915 * searching until it gets past min_objectid or finds an item of 'type'
3916 *
3917 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3918 */
Chris Mason0b86a832008-03-24 15:01:56 -04003919int btrfs_previous_item(struct btrfs_root *root,
3920 struct btrfs_path *path, u64 min_objectid,
3921 int type)
3922{
3923 struct btrfs_key found_key;
3924 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003925 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003926 int ret;
3927
Chris Masond3977122009-01-05 21:25:51 -05003928 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003929 if (path->slots[0] == 0) {
3930 ret = btrfs_prev_leaf(root, path);
3931 if (ret != 0)
3932 return ret;
3933 } else {
3934 path->slots[0]--;
3935 }
3936 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003937 nritems = btrfs_header_nritems(leaf);
3938 if (nritems == 0)
3939 return 1;
3940 if (path->slots[0] == nritems)
3941 path->slots[0]--;
3942
Chris Mason0b86a832008-03-24 15:01:56 -04003943 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3944 if (found_key.type == type)
3945 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003946 if (found_key.objectid < min_objectid)
3947 break;
3948 if (found_key.objectid == min_objectid &&
3949 found_key.type < type)
3950 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003951 }
3952 return 1;
3953}