blob: 3b6e35aafc9e3c882fa0c53be65fc338ca59df36 [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
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400391 if (btrfs_header_generation(buf) == trans->transid &&
392 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400393 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500394 *cow_ret = buf;
Chris Mason65b51a02008-08-01 15:11:20 -0400395 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500396 return 0;
397 }
Chris Masonc4876852009-02-04 09:24:25 -0500398
Chris Mason0b86a832008-03-24 15:01:56 -0400399 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400400 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400401 parent_slot, cow_ret, search_start, 0,
402 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400403 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400404}
405
Chris Masond352ac62008-09-29 15:18:18 -0400406/*
407 * helper function for defrag to decide if two blocks pointed to by a
408 * node are actually close by
409 */
Chris Mason6b800532007-10-15 16:17:34 -0400410static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400411{
Chris Mason6b800532007-10-15 16:17:34 -0400412 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400413 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400414 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400415 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500416 return 0;
417}
418
Chris Mason081e9572007-11-06 10:26:24 -0500419/*
420 * compare two keys in a memcmp fashion
421 */
422static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
423{
424 struct btrfs_key k1;
425
426 btrfs_disk_key_to_cpu(&k1, disk);
427
428 if (k1.objectid > k2->objectid)
429 return 1;
430 if (k1.objectid < k2->objectid)
431 return -1;
432 if (k1.type > k2->type)
433 return 1;
434 if (k1.type < k2->type)
435 return -1;
436 if (k1.offset > k2->offset)
437 return 1;
438 if (k1.offset < k2->offset)
439 return -1;
440 return 0;
441}
442
Josef Bacikf3465ca2008-11-12 14:19:50 -0500443/*
444 * same as comp_keys only with two btrfs_key's
445 */
446static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
447{
448 if (k1->objectid > k2->objectid)
449 return 1;
450 if (k1->objectid < k2->objectid)
451 return -1;
452 if (k1->type > k2->type)
453 return 1;
454 if (k1->type < k2->type)
455 return -1;
456 if (k1->offset > k2->offset)
457 return 1;
458 if (k1->offset < k2->offset)
459 return -1;
460 return 0;
461}
Chris Mason081e9572007-11-06 10:26:24 -0500462
Chris Masond352ac62008-09-29 15:18:18 -0400463/*
464 * this is used by the defrag code to go through all the
465 * leaves pointed to by a node and reallocate them so that
466 * disk order is close to key order
467 */
Chris Mason6702ed42007-08-07 16:15:09 -0400468int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400469 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400470 int start_slot, int cache_only, u64 *last_ret,
471 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400472{
Chris Mason6b800532007-10-15 16:17:34 -0400473 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400474 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400475 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400476 u64 search_start = *last_ret;
477 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400478 u64 other;
479 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400480 int end_slot;
481 int i;
482 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400483 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400484 int uptodate;
485 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500486 int progress_passed = 0;
487 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400488
Chris Mason5708b952007-10-25 15:43:18 -0400489 parent_level = btrfs_header_level(parent);
490 if (cache_only && parent_level != 1)
491 return 0;
492
Chris Masond3977122009-01-05 21:25:51 -0500493 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400494 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500495 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400496 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400497
Chris Mason6b800532007-10-15 16:17:34 -0400498 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400499 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400500 end_slot = parent_nritems;
501
502 if (parent_nritems == 1)
503 return 0;
504
505 for (i = start_slot; i < end_slot; i++) {
506 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400507
Chris Mason5708b952007-10-25 15:43:18 -0400508 if (!parent->map_token) {
509 map_extent_buffer(parent,
510 btrfs_node_key_ptr_offset(i),
511 sizeof(struct btrfs_key_ptr),
512 &parent->map_token, &parent->kaddr,
513 &parent->map_start, &parent->map_len,
514 KM_USER1);
515 }
Chris Mason081e9572007-11-06 10:26:24 -0500516 btrfs_node_key(parent, &disk_key, i);
517 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
518 continue;
519
520 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400521 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400522 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400523 if (last_block == 0)
524 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400525
Chris Mason6702ed42007-08-07 16:15:09 -0400526 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400527 other = btrfs_node_blockptr(parent, i - 1);
528 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400529 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400530 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400531 other = btrfs_node_blockptr(parent, i + 1);
532 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400533 }
Chris Masone9d0b132007-08-10 14:06:19 -0400534 if (close) {
535 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400536 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400537 }
Chris Mason5708b952007-10-25 15:43:18 -0400538 if (parent->map_token) {
539 unmap_extent_buffer(parent, parent->map_token,
540 KM_USER1);
541 parent->map_token = NULL;
542 }
Chris Mason6702ed42007-08-07 16:15:09 -0400543
Chris Mason6b800532007-10-15 16:17:34 -0400544 cur = btrfs_find_tree_block(root, blocknr, blocksize);
545 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400546 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400547 else
548 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400549 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400550 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400551 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400552 continue;
553 }
Chris Mason6b800532007-10-15 16:17:34 -0400554 if (!cur) {
555 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400556 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400557 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400558 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400559 }
Chris Mason6702ed42007-08-07 16:15:09 -0400560 }
Chris Masone9d0b132007-08-10 14:06:19 -0400561 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400562 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400563
Chris Masone7a84562008-06-25 16:01:31 -0400564 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400565 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400566 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400567 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400568 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400569 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400570 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400571 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400572 break;
Yan252c38f2007-08-29 09:11:44 -0400573 }
Chris Masone7a84562008-06-25 16:01:31 -0400574 search_start = cur->start;
575 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400576 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400577 btrfs_tree_unlock(cur);
578 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400579 }
Chris Mason5708b952007-10-25 15:43:18 -0400580 if (parent->map_token) {
581 unmap_extent_buffer(parent, parent->map_token,
582 KM_USER1);
583 parent->map_token = NULL;
584 }
Chris Mason6702ed42007-08-07 16:15:09 -0400585 return err;
586}
587
Chris Mason74123bd2007-02-02 11:05:29 -0500588/*
589 * The leaf data grows from end-to-front in the node.
590 * this returns the address of the start of the last item,
591 * which is the stop of the leaf data stack
592 */
Chris Mason123abc82007-03-14 14:14:43 -0400593static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400594 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500595{
Chris Mason5f39d392007-10-15 16:14:19 -0400596 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500597 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400598 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400599 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500600}
601
Chris Masond352ac62008-09-29 15:18:18 -0400602/*
603 * extra debugging checks to make sure all the items in a key are
604 * well formed and in the proper order
605 */
Chris Mason123abc82007-03-14 14:14:43 -0400606static int check_node(struct btrfs_root *root, struct btrfs_path *path,
607 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500608{
Chris Mason5f39d392007-10-15 16:14:19 -0400609 struct extent_buffer *parent = NULL;
610 struct extent_buffer *node = path->nodes[level];
611 struct btrfs_disk_key parent_key;
612 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500613 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400614 int slot;
615 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400616 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500617
618 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400619 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400620
Chris Mason8d7be552007-05-10 11:24:42 -0400621 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400622 BUG_ON(nritems == 0);
623 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400624 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400625 btrfs_node_key(parent, &parent_key, parent_slot);
626 btrfs_node_key(node, &node_key, 0);
627 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400628 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400629 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400630 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500631 }
Chris Mason123abc82007-03-14 14:14:43 -0400632 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400633 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400634 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
635 btrfs_node_key(node, &node_key, slot);
636 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400637 }
638 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400639 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
640 btrfs_node_key(node, &node_key, slot);
641 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500642 }
643 return 0;
644}
645
Chris Masond352ac62008-09-29 15:18:18 -0400646/*
647 * extra checking to make sure all the items in a leaf are
648 * well formed and in the proper order
649 */
Chris Mason123abc82007-03-14 14:14:43 -0400650static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
651 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500652{
Chris Mason5f39d392007-10-15 16:14:19 -0400653 struct extent_buffer *leaf = path->nodes[level];
654 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500655 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400656 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400657 struct btrfs_disk_key parent_key;
658 struct btrfs_disk_key leaf_key;
659 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400660
Chris Mason5f39d392007-10-15 16:14:19 -0400661 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500662
663 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400664 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400665
666 if (nritems == 0)
667 return 0;
668
669 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400670 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400671 btrfs_node_key(parent, &parent_key, parent_slot);
672 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400673
Chris Mason5f39d392007-10-15 16:14:19 -0400674 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400675 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400676 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400677 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500678 }
Chris Mason5f39d392007-10-15 16:14:19 -0400679 if (slot != 0 && slot < nritems - 1) {
680 btrfs_item_key(leaf, &leaf_key, slot);
681 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
682 if (comp_keys(&leaf_key, &cpukey) <= 0) {
683 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500684 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400685 BUG_ON(1);
686 }
687 if (btrfs_item_offset_nr(leaf, slot - 1) !=
688 btrfs_item_end_nr(leaf, slot)) {
689 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500690 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400691 BUG_ON(1);
692 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500693 }
Chris Mason8d7be552007-05-10 11:24:42 -0400694 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400695 btrfs_item_key(leaf, &leaf_key, slot);
696 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
697 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
698 if (btrfs_item_offset_nr(leaf, slot) !=
699 btrfs_item_end_nr(leaf, slot + 1)) {
700 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500701 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400702 BUG_ON(1);
703 }
Chris Mason8d7be552007-05-10 11:24:42 -0400704 }
Chris Mason5f39d392007-10-15 16:14:19 -0400705 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
706 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500707 return 0;
708}
709
Chris Masond3977122009-01-05 21:25:51 -0500710static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500711 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500712{
Chris Mason85d824c2008-04-10 10:23:19 -0400713 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500714 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400715 return check_leaf(root, path, level);
716 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500717}
718
Chris Mason74123bd2007-02-02 11:05:29 -0500719/*
Chris Mason5f39d392007-10-15 16:14:19 -0400720 * search for key in the extent_buffer. The items start at offset p,
721 * and they are item_size apart. There are 'max' items in p.
722 *
Chris Mason74123bd2007-02-02 11:05:29 -0500723 * the slot in the array is returned via slot, and it points to
724 * the place where you would insert key if it is not found in
725 * the array.
726 *
727 * slot may point to max if the key is bigger than all of the keys
728 */
Chris Masone02119d2008-09-05 16:13:11 -0400729static noinline int generic_bin_search(struct extent_buffer *eb,
730 unsigned long p,
731 int item_size, struct btrfs_key *key,
732 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500733{
734 int low = 0;
735 int high = max;
736 int mid;
737 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400738 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400739 struct btrfs_disk_key unaligned;
740 unsigned long offset;
741 char *map_token = NULL;
742 char *kaddr = NULL;
743 unsigned long map_start = 0;
744 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400745 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500746
Chris Masond3977122009-01-05 21:25:51 -0500747 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500748 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400749 offset = p + mid * item_size;
750
751 if (!map_token || offset < map_start ||
752 (offset + sizeof(struct btrfs_disk_key)) >
753 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400754 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400755 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400756 map_token = NULL;
757 }
Chris Mason934d3752008-12-08 16:43:10 -0500758
759 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400760 sizeof(struct btrfs_disk_key),
761 &map_token, &kaddr,
762 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400763
Chris Mason479965d2007-10-15 16:14:27 -0400764 if (!err) {
765 tmp = (struct btrfs_disk_key *)(kaddr + offset -
766 map_start);
767 } else {
768 read_extent_buffer(eb, &unaligned,
769 offset, sizeof(unaligned));
770 tmp = &unaligned;
771 }
772
Chris Mason5f39d392007-10-15 16:14:19 -0400773 } else {
774 tmp = (struct btrfs_disk_key *)(kaddr + offset -
775 map_start);
776 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777 ret = comp_keys(tmp, key);
778
779 if (ret < 0)
780 low = mid + 1;
781 else if (ret > 0)
782 high = mid;
783 else {
784 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400785 if (map_token)
786 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500787 return 0;
788 }
789 }
790 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400791 if (map_token)
792 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500793 return 1;
794}
795
Chris Mason97571fd2007-02-24 13:39:08 -0500796/*
797 * simple bin_search frontend that does the right thing for
798 * leaves vs nodes
799 */
Chris Mason5f39d392007-10-15 16:14:19 -0400800static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
801 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500802{
Chris Mason5f39d392007-10-15 16:14:19 -0400803 if (level == 0) {
804 return generic_bin_search(eb,
805 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400806 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400807 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400808 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500809 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400810 return generic_bin_search(eb,
811 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400812 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400813 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400814 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500815 }
816 return -1;
817}
818
Chris Masond352ac62008-09-29 15:18:18 -0400819/* given a node and slot number, this reads the blocks it points to. The
820 * extent buffer is returned with a reference taken (but unlocked).
821 * NULL is returned on error.
822 */
Chris Masone02119d2008-09-05 16:13:11 -0400823static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400824 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500825{
Chris Masonca7a79a2008-05-12 12:59:19 -0400826 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500827 if (slot < 0)
828 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400829 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500830 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400831
832 BUG_ON(level == 0);
833
Chris Masondb945352007-10-15 16:15:53 -0400834 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400835 btrfs_level_size(root, level - 1),
836 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500837}
838
Chris Masond352ac62008-09-29 15:18:18 -0400839/*
840 * node level balancing, used to make sure nodes are in proper order for
841 * item deletion. We balance from the top down, so we have to make sure
842 * that a deletion won't leave an node completely empty later on.
843 */
Chris Masone02119d2008-09-05 16:13:11 -0400844static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500845 struct btrfs_root *root,
846 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500847{
Chris Mason5f39d392007-10-15 16:14:19 -0400848 struct extent_buffer *right = NULL;
849 struct extent_buffer *mid;
850 struct extent_buffer *left = NULL;
851 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500852 int ret = 0;
853 int wret;
854 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500855 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500857 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500858
859 if (level == 0)
860 return 0;
861
Chris Mason5f39d392007-10-15 16:14:19 -0400862 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400863 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500864 WARN_ON(btrfs_header_generation(mid) != trans->transid);
865
Chris Mason1d4f8a02007-03-13 09:28:32 -0400866 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500867
Chris Mason234b63a2007-03-13 10:46:10 -0400868 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400869 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500870 pslot = path->slots[level + 1];
871
Chris Mason40689472007-03-17 14:29:23 -0400872 /*
873 * deal with the case where there is only one pointer in the root
874 * by promoting the node below to a root
875 */
Chris Mason5f39d392007-10-15 16:14:19 -0400876 if (!parent) {
877 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500878
Chris Mason5f39d392007-10-15 16:14:19 -0400879 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500880 return 0;
881
882 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400883 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400884 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500885 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400886 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500887 BUG_ON(ret);
888
Chris Mason925baed2008-06-25 16:01:30 -0400889 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500890 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400891 spin_unlock(&root->node_lock);
892
Zheng Yan31840ae2008-09-23 13:14:14 -0400893 ret = btrfs_update_extent_ref(trans, root, child->start,
894 mid->start, child->start,
895 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400896 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400897 BUG_ON(ret);
898
Chris Mason0b86a832008-03-24 15:01:56 -0400899 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400900 btrfs_tree_unlock(child);
901 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500902 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400903 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400904 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500905 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500907 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400908 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400909 btrfs_header_generation(mid),
910 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500911 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400912 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400913 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500914 }
Chris Mason5f39d392007-10-15 16:14:19 -0400915 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400916 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500917 return 0;
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400920 err_on_enospc = 1;
921
Chris Mason5f39d392007-10-15 16:14:19 -0400922 left = read_node_slot(root, parent, pslot - 1);
923 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400924 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400925 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400926 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400927 if (wret) {
928 ret = wret;
929 goto enospc;
930 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400931 }
Chris Mason5f39d392007-10-15 16:14:19 -0400932 right = read_node_slot(root, parent, pslot + 1);
933 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400935 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400936 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400937 if (wret) {
938 ret = wret;
939 goto enospc;
940 }
941 }
942
943 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400944 if (left) {
945 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400946 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500947 if (wret < 0)
948 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400949 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400950 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500951 }
Chris Mason79f95c82007-03-01 15:16:26 -0500952
953 /*
954 * then try to empty the right most buffer into the middle
955 */
Chris Mason5f39d392007-10-15 16:14:19 -0400956 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400957 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400958 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500959 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400960 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400961 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500962 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400963 u32 blocksize = right->len;
964
Chris Mason5f39d392007-10-15 16:14:19 -0400965 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400966 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400967 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500968 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400969 wret = del_ptr(trans, root, path, level + 1, pslot +
970 1);
Chris Masonbb803952007-03-01 12:04:21 -0500971 if (wret)
972 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400973 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -0400974 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500975 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400976 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500977 if (wret)
978 ret = wret;
979 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400980 struct btrfs_disk_key right_key;
981 btrfs_node_key(right, &right_key, 0);
982 btrfs_set_node_key(parent, &right_key, pslot + 1);
983 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500984 }
985 }
Chris Mason5f39d392007-10-15 16:14:19 -0400986 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500987 /*
988 * we're not allowed to leave a node with one item in the
989 * tree during a delete. A deletion from lower in the tree
990 * could try to delete the only pointer in this node.
991 * So, pull some keys from the left.
992 * There has to be a left pointer at this point because
993 * otherwise we would have pulled some pointers from the
994 * right
995 */
Chris Mason5f39d392007-10-15 16:14:19 -0400996 BUG_ON(!left);
997 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400998 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500999 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001000 goto enospc;
1001 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001002 if (wret == 1) {
1003 wret = push_node_left(trans, root, left, mid, 1);
1004 if (wret < 0)
1005 ret = wret;
1006 }
Chris Mason79f95c82007-03-01 15:16:26 -05001007 BUG_ON(wret == 1);
1008 }
Chris Mason5f39d392007-10-15 16:14:19 -04001009 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001010 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001011 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001012 u64 bytenr = mid->start;
1013 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001014
Chris Mason5f39d392007-10-15 16:14:19 -04001015 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001016 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001017 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001018 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001019 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001020 if (wret)
1021 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001022 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001023 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001024 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001025 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001026 if (wret)
1027 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001028 } else {
1029 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001030 struct btrfs_disk_key mid_key;
1031 btrfs_node_key(mid, &mid_key, 0);
1032 btrfs_set_node_key(parent, &mid_key, pslot);
1033 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001034 }
Chris Masonbb803952007-03-01 12:04:21 -05001035
Chris Mason79f95c82007-03-01 15:16:26 -05001036 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001037 if (left) {
1038 if (btrfs_header_nritems(left) > orig_slot) {
1039 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001040 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001041 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001042 path->slots[level + 1] -= 1;
1043 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001044 if (mid) {
1045 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001046 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001047 }
Chris Masonbb803952007-03-01 12:04:21 -05001048 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001049 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001050 path->slots[level] = orig_slot;
1051 }
1052 }
Chris Mason79f95c82007-03-01 15:16:26 -05001053 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001054 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001055 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001056 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001057 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001058enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001059 if (right) {
1060 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001061 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001062 }
1063 if (left) {
1064 if (path->nodes[level] != left)
1065 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001067 }
Chris Masonbb803952007-03-01 12:04:21 -05001068 return ret;
1069}
1070
Chris Masond352ac62008-09-29 15:18:18 -04001071/* Node balancing for insertion. Here we only split or push nodes around
1072 * when they are completely full. This is also done top down, so we
1073 * have to be pessimistic.
1074 */
Chris Masond3977122009-01-05 21:25:51 -05001075static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001076 struct btrfs_root *root,
1077 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001078{
Chris Mason5f39d392007-10-15 16:14:19 -04001079 struct extent_buffer *right = NULL;
1080 struct extent_buffer *mid;
1081 struct extent_buffer *left = NULL;
1082 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001083 int ret = 0;
1084 int wret;
1085 int pslot;
1086 int orig_slot = path->slots[level];
1087 u64 orig_ptr;
1088
1089 if (level == 0)
1090 return 1;
1091
Chris Mason5f39d392007-10-15 16:14:19 -04001092 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001093 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001094 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1095
1096 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001097 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001098 pslot = path->slots[level + 1];
1099
Chris Mason5f39d392007-10-15 16:14:19 -04001100 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001101 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001102
Chris Mason5f39d392007-10-15 16:14:19 -04001103 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001104
1105 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001106 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001107 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001108
1109 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001110 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001111 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1112 wret = 1;
1113 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001114 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001115 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001116 if (ret)
1117 wret = 1;
1118 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001119 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001120 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001121 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001122 }
Chris Masone66f7092007-04-20 13:16:02 -04001123 if (wret < 0)
1124 ret = wret;
1125 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001126 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001127 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001128 btrfs_node_key(mid, &disk_key, 0);
1129 btrfs_set_node_key(parent, &disk_key, pslot);
1130 btrfs_mark_buffer_dirty(parent);
1131 if (btrfs_header_nritems(left) > orig_slot) {
1132 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001133 path->slots[level + 1] -= 1;
1134 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001135 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001136 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001137 } else {
1138 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001139 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001140 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001141 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001142 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001143 }
Chris Masone66f7092007-04-20 13:16:02 -04001144 return 0;
1145 }
Chris Mason925baed2008-06-25 16:01:30 -04001146 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001147 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001148 }
Chris Mason925baed2008-06-25 16:01:30 -04001149 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001150
1151 /*
1152 * then try to empty the right most buffer into the middle
1153 */
Chris Mason5f39d392007-10-15 16:14:19 -04001154 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001155 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001156 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001157 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001158 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1159 wret = 1;
1160 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001161 ret = btrfs_cow_block(trans, root, right,
1162 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001163 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001164 if (ret)
1165 wret = 1;
1166 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001167 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001168 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001169 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001170 }
Chris Masone66f7092007-04-20 13:16:02 -04001171 if (wret < 0)
1172 ret = wret;
1173 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001174 struct btrfs_disk_key disk_key;
1175
1176 btrfs_node_key(right, &disk_key, 0);
1177 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1178 btrfs_mark_buffer_dirty(parent);
1179
1180 if (btrfs_header_nritems(mid) <= orig_slot) {
1181 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001182 path->slots[level + 1] += 1;
1183 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001184 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001185 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001186 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001187 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001188 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001189 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001190 }
Chris Masone66f7092007-04-20 13:16:02 -04001191 return 0;
1192 }
Chris Mason925baed2008-06-25 16:01:30 -04001193 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001194 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001195 }
Chris Masone66f7092007-04-20 13:16:02 -04001196 return 1;
1197}
1198
Chris Mason74123bd2007-02-02 11:05:29 -05001199/*
Chris Masond352ac62008-09-29 15:18:18 -04001200 * readahead one full node of leaves, finding things that are close
1201 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001202 */
Chris Masone02119d2008-09-05 16:13:11 -04001203static noinline void reada_for_search(struct btrfs_root *root,
1204 struct btrfs_path *path,
1205 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001206{
Chris Mason5f39d392007-10-15 16:14:19 -04001207 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001208 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001209 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001210 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001211 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001212 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001213 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001214 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001215 u32 nr;
1216 u32 blocksize;
1217 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001218
Chris Masona6b6e752007-10-15 16:22:39 -04001219 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001220 return;
1221
Chris Mason6702ed42007-08-07 16:15:09 -04001222 if (!path->nodes[level])
1223 return;
1224
Chris Mason5f39d392007-10-15 16:14:19 -04001225 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001226
Chris Mason3c69fae2007-08-07 15:52:22 -04001227 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001228 blocksize = btrfs_level_size(root, level - 1);
1229 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001230 if (eb) {
1231 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001232 return;
1233 }
1234
Chris Masona7175312009-01-22 09:23:10 -05001235 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001236
Chris Mason5f39d392007-10-15 16:14:19 -04001237 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001238 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001239 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001240 if (direction < 0) {
1241 if (nr == 0)
1242 break;
1243 nr--;
1244 } else if (direction > 0) {
1245 nr++;
1246 if (nr >= nritems)
1247 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001248 }
Chris Mason01f46652007-12-21 16:24:26 -05001249 if (path->reada < 0 && objectid) {
1250 btrfs_node_key(node, &disk_key, nr);
1251 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1252 break;
1253 }
Chris Mason6b800532007-10-15 16:17:34 -04001254 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001255 if ((search <= target && target - search <= 65536) ||
1256 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001257 readahead_tree_block(root, search, blocksize,
1258 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001259 nread += blocksize;
1260 }
1261 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001262 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001263 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001264 }
1265}
Chris Mason925baed2008-06-25 16:01:30 -04001266
Chris Masond352ac62008-09-29 15:18:18 -04001267/*
Chris Masond3977122009-01-05 21:25:51 -05001268 * when we walk down the tree, it is usually safe to unlock the higher layers
1269 * in the tree. The exceptions are when our path goes through slot 0, because
1270 * operations on the tree might require changing key pointers higher up in the
1271 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001272 *
Chris Masond3977122009-01-05 21:25:51 -05001273 * callers might also have set path->keep_locks, which tells this code to keep
1274 * the lock if the path points to the last slot in the block. This is part of
1275 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001276 *
Chris Masond3977122009-01-05 21:25:51 -05001277 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1278 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001279 */
Chris Masone02119d2008-09-05 16:13:11 -04001280static noinline void unlock_up(struct btrfs_path *path, int level,
1281 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001282{
1283 int i;
1284 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001285 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001286 struct extent_buffer *t;
1287
1288 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1289 if (!path->nodes[i])
1290 break;
1291 if (!path->locks[i])
1292 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001293 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001294 skip_level = i + 1;
1295 continue;
1296 }
Chris Mason051e1b92008-06-25 16:01:30 -04001297 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001298 u32 nritems;
1299 t = path->nodes[i];
1300 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001301 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001302 skip_level = i + 1;
1303 continue;
1304 }
1305 }
Chris Mason051e1b92008-06-25 16:01:30 -04001306 if (skip_level < i && i >= lowest_unlock)
1307 no_skips = 1;
1308
Chris Mason925baed2008-06-25 16:01:30 -04001309 t = path->nodes[i];
1310 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1311 btrfs_tree_unlock(t);
1312 path->locks[i] = 0;
1313 }
1314 }
1315}
1316
Chris Mason3c69fae2007-08-07 15:52:22 -04001317/*
Chris Mason74123bd2007-02-02 11:05:29 -05001318 * look for key in the tree. path is filled in with nodes along the way
1319 * if key is found, we return zero and you can find the item in the leaf
1320 * level of the path (level 0)
1321 *
1322 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001323 * be inserted, and 1 is returned. If there are other errors during the
1324 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001325 *
1326 * if ins_len > 0, nodes and leaves will be split as we walk down the
1327 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1328 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001329 */
Chris Masone089f052007-03-16 16:20:31 -04001330int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1331 *root, struct btrfs_key *key, struct btrfs_path *p, int
1332 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001333{
Chris Mason5f39d392007-10-15 16:14:19 -04001334 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001335 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001336 int slot;
1337 int ret;
1338 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001339 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001340 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001341 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001342 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001343 u64 blocknr;
1344 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001345 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001346
Chris Mason6702ed42007-08-07 16:15:09 -04001347 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001348 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001349 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001350
Chris Mason925baed2008-06-25 16:01:30 -04001351 if (ins_len < 0)
1352 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001353
1354 prealloc_block.objectid = 0;
1355
Chris Masonbb803952007-03-01 12:04:21 -05001356again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001357 if (p->skip_locking)
1358 b = btrfs_root_node(root);
1359 else
1360 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001361
Chris Masoneb60cea2007-02-02 09:18:22 -05001362 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001363 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001364
1365 /*
1366 * setup the path here so we can release it under lock
1367 * contention with the cow code
1368 */
1369 p->nodes[level] = b;
1370 if (!p->skip_locking)
1371 p->locks[level] = 1;
1372
Chris Mason02217ed2007-03-02 16:08:05 -05001373 if (cow) {
1374 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001375
1376 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001377 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001378 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001379 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001380 goto cow_done;
1381 }
Chris Mason65b51a02008-08-01 15:11:20 -04001382
1383 /* ok, we have to cow, is our old prealloc the right
1384 * size?
1385 */
1386 if (prealloc_block.objectid &&
1387 prealloc_block.offset != b->len) {
1388 btrfs_free_reserved_extent(root,
1389 prealloc_block.objectid,
1390 prealloc_block.offset);
1391 prealloc_block.objectid = 0;
1392 }
1393
1394 /*
1395 * for higher level blocks, try not to allocate blocks
1396 * with the block and the parent locks held.
1397 */
1398 if (level > 1 && !prealloc_block.objectid &&
1399 btrfs_path_lock_waiting(p, level)) {
1400 u32 size = b->len;
1401 u64 hint = b->start;
1402
1403 btrfs_release_path(root, p);
1404 ret = btrfs_reserve_extent(trans, root,
1405 size, size, 0,
1406 hint, (u64)-1,
1407 &prealloc_block, 0);
1408 BUG_ON(ret);
1409 goto again;
1410 }
1411
Chris Masone20d96d2007-03-22 12:13:20 -04001412 wret = btrfs_cow_block(trans, root, b,
1413 p->nodes[level + 1],
1414 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001415 &b, prealloc_block.objectid);
1416 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001417 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001418 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001419 ret = wret;
1420 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001421 }
Chris Mason02217ed2007-03-02 16:08:05 -05001422 }
Chris Mason65b51a02008-08-01 15:11:20 -04001423cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001424 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001425 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001426 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001427 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001428
Chris Masoneb60cea2007-02-02 09:18:22 -05001429 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001430 if (!p->skip_locking)
1431 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001432
Chris Mason123abc82007-03-14 14:14:43 -04001433 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001434 if (ret) {
1435 ret = -1;
1436 goto done;
1437 }
Chris Mason925baed2008-06-25 16:01:30 -04001438
Chris Mason5f39d392007-10-15 16:14:19 -04001439 ret = bin_search(b, key, level, &slot);
1440 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001441 if (ret && slot > 0)
1442 slot -= 1;
1443 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001444 if ((p->search_for_split || ins_len > 0) &&
1445 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001446 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001447 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001448 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001449 if (sret) {
1450 ret = sret;
1451 goto done;
1452 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001453 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001454 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001455 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001456 int sret = balance_level(trans, root, p,
1457 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001458 if (sret) {
1459 ret = sret;
1460 goto done;
1461 }
Chris Masonbb803952007-03-01 12:04:21 -05001462 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001463 if (!b) {
1464 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001465 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001466 }
Chris Masonbb803952007-03-01 12:04:21 -05001467 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001468 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001469 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001470 unlock_up(p, level, lowest_unlock);
1471
Chris Mason9f3a7422007-08-07 15:52:19 -04001472 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001473 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001474 ret = 0;
1475 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001476 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001477
Chris Mason594a24e2008-06-25 16:01:30 -04001478 blocknr = btrfs_node_blockptr(b, slot);
1479 gen = btrfs_node_ptr_generation(b, slot);
1480 blocksize = btrfs_level_size(root, level - 1);
1481
1482 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1483 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001484 b = tmp;
1485 } else {
1486 /*
1487 * reduce lock contention at high levels
1488 * of the btree by dropping locks before
1489 * we read.
1490 */
1491 if (level > 1) {
1492 btrfs_release_path(NULL, p);
1493 if (tmp)
1494 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001495 if (should_reada)
1496 reada_for_search(root, p,
1497 level, slot,
1498 key->objectid);
1499
Chris Mason594a24e2008-06-25 16:01:30 -04001500 tmp = read_tree_block(root, blocknr,
1501 blocksize, gen);
1502 if (tmp)
1503 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001504 goto again;
1505 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001506 if (tmp)
1507 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001508 if (should_reada)
1509 reada_for_search(root, p,
1510 level, slot,
1511 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001512 b = read_node_slot(root, b, slot);
1513 }
1514 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001515 if (!p->skip_locking)
1516 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001517 } else {
1518 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001519 if (ins_len > 0 &&
1520 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001521 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001522 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001523 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001524 if (sret) {
1525 ret = sret;
1526 goto done;
1527 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001528 }
Chris Mason459931e2008-12-10 09:10:46 -05001529 if (!p->search_for_split)
1530 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001531 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001532 }
1533 }
Chris Mason65b51a02008-08-01 15:11:20 -04001534 ret = 1;
1535done:
1536 if (prealloc_block.objectid) {
1537 btrfs_free_reserved_extent(root,
1538 prealloc_block.objectid,
1539 prealloc_block.offset);
1540 }
1541
1542 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001543}
1544
Zheng Yan1a40e232008-09-26 10:09:34 -04001545int btrfs_merge_path(struct btrfs_trans_handle *trans,
1546 struct btrfs_root *root,
1547 struct btrfs_key *node_keys,
1548 u64 *nodes, int lowest_level)
1549{
1550 struct extent_buffer *eb;
1551 struct extent_buffer *parent;
1552 struct btrfs_key key;
1553 u64 bytenr;
1554 u64 generation;
1555 u32 blocksize;
1556 int level;
1557 int slot;
1558 int key_match;
1559 int ret;
1560
1561 eb = btrfs_lock_root_node(root);
1562 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1563 BUG_ON(ret);
1564
1565 parent = eb;
1566 while (1) {
1567 level = btrfs_header_level(parent);
1568 if (level == 0 || level <= lowest_level)
1569 break;
1570
1571 ret = bin_search(parent, &node_keys[lowest_level], level,
1572 &slot);
1573 if (ret && slot > 0)
1574 slot--;
1575
1576 bytenr = btrfs_node_blockptr(parent, slot);
1577 if (nodes[level - 1] == bytenr)
1578 break;
1579
1580 blocksize = btrfs_level_size(root, level - 1);
1581 generation = btrfs_node_ptr_generation(parent, slot);
1582 btrfs_node_key_to_cpu(eb, &key, slot);
1583 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1584
Yan Zhengf82d02d2008-10-29 14:49:05 -04001585 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001586 eb = read_tree_block(root, bytenr, blocksize,
1587 generation);
1588 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001589 }
1590
1591 /*
1592 * if node keys match and node pointer hasn't been modified
1593 * in the running transaction, we can merge the path. for
1594 * blocks owened by reloc trees, the node pointer check is
1595 * skipped, this is because these blocks are fully controlled
1596 * by the space balance code, no one else can modify them.
1597 */
1598 if (!nodes[level - 1] || !key_match ||
1599 (generation == trans->transid &&
1600 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1601 if (level == 1 || level == lowest_level + 1) {
1602 if (generation == trans->transid) {
1603 btrfs_tree_unlock(eb);
1604 free_extent_buffer(eb);
1605 }
1606 break;
1607 }
1608
1609 if (generation != trans->transid) {
1610 eb = read_tree_block(root, bytenr, blocksize,
1611 generation);
1612 btrfs_tree_lock(eb);
1613 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001614
1615 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1616 &eb, 0);
1617 BUG_ON(ret);
1618
Yan Zhengf82d02d2008-10-29 14:49:05 -04001619 if (root->root_key.objectid ==
1620 BTRFS_TREE_RELOC_OBJECTID) {
1621 if (!nodes[level - 1]) {
1622 nodes[level - 1] = eb->start;
1623 memcpy(&node_keys[level - 1], &key,
1624 sizeof(node_keys[0]));
1625 } else {
1626 WARN_ON(1);
1627 }
1628 }
1629
Zheng Yan1a40e232008-09-26 10:09:34 -04001630 btrfs_tree_unlock(parent);
1631 free_extent_buffer(parent);
1632 parent = eb;
1633 continue;
1634 }
1635
Zheng Yan1a40e232008-09-26 10:09:34 -04001636 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1637 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1638 btrfs_mark_buffer_dirty(parent);
1639
1640 ret = btrfs_inc_extent_ref(trans, root,
1641 nodes[level - 1],
1642 blocksize, parent->start,
1643 btrfs_header_owner(parent),
1644 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001645 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001646 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001647
1648 /*
1649 * If the block was created in the running transaction,
1650 * it's possible this is the last reference to it, so we
1651 * should drop the subtree.
1652 */
1653 if (generation == trans->transid) {
1654 ret = btrfs_drop_subtree(trans, root, eb, parent);
1655 BUG_ON(ret);
1656 btrfs_tree_unlock(eb);
1657 free_extent_buffer(eb);
1658 } else {
1659 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001660 blocksize, parent->start,
1661 btrfs_header_owner(parent),
1662 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001663 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001664 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001665 }
1666 break;
1667 }
1668 btrfs_tree_unlock(parent);
1669 free_extent_buffer(parent);
1670 return 0;
1671}
1672
Chris Mason74123bd2007-02-02 11:05:29 -05001673/*
1674 * adjust the pointers going up the tree, starting at level
1675 * making sure the right key of each node is points to 'key'.
1676 * This is used after shifting pointers to the left, so it stops
1677 * fixing up pointers when a given leaf/node is not in slot 0 of the
1678 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001679 *
1680 * If this fails to write a tree block, it returns -1, but continues
1681 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001682 */
Chris Mason5f39d392007-10-15 16:14:19 -04001683static int fixup_low_keys(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root, struct btrfs_path *path,
1685 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001686{
1687 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001688 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001689 struct extent_buffer *t;
1690
Chris Mason234b63a2007-03-13 10:46:10 -04001691 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001692 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001693 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001694 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001695 t = path->nodes[i];
1696 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001697 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001698 if (tslot != 0)
1699 break;
1700 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001701 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001702}
1703
Chris Mason74123bd2007-02-02 11:05:29 -05001704/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001705 * update item key.
1706 *
1707 * This function isn't completely safe. It's the caller's responsibility
1708 * that the new key won't break the order
1709 */
1710int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1711 struct btrfs_root *root, struct btrfs_path *path,
1712 struct btrfs_key *new_key)
1713{
1714 struct btrfs_disk_key disk_key;
1715 struct extent_buffer *eb;
1716 int slot;
1717
1718 eb = path->nodes[0];
1719 slot = path->slots[0];
1720 if (slot > 0) {
1721 btrfs_item_key(eb, &disk_key, slot - 1);
1722 if (comp_keys(&disk_key, new_key) >= 0)
1723 return -1;
1724 }
1725 if (slot < btrfs_header_nritems(eb) - 1) {
1726 btrfs_item_key(eb, &disk_key, slot + 1);
1727 if (comp_keys(&disk_key, new_key) <= 0)
1728 return -1;
1729 }
1730
1731 btrfs_cpu_key_to_disk(&disk_key, new_key);
1732 btrfs_set_item_key(eb, &disk_key, slot);
1733 btrfs_mark_buffer_dirty(eb);
1734 if (slot == 0)
1735 fixup_low_keys(trans, root, path, &disk_key, 1);
1736 return 0;
1737}
1738
1739/*
Chris Mason74123bd2007-02-02 11:05:29 -05001740 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001741 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001742 *
1743 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1744 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001745 */
Chris Mason98ed5172008-01-03 10:01:48 -05001746static int push_node_left(struct btrfs_trans_handle *trans,
1747 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001748 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001749{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001750 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001751 int src_nritems;
1752 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001753 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001754
Chris Mason5f39d392007-10-15 16:14:19 -04001755 src_nritems = btrfs_header_nritems(src);
1756 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001757 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001758 WARN_ON(btrfs_header_generation(src) != trans->transid);
1759 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001760
Chris Masonbce4eae2008-04-24 14:42:46 -04001761 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001762 return 1;
1763
Chris Masond3977122009-01-05 21:25:51 -05001764 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001765 return 1;
1766
Chris Masonbce4eae2008-04-24 14:42:46 -04001767 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001768 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001769 if (push_items < src_nritems) {
1770 /* leave at least 8 pointers in the node if
1771 * we aren't going to empty it
1772 */
1773 if (src_nritems - push_items < 8) {
1774 if (push_items <= 8)
1775 return 1;
1776 push_items -= 8;
1777 }
1778 }
1779 } else
1780 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001781
Chris Mason5f39d392007-10-15 16:14:19 -04001782 copy_extent_buffer(dst, src,
1783 btrfs_node_key_ptr_offset(dst_nritems),
1784 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001785 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001786
Chris Masonbb803952007-03-01 12:04:21 -05001787 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001788 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1789 btrfs_node_key_ptr_offset(push_items),
1790 (src_nritems - push_items) *
1791 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001792 }
Chris Mason5f39d392007-10-15 16:14:19 -04001793 btrfs_set_header_nritems(src, src_nritems - push_items);
1794 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1795 btrfs_mark_buffer_dirty(src);
1796 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001797
1798 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1799 BUG_ON(ret);
1800
Chris Masonbb803952007-03-01 12:04:21 -05001801 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001802}
1803
Chris Mason97571fd2007-02-24 13:39:08 -05001804/*
Chris Mason79f95c82007-03-01 15:16:26 -05001805 * try to push data from one node into the next node right in the
1806 * tree.
1807 *
1808 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1809 * error, and > 0 if there was no room in the right hand block.
1810 *
1811 * this will only push up to 1/2 the contents of the left node over
1812 */
Chris Mason5f39d392007-10-15 16:14:19 -04001813static int balance_node_right(struct btrfs_trans_handle *trans,
1814 struct btrfs_root *root,
1815 struct extent_buffer *dst,
1816 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001817{
Chris Mason79f95c82007-03-01 15:16:26 -05001818 int push_items = 0;
1819 int max_push;
1820 int src_nritems;
1821 int dst_nritems;
1822 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001823
Chris Mason7bb86312007-12-11 09:25:06 -05001824 WARN_ON(btrfs_header_generation(src) != trans->transid);
1825 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1826
Chris Mason5f39d392007-10-15 16:14:19 -04001827 src_nritems = btrfs_header_nritems(src);
1828 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001829 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001830 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001831 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001832
Chris Masond3977122009-01-05 21:25:51 -05001833 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001834 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001835
1836 max_push = src_nritems / 2 + 1;
1837 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001838 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001839 return 1;
Yan252c38f2007-08-29 09:11:44 -04001840
Chris Mason79f95c82007-03-01 15:16:26 -05001841 if (max_push < push_items)
1842 push_items = max_push;
1843
Chris Mason5f39d392007-10-15 16:14:19 -04001844 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1845 btrfs_node_key_ptr_offset(0),
1846 (dst_nritems) *
1847 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001848
Chris Mason5f39d392007-10-15 16:14:19 -04001849 copy_extent_buffer(dst, src,
1850 btrfs_node_key_ptr_offset(0),
1851 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001852 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001853
Chris Mason5f39d392007-10-15 16:14:19 -04001854 btrfs_set_header_nritems(src, src_nritems - push_items);
1855 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001856
Chris Mason5f39d392007-10-15 16:14:19 -04001857 btrfs_mark_buffer_dirty(src);
1858 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001859
1860 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1861 BUG_ON(ret);
1862
Chris Mason79f95c82007-03-01 15:16:26 -05001863 return ret;
1864}
1865
1866/*
Chris Mason97571fd2007-02-24 13:39:08 -05001867 * helper function to insert a new root level in the tree.
1868 * A new node is allocated, and a single item is inserted to
1869 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001870 *
1871 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001872 */
Chris Masond3977122009-01-05 21:25:51 -05001873static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001874 struct btrfs_root *root,
1875 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001876{
Chris Mason7bb86312007-12-11 09:25:06 -05001877 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001878 struct extent_buffer *lower;
1879 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001880 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001881 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001882 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001883
1884 BUG_ON(path->nodes[level]);
1885 BUG_ON(path->nodes[level-1] != root->node);
1886
Chris Mason7bb86312007-12-11 09:25:06 -05001887 lower = path->nodes[level-1];
1888 if (level == 1)
1889 btrfs_item_key(lower, &lower_key, 0);
1890 else
1891 btrfs_node_key(lower, &lower_key, 0);
1892
Zheng Yan31840ae2008-09-23 13:14:14 -04001893 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1894 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001895 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (IS_ERR(c))
1897 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001898
Chris Mason5f39d392007-10-15 16:14:19 -04001899 memset_extent_buffer(c, 0, 0, root->nodesize);
1900 btrfs_set_header_nritems(c, 1);
1901 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001902 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001903 btrfs_set_header_generation(c, trans->transid);
1904 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001905
Chris Mason5f39d392007-10-15 16:14:19 -04001906 write_extent_buffer(c, root->fs_info->fsid,
1907 (unsigned long)btrfs_header_fsid(c),
1908 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001909
1910 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1911 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1912 BTRFS_UUID_SIZE);
1913
Chris Mason5f39d392007-10-15 16:14:19 -04001914 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001915 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001916 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001917 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001918
1919 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001920
1921 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001922
Chris Mason925baed2008-06-25 16:01:30 -04001923 spin_lock(&root->node_lock);
1924 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001925 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001926 spin_unlock(&root->node_lock);
1927
Zheng Yan31840ae2008-09-23 13:14:14 -04001928 ret = btrfs_update_extent_ref(trans, root, lower->start,
1929 lower->start, c->start,
1930 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001931 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001932 BUG_ON(ret);
1933
Chris Mason925baed2008-06-25 16:01:30 -04001934 /* the super has an extra ref to root->node */
1935 free_extent_buffer(old);
1936
Chris Mason0b86a832008-03-24 15:01:56 -04001937 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001938 extent_buffer_get(c);
1939 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001940 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001941 path->slots[level] = 0;
1942 return 0;
1943}
1944
Chris Mason74123bd2007-02-02 11:05:29 -05001945/*
1946 * worker function to insert a single pointer in a node.
1947 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001948 *
Chris Mason74123bd2007-02-02 11:05:29 -05001949 * slot and level indicate where you want the key to go, and
1950 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001951 *
1952 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001953 */
Chris Masone089f052007-03-16 16:20:31 -04001954static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1955 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001956 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001957{
Chris Mason5f39d392007-10-15 16:14:19 -04001958 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001959 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001960
1961 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001962 lower = path->nodes[level];
1963 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001964 if (slot > nritems)
1965 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001966 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001967 BUG();
1968 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001969 memmove_extent_buffer(lower,
1970 btrfs_node_key_ptr_offset(slot + 1),
1971 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001972 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001973 }
Chris Mason5f39d392007-10-15 16:14:19 -04001974 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001975 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001976 WARN_ON(trans->transid == 0);
1977 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001978 btrfs_set_header_nritems(lower, nritems + 1);
1979 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001980 return 0;
1981}
1982
Chris Mason97571fd2007-02-24 13:39:08 -05001983/*
1984 * split the node at the specified level in path in two.
1985 * The path is corrected to point to the appropriate node after the split
1986 *
1987 * Before splitting this tries to make some room in the node by pushing
1988 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001989 *
1990 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001991 */
Chris Masone02119d2008-09-05 16:13:11 -04001992static noinline int split_node(struct btrfs_trans_handle *trans,
1993 struct btrfs_root *root,
1994 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001995{
Chris Mason5f39d392007-10-15 16:14:19 -04001996 struct extent_buffer *c;
1997 struct extent_buffer *split;
1998 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001999 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002000 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002001 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002002 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002003
Chris Mason5f39d392007-10-15 16:14:19 -04002004 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002005 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002006 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002007 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002008 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002009 if (ret)
2010 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002011 } else {
2012 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002013 c = path->nodes[level];
2014 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002015 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002016 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002017 if (ret < 0)
2018 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002019 }
Chris Masone66f7092007-04-20 13:16:02 -04002020
Chris Mason5f39d392007-10-15 16:14:19 -04002021 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002022
Chris Mason925baed2008-06-25 16:01:30 -04002023 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002024 path->nodes[level + 1]->start,
2025 root->root_key.objectid,
2026 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002027 if (IS_ERR(split))
2028 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 btrfs_set_header_flags(split, btrfs_header_flags(c));
2031 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002032 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002033 btrfs_set_header_generation(split, trans->transid);
2034 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002035 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002036 write_extent_buffer(split, root->fs_info->fsid,
2037 (unsigned long)btrfs_header_fsid(split),
2038 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002039 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2040 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2041 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002042
Chris Mason7518a232007-03-12 12:01:18 -04002043 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002044
2045 copy_extent_buffer(split, c,
2046 btrfs_node_key_ptr_offset(0),
2047 btrfs_node_key_ptr_offset(mid),
2048 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2049 btrfs_set_header_nritems(split, c_nritems - mid);
2050 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002051 ret = 0;
2052
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_mark_buffer_dirty(c);
2054 btrfs_mark_buffer_dirty(split);
2055
2056 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002057 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002058 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002059 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002060 if (wret)
2061 ret = wret;
2062
Zheng Yan31840ae2008-09-23 13:14:14 -04002063 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2064 BUG_ON(ret);
2065
Chris Mason5de08d72007-02-24 06:24:44 -05002066 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002067 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002068 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002069 free_extent_buffer(c);
2070 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002071 path->slots[level + 1] += 1;
2072 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002073 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002074 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002075 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002077}
2078
Chris Mason74123bd2007-02-02 11:05:29 -05002079/*
2080 * how many bytes are required to store the items in a leaf. start
2081 * and nr indicate which items in the leaf to check. This totals up the
2082 * space used both by the item structs and the item data
2083 */
Chris Mason5f39d392007-10-15 16:14:19 -04002084static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002085{
2086 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002087 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002088 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002089
2090 if (!nr)
2091 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002092 data_len = btrfs_item_end_nr(l, start);
2093 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002094 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002095 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002096 return data_len;
2097}
2098
Chris Mason74123bd2007-02-02 11:05:29 -05002099/*
Chris Masond4dbff92007-04-04 14:08:15 -04002100 * The space between the end of the leaf items and
2101 * the start of the leaf data. IOW, how much room
2102 * the leaf has left for both items and data
2103 */
Chris Masond3977122009-01-05 21:25:51 -05002104noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002105 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002106{
Chris Mason5f39d392007-10-15 16:14:19 -04002107 int nritems = btrfs_header_nritems(leaf);
2108 int ret;
2109 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2110 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002111 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2112 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002113 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002114 leaf_space_used(leaf, 0, nritems), nritems);
2115 }
2116 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002117}
2118
2119/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002120 * push some data in the path leaf to the right, trying to free up at
2121 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002122 *
2123 * returns 1 if the push failed because the other node didn't have enough
2124 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002125 */
Chris Masone089f052007-03-16 16:20:31 -04002126static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002127 *root, struct btrfs_path *path, int data_size,
2128 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002129{
Chris Mason5f39d392007-10-15 16:14:19 -04002130 struct extent_buffer *left = path->nodes[0];
2131 struct extent_buffer *right;
2132 struct extent_buffer *upper;
2133 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002134 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002135 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002136 int free_space;
2137 int push_space = 0;
2138 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002139 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002140 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002141 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002142 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002143 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002144 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002145 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002146
2147 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002148 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002149 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002150
Chris Mason00ec4c52007-02-24 12:47:20 -05002151 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002152 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002153 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002154
Chris Masona2135012008-06-25 16:01:30 -04002155 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2156
Chris Masonca7a79a2008-05-12 12:59:19 -04002157 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002158 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002159 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002160 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002161 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002162
Chris Mason5f39d392007-10-15 16:14:19 -04002163 /* cow and double check */
2164 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002165 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002166 if (ret)
2167 goto out_unlock;
2168
Chris Mason5f39d392007-10-15 16:14:19 -04002169 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002170 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002171 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002172
2173 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002174 if (left_nritems == 0)
2175 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002176
Chris Mason34a38212007-11-07 13:31:03 -05002177 if (empty)
2178 nr = 0;
2179 else
2180 nr = 1;
2181
Zheng Yan31840ae2008-09-23 13:14:14 -04002182 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002183 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002184
Chris Mason34a38212007-11-07 13:31:03 -05002185 i = left_nritems - 1;
2186 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002187 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002188
Zheng Yan31840ae2008-09-23 13:14:14 -04002189 if (!empty && push_items > 0) {
2190 if (path->slots[0] > i)
2191 break;
2192 if (path->slots[0] == i) {
2193 int space = btrfs_leaf_free_space(root, left);
2194 if (space + push_space * 2 > free_space)
2195 break;
2196 }
2197 }
2198
Chris Mason00ec4c52007-02-24 12:47:20 -05002199 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002200 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002201
2202 if (!left->map_token) {
2203 map_extent_buffer(left, (unsigned long)item,
2204 sizeof(struct btrfs_item),
2205 &left->map_token, &left->kaddr,
2206 &left->map_start, &left->map_len,
2207 KM_USER1);
2208 }
2209
2210 this_item_size = btrfs_item_size(left, item);
2211 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002212 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002213
Chris Mason00ec4c52007-02-24 12:47:20 -05002214 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002215 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002216 if (i == 0)
2217 break;
2218 i--;
Chris Masondb945352007-10-15 16:15:53 -04002219 }
2220 if (left->map_token) {
2221 unmap_extent_buffer(left, left->map_token, KM_USER1);
2222 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002223 }
Chris Mason5f39d392007-10-15 16:14:19 -04002224
Chris Mason925baed2008-06-25 16:01:30 -04002225 if (push_items == 0)
2226 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002227
Chris Mason34a38212007-11-07 13:31:03 -05002228 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002229 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002230
Chris Mason00ec4c52007-02-24 12:47:20 -05002231 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002232 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002233
Chris Mason5f39d392007-10-15 16:14:19 -04002234 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002235 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002236
Chris Mason00ec4c52007-02-24 12:47:20 -05002237 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002238 data_end = leaf_data_end(root, right);
2239 memmove_extent_buffer(right,
2240 btrfs_leaf_data(right) + data_end - push_space,
2241 btrfs_leaf_data(right) + data_end,
2242 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2243
Chris Mason00ec4c52007-02-24 12:47:20 -05002244 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002245 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002246 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2247 btrfs_leaf_data(left) + leaf_data_end(root, left),
2248 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002249
2250 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2251 btrfs_item_nr_offset(0),
2252 right_nritems * sizeof(struct btrfs_item));
2253
Chris Mason00ec4c52007-02-24 12:47:20 -05002254 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002255 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2256 btrfs_item_nr_offset(left_nritems - push_items),
2257 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002258
2259 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002260 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002261 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002262 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002263 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002264 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002265 if (!right->map_token) {
2266 map_extent_buffer(right, (unsigned long)item,
2267 sizeof(struct btrfs_item),
2268 &right->map_token, &right->kaddr,
2269 &right->map_start, &right->map_len,
2270 KM_USER1);
2271 }
2272 push_space -= btrfs_item_size(right, item);
2273 btrfs_set_item_offset(right, item, push_space);
2274 }
2275
2276 if (right->map_token) {
2277 unmap_extent_buffer(right, right->map_token, KM_USER1);
2278 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002279 }
Chris Mason7518a232007-03-12 12:01:18 -04002280 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002281 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002282
Chris Mason34a38212007-11-07 13:31:03 -05002283 if (left_nritems)
2284 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002285 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002286
Zheng Yan31840ae2008-09-23 13:14:14 -04002287 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2288 BUG_ON(ret);
2289
Chris Mason5f39d392007-10-15 16:14:19 -04002290 btrfs_item_key(right, &disk_key, 0);
2291 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002292 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002293
Chris Mason00ec4c52007-02-24 12:47:20 -05002294 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002295 if (path->slots[0] >= left_nritems) {
2296 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002297 if (btrfs_header_nritems(path->nodes[0]) == 0)
2298 clean_tree_block(trans, root, path->nodes[0]);
2299 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002300 free_extent_buffer(path->nodes[0]);
2301 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002302 path->slots[1] += 1;
2303 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002304 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002305 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002306 }
2307 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002308
2309out_unlock:
2310 btrfs_tree_unlock(right);
2311 free_extent_buffer(right);
2312 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002313}
Chris Mason925baed2008-06-25 16:01:30 -04002314
Chris Mason00ec4c52007-02-24 12:47:20 -05002315/*
Chris Mason74123bd2007-02-02 11:05:29 -05002316 * push some data in the path leaf to the left, trying to free up at
2317 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2318 */
Chris Masone089f052007-03-16 16:20:31 -04002319static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002320 *root, struct btrfs_path *path, int data_size,
2321 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002322{
Chris Mason5f39d392007-10-15 16:14:19 -04002323 struct btrfs_disk_key disk_key;
2324 struct extent_buffer *right = path->nodes[0];
2325 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002326 int slot;
2327 int i;
2328 int free_space;
2329 int push_space = 0;
2330 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002331 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002332 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002333 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002334 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002335 int ret = 0;
2336 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002337 u32 this_item_size;
2338 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002339
2340 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002341 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002342 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002343 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002344 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002345
Chris Mason3685f792007-10-19 09:23:27 -04002346 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002347 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002348 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002349
Chris Masona2135012008-06-25 16:01:30 -04002350 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2351
Chris Masonca7a79a2008-05-12 12:59:19 -04002352 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002353 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002354 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002355 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002356 ret = 1;
2357 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002358 }
Chris Mason02217ed2007-03-02 16:08:05 -05002359
2360 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002361 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002362 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002363 if (ret) {
2364 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002365 ret = 1;
2366 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002367 }
Chris Mason3685f792007-10-19 09:23:27 -04002368
Chris Mason123abc82007-03-14 14:14:43 -04002369 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002370 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002371 ret = 1;
2372 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002373 }
2374
Chris Mason34a38212007-11-07 13:31:03 -05002375 if (empty)
2376 nr = right_nritems;
2377 else
2378 nr = right_nritems - 1;
2379
2380 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002381 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002382 if (!right->map_token) {
2383 map_extent_buffer(right, (unsigned long)item,
2384 sizeof(struct btrfs_item),
2385 &right->map_token, &right->kaddr,
2386 &right->map_start, &right->map_len,
2387 KM_USER1);
2388 }
2389
Zheng Yan31840ae2008-09-23 13:14:14 -04002390 if (!empty && push_items > 0) {
2391 if (path->slots[0] < i)
2392 break;
2393 if (path->slots[0] == i) {
2394 int space = btrfs_leaf_free_space(root, right);
2395 if (space + push_space * 2 > free_space)
2396 break;
2397 }
2398 }
2399
Chris Masonbe0e5c02007-01-26 15:51:26 -05002400 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002401 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002402
2403 this_item_size = btrfs_item_size(right, item);
2404 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002405 break;
Chris Masondb945352007-10-15 16:15:53 -04002406
Chris Masonbe0e5c02007-01-26 15:51:26 -05002407 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002408 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002409 }
Chris Masondb945352007-10-15 16:15:53 -04002410
2411 if (right->map_token) {
2412 unmap_extent_buffer(right, right->map_token, KM_USER1);
2413 right->map_token = NULL;
2414 }
2415
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002417 ret = 1;
2418 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002419 }
Chris Mason34a38212007-11-07 13:31:03 -05002420 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002421 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002422
Chris Masonbe0e5c02007-01-26 15:51:26 -05002423 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002424 copy_extent_buffer(left, right,
2425 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2426 btrfs_item_nr_offset(0),
2427 push_items * sizeof(struct btrfs_item));
2428
Chris Mason123abc82007-03-14 14:14:43 -04002429 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002430 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002431
2432 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002433 leaf_data_end(root, left) - push_space,
2434 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002435 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002436 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002437 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002438 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002439
Chris Masondb945352007-10-15 16:15:53 -04002440 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002441 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002442 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002443
Chris Mason5f39d392007-10-15 16:14:19 -04002444 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002445 if (!left->map_token) {
2446 map_extent_buffer(left, (unsigned long)item,
2447 sizeof(struct btrfs_item),
2448 &left->map_token, &left->kaddr,
2449 &left->map_start, &left->map_len,
2450 KM_USER1);
2451 }
2452
Chris Mason5f39d392007-10-15 16:14:19 -04002453 ioff = btrfs_item_offset(left, item);
2454 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002455 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002456 }
Chris Mason5f39d392007-10-15 16:14:19 -04002457 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002458 if (left->map_token) {
2459 unmap_extent_buffer(left, left->map_token, KM_USER1);
2460 left->map_token = NULL;
2461 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002462
2463 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002464 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002465 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2466 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002467 WARN_ON(1);
2468 }
Chris Mason5f39d392007-10-15 16:14:19 -04002469
Chris Mason34a38212007-11-07 13:31:03 -05002470 if (push_items < right_nritems) {
2471 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2472 leaf_data_end(root, right);
2473 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2474 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2475 btrfs_leaf_data(right) +
2476 leaf_data_end(root, right), push_space);
2477
2478 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002479 btrfs_item_nr_offset(push_items),
2480 (btrfs_header_nritems(right) - push_items) *
2481 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002482 }
Yaneef1c492007-11-26 10:58:13 -05002483 right_nritems -= push_items;
2484 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002485 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002486 for (i = 0; i < right_nritems; i++) {
2487 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002488
2489 if (!right->map_token) {
2490 map_extent_buffer(right, (unsigned long)item,
2491 sizeof(struct btrfs_item),
2492 &right->map_token, &right->kaddr,
2493 &right->map_start, &right->map_len,
2494 KM_USER1);
2495 }
2496
2497 push_space = push_space - btrfs_item_size(right, item);
2498 btrfs_set_item_offset(right, item, push_space);
2499 }
2500 if (right->map_token) {
2501 unmap_extent_buffer(right, right->map_token, KM_USER1);
2502 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002503 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002504
Chris Mason5f39d392007-10-15 16:14:19 -04002505 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002506 if (right_nritems)
2507 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002508
Zheng Yan31840ae2008-09-23 13:14:14 -04002509 ret = btrfs_update_ref(trans, root, right, left,
2510 old_left_nritems, push_items);
2511 BUG_ON(ret);
2512
Chris Mason5f39d392007-10-15 16:14:19 -04002513 btrfs_item_key(right, &disk_key, 0);
2514 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002515 if (wret)
2516 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002517
2518 /* then fixup the leaf pointer in the path */
2519 if (path->slots[0] < push_items) {
2520 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002521 if (btrfs_header_nritems(path->nodes[0]) == 0)
2522 clean_tree_block(trans, root, path->nodes[0]);
2523 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002524 free_extent_buffer(path->nodes[0]);
2525 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002526 path->slots[1] -= 1;
2527 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002528 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002529 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002530 path->slots[0] -= push_items;
2531 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002532 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002533 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002534out:
2535 btrfs_tree_unlock(left);
2536 free_extent_buffer(left);
2537 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002538}
2539
Chris Mason74123bd2007-02-02 11:05:29 -05002540/*
2541 * split the path's leaf in two, making sure there is at least data_size
2542 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002543 *
2544 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002545 */
Chris Masone02119d2008-09-05 16:13:11 -04002546static noinline int split_leaf(struct btrfs_trans_handle *trans,
2547 struct btrfs_root *root,
2548 struct btrfs_key *ins_key,
2549 struct btrfs_path *path, int data_size,
2550 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002551{
Chris Mason5f39d392007-10-15 16:14:19 -04002552 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002553 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002554 int mid;
2555 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002556 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 int data_copy_size;
2558 int rt_data_off;
2559 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002560 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002561 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002562 int double_split;
2563 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002564 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002565
Chris Mason40689472007-03-17 14:29:23 -04002566 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002567 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002568 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002569 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002570 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002571 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002572 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002573 if (wret < 0)
2574 return wret;
2575 }
2576 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002577
Chris Mason3685f792007-10-19 09:23:27 -04002578 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002579 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002580 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002581 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002582
Chris Mason5c680ed2007-02-22 11:39:13 -05002583 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002584 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002585 if (ret)
2586 return ret;
2587 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002588again:
2589 double_split = 0;
2590 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002591 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002592 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002593 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002594
Chris Mason925baed2008-06-25 16:01:30 -04002595 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002596 path->nodes[1]->start,
2597 root->root_key.objectid,
2598 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002599 if (IS_ERR(right)) {
2600 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002601 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002602 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002603
Chris Mason5f39d392007-10-15 16:14:19 -04002604 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002605 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002606 btrfs_set_header_generation(right, trans->transid);
2607 btrfs_set_header_owner(right, root->root_key.objectid);
2608 btrfs_set_header_level(right, 0);
2609 write_extent_buffer(right, root->fs_info->fsid,
2610 (unsigned long)btrfs_header_fsid(right),
2611 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002612
2613 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2614 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2615 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002616 if (mid <= slot) {
2617 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002618 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002619 BTRFS_LEAF_DATA_SIZE(root)) {
2620 if (slot >= nritems) {
2621 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002622 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002623 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002624 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002625 path->slots[1] + 1, 1);
2626 if (wret)
2627 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002628
2629 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002630 free_extent_buffer(path->nodes[0]);
2631 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002632 path->slots[0] = 0;
2633 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002634 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002635 return ret;
2636 }
2637 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002638 if (mid != nritems &&
2639 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002640 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002641 double_split = 1;
2642 }
Chris Masond4dbff92007-04-04 14:08:15 -04002643 }
2644 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002645 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002646 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002647 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002648 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002649 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002650 wret = insert_ptr(trans, root, path,
2651 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002652 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002653 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002654 if (wret)
2655 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002656 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002657 free_extent_buffer(path->nodes[0]);
2658 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002659 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002660 if (path->slots[1] == 0) {
2661 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002662 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002663 if (wret)
2664 ret = wret;
2665 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002666 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002667 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002668 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002669 mid = 1;
2670 } else {
2671 mid = slot;
2672 if (mid != nritems &&
2673 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002674 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002675 double_split = 1;
2676 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002677 }
Chris Masond4dbff92007-04-04 14:08:15 -04002678 }
2679 }
Chris Mason5f39d392007-10-15 16:14:19 -04002680 nritems = nritems - mid;
2681 btrfs_set_header_nritems(right, nritems);
2682 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2683
2684 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2685 btrfs_item_nr_offset(mid),
2686 nritems * sizeof(struct btrfs_item));
2687
2688 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002689 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2690 data_copy_size, btrfs_leaf_data(l) +
2691 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002692
Chris Mason5f39d392007-10-15 16:14:19 -04002693 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2694 btrfs_item_end_nr(l, mid);
2695
2696 for (i = 0; i < nritems; i++) {
2697 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002698 u32 ioff;
2699
2700 if (!right->map_token) {
2701 map_extent_buffer(right, (unsigned long)item,
2702 sizeof(struct btrfs_item),
2703 &right->map_token, &right->kaddr,
2704 &right->map_start, &right->map_len,
2705 KM_USER1);
2706 }
2707
2708 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002709 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002710 }
Chris Mason74123bd2007-02-02 11:05:29 -05002711
Chris Masondb945352007-10-15 16:15:53 -04002712 if (right->map_token) {
2713 unmap_extent_buffer(right, right->map_token, KM_USER1);
2714 right->map_token = NULL;
2715 }
2716
Chris Mason5f39d392007-10-15 16:14:19 -04002717 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002718 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002719 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002720 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2721 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002722 if (wret)
2723 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002724
2725 btrfs_mark_buffer_dirty(right);
2726 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002727 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002728
Zheng Yan31840ae2008-09-23 13:14:14 -04002729 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2730 BUG_ON(ret);
2731
Chris Masonbe0e5c02007-01-26 15:51:26 -05002732 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002733 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002734 free_extent_buffer(path->nodes[0]);
2735 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002736 path->slots[0] -= mid;
2737 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002738 } else {
2739 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002740 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002741 }
Chris Mason5f39d392007-10-15 16:14:19 -04002742
Chris Masoneb60cea2007-02-02 09:18:22 -05002743 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002744
Chris Masoncc0c5532007-10-25 15:42:57 -04002745 if (double_split) {
2746 BUG_ON(num_doubles != 0);
2747 num_doubles++;
2748 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002749 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002750 return ret;
2751}
2752
Chris Masond352ac62008-09-29 15:18:18 -04002753/*
Chris Mason459931e2008-12-10 09:10:46 -05002754 * This function splits a single item into two items,
2755 * giving 'new_key' to the new item and splitting the
2756 * old one at split_offset (from the start of the item).
2757 *
2758 * The path may be released by this operation. After
2759 * the split, the path is pointing to the old item. The
2760 * new item is going to be in the same node as the old one.
2761 *
2762 * Note, the item being split must be smaller enough to live alone on
2763 * a tree block with room for one extra struct btrfs_item
2764 *
2765 * This allows us to split the item in place, keeping a lock on the
2766 * leaf the entire time.
2767 */
2768int btrfs_split_item(struct btrfs_trans_handle *trans,
2769 struct btrfs_root *root,
2770 struct btrfs_path *path,
2771 struct btrfs_key *new_key,
2772 unsigned long split_offset)
2773{
2774 u32 item_size;
2775 struct extent_buffer *leaf;
2776 struct btrfs_key orig_key;
2777 struct btrfs_item *item;
2778 struct btrfs_item *new_item;
2779 int ret = 0;
2780 int slot;
2781 u32 nritems;
2782 u32 orig_offset;
2783 struct btrfs_disk_key disk_key;
2784 char *buf;
2785
2786 leaf = path->nodes[0];
2787 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2788 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2789 goto split;
2790
2791 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2792 btrfs_release_path(root, path);
2793
2794 path->search_for_split = 1;
2795 path->keep_locks = 1;
2796
2797 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2798 path->search_for_split = 0;
2799
2800 /* if our item isn't there or got smaller, return now */
2801 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2802 path->slots[0])) {
2803 path->keep_locks = 0;
2804 return -EAGAIN;
2805 }
2806
Yan Zheng87b29b22008-12-17 10:21:48 -05002807 ret = split_leaf(trans, root, &orig_key, path,
2808 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05002809 path->keep_locks = 0;
2810 BUG_ON(ret);
2811
Chris Mason459931e2008-12-10 09:10:46 -05002812 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05002813 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05002814
2815split:
2816 item = btrfs_item_nr(leaf, path->slots[0]);
2817 orig_offset = btrfs_item_offset(leaf, item);
2818 item_size = btrfs_item_size(leaf, item);
2819
2820
2821 buf = kmalloc(item_size, GFP_NOFS);
2822 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2823 path->slots[0]), item_size);
2824 slot = path->slots[0] + 1;
2825 leaf = path->nodes[0];
2826
2827 nritems = btrfs_header_nritems(leaf);
2828
2829 if (slot != nritems) {
2830 /* shift the items */
2831 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2832 btrfs_item_nr_offset(slot),
2833 (nritems - slot) * sizeof(struct btrfs_item));
2834
2835 }
2836
2837 btrfs_cpu_key_to_disk(&disk_key, new_key);
2838 btrfs_set_item_key(leaf, &disk_key, slot);
2839
2840 new_item = btrfs_item_nr(leaf, slot);
2841
2842 btrfs_set_item_offset(leaf, new_item, orig_offset);
2843 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2844
2845 btrfs_set_item_offset(leaf, item,
2846 orig_offset + item_size - split_offset);
2847 btrfs_set_item_size(leaf, item, split_offset);
2848
2849 btrfs_set_header_nritems(leaf, nritems + 1);
2850
2851 /* write the data for the start of the original item */
2852 write_extent_buffer(leaf, buf,
2853 btrfs_item_ptr_offset(leaf, path->slots[0]),
2854 split_offset);
2855
2856 /* write the data for the new item */
2857 write_extent_buffer(leaf, buf + split_offset,
2858 btrfs_item_ptr_offset(leaf, slot),
2859 item_size - split_offset);
2860 btrfs_mark_buffer_dirty(leaf);
2861
2862 ret = 0;
2863 if (btrfs_leaf_free_space(root, leaf) < 0) {
2864 btrfs_print_leaf(root, leaf);
2865 BUG();
2866 }
2867 kfree(buf);
2868 return ret;
2869}
2870
2871/*
Chris Masond352ac62008-09-29 15:18:18 -04002872 * make the item pointed to by the path smaller. new_size indicates
2873 * how small to make it, and from_end tells us if we just chop bytes
2874 * off the end of the item or if we shift the item to chop bytes off
2875 * the front.
2876 */
Chris Masonb18c6682007-04-17 13:26:50 -04002877int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2878 struct btrfs_root *root,
2879 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002880 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002881{
2882 int ret = 0;
2883 int slot;
2884 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002885 struct extent_buffer *leaf;
2886 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002887 u32 nritems;
2888 unsigned int data_end;
2889 unsigned int old_data_start;
2890 unsigned int old_size;
2891 unsigned int size_diff;
2892 int i;
2893
2894 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002895 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002896 slot = path->slots[0];
2897
2898 old_size = btrfs_item_size_nr(leaf, slot);
2899 if (old_size == new_size)
2900 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002901
Chris Mason5f39d392007-10-15 16:14:19 -04002902 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002903 data_end = leaf_data_end(root, leaf);
2904
Chris Mason5f39d392007-10-15 16:14:19 -04002905 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002906
Chris Masonb18c6682007-04-17 13:26:50 -04002907 size_diff = old_size - new_size;
2908
2909 BUG_ON(slot < 0);
2910 BUG_ON(slot >= nritems);
2911
2912 /*
2913 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2914 */
2915 /* first correct the data pointers */
2916 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002917 u32 ioff;
2918 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002919
2920 if (!leaf->map_token) {
2921 map_extent_buffer(leaf, (unsigned long)item,
2922 sizeof(struct btrfs_item),
2923 &leaf->map_token, &leaf->kaddr,
2924 &leaf->map_start, &leaf->map_len,
2925 KM_USER1);
2926 }
2927
Chris Mason5f39d392007-10-15 16:14:19 -04002928 ioff = btrfs_item_offset(leaf, item);
2929 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002930 }
Chris Masondb945352007-10-15 16:15:53 -04002931
2932 if (leaf->map_token) {
2933 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2934 leaf->map_token = NULL;
2935 }
2936
Chris Masonb18c6682007-04-17 13:26:50 -04002937 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002938 if (from_end) {
2939 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2940 data_end + size_diff, btrfs_leaf_data(leaf) +
2941 data_end, old_data_start + new_size - data_end);
2942 } else {
2943 struct btrfs_disk_key disk_key;
2944 u64 offset;
2945
2946 btrfs_item_key(leaf, &disk_key, slot);
2947
2948 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2949 unsigned long ptr;
2950 struct btrfs_file_extent_item *fi;
2951
2952 fi = btrfs_item_ptr(leaf, slot,
2953 struct btrfs_file_extent_item);
2954 fi = (struct btrfs_file_extent_item *)(
2955 (unsigned long)fi - size_diff);
2956
2957 if (btrfs_file_extent_type(leaf, fi) ==
2958 BTRFS_FILE_EXTENT_INLINE) {
2959 ptr = btrfs_item_ptr_offset(leaf, slot);
2960 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05002961 (unsigned long)fi,
2962 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04002963 disk_bytenr));
2964 }
2965 }
2966
2967 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2968 data_end + size_diff, btrfs_leaf_data(leaf) +
2969 data_end, old_data_start - data_end);
2970
2971 offset = btrfs_disk_key_offset(&disk_key);
2972 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2973 btrfs_set_item_key(leaf, &disk_key, slot);
2974 if (slot == 0)
2975 fixup_low_keys(trans, root, path, &disk_key, 1);
2976 }
Chris Mason5f39d392007-10-15 16:14:19 -04002977
2978 item = btrfs_item_nr(leaf, slot);
2979 btrfs_set_item_size(leaf, item, new_size);
2980 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002981
2982 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002983 if (btrfs_leaf_free_space(root, leaf) < 0) {
2984 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002985 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002986 }
Chris Masonb18c6682007-04-17 13:26:50 -04002987 return ret;
2988}
2989
Chris Masond352ac62008-09-29 15:18:18 -04002990/*
2991 * make the item pointed to by the path bigger, data_size is the new size.
2992 */
Chris Mason5f39d392007-10-15 16:14:19 -04002993int btrfs_extend_item(struct btrfs_trans_handle *trans,
2994 struct btrfs_root *root, struct btrfs_path *path,
2995 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002996{
2997 int ret = 0;
2998 int slot;
2999 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003000 struct extent_buffer *leaf;
3001 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003002 u32 nritems;
3003 unsigned int data_end;
3004 unsigned int old_data;
3005 unsigned int old_size;
3006 int i;
3007
3008 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003009 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003010
Chris Mason5f39d392007-10-15 16:14:19 -04003011 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003012 data_end = leaf_data_end(root, leaf);
3013
Chris Mason5f39d392007-10-15 16:14:19 -04003014 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3015 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003016 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003017 }
Chris Mason6567e832007-04-16 09:22:45 -04003018 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003019 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003020
3021 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003022 if (slot >= nritems) {
3023 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003024 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3025 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003026 BUG_ON(1);
3027 }
Chris Mason6567e832007-04-16 09:22:45 -04003028
3029 /*
3030 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3031 */
3032 /* first correct the data pointers */
3033 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003034 u32 ioff;
3035 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003036
3037 if (!leaf->map_token) {
3038 map_extent_buffer(leaf, (unsigned long)item,
3039 sizeof(struct btrfs_item),
3040 &leaf->map_token, &leaf->kaddr,
3041 &leaf->map_start, &leaf->map_len,
3042 KM_USER1);
3043 }
Chris Mason5f39d392007-10-15 16:14:19 -04003044 ioff = btrfs_item_offset(leaf, item);
3045 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003046 }
Chris Mason5f39d392007-10-15 16:14:19 -04003047
Chris Masondb945352007-10-15 16:15:53 -04003048 if (leaf->map_token) {
3049 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3050 leaf->map_token = NULL;
3051 }
3052
Chris Mason6567e832007-04-16 09:22:45 -04003053 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003054 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003055 data_end - data_size, btrfs_leaf_data(leaf) +
3056 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003057
Chris Mason6567e832007-04-16 09:22:45 -04003058 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003059 old_size = btrfs_item_size_nr(leaf, slot);
3060 item = btrfs_item_nr(leaf, slot);
3061 btrfs_set_item_size(leaf, item, old_size + data_size);
3062 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003063
3064 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003065 if (btrfs_leaf_free_space(root, leaf) < 0) {
3066 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003067 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003068 }
Chris Mason6567e832007-04-16 09:22:45 -04003069 return ret;
3070}
3071
Chris Mason74123bd2007-02-02 11:05:29 -05003072/*
Chris Masond352ac62008-09-29 15:18:18 -04003073 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003074 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003075 * Returns the number of keys that were inserted.
3076 */
3077int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3078 struct btrfs_root *root,
3079 struct btrfs_path *path,
3080 struct btrfs_key *cpu_key, u32 *data_size,
3081 int nr)
3082{
3083 struct extent_buffer *leaf;
3084 struct btrfs_item *item;
3085 int ret = 0;
3086 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003087 int i;
3088 u32 nritems;
3089 u32 total_data = 0;
3090 u32 total_size = 0;
3091 unsigned int data_end;
3092 struct btrfs_disk_key disk_key;
3093 struct btrfs_key found_key;
3094
Yan Zheng87b29b22008-12-17 10:21:48 -05003095 for (i = 0; i < nr; i++) {
3096 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3097 BTRFS_LEAF_DATA_SIZE(root)) {
3098 break;
3099 nr = i;
3100 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003101 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003102 total_size += data_size[i] + sizeof(struct btrfs_item);
3103 }
3104 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003105
Josef Bacikf3465ca2008-11-12 14:19:50 -05003106 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3107 if (ret == 0)
3108 return -EEXIST;
3109 if (ret < 0)
3110 goto out;
3111
Josef Bacikf3465ca2008-11-12 14:19:50 -05003112 leaf = path->nodes[0];
3113
3114 nritems = btrfs_header_nritems(leaf);
3115 data_end = leaf_data_end(root, leaf);
3116
3117 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3118 for (i = nr; i >= 0; i--) {
3119 total_data -= data_size[i];
3120 total_size -= data_size[i] + sizeof(struct btrfs_item);
3121 if (total_size < btrfs_leaf_free_space(root, leaf))
3122 break;
3123 }
3124 nr = i;
3125 }
3126
3127 slot = path->slots[0];
3128 BUG_ON(slot < 0);
3129
3130 if (slot != nritems) {
3131 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3132
3133 item = btrfs_item_nr(leaf, slot);
3134 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3135
3136 /* figure out how many keys we can insert in here */
3137 total_data = data_size[0];
3138 for (i = 1; i < nr; i++) {
3139 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3140 break;
3141 total_data += data_size[i];
3142 }
3143 nr = i;
3144
3145 if (old_data < data_end) {
3146 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003147 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003148 slot, old_data, data_end);
3149 BUG_ON(1);
3150 }
3151 /*
3152 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3153 */
3154 /* first correct the data pointers */
3155 WARN_ON(leaf->map_token);
3156 for (i = slot; i < nritems; i++) {
3157 u32 ioff;
3158
3159 item = btrfs_item_nr(leaf, i);
3160 if (!leaf->map_token) {
3161 map_extent_buffer(leaf, (unsigned long)item,
3162 sizeof(struct btrfs_item),
3163 &leaf->map_token, &leaf->kaddr,
3164 &leaf->map_start, &leaf->map_len,
3165 KM_USER1);
3166 }
3167
3168 ioff = btrfs_item_offset(leaf, item);
3169 btrfs_set_item_offset(leaf, item, ioff - total_data);
3170 }
3171 if (leaf->map_token) {
3172 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3173 leaf->map_token = NULL;
3174 }
3175
3176 /* shift the items */
3177 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3178 btrfs_item_nr_offset(slot),
3179 (nritems - slot) * sizeof(struct btrfs_item));
3180
3181 /* shift the data */
3182 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3183 data_end - total_data, btrfs_leaf_data(leaf) +
3184 data_end, old_data - data_end);
3185 data_end = old_data;
3186 } else {
3187 /*
3188 * this sucks but it has to be done, if we are inserting at
3189 * the end of the leaf only insert 1 of the items, since we
3190 * have no way of knowing whats on the next leaf and we'd have
3191 * to drop our current locks to figure it out
3192 */
3193 nr = 1;
3194 }
3195
3196 /* setup the item for the new data */
3197 for (i = 0; i < nr; i++) {
3198 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3199 btrfs_set_item_key(leaf, &disk_key, slot + i);
3200 item = btrfs_item_nr(leaf, slot + i);
3201 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3202 data_end -= data_size[i];
3203 btrfs_set_item_size(leaf, item, data_size[i]);
3204 }
3205 btrfs_set_header_nritems(leaf, nritems + nr);
3206 btrfs_mark_buffer_dirty(leaf);
3207
3208 ret = 0;
3209 if (slot == 0) {
3210 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3211 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3212 }
3213
3214 if (btrfs_leaf_free_space(root, leaf) < 0) {
3215 btrfs_print_leaf(root, leaf);
3216 BUG();
3217 }
3218out:
3219 if (!ret)
3220 ret = nr;
3221 return ret;
3222}
3223
3224/*
3225 * Given a key and some data, insert items into the tree.
3226 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003227 */
Chris Mason9c583092008-01-29 15:15:18 -05003228int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003229 struct btrfs_root *root,
3230 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003231 struct btrfs_key *cpu_key, u32 *data_size,
3232 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003233{
Chris Mason5f39d392007-10-15 16:14:19 -04003234 struct extent_buffer *leaf;
3235 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003236 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003237 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003238 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003239 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003240 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003241 u32 total_size = 0;
3242 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003243 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003244 struct btrfs_disk_key disk_key;
3245
Chris Masond3977122009-01-05 21:25:51 -05003246 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003247 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003248
Josef Bacik7b128762008-07-24 12:17:14 -04003249 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003250 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003251 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003252 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003253 if (ret < 0)
3254 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003255
Chris Mason62e27492007-03-15 12:56:47 -04003256 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003257 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003258
Chris Mason5f39d392007-10-15 16:14:19 -04003259 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003260 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003261
Chris Masonf25956c2008-09-12 15:32:53 -04003262 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003263 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003264 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003265 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003266 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003267 }
Chris Mason5f39d392007-10-15 16:14:19 -04003268
Chris Mason62e27492007-03-15 12:56:47 -04003269 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003270 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003271
Chris Masonbe0e5c02007-01-26 15:51:26 -05003272 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003273 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003274
Chris Mason5f39d392007-10-15 16:14:19 -04003275 if (old_data < data_end) {
3276 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003277 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003278 slot, old_data, data_end);
3279 BUG_ON(1);
3280 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003281 /*
3282 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3283 */
3284 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003285 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003286 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003287 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003288
Chris Mason5f39d392007-10-15 16:14:19 -04003289 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003290 if (!leaf->map_token) {
3291 map_extent_buffer(leaf, (unsigned long)item,
3292 sizeof(struct btrfs_item),
3293 &leaf->map_token, &leaf->kaddr,
3294 &leaf->map_start, &leaf->map_len,
3295 KM_USER1);
3296 }
3297
Chris Mason5f39d392007-10-15 16:14:19 -04003298 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003299 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003300 }
Chris Masondb945352007-10-15 16:15:53 -04003301 if (leaf->map_token) {
3302 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3303 leaf->map_token = NULL;
3304 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003305
3306 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003307 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003308 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003309 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003310
3311 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003312 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003313 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003314 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003315 data_end = old_data;
3316 }
Chris Mason5f39d392007-10-15 16:14:19 -04003317
Chris Mason62e27492007-03-15 12:56:47 -04003318 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003319 for (i = 0; i < nr; i++) {
3320 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3321 btrfs_set_item_key(leaf, &disk_key, slot + i);
3322 item = btrfs_item_nr(leaf, slot + i);
3323 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3324 data_end -= data_size[i];
3325 btrfs_set_item_size(leaf, item, data_size[i]);
3326 }
3327 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003328 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003329
3330 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003331 if (slot == 0) {
3332 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003333 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003334 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003335
Chris Mason5f39d392007-10-15 16:14:19 -04003336 if (btrfs_leaf_free_space(root, leaf) < 0) {
3337 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003338 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003339 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003340out:
Chris Mason62e27492007-03-15 12:56:47 -04003341 return ret;
3342}
3343
3344/*
3345 * Given a key and some data, insert an item into the tree.
3346 * This does all the path init required, making room in the tree if needed.
3347 */
Chris Masone089f052007-03-16 16:20:31 -04003348int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3349 *root, struct btrfs_key *cpu_key, void *data, u32
3350 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003351{
3352 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003353 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003354 struct extent_buffer *leaf;
3355 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003356
Chris Mason2c90e5d2007-04-02 10:50:19 -04003357 path = btrfs_alloc_path();
3358 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003359 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003360 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003361 leaf = path->nodes[0];
3362 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3363 write_extent_buffer(leaf, data, ptr, data_size);
3364 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003365 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003366 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003367 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003368}
3369
Chris Mason74123bd2007-02-02 11:05:29 -05003370/*
Chris Mason5de08d72007-02-24 06:24:44 -05003371 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003372 *
Chris Masond352ac62008-09-29 15:18:18 -04003373 * the tree should have been previously balanced so the deletion does not
3374 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003375 */
Chris Masone089f052007-03-16 16:20:31 -04003376static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3377 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003378{
Chris Mason5f39d392007-10-15 16:14:19 -04003379 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003380 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003381 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003382 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003383
Chris Mason5f39d392007-10-15 16:14:19 -04003384 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003385 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003386 memmove_extent_buffer(parent,
3387 btrfs_node_key_ptr_offset(slot),
3388 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003389 sizeof(struct btrfs_key_ptr) *
3390 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003391 }
Chris Mason7518a232007-03-12 12:01:18 -04003392 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003393 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003394 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003395 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003396 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003397 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003398 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003399 struct btrfs_disk_key disk_key;
3400
3401 btrfs_node_key(parent, &disk_key, 0);
3402 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003403 if (wret)
3404 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003405 }
Chris Masond6025572007-03-30 14:27:56 -04003406 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003407 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003408}
3409
Chris Mason74123bd2007-02-02 11:05:29 -05003410/*
Chris Mason323ac952008-10-01 19:05:46 -04003411 * a helper function to delete the leaf pointed to by path->slots[1] and
3412 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3413 * already know it, it is faster to have them pass it down than to
3414 * read it out of the node again.
3415 *
3416 * This deletes the pointer in path->nodes[1] and frees the leaf
3417 * block extent. zero is returned if it all worked out, < 0 otherwise.
3418 *
3419 * The path must have already been setup for deleting the leaf, including
3420 * all the proper balancing. path->nodes[1] must be locked.
3421 */
3422noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3423 struct btrfs_root *root,
3424 struct btrfs_path *path, u64 bytenr)
3425{
3426 int ret;
3427 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3428
3429 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3430 if (ret)
3431 return ret;
3432
3433 ret = btrfs_free_extent(trans, root, bytenr,
3434 btrfs_level_size(root, 0),
3435 path->nodes[1]->start,
3436 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003437 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003438 return ret;
3439}
3440/*
Chris Mason74123bd2007-02-02 11:05:29 -05003441 * delete the item at the leaf level in path. If that empties
3442 * the leaf, remove it from the tree
3443 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003444int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3445 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003446{
Chris Mason5f39d392007-10-15 16:14:19 -04003447 struct extent_buffer *leaf;
3448 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003449 int last_off;
3450 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003451 int ret = 0;
3452 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003453 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003454 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003455
Chris Mason5f39d392007-10-15 16:14:19 -04003456 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003457 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3458
3459 for (i = 0; i < nr; i++)
3460 dsize += btrfs_item_size_nr(leaf, slot + i);
3461
Chris Mason5f39d392007-10-15 16:14:19 -04003462 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003463
Chris Mason85e21ba2008-01-29 15:11:36 -05003464 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003465 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003466
3467 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003468 data_end + dsize,
3469 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003470 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003471
Chris Mason85e21ba2008-01-29 15:11:36 -05003472 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003473 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003474
Chris Mason5f39d392007-10-15 16:14:19 -04003475 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003476 if (!leaf->map_token) {
3477 map_extent_buffer(leaf, (unsigned long)item,
3478 sizeof(struct btrfs_item),
3479 &leaf->map_token, &leaf->kaddr,
3480 &leaf->map_start, &leaf->map_len,
3481 KM_USER1);
3482 }
Chris Mason5f39d392007-10-15 16:14:19 -04003483 ioff = btrfs_item_offset(leaf, item);
3484 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003485 }
Chris Masondb945352007-10-15 16:15:53 -04003486
3487 if (leaf->map_token) {
3488 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3489 leaf->map_token = NULL;
3490 }
3491
Chris Mason5f39d392007-10-15 16:14:19 -04003492 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003493 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003494 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003495 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003496 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003497 btrfs_set_header_nritems(leaf, nritems - nr);
3498 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003499
Chris Mason74123bd2007-02-02 11:05:29 -05003500 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003501 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003502 if (leaf == root->node) {
3503 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003504 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003505 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3506 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003507 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003508 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003509 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003510 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003511 struct btrfs_disk_key disk_key;
3512
3513 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003514 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003515 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003516 if (wret)
3517 ret = wret;
3518 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003519
Chris Mason74123bd2007-02-02 11:05:29 -05003520 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003521 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003522 /* push_leaf_left fixes the path.
3523 * make sure the path still points to our leaf
3524 * for possible call to del_ptr below
3525 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003526 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003527 extent_buffer_get(leaf);
3528
Chris Mason85e21ba2008-01-29 15:11:36 -05003529 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003530 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003531 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003532
3533 if (path->nodes[0] == leaf &&
3534 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003535 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003536 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003537 ret = wret;
3538 }
Chris Mason5f39d392007-10-15 16:14:19 -04003539
3540 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003541 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003542 ret = btrfs_del_leaf(trans, root, path,
3543 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003544 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003545 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003546 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003547 /* if we're still in the path, make sure
3548 * we're dirty. Otherwise, one of the
3549 * push_leaf functions must have already
3550 * dirtied this buffer
3551 */
3552 if (path->nodes[0] == leaf)
3553 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003554 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003555 }
Chris Masond5719762007-03-23 10:01:08 -04003556 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003557 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003558 }
3559 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003560 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003561}
3562
Chris Mason97571fd2007-02-24 13:39:08 -05003563/*
Chris Mason925baed2008-06-25 16:01:30 -04003564 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003565 * returns 0 if it found something or 1 if there are no lesser leaves.
3566 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003567 *
3568 * This may release the path, and so you may lose any locks held at the
3569 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003570 */
3571int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3572{
Chris Mason925baed2008-06-25 16:01:30 -04003573 struct btrfs_key key;
3574 struct btrfs_disk_key found_key;
3575 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003576
Chris Mason925baed2008-06-25 16:01:30 -04003577 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003578
Chris Mason925baed2008-06-25 16:01:30 -04003579 if (key.offset > 0)
3580 key.offset--;
3581 else if (key.type > 0)
3582 key.type--;
3583 else if (key.objectid > 0)
3584 key.objectid--;
3585 else
3586 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003587
Chris Mason925baed2008-06-25 16:01:30 -04003588 btrfs_release_path(root, path);
3589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3590 if (ret < 0)
3591 return ret;
3592 btrfs_item_key(path->nodes[0], &found_key, 0);
3593 ret = comp_keys(&found_key, &key);
3594 if (ret < 0)
3595 return 0;
3596 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003597}
3598
Chris Mason3f157a22008-06-25 16:01:31 -04003599/*
3600 * A helper function to walk down the tree starting at min_key, and looking
3601 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003602 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003603 *
3604 * This does not cow, but it does stuff the starting key it finds back
3605 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3606 * key and get a writable path.
3607 *
3608 * This does lock as it descends, and path->keep_locks should be set
3609 * to 1 by the caller.
3610 *
3611 * This honors path->lowest_level to prevent descent past a given level
3612 * of the tree.
3613 *
Chris Masond352ac62008-09-29 15:18:18 -04003614 * min_trans indicates the oldest transaction that you are interested
3615 * in walking through. Any nodes or leaves older than min_trans are
3616 * skipped over (without reading them).
3617 *
Chris Mason3f157a22008-06-25 16:01:31 -04003618 * returns zero if something useful was found, < 0 on error and 1 if there
3619 * was nothing in the tree that matched the search criteria.
3620 */
3621int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003622 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003623 struct btrfs_path *path, int cache_only,
3624 u64 min_trans)
3625{
3626 struct extent_buffer *cur;
3627 struct btrfs_key found_key;
3628 int slot;
Yan96524802008-07-24 12:19:49 -04003629 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003630 u32 nritems;
3631 int level;
3632 int ret = 1;
3633
Chris Mason934d3752008-12-08 16:43:10 -05003634 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003635again:
3636 cur = btrfs_lock_root_node(root);
3637 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003638 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003639 path->nodes[level] = cur;
3640 path->locks[level] = 1;
3641
3642 if (btrfs_header_generation(cur) < min_trans) {
3643 ret = 1;
3644 goto out;
3645 }
Chris Masond3977122009-01-05 21:25:51 -05003646 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003647 nritems = btrfs_header_nritems(cur);
3648 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003649 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003650
Chris Mason323ac952008-10-01 19:05:46 -04003651 /* at the lowest level, we're done, setup the path and exit */
3652 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003653 if (slot >= nritems)
3654 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003655 ret = 0;
3656 path->slots[level] = slot;
3657 btrfs_item_key_to_cpu(cur, &found_key, slot);
3658 goto out;
3659 }
Yan96524802008-07-24 12:19:49 -04003660 if (sret && slot > 0)
3661 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003662 /*
3663 * check this node pointer against the cache_only and
3664 * min_trans parameters. If it isn't in cache or is too
3665 * old, skip to the next one.
3666 */
Chris Masond3977122009-01-05 21:25:51 -05003667 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003668 u64 blockptr;
3669 u64 gen;
3670 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003671 struct btrfs_disk_key disk_key;
3672
Chris Mason3f157a22008-06-25 16:01:31 -04003673 blockptr = btrfs_node_blockptr(cur, slot);
3674 gen = btrfs_node_ptr_generation(cur, slot);
3675 if (gen < min_trans) {
3676 slot++;
3677 continue;
3678 }
3679 if (!cache_only)
3680 break;
3681
Chris Masone02119d2008-09-05 16:13:11 -04003682 if (max_key) {
3683 btrfs_node_key(cur, &disk_key, slot);
3684 if (comp_keys(&disk_key, max_key) >= 0) {
3685 ret = 1;
3686 goto out;
3687 }
3688 }
3689
Chris Mason3f157a22008-06-25 16:01:31 -04003690 tmp = btrfs_find_tree_block(root, blockptr,
3691 btrfs_level_size(root, level - 1));
3692
3693 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3694 free_extent_buffer(tmp);
3695 break;
3696 }
3697 if (tmp)
3698 free_extent_buffer(tmp);
3699 slot++;
3700 }
Chris Masone02119d2008-09-05 16:13:11 -04003701find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003702 /*
3703 * we didn't find a candidate key in this node, walk forward
3704 * and find another one
3705 */
3706 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003707 path->slots[level] = slot;
3708 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003709 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003710 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003711 btrfs_release_path(root, path);
3712 goto again;
3713 } else {
3714 goto out;
3715 }
3716 }
3717 /* save our key for returning back */
3718 btrfs_node_key_to_cpu(cur, &found_key, slot);
3719 path->slots[level] = slot;
3720 if (level == path->lowest_level) {
3721 ret = 0;
3722 unlock_up(path, level, 1);
3723 goto out;
3724 }
3725 cur = read_node_slot(root, cur, slot);
3726
3727 btrfs_tree_lock(cur);
3728 path->locks[level - 1] = 1;
3729 path->nodes[level - 1] = cur;
3730 unlock_up(path, level, 1);
3731 }
3732out:
3733 if (ret == 0)
3734 memcpy(min_key, &found_key, sizeof(found_key));
3735 return ret;
3736}
3737
3738/*
3739 * this is similar to btrfs_next_leaf, but does not try to preserve
3740 * and fixup the path. It looks for and returns the next key in the
3741 * tree based on the current path and the cache_only and min_trans
3742 * parameters.
3743 *
3744 * 0 is returned if another key is found, < 0 if there are any errors
3745 * and 1 is returned if there are no higher keys in the tree
3746 *
3747 * path->keep_locks should be set to 1 on the search made before
3748 * calling this function.
3749 */
Chris Masone7a84562008-06-25 16:01:31 -04003750int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003751 struct btrfs_key *key, int lowest_level,
3752 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003753{
3754 int level = lowest_level;
3755 int slot;
3756 struct extent_buffer *c;
3757
Chris Mason934d3752008-12-08 16:43:10 -05003758 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003759 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003760 if (!path->nodes[level])
3761 return 1;
3762
3763 slot = path->slots[level] + 1;
3764 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003765next:
Chris Masone7a84562008-06-25 16:01:31 -04003766 if (slot >= btrfs_header_nritems(c)) {
3767 level++;
Chris Masond3977122009-01-05 21:25:51 -05003768 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04003769 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04003770 continue;
3771 }
3772 if (level == 0)
3773 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003774 else {
3775 u64 blockptr = btrfs_node_blockptr(c, slot);
3776 u64 gen = btrfs_node_ptr_generation(c, slot);
3777
3778 if (cache_only) {
3779 struct extent_buffer *cur;
3780 cur = btrfs_find_tree_block(root, blockptr,
3781 btrfs_level_size(root, level - 1));
3782 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3783 slot++;
3784 if (cur)
3785 free_extent_buffer(cur);
3786 goto next;
3787 }
3788 free_extent_buffer(cur);
3789 }
3790 if (gen < min_trans) {
3791 slot++;
3792 goto next;
3793 }
Chris Masone7a84562008-06-25 16:01:31 -04003794 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003795 }
Chris Masone7a84562008-06-25 16:01:31 -04003796 return 0;
3797 }
3798 return 1;
3799}
3800
Chris Mason7bb86312007-12-11 09:25:06 -05003801/*
Chris Mason925baed2008-06-25 16:01:30 -04003802 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003803 * returns 0 if it found something or 1 if there are no greater leaves.
3804 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003805 */
Chris Mason234b63a2007-03-13 10:46:10 -04003806int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003807{
3808 int slot;
3809 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003810 struct extent_buffer *c;
3811 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003812 struct btrfs_key key;
3813 u32 nritems;
3814 int ret;
3815
3816 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05003817 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04003818 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003819
3820 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3821
Chris Mason925baed2008-06-25 16:01:30 -04003822 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003823 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003824 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3825 path->keep_locks = 0;
3826
3827 if (ret < 0)
3828 return ret;
3829
Chris Masona2135012008-06-25 16:01:30 -04003830 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003831 /*
3832 * by releasing the path above we dropped all our locks. A balance
3833 * could have added more items next to the key that used to be
3834 * at the very end of the block. So, check again here and
3835 * advance the path if there are now more items available.
3836 */
Chris Masona2135012008-06-25 16:01:30 -04003837 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003838 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003839 goto done;
3840 }
Chris Masond97e63b2007-02-20 16:40:44 -05003841
Chris Masond3977122009-01-05 21:25:51 -05003842 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003843 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003844 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003845
Chris Masond97e63b2007-02-20 16:40:44 -05003846 slot = path->slots[level] + 1;
3847 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003848 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003849 level++;
Chris Masond3977122009-01-05 21:25:51 -05003850 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05003851 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003852 continue;
3853 }
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Chris Mason925baed2008-06-25 16:01:30 -04003855 if (next) {
3856 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003857 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003858 }
Chris Mason5f39d392007-10-15 16:14:19 -04003859
Chris Mason0bd40a72008-07-17 12:54:43 -04003860 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3861 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003862 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003863
Chris Masonca7a79a2008-05-12 12:59:19 -04003864 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003865 if (!path->skip_locking) {
3866 WARN_ON(!btrfs_tree_locked(c));
3867 btrfs_tree_lock(next);
3868 }
Chris Masond97e63b2007-02-20 16:40:44 -05003869 break;
3870 }
3871 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003872 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05003873 level--;
3874 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003875 if (path->locks[level])
3876 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003877 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003878 path->nodes[level] = next;
3879 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003880 if (!path->skip_locking)
3881 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003882 if (!level)
3883 break;
Chris Mason925baed2008-06-25 16:01:30 -04003884 if (level == 1 && path->locks[1] && path->reada)
3885 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003886 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003887 if (!path->skip_locking) {
3888 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3889 btrfs_tree_lock(next);
3890 }
Chris Masond97e63b2007-02-20 16:40:44 -05003891 }
Chris Mason925baed2008-06-25 16:01:30 -04003892done:
3893 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003894 return 0;
3895}
Chris Mason0b86a832008-03-24 15:01:56 -04003896
Chris Mason3f157a22008-06-25 16:01:31 -04003897/*
3898 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3899 * searching until it gets past min_objectid or finds an item of 'type'
3900 *
3901 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3902 */
Chris Mason0b86a832008-03-24 15:01:56 -04003903int btrfs_previous_item(struct btrfs_root *root,
3904 struct btrfs_path *path, u64 min_objectid,
3905 int type)
3906{
3907 struct btrfs_key found_key;
3908 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003909 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003910 int ret;
3911
Chris Masond3977122009-01-05 21:25:51 -05003912 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003913 if (path->slots[0] == 0) {
3914 ret = btrfs_prev_leaf(root, path);
3915 if (ret != 0)
3916 return ret;
3917 } else {
3918 path->slots[0]--;
3919 }
3920 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003921 nritems = btrfs_header_nritems(leaf);
3922 if (nritems == 0)
3923 return 1;
3924 if (path->slots[0] == nritems)
3925 path->slots[0]--;
3926
Chris Mason0b86a832008-03-24 15:01:56 -04003927 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3928 if (found_key.type == type)
3929 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003930 if (found_key.objectid < min_objectid)
3931 break;
3932 if (found_key.objectid == min_objectid &&
3933 found_key.type < type)
3934 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003935 }
3936 return 1;
3937}