blob: 37f31b5529aa0abe62a4e30ec7170c69c443eb0a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Mason2c90e5d2007-04-02 10:50:19 -040041struct btrfs_path *btrfs_alloc_path(void)
42{
Chris Masondf24a2b2007-04-04 09:36:31 -040043 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050044 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
45 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040046 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040047 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040048}
49
Chris Masonb4ce94d2009-02-04 09:25:08 -050050/*
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
53 */
54noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55{
56 int i;
57 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
58 if (p->nodes[i] && p->locks[i])
59 btrfs_set_lock_blocking(p->nodes[i]);
60 }
61}
62
63/*
64 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050065 *
66 * held is used to keep lockdep happy, when lockdep is enabled
67 * we set held to a blocking lock before we go around and
68 * retake all the spinlocks in the path. You can safely use NULL
69 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050070 */
Chris Mason4008c042009-02-12 14:09:45 -050071noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
72 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050073{
74 int i;
Chris Mason4008c042009-02-12 14:09:45 -050075
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 /* lockdep really cares that we take all of these spinlocks
78 * in the right order. If any of the locks in the path are not
79 * currently blocking, it is going to complain. So, make really
80 * really sure by forcing the path to blocking before we clear
81 * the path blocking.
82 */
83 if (held)
84 btrfs_set_lock_blocking(held);
85 btrfs_set_path_blocking(p);
86#endif
87
88 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050089 if (p->nodes[i] && p->locks[i])
90 btrfs_clear_lock_blocking(p->nodes[i]);
91 }
Chris Mason4008c042009-02-12 14:09:45 -050092
93#ifdef CONFIG_DEBUG_LOCK_ALLOC
94 if (held)
95 btrfs_clear_lock_blocking(held);
96#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050097}
98
Chris Masond352ac62008-09-29 15:18:18 -040099/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400100void btrfs_free_path(struct btrfs_path *p)
101{
Chris Masondf24a2b2007-04-04 09:36:31 -0400102 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400103 kmem_cache_free(btrfs_path_cachep, p);
104}
105
Chris Masond352ac62008-09-29 15:18:18 -0400106/*
107 * path release drops references on the extent buffers in the path
108 * and it drops any locks held by this path
109 *
110 * It is safe to call this on paths that no locks or extent buffers held.
111 */
Chris Masond3977122009-01-05 21:25:51 -0500112noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500113{
114 int i;
Chris Masona2135012008-06-25 16:01:30 -0400115
Chris Mason234b63a2007-03-13 10:46:10 -0400116 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400117 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500118 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400119 continue;
120 if (p->locks[i]) {
121 btrfs_tree_unlock(p->nodes[i]);
122 p->locks[i] = 0;
123 }
Chris Mason5f39d392007-10-15 16:14:19 -0400124 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 }
127}
128
Chris Masond352ac62008-09-29 15:18:18 -0400129/*
130 * safely gets a reference on the root node of a tree. A lock
131 * is not taken, so a concurrent writer may put a different node
132 * at the root of the tree. See btrfs_lock_root_node for the
133 * looping required.
134 *
135 * The extent buffer returned by this has a reference taken, so
136 * it won't disappear. It may stop being the root of the tree
137 * at any time because there are no locks held.
138 */
Chris Mason925baed2008-06-25 16:01:30 -0400139struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
140{
141 struct extent_buffer *eb;
142 spin_lock(&root->node_lock);
143 eb = root->node;
144 extent_buffer_get(eb);
145 spin_unlock(&root->node_lock);
146 return eb;
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/* loop around taking references on and locking the root node of the
150 * tree until you end up with a lock on the root. A locked buffer
151 * is returned, with a reference held.
152 */
Chris Mason925baed2008-06-25 16:01:30 -0400153struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
154{
155 struct extent_buffer *eb;
156
Chris Masond3977122009-01-05 21:25:51 -0500157 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400158 eb = btrfs_root_node(root);
159 btrfs_tree_lock(eb);
160
161 spin_lock(&root->node_lock);
162 if (eb == root->node) {
163 spin_unlock(&root->node_lock);
164 break;
165 }
166 spin_unlock(&root->node_lock);
167
168 btrfs_tree_unlock(eb);
169 free_extent_buffer(eb);
170 }
171 return eb;
172}
173
Chris Masond352ac62008-09-29 15:18:18 -0400174/* cowonly root (everything not a reference counted cow subvolume), just get
175 * put onto a simple dirty list. transaction.c walks this to make sure they
176 * get properly updated on disk.
177 */
Chris Mason0b86a832008-03-24 15:01:56 -0400178static void add_root_to_dirty_list(struct btrfs_root *root)
179{
180 if (root->track_dirty && list_empty(&root->dirty_list)) {
181 list_add(&root->dirty_list,
182 &root->fs_info->dirty_cowonly_roots);
183 }
184}
185
Chris Masond352ac62008-09-29 15:18:18 -0400186/*
187 * used by snapshot creation to make a copy of a root for a tree with
188 * a given objectid. The buffer with the new root node is returned in
189 * cow_ret, and this func returns zero on success or a negative error code.
190 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500191int btrfs_copy_root(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root,
193 struct extent_buffer *buf,
194 struct extent_buffer **cow_ret, u64 new_root_objectid)
195{
196 struct extent_buffer *cow;
197 u32 nritems;
198 int ret = 0;
199 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500200 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201
Chris Mason4aec2b52007-12-18 16:25:45 -0500202 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
203 if (!new_root)
204 return -ENOMEM;
205
206 memcpy(new_root, root, sizeof(*new_root));
207 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208
209 WARN_ON(root->ref_cows && trans->transid !=
210 root->fs_info->running_transaction->transid);
211 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
212
213 level = btrfs_header_level(buf);
214 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400215
216 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
217 new_root_objectid, trans->transid,
218 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500219 if (IS_ERR(cow)) {
220 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500222 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
224 copy_extent_buffer(cow, buf, 0, 0, cow->len);
225 btrfs_set_header_bytenr(cow, cow->start);
226 btrfs_set_header_generation(cow, trans->transid);
227 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400228 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229
Yan Zheng2b820322008-11-17 21:11:30 -0500230 write_extent_buffer(cow, root->fs_info->fsid,
231 (unsigned long)btrfs_header_fsid(cow),
232 BTRFS_FSID_SIZE);
233
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400235 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500236 kfree(new_root);
237
Chris Masonbe20aa92007-12-17 20:14:01 -0500238 if (ret)
239 return ret;
240
241 btrfs_mark_buffer_dirty(cow);
242 *cow_ret = cow;
243 return 0;
244}
245
Chris Masond352ac62008-09-29 15:18:18 -0400246/*
Chris Masond3977122009-01-05 21:25:51 -0500247 * does the dirty work in cow of a single block. The parent block (if
248 * supplied) is updated to point to the new cow copy. The new buffer is marked
249 * dirty and returned locked. If you modify the block it needs to be marked
250 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400251 *
252 * search_start -- an allocation hint for the new block
253 *
Chris Masond3977122009-01-05 21:25:51 -0500254 * empty_size -- a hint that you plan on doing more cow. This is the size in
255 * bytes the allocator should try to find free next to the block it returns.
256 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400257 *
258 * prealloc_dest -- if you have already reserved a destination for the cow,
Chris Masond3977122009-01-05 21:25:51 -0500259 * this uses that block instead of allocating a new one.
260 * btrfs_alloc_reserved_extent is used to finish the allocation.
Chris Masond352ac62008-09-29 15:18:18 -0400261 */
Chris Masond3977122009-01-05 21:25:51 -0500262static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400263 struct btrfs_root *root,
264 struct extent_buffer *buf,
265 struct extent_buffer *parent, int parent_slot,
266 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400267 u64 search_start, u64 empty_size,
268 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400269{
Zheng Yan31840ae2008-09-23 13:14:14 -0400270 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400271 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500272 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400273 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500274 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400275 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400276
Chris Mason925baed2008-06-25 16:01:30 -0400277 if (*cow_ret == buf)
278 unlock_orig = 1;
279
Chris Masonb9447ef82009-03-09 11:45:38 -0400280 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400281
Zheng Yan31840ae2008-09-23 13:14:14 -0400282 if (parent)
283 parent_start = parent->start;
284 else
285 parent_start = 0;
286
Chris Mason7bb86312007-12-11 09:25:06 -0500287 WARN_ON(root->ref_cows && trans->transid !=
288 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400289 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400290
Chris Mason7bb86312007-12-11 09:25:06 -0500291 level = btrfs_header_level(buf);
292 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400293
Chris Mason65b51a02008-08-01 15:11:20 -0400294 if (prealloc_dest) {
295 struct btrfs_key ins;
296
297 ins.objectid = prealloc_dest;
298 ins.offset = buf->len;
299 ins.type = BTRFS_EXTENT_ITEM_KEY;
300
Zheng Yan31840ae2008-09-23 13:14:14 -0400301 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400302 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400303 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400304 BUG_ON(ret);
305 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
Chris Mason4008c042009-02-12 14:09:45 -0500306 buf->len, level);
Chris Mason65b51a02008-08-01 15:11:20 -0400307 } else {
308 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400309 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400310 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400311 trans->transid, level,
312 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400313 }
Chris Mason6702ed42007-08-07 16:15:09 -0400314 if (IS_ERR(cow))
315 return PTR_ERR(cow);
316
Chris Masonb4ce94d2009-02-04 09:25:08 -0500317 /* cow is set to blocking by btrfs_init_new_buffer */
318
Chris Mason5f39d392007-10-15 16:14:19 -0400319 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400320 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400321 btrfs_set_header_generation(cow, trans->transid);
322 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400323 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400324
Yan Zheng2b820322008-11-17 21:11:30 -0500325 write_extent_buffer(cow, root->fs_info->fsid,
326 (unsigned long)btrfs_header_fsid(cow),
327 BTRFS_FSID_SIZE);
328
Chris Mason5f39d392007-10-15 16:14:19 -0400329 WARN_ON(btrfs_header_generation(buf) > trans->transid);
330 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400331 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400332 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400333 if (ret)
334 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400335
336 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
337 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400338 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
339 /*
340 * There are only two places that can drop reference to
341 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400342 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400343 * we check reference count while tree block is locked.
344 * Furthermore, if reference count is one, it won't get
345 * increased by someone else.
346 */
347 u32 refs;
348 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
349 buf->len, &refs);
350 BUG_ON(ret);
351 if (refs == 1) {
352 ret = btrfs_update_ref(trans, root, buf, cow,
353 0, nritems);
354 clean_tree_block(trans, root, buf);
355 } else {
356 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
357 }
358 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400359 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400360 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
361 if (ret)
362 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400363 clean_tree_block(trans, root, buf);
364 }
365
Zheng Yan1a40e232008-09-26 10:09:34 -0400366 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400367 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
368 WARN_ON(ret);
369 }
370
Chris Mason6702ed42007-08-07 16:15:09 -0400371 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400372 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400373
374 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400375 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400376 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400377 spin_unlock(&root->node_lock);
378
Chris Mason6702ed42007-08-07 16:15:09 -0400379 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400380 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400381 buf->len, buf->start,
382 root->root_key.objectid,
383 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400384 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400385 }
Chris Mason5f39d392007-10-15 16:14:19 -0400386 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400387 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400388 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400389 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400390 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500391 WARN_ON(trans->transid == 0);
392 btrfs_set_node_ptr_generation(parent, parent_slot,
393 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400394 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400395 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500396 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400397 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400398 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400399 }
Chris Mason925baed2008-06-25 16:01:30 -0400400 if (unlock_orig)
401 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400402 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400403 btrfs_mark_buffer_dirty(cow);
404 *cow_ret = cow;
405 return 0;
406}
407
Chris Masond352ac62008-09-29 15:18:18 -0400408/*
409 * cows a single block, see __btrfs_cow_block for the real work.
410 * This version of it has extra checks so that a block isn't cow'd more than
411 * once per transaction, as long as it hasn't been written yet
412 */
Chris Masond3977122009-01-05 21:25:51 -0500413noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400414 struct btrfs_root *root, struct extent_buffer *buf,
415 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400416 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500417{
Chris Mason6702ed42007-08-07 16:15:09 -0400418 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400419 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500420
Chris Masonccd467d2007-06-28 15:57:36 -0400421 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500422 printk(KERN_CRIT "trans %llu running %llu\n",
423 (unsigned long long)trans->transid,
424 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400425 root->fs_info->running_transaction->transid);
426 WARN_ON(1);
427 }
428 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500429 printk(KERN_CRIT "trans %llu running %llu\n",
430 (unsigned long long)trans->transid,
431 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400432 WARN_ON(1);
433 }
Chris Masondc17ff82008-01-08 15:46:30 -0500434
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400435 if (btrfs_header_generation(buf) == trans->transid &&
436 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400437 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500438 *cow_ret = buf;
Chris Mason65b51a02008-08-01 15:11:20 -0400439 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500440 return 0;
441 }
Chris Masonc4876852009-02-04 09:24:25 -0500442
Chris Mason0b86a832008-03-24 15:01:56 -0400443 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500444
445 if (parent)
446 btrfs_set_lock_blocking(parent);
447 btrfs_set_lock_blocking(buf);
448
Chris Masonf510cfe2007-10-15 16:14:48 -0400449 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400450 parent_slot, cow_ret, search_start, 0,
451 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400452 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400453}
454
Chris Masond352ac62008-09-29 15:18:18 -0400455/*
456 * helper function for defrag to decide if two blocks pointed to by a
457 * node are actually close by
458 */
Chris Mason6b800532007-10-15 16:17:34 -0400459static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400460{
Chris Mason6b800532007-10-15 16:17:34 -0400461 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400462 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400463 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400464 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500465 return 0;
466}
467
Chris Mason081e9572007-11-06 10:26:24 -0500468/*
469 * compare two keys in a memcmp fashion
470 */
471static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
472{
473 struct btrfs_key k1;
474
475 btrfs_disk_key_to_cpu(&k1, disk);
476
477 if (k1.objectid > k2->objectid)
478 return 1;
479 if (k1.objectid < k2->objectid)
480 return -1;
481 if (k1.type > k2->type)
482 return 1;
483 if (k1.type < k2->type)
484 return -1;
485 if (k1.offset > k2->offset)
486 return 1;
487 if (k1.offset < k2->offset)
488 return -1;
489 return 0;
490}
491
Josef Bacikf3465ca2008-11-12 14:19:50 -0500492/*
493 * same as comp_keys only with two btrfs_key's
494 */
495static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
496{
497 if (k1->objectid > k2->objectid)
498 return 1;
499 if (k1->objectid < k2->objectid)
500 return -1;
501 if (k1->type > k2->type)
502 return 1;
503 if (k1->type < k2->type)
504 return -1;
505 if (k1->offset > k2->offset)
506 return 1;
507 if (k1->offset < k2->offset)
508 return -1;
509 return 0;
510}
Chris Mason081e9572007-11-06 10:26:24 -0500511
Chris Masond352ac62008-09-29 15:18:18 -0400512/*
513 * this is used by the defrag code to go through all the
514 * leaves pointed to by a node and reallocate them so that
515 * disk order is close to key order
516 */
Chris Mason6702ed42007-08-07 16:15:09 -0400517int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400518 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400519 int start_slot, int cache_only, u64 *last_ret,
520 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400521{
Chris Mason6b800532007-10-15 16:17:34 -0400522 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400523 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400524 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400525 u64 search_start = *last_ret;
526 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400527 u64 other;
528 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400529 int end_slot;
530 int i;
531 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400532 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400533 int uptodate;
534 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500535 int progress_passed = 0;
536 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400537
Chris Mason5708b952007-10-25 15:43:18 -0400538 parent_level = btrfs_header_level(parent);
539 if (cache_only && parent_level != 1)
540 return 0;
541
Chris Masond3977122009-01-05 21:25:51 -0500542 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400543 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500544 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400545 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400546
Chris Mason6b800532007-10-15 16:17:34 -0400547 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400548 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400549 end_slot = parent_nritems;
550
551 if (parent_nritems == 1)
552 return 0;
553
Chris Masonb4ce94d2009-02-04 09:25:08 -0500554 btrfs_set_lock_blocking(parent);
555
Chris Mason6702ed42007-08-07 16:15:09 -0400556 for (i = start_slot; i < end_slot; i++) {
557 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400558
Chris Mason5708b952007-10-25 15:43:18 -0400559 if (!parent->map_token) {
560 map_extent_buffer(parent,
561 btrfs_node_key_ptr_offset(i),
562 sizeof(struct btrfs_key_ptr),
563 &parent->map_token, &parent->kaddr,
564 &parent->map_start, &parent->map_len,
565 KM_USER1);
566 }
Chris Mason081e9572007-11-06 10:26:24 -0500567 btrfs_node_key(parent, &disk_key, i);
568 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
569 continue;
570
571 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400572 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400573 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400574 if (last_block == 0)
575 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400576
Chris Mason6702ed42007-08-07 16:15:09 -0400577 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400578 other = btrfs_node_blockptr(parent, i - 1);
579 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400580 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400581 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400582 other = btrfs_node_blockptr(parent, i + 1);
583 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400584 }
Chris Masone9d0b132007-08-10 14:06:19 -0400585 if (close) {
586 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400587 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400588 }
Chris Mason5708b952007-10-25 15:43:18 -0400589 if (parent->map_token) {
590 unmap_extent_buffer(parent, parent->map_token,
591 KM_USER1);
592 parent->map_token = NULL;
593 }
Chris Mason6702ed42007-08-07 16:15:09 -0400594
Chris Mason6b800532007-10-15 16:17:34 -0400595 cur = btrfs_find_tree_block(root, blocknr, blocksize);
596 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400597 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400598 else
599 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400600 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400601 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400602 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400603 continue;
604 }
Chris Mason6b800532007-10-15 16:17:34 -0400605 if (!cur) {
606 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400607 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400608 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400609 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400610 }
Chris Mason6702ed42007-08-07 16:15:09 -0400611 }
Chris Masone9d0b132007-08-10 14:06:19 -0400612 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400613 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400614
Chris Masone7a84562008-06-25 16:01:31 -0400615 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500616 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400617 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400618 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400619 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400620 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400621 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400622 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400623 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400624 break;
Yan252c38f2007-08-29 09:11:44 -0400625 }
Chris Masone7a84562008-06-25 16:01:31 -0400626 search_start = cur->start;
627 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400628 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400629 btrfs_tree_unlock(cur);
630 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400631 }
Chris Mason5708b952007-10-25 15:43:18 -0400632 if (parent->map_token) {
633 unmap_extent_buffer(parent, parent->map_token,
634 KM_USER1);
635 parent->map_token = NULL;
636 }
Chris Mason6702ed42007-08-07 16:15:09 -0400637 return err;
638}
639
Chris Mason74123bd2007-02-02 11:05:29 -0500640/*
641 * The leaf data grows from end-to-front in the node.
642 * this returns the address of the start of the last item,
643 * which is the stop of the leaf data stack
644 */
Chris Mason123abc82007-03-14 14:14:43 -0400645static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400646 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500647{
Chris Mason5f39d392007-10-15 16:14:19 -0400648 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500649 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400650 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400651 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500652}
653
Chris Masond352ac62008-09-29 15:18:18 -0400654/*
655 * extra debugging checks to make sure all the items in a key are
656 * well formed and in the proper order
657 */
Chris Mason123abc82007-03-14 14:14:43 -0400658static int check_node(struct btrfs_root *root, struct btrfs_path *path,
659 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500660{
Chris Mason5f39d392007-10-15 16:14:19 -0400661 struct extent_buffer *parent = NULL;
662 struct extent_buffer *node = path->nodes[level];
663 struct btrfs_disk_key parent_key;
664 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500665 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400666 int slot;
667 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400668 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500669
670 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400671 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400672
Chris Mason8d7be552007-05-10 11:24:42 -0400673 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400674 BUG_ON(nritems == 0);
675 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400676 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400677 btrfs_node_key(parent, &parent_key, parent_slot);
678 btrfs_node_key(node, &node_key, 0);
679 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400680 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400681 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400682 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500683 }
Chris Mason123abc82007-03-14 14:14:43 -0400684 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400685 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400686 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
687 btrfs_node_key(node, &node_key, slot);
688 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400689 }
690 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400691 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
692 btrfs_node_key(node, &node_key, slot);
693 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500694 }
695 return 0;
696}
697
Chris Masond352ac62008-09-29 15:18:18 -0400698/*
699 * extra checking to make sure all the items in a leaf are
700 * well formed and in the proper order
701 */
Chris Mason123abc82007-03-14 14:14:43 -0400702static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
703 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500704{
Chris Mason5f39d392007-10-15 16:14:19 -0400705 struct extent_buffer *leaf = path->nodes[level];
706 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500707 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400708 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400709 struct btrfs_disk_key parent_key;
710 struct btrfs_disk_key leaf_key;
711 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400712
Chris Mason5f39d392007-10-15 16:14:19 -0400713 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500714
715 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400716 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400717
718 if (nritems == 0)
719 return 0;
720
721 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400722 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400723 btrfs_node_key(parent, &parent_key, parent_slot);
724 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400725
Chris Mason5f39d392007-10-15 16:14:19 -0400726 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400727 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400728 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400729 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500730 }
Chris Mason5f39d392007-10-15 16:14:19 -0400731 if (slot != 0 && slot < nritems - 1) {
732 btrfs_item_key(leaf, &leaf_key, slot);
733 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
734 if (comp_keys(&leaf_key, &cpukey) <= 0) {
735 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500736 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400737 BUG_ON(1);
738 }
739 if (btrfs_item_offset_nr(leaf, slot - 1) !=
740 btrfs_item_end_nr(leaf, slot)) {
741 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500742 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400743 BUG_ON(1);
744 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500745 }
Chris Mason8d7be552007-05-10 11:24:42 -0400746 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400747 btrfs_item_key(leaf, &leaf_key, slot);
748 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
749 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
750 if (btrfs_item_offset_nr(leaf, slot) !=
751 btrfs_item_end_nr(leaf, slot + 1)) {
752 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500753 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400754 BUG_ON(1);
755 }
Chris Mason8d7be552007-05-10 11:24:42 -0400756 }
Chris Mason5f39d392007-10-15 16:14:19 -0400757 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
758 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500759 return 0;
760}
761
Chris Masond3977122009-01-05 21:25:51 -0500762static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500763 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500764{
Chris Mason85d824c2008-04-10 10:23:19 -0400765 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500766 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400767 return check_leaf(root, path, level);
768 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500769}
770
Chris Mason74123bd2007-02-02 11:05:29 -0500771/*
Chris Mason5f39d392007-10-15 16:14:19 -0400772 * search for key in the extent_buffer. The items start at offset p,
773 * and they are item_size apart. There are 'max' items in p.
774 *
Chris Mason74123bd2007-02-02 11:05:29 -0500775 * the slot in the array is returned via slot, and it points to
776 * the place where you would insert key if it is not found in
777 * the array.
778 *
779 * slot may point to max if the key is bigger than all of the keys
780 */
Chris Masone02119d2008-09-05 16:13:11 -0400781static noinline int generic_bin_search(struct extent_buffer *eb,
782 unsigned long p,
783 int item_size, struct btrfs_key *key,
784 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500785{
786 int low = 0;
787 int high = max;
788 int mid;
789 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400790 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400791 struct btrfs_disk_key unaligned;
792 unsigned long offset;
793 char *map_token = NULL;
794 char *kaddr = NULL;
795 unsigned long map_start = 0;
796 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400797 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500798
Chris Masond3977122009-01-05 21:25:51 -0500799 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500800 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400801 offset = p + mid * item_size;
802
803 if (!map_token || offset < map_start ||
804 (offset + sizeof(struct btrfs_disk_key)) >
805 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400806 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400807 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400808 map_token = NULL;
809 }
Chris Mason934d3752008-12-08 16:43:10 -0500810
811 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400812 sizeof(struct btrfs_disk_key),
813 &map_token, &kaddr,
814 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400815
Chris Mason479965d2007-10-15 16:14:27 -0400816 if (!err) {
817 tmp = (struct btrfs_disk_key *)(kaddr + offset -
818 map_start);
819 } else {
820 read_extent_buffer(eb, &unaligned,
821 offset, sizeof(unaligned));
822 tmp = &unaligned;
823 }
824
Chris Mason5f39d392007-10-15 16:14:19 -0400825 } else {
826 tmp = (struct btrfs_disk_key *)(kaddr + offset -
827 map_start);
828 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500829 ret = comp_keys(tmp, key);
830
831 if (ret < 0)
832 low = mid + 1;
833 else if (ret > 0)
834 high = mid;
835 else {
836 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400837 if (map_token)
838 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500839 return 0;
840 }
841 }
842 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400843 if (map_token)
844 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500845 return 1;
846}
847
Chris Mason97571fd2007-02-24 13:39:08 -0500848/*
849 * simple bin_search frontend that does the right thing for
850 * leaves vs nodes
851 */
Chris Mason5f39d392007-10-15 16:14:19 -0400852static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
853 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500854{
Chris Mason5f39d392007-10-15 16:14:19 -0400855 if (level == 0) {
856 return generic_bin_search(eb,
857 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400858 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400859 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400860 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500861 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400862 return generic_bin_search(eb,
863 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400864 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400865 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400866 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500867 }
868 return -1;
869}
870
Chris Masond352ac62008-09-29 15:18:18 -0400871/* given a node and slot number, this reads the blocks it points to. The
872 * extent buffer is returned with a reference taken (but unlocked).
873 * NULL is returned on error.
874 */
Chris Masone02119d2008-09-05 16:13:11 -0400875static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400876 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500877{
Chris Masonca7a79a2008-05-12 12:59:19 -0400878 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500879 if (slot < 0)
880 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400881 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500882 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400883
884 BUG_ON(level == 0);
885
Chris Masondb945352007-10-15 16:15:53 -0400886 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400887 btrfs_level_size(root, level - 1),
888 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500889}
890
Chris Masond352ac62008-09-29 15:18:18 -0400891/*
892 * node level balancing, used to make sure nodes are in proper order for
893 * item deletion. We balance from the top down, so we have to make sure
894 * that a deletion won't leave an node completely empty later on.
895 */
Chris Masone02119d2008-09-05 16:13:11 -0400896static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500897 struct btrfs_root *root,
898 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500899{
Chris Mason5f39d392007-10-15 16:14:19 -0400900 struct extent_buffer *right = NULL;
901 struct extent_buffer *mid;
902 struct extent_buffer *left = NULL;
903 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500904 int ret = 0;
905 int wret;
906 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500907 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400908 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500909 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500910
911 if (level == 0)
912 return 0;
913
Chris Mason5f39d392007-10-15 16:14:19 -0400914 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500915
Chris Mason925baed2008-06-25 16:01:30 -0400916 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500917 WARN_ON(btrfs_header_generation(mid) != trans->transid);
918
Chris Mason1d4f8a02007-03-13 09:28:32 -0400919 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500920
Chris Mason234b63a2007-03-13 10:46:10 -0400921 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400922 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500923 pslot = path->slots[level + 1];
924
Chris Mason40689472007-03-17 14:29:23 -0400925 /*
926 * deal with the case where there is only one pointer in the root
927 * by promoting the node below to a root
928 */
Chris Mason5f39d392007-10-15 16:14:19 -0400929 if (!parent) {
930 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500931
Chris Mason5f39d392007-10-15 16:14:19 -0400932 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500933 return 0;
934
935 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400936 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500937 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400938 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500939 btrfs_set_lock_blocking(child);
Chris Mason65b51a02008-08-01 15:11:20 -0400940 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500941 BUG_ON(ret);
942
Chris Mason925baed2008-06-25 16:01:30 -0400943 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500944 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400945 spin_unlock(&root->node_lock);
946
Zheng Yan31840ae2008-09-23 13:14:14 -0400947 ret = btrfs_update_extent_ref(trans, root, child->start,
948 mid->start, child->start,
949 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400950 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400951 BUG_ON(ret);
952
Chris Mason0b86a832008-03-24 15:01:56 -0400953 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400954 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500955
Chris Mason925baed2008-06-25 16:01:30 -0400956 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500957 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400958 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400959 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500960 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400961 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500962 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400963 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400964 btrfs_header_generation(mid),
965 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500966 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400967 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400968 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500969 }
Chris Mason5f39d392007-10-15 16:14:19 -0400970 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400971 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500972 return 0;
973
Chris Mason5f39d392007-10-15 16:14:19 -0400974 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400975 err_on_enospc = 1;
976
Chris Mason5f39d392007-10-15 16:14:19 -0400977 left = read_node_slot(root, parent, pslot - 1);
978 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400979 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500980 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400981 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400982 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400983 if (wret) {
984 ret = wret;
985 goto enospc;
986 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400987 }
Chris Mason5f39d392007-10-15 16:14:19 -0400988 right = read_node_slot(root, parent, pslot + 1);
989 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400990 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500991 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400992 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400993 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400994 if (wret) {
995 ret = wret;
996 goto enospc;
997 }
998 }
999
1000 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001001 if (left) {
1002 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001003 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001004 if (wret < 0)
1005 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001006 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001008 }
Chris Mason79f95c82007-03-01 15:16:26 -05001009
1010 /*
1011 * then try to empty the right most buffer into the middle
1012 */
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001014 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001015 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001016 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001017 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001018 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -05001019 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001020 u32 blocksize = right->len;
1021
Chris Mason5f39d392007-10-15 16:14:19 -04001022 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001023 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001024 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001025 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001026 wret = del_ptr(trans, root, path, level + 1, pslot +
1027 1);
Chris Masonbb803952007-03-01 12:04:21 -05001028 if (wret)
1029 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001030 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001031 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001032 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001033 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001034 if (wret)
1035 ret = wret;
1036 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001037 struct btrfs_disk_key right_key;
1038 btrfs_node_key(right, &right_key, 0);
1039 btrfs_set_node_key(parent, &right_key, pslot + 1);
1040 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001041 }
1042 }
Chris Mason5f39d392007-10-15 16:14:19 -04001043 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001044 /*
1045 * we're not allowed to leave a node with one item in the
1046 * tree during a delete. A deletion from lower in the tree
1047 * could try to delete the only pointer in this node.
1048 * So, pull some keys from the left.
1049 * There has to be a left pointer at this point because
1050 * otherwise we would have pulled some pointers from the
1051 * right
1052 */
Chris Mason5f39d392007-10-15 16:14:19 -04001053 BUG_ON(!left);
1054 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001055 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001056 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001057 goto enospc;
1058 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001059 if (wret == 1) {
1060 wret = push_node_left(trans, root, left, mid, 1);
1061 if (wret < 0)
1062 ret = wret;
1063 }
Chris Mason79f95c82007-03-01 15:16:26 -05001064 BUG_ON(wret == 1);
1065 }
Chris Mason5f39d392007-10-15 16:14:19 -04001066 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001067 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001068 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001069 u64 bytenr = mid->start;
1070 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001071
Chris Mason5f39d392007-10-15 16:14:19 -04001072 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001073 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001074 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001075 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001076 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001077 if (wret)
1078 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001079 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001080 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001081 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001082 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001083 if (wret)
1084 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001085 } else {
1086 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001087 struct btrfs_disk_key mid_key;
1088 btrfs_node_key(mid, &mid_key, 0);
1089 btrfs_set_node_key(parent, &mid_key, pslot);
1090 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001091 }
Chris Masonbb803952007-03-01 12:04:21 -05001092
Chris Mason79f95c82007-03-01 15:16:26 -05001093 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001094 if (left) {
1095 if (btrfs_header_nritems(left) > orig_slot) {
1096 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001097 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001098 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001099 path->slots[level + 1] -= 1;
1100 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001101 if (mid) {
1102 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001103 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001104 }
Chris Masonbb803952007-03-01 12:04:21 -05001105 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001106 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001107 path->slots[level] = orig_slot;
1108 }
1109 }
Chris Mason79f95c82007-03-01 15:16:26 -05001110 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001111 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001112 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001113 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001114 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001115enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001116 if (right) {
1117 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001118 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001119 }
1120 if (left) {
1121 if (path->nodes[level] != left)
1122 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001123 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001124 }
Chris Masonbb803952007-03-01 12:04:21 -05001125 return ret;
1126}
1127
Chris Masond352ac62008-09-29 15:18:18 -04001128/* Node balancing for insertion. Here we only split or push nodes around
1129 * when they are completely full. This is also done top down, so we
1130 * have to be pessimistic.
1131 */
Chris Masond3977122009-01-05 21:25:51 -05001132static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001133 struct btrfs_root *root,
1134 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001135{
Chris Mason5f39d392007-10-15 16:14:19 -04001136 struct extent_buffer *right = NULL;
1137 struct extent_buffer *mid;
1138 struct extent_buffer *left = NULL;
1139 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001140 int ret = 0;
1141 int wret;
1142 int pslot;
1143 int orig_slot = path->slots[level];
1144 u64 orig_ptr;
1145
1146 if (level == 0)
1147 return 1;
1148
Chris Mason5f39d392007-10-15 16:14:19 -04001149 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001150 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001151 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1152
1153 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001154 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001155 pslot = path->slots[level + 1];
1156
Chris Mason5f39d392007-10-15 16:14:19 -04001157 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001158 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001159
Chris Mason5f39d392007-10-15 16:14:19 -04001160 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001161
1162 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001163 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001164 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001165
1166 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001167 btrfs_set_lock_blocking(left);
1168
Chris Mason5f39d392007-10-15 16:14:19 -04001169 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001170 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1171 wret = 1;
1172 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001173 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001174 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001175 if (ret)
1176 wret = 1;
1177 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001178 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001179 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001180 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001181 }
Chris Masone66f7092007-04-20 13:16:02 -04001182 if (wret < 0)
1183 ret = wret;
1184 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001185 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001186 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001187 btrfs_node_key(mid, &disk_key, 0);
1188 btrfs_set_node_key(parent, &disk_key, pslot);
1189 btrfs_mark_buffer_dirty(parent);
1190 if (btrfs_header_nritems(left) > orig_slot) {
1191 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001192 path->slots[level + 1] -= 1;
1193 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001194 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001195 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001196 } else {
1197 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001198 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001199 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001200 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001201 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001202 }
Chris Masone66f7092007-04-20 13:16:02 -04001203 return 0;
1204 }
Chris Mason925baed2008-06-25 16:01:30 -04001205 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001206 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001207 }
Chris Mason925baed2008-06-25 16:01:30 -04001208 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001209
1210 /*
1211 * then try to empty the right most buffer into the middle
1212 */
Chris Mason5f39d392007-10-15 16:14:19 -04001213 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001214 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001215
Chris Mason925baed2008-06-25 16:01:30 -04001216 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001217 btrfs_set_lock_blocking(right);
1218
Chris Mason5f39d392007-10-15 16:14:19 -04001219 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001220 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1221 wret = 1;
1222 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001223 ret = btrfs_cow_block(trans, root, right,
1224 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001225 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001226 if (ret)
1227 wret = 1;
1228 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001229 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001230 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001231 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001232 }
Chris Masone66f7092007-04-20 13:16:02 -04001233 if (wret < 0)
1234 ret = wret;
1235 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001236 struct btrfs_disk_key disk_key;
1237
1238 btrfs_node_key(right, &disk_key, 0);
1239 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1240 btrfs_mark_buffer_dirty(parent);
1241
1242 if (btrfs_header_nritems(mid) <= orig_slot) {
1243 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001244 path->slots[level + 1] += 1;
1245 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001246 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001247 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001248 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001249 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001250 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001251 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001252 }
Chris Masone66f7092007-04-20 13:16:02 -04001253 return 0;
1254 }
Chris Mason925baed2008-06-25 16:01:30 -04001255 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001256 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001257 }
Chris Masone66f7092007-04-20 13:16:02 -04001258 return 1;
1259}
1260
Chris Mason74123bd2007-02-02 11:05:29 -05001261/*
Chris Masond352ac62008-09-29 15:18:18 -04001262 * readahead one full node of leaves, finding things that are close
1263 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001264 */
Chris Masone02119d2008-09-05 16:13:11 -04001265static noinline void reada_for_search(struct btrfs_root *root,
1266 struct btrfs_path *path,
1267 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001268{
Chris Mason5f39d392007-10-15 16:14:19 -04001269 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001270 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001272 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001273 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001274 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001275 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001276 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001277 u32 nr;
1278 u32 blocksize;
1279 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001280
Chris Masona6b6e752007-10-15 16:22:39 -04001281 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001282 return;
1283
Chris Mason6702ed42007-08-07 16:15:09 -04001284 if (!path->nodes[level])
1285 return;
1286
Chris Mason5f39d392007-10-15 16:14:19 -04001287 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001288
Chris Mason3c69fae2007-08-07 15:52:22 -04001289 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001290 blocksize = btrfs_level_size(root, level - 1);
1291 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001292 if (eb) {
1293 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001294 return;
1295 }
1296
Chris Masona7175312009-01-22 09:23:10 -05001297 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001298
Chris Mason5f39d392007-10-15 16:14:19 -04001299 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001300 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001301 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001302 if (direction < 0) {
1303 if (nr == 0)
1304 break;
1305 nr--;
1306 } else if (direction > 0) {
1307 nr++;
1308 if (nr >= nritems)
1309 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001310 }
Chris Mason01f46652007-12-21 16:24:26 -05001311 if (path->reada < 0 && objectid) {
1312 btrfs_node_key(node, &disk_key, nr);
1313 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1314 break;
1315 }
Chris Mason6b800532007-10-15 16:17:34 -04001316 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001317 if ((search <= target && target - search <= 65536) ||
1318 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001319 readahead_tree_block(root, search, blocksize,
1320 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001321 nread += blocksize;
1322 }
1323 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001324 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001325 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001326 }
1327}
Chris Mason925baed2008-06-25 16:01:30 -04001328
Chris Masond352ac62008-09-29 15:18:18 -04001329/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001330 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1331 * cache
1332 */
1333static noinline int reada_for_balance(struct btrfs_root *root,
1334 struct btrfs_path *path, int level)
1335{
1336 int slot;
1337 int nritems;
1338 struct extent_buffer *parent;
1339 struct extent_buffer *eb;
1340 u64 gen;
1341 u64 block1 = 0;
1342 u64 block2 = 0;
1343 int ret = 0;
1344 int blocksize;
1345
1346 parent = path->nodes[level - 1];
1347 if (!parent)
1348 return 0;
1349
1350 nritems = btrfs_header_nritems(parent);
1351 slot = path->slots[level];
1352 blocksize = btrfs_level_size(root, level);
1353
1354 if (slot > 0) {
1355 block1 = btrfs_node_blockptr(parent, slot - 1);
1356 gen = btrfs_node_ptr_generation(parent, slot - 1);
1357 eb = btrfs_find_tree_block(root, block1, blocksize);
1358 if (eb && btrfs_buffer_uptodate(eb, gen))
1359 block1 = 0;
1360 free_extent_buffer(eb);
1361 }
1362 if (slot < nritems) {
1363 block2 = btrfs_node_blockptr(parent, slot + 1);
1364 gen = btrfs_node_ptr_generation(parent, slot + 1);
1365 eb = btrfs_find_tree_block(root, block2, blocksize);
1366 if (eb && btrfs_buffer_uptodate(eb, gen))
1367 block2 = 0;
1368 free_extent_buffer(eb);
1369 }
1370 if (block1 || block2) {
1371 ret = -EAGAIN;
1372 btrfs_release_path(root, path);
1373 if (block1)
1374 readahead_tree_block(root, block1, blocksize, 0);
1375 if (block2)
1376 readahead_tree_block(root, block2, blocksize, 0);
1377
1378 if (block1) {
1379 eb = read_tree_block(root, block1, blocksize, 0);
1380 free_extent_buffer(eb);
1381 }
1382 if (block1) {
1383 eb = read_tree_block(root, block2, blocksize, 0);
1384 free_extent_buffer(eb);
1385 }
1386 }
1387 return ret;
1388}
1389
1390
1391/*
Chris Masond3977122009-01-05 21:25:51 -05001392 * when we walk down the tree, it is usually safe to unlock the higher layers
1393 * in the tree. The exceptions are when our path goes through slot 0, because
1394 * operations on the tree might require changing key pointers higher up in the
1395 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001396 *
Chris Masond3977122009-01-05 21:25:51 -05001397 * callers might also have set path->keep_locks, which tells this code to keep
1398 * the lock if the path points to the last slot in the block. This is part of
1399 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001400 *
Chris Masond3977122009-01-05 21:25:51 -05001401 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1402 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001403 */
Chris Masone02119d2008-09-05 16:13:11 -04001404static noinline void unlock_up(struct btrfs_path *path, int level,
1405 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001406{
1407 int i;
1408 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001409 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001410 struct extent_buffer *t;
1411
1412 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1413 if (!path->nodes[i])
1414 break;
1415 if (!path->locks[i])
1416 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001417 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001418 skip_level = i + 1;
1419 continue;
1420 }
Chris Mason051e1b92008-06-25 16:01:30 -04001421 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001422 u32 nritems;
1423 t = path->nodes[i];
1424 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001425 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001426 skip_level = i + 1;
1427 continue;
1428 }
1429 }
Chris Mason051e1b92008-06-25 16:01:30 -04001430 if (skip_level < i && i >= lowest_unlock)
1431 no_skips = 1;
1432
Chris Mason925baed2008-06-25 16:01:30 -04001433 t = path->nodes[i];
1434 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1435 btrfs_tree_unlock(t);
1436 path->locks[i] = 0;
1437 }
1438 }
1439}
1440
Chris Mason3c69fae2007-08-07 15:52:22 -04001441/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001442 * This releases any locks held in the path starting at level and
1443 * going all the way up to the root.
1444 *
1445 * btrfs_search_slot will keep the lock held on higher nodes in a few
1446 * corner cases, such as COW of the block at slot zero in the node. This
1447 * ignores those rules, and it should only be called when there are no
1448 * more updates to be done higher up in the tree.
1449 */
1450noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1451{
1452 int i;
1453
1454 if (path->keep_locks || path->lowest_level)
1455 return;
1456
1457 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1458 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001459 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001460 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001461 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001462 btrfs_tree_unlock(path->nodes[i]);
1463 path->locks[i] = 0;
1464 }
1465}
1466
1467/*
Chris Mason74123bd2007-02-02 11:05:29 -05001468 * look for key in the tree. path is filled in with nodes along the way
1469 * if key is found, we return zero and you can find the item in the leaf
1470 * level of the path (level 0)
1471 *
1472 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001473 * be inserted, and 1 is returned. If there are other errors during the
1474 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001475 *
1476 * if ins_len > 0, nodes and leaves will be split as we walk down the
1477 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1478 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001479 */
Chris Masone089f052007-03-16 16:20:31 -04001480int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1481 *root, struct btrfs_key *key, struct btrfs_path *p, int
1482 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001483{
Chris Mason5f39d392007-10-15 16:14:19 -04001484 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001485 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001486 int slot;
1487 int ret;
1488 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001489 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001490 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001491 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001492 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001493 u64 blocknr;
1494 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001495 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001496
Chris Mason6702ed42007-08-07 16:15:09 -04001497 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001498 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001499 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001500
Chris Mason925baed2008-06-25 16:01:30 -04001501 if (ins_len < 0)
1502 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001503
1504 prealloc_block.objectid = 0;
1505
Chris Masonbb803952007-03-01 12:04:21 -05001506again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001507 if (p->skip_locking)
1508 b = btrfs_root_node(root);
1509 else
1510 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001511
Chris Masoneb60cea2007-02-02 09:18:22 -05001512 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001513 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001514
1515 /*
1516 * setup the path here so we can release it under lock
1517 * contention with the cow code
1518 */
1519 p->nodes[level] = b;
1520 if (!p->skip_locking)
1521 p->locks[level] = 1;
1522
Chris Mason02217ed2007-03-02 16:08:05 -05001523 if (cow) {
1524 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001525
1526 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001527 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001528 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001529 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001530 goto cow_done;
1531 }
Chris Mason65b51a02008-08-01 15:11:20 -04001532
1533 /* ok, we have to cow, is our old prealloc the right
1534 * size?
1535 */
1536 if (prealloc_block.objectid &&
1537 prealloc_block.offset != b->len) {
Chris Mason7b78c172009-02-04 09:12:46 -05001538 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001539 btrfs_free_reserved_extent(root,
1540 prealloc_block.objectid,
1541 prealloc_block.offset);
1542 prealloc_block.objectid = 0;
Chris Mason7b78c172009-02-04 09:12:46 -05001543 goto again;
Chris Mason65b51a02008-08-01 15:11:20 -04001544 }
1545
1546 /*
1547 * for higher level blocks, try not to allocate blocks
1548 * with the block and the parent locks held.
1549 */
Chris Mason284b0662009-02-09 16:22:03 -05001550 if (level > 0 && !prealloc_block.objectid) {
Chris Mason65b51a02008-08-01 15:11:20 -04001551 u32 size = b->len;
1552 u64 hint = b->start;
1553
1554 btrfs_release_path(root, p);
1555 ret = btrfs_reserve_extent(trans, root,
1556 size, size, 0,
1557 hint, (u64)-1,
1558 &prealloc_block, 0);
1559 BUG_ON(ret);
1560 goto again;
1561 }
1562
Chris Masonb4ce94d2009-02-04 09:25:08 -05001563 btrfs_set_path_blocking(p);
1564
Chris Masone20d96d2007-03-22 12:13:20 -04001565 wret = btrfs_cow_block(trans, root, b,
1566 p->nodes[level + 1],
1567 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001568 &b, prealloc_block.objectid);
1569 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001570 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001571 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001572 ret = wret;
1573 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001574 }
Chris Mason02217ed2007-03-02 16:08:05 -05001575 }
Chris Mason65b51a02008-08-01 15:11:20 -04001576cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001577 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001578 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001579 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001580 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001581
Chris Masoneb60cea2007-02-02 09:18:22 -05001582 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001583 if (!p->skip_locking)
1584 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001585
Chris Mason4008c042009-02-12 14:09:45 -05001586 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001587
1588 /*
1589 * we have a lock on b and as long as we aren't changing
1590 * the tree, there is no way to for the items in b to change.
1591 * It is safe to drop the lock on our parent before we
1592 * go through the expensive btree search on b.
1593 *
1594 * If cow is true, then we might be changing slot zero,
1595 * which may require changing the parent. So, we can't
1596 * drop the lock until after we know which slot we're
1597 * operating on.
1598 */
1599 if (!cow)
1600 btrfs_unlock_up_safe(p, level + 1);
1601
Chris Mason123abc82007-03-14 14:14:43 -04001602 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001603 if (ret) {
1604 ret = -1;
1605 goto done;
1606 }
Chris Mason925baed2008-06-25 16:01:30 -04001607
Chris Mason5f39d392007-10-15 16:14:19 -04001608 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001609
Chris Mason5f39d392007-10-15 16:14:19 -04001610 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001611 if (ret && slot > 0)
1612 slot -= 1;
1613 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001614 if ((p->search_for_split || ins_len > 0) &&
1615 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001616 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001617 int sret;
1618
1619 sret = reada_for_balance(root, p, level);
1620 if (sret)
1621 goto again;
1622
1623 btrfs_set_path_blocking(p);
1624 sret = split_node(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001625 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001626
Chris Mason5c680ed2007-02-22 11:39:13 -05001627 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001628 if (sret) {
1629 ret = sret;
1630 goto done;
1631 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001632 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001633 slot = p->slots[level];
Chris Mason7b78c172009-02-04 09:12:46 -05001634 } else if (ins_len < 0 &&
1635 btrfs_header_nritems(b) <
1636 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001637 int sret;
1638
1639 sret = reada_for_balance(root, p, level);
1640 if (sret)
1641 goto again;
1642
1643 btrfs_set_path_blocking(p);
1644 sret = balance_level(trans, root, p, level);
Chris Mason4008c042009-02-12 14:09:45 -05001645 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001646
Chris Mason65b51a02008-08-01 15:11:20 -04001647 if (sret) {
1648 ret = sret;
1649 goto done;
1650 }
Chris Masonbb803952007-03-01 12:04:21 -05001651 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001652 if (!b) {
1653 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001654 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001655 }
Chris Masonbb803952007-03-01 12:04:21 -05001656 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001657 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001658 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001659 unlock_up(p, level, lowest_unlock);
1660
Chris Mason9f3a7422007-08-07 15:52:19 -04001661 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001662 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001663 ret = 0;
1664 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001665 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001666
Chris Mason594a24e2008-06-25 16:01:30 -04001667 blocknr = btrfs_node_blockptr(b, slot);
1668 gen = btrfs_node_ptr_generation(b, slot);
1669 blocksize = btrfs_level_size(root, level - 1);
1670
1671 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1672 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001673 b = tmp;
1674 } else {
1675 /*
1676 * reduce lock contention at high levels
1677 * of the btree by dropping locks before
1678 * we read.
1679 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001680 if (level > 0) {
Chris Mason051e1b92008-06-25 16:01:30 -04001681 btrfs_release_path(NULL, p);
1682 if (tmp)
1683 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001684 if (should_reada)
1685 reada_for_search(root, p,
1686 level, slot,
1687 key->objectid);
1688
Chris Mason594a24e2008-06-25 16:01:30 -04001689 tmp = read_tree_block(root, blocknr,
1690 blocksize, gen);
1691 if (tmp)
1692 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001693 goto again;
1694 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001695 btrfs_set_path_blocking(p);
Chris Masona74a4b92008-06-25 16:01:31 -04001696 if (tmp)
1697 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001698 if (should_reada)
1699 reada_for_search(root, p,
1700 level, slot,
1701 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001702 b = read_node_slot(root, b, slot);
1703 }
1704 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001705 if (!p->skip_locking) {
1706 int lret;
1707
Chris Mason4008c042009-02-12 14:09:45 -05001708 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001709 lret = btrfs_try_spin_lock(b);
1710
1711 if (!lret) {
1712 btrfs_set_path_blocking(p);
1713 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001714 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001715 }
1716 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001717 } else {
1718 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001719 if (ins_len > 0 &&
1720 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001721 int sret;
1722
1723 btrfs_set_path_blocking(p);
1724 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001725 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001726 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001727
Chris Mason5c680ed2007-02-22 11:39:13 -05001728 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001729 if (sret) {
1730 ret = sret;
1731 goto done;
1732 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001733 }
Chris Mason459931e2008-12-10 09:10:46 -05001734 if (!p->search_for_split)
1735 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001736 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001737 }
1738 }
Chris Mason65b51a02008-08-01 15:11:20 -04001739 ret = 1;
1740done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001741 /*
1742 * we don't really know what they plan on doing with the path
1743 * from here on, so for now just mark it as blocking
1744 */
1745 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001746 if (prealloc_block.objectid) {
1747 btrfs_free_reserved_extent(root,
1748 prealloc_block.objectid,
1749 prealloc_block.offset);
1750 }
Chris Mason65b51a02008-08-01 15:11:20 -04001751 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001752}
1753
Zheng Yan1a40e232008-09-26 10:09:34 -04001754int btrfs_merge_path(struct btrfs_trans_handle *trans,
1755 struct btrfs_root *root,
1756 struct btrfs_key *node_keys,
1757 u64 *nodes, int lowest_level)
1758{
1759 struct extent_buffer *eb;
1760 struct extent_buffer *parent;
1761 struct btrfs_key key;
1762 u64 bytenr;
1763 u64 generation;
1764 u32 blocksize;
1765 int level;
1766 int slot;
1767 int key_match;
1768 int ret;
1769
1770 eb = btrfs_lock_root_node(root);
1771 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1772 BUG_ON(ret);
1773
Chris Masonb4ce94d2009-02-04 09:25:08 -05001774 btrfs_set_lock_blocking(eb);
1775
Zheng Yan1a40e232008-09-26 10:09:34 -04001776 parent = eb;
1777 while (1) {
1778 level = btrfs_header_level(parent);
1779 if (level == 0 || level <= lowest_level)
1780 break;
1781
1782 ret = bin_search(parent, &node_keys[lowest_level], level,
1783 &slot);
1784 if (ret && slot > 0)
1785 slot--;
1786
1787 bytenr = btrfs_node_blockptr(parent, slot);
1788 if (nodes[level - 1] == bytenr)
1789 break;
1790
1791 blocksize = btrfs_level_size(root, level - 1);
1792 generation = btrfs_node_ptr_generation(parent, slot);
1793 btrfs_node_key_to_cpu(eb, &key, slot);
1794 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1795
Yan Zhengf82d02d2008-10-29 14:49:05 -04001796 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001797 eb = read_tree_block(root, bytenr, blocksize,
1798 generation);
1799 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001800 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001801 }
1802
1803 /*
1804 * if node keys match and node pointer hasn't been modified
1805 * in the running transaction, we can merge the path. for
1806 * blocks owened by reloc trees, the node pointer check is
1807 * skipped, this is because these blocks are fully controlled
1808 * by the space balance code, no one else can modify them.
1809 */
1810 if (!nodes[level - 1] || !key_match ||
1811 (generation == trans->transid &&
1812 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1813 if (level == 1 || level == lowest_level + 1) {
1814 if (generation == trans->transid) {
1815 btrfs_tree_unlock(eb);
1816 free_extent_buffer(eb);
1817 }
1818 break;
1819 }
1820
1821 if (generation != trans->transid) {
1822 eb = read_tree_block(root, bytenr, blocksize,
1823 generation);
1824 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001825 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001826 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001827
1828 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1829 &eb, 0);
1830 BUG_ON(ret);
1831
Yan Zhengf82d02d2008-10-29 14:49:05 -04001832 if (root->root_key.objectid ==
1833 BTRFS_TREE_RELOC_OBJECTID) {
1834 if (!nodes[level - 1]) {
1835 nodes[level - 1] = eb->start;
1836 memcpy(&node_keys[level - 1], &key,
1837 sizeof(node_keys[0]));
1838 } else {
1839 WARN_ON(1);
1840 }
1841 }
1842
Zheng Yan1a40e232008-09-26 10:09:34 -04001843 btrfs_tree_unlock(parent);
1844 free_extent_buffer(parent);
1845 parent = eb;
1846 continue;
1847 }
1848
Zheng Yan1a40e232008-09-26 10:09:34 -04001849 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1850 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1851 btrfs_mark_buffer_dirty(parent);
1852
1853 ret = btrfs_inc_extent_ref(trans, root,
1854 nodes[level - 1],
1855 blocksize, parent->start,
1856 btrfs_header_owner(parent),
1857 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001858 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001859 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001860
1861 /*
1862 * If the block was created in the running transaction,
1863 * it's possible this is the last reference to it, so we
1864 * should drop the subtree.
1865 */
1866 if (generation == trans->transid) {
1867 ret = btrfs_drop_subtree(trans, root, eb, parent);
1868 BUG_ON(ret);
1869 btrfs_tree_unlock(eb);
1870 free_extent_buffer(eb);
1871 } else {
1872 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001873 blocksize, parent->start,
1874 btrfs_header_owner(parent),
1875 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001876 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001877 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001878 }
1879 break;
1880 }
1881 btrfs_tree_unlock(parent);
1882 free_extent_buffer(parent);
1883 return 0;
1884}
1885
Chris Mason74123bd2007-02-02 11:05:29 -05001886/*
1887 * adjust the pointers going up the tree, starting at level
1888 * making sure the right key of each node is points to 'key'.
1889 * This is used after shifting pointers to the left, so it stops
1890 * fixing up pointers when a given leaf/node is not in slot 0 of the
1891 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001892 *
1893 * If this fails to write a tree block, it returns -1, but continues
1894 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001895 */
Chris Mason5f39d392007-10-15 16:14:19 -04001896static int fixup_low_keys(struct btrfs_trans_handle *trans,
1897 struct btrfs_root *root, struct btrfs_path *path,
1898 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001899{
1900 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001901 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001902 struct extent_buffer *t;
1903
Chris Mason234b63a2007-03-13 10:46:10 -04001904 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001905 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001906 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001907 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001908 t = path->nodes[i];
1909 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001910 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001911 if (tslot != 0)
1912 break;
1913 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001914 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001915}
1916
Chris Mason74123bd2007-02-02 11:05:29 -05001917/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001918 * update item key.
1919 *
1920 * This function isn't completely safe. It's the caller's responsibility
1921 * that the new key won't break the order
1922 */
1923int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1924 struct btrfs_root *root, struct btrfs_path *path,
1925 struct btrfs_key *new_key)
1926{
1927 struct btrfs_disk_key disk_key;
1928 struct extent_buffer *eb;
1929 int slot;
1930
1931 eb = path->nodes[0];
1932 slot = path->slots[0];
1933 if (slot > 0) {
1934 btrfs_item_key(eb, &disk_key, slot - 1);
1935 if (comp_keys(&disk_key, new_key) >= 0)
1936 return -1;
1937 }
1938 if (slot < btrfs_header_nritems(eb) - 1) {
1939 btrfs_item_key(eb, &disk_key, slot + 1);
1940 if (comp_keys(&disk_key, new_key) <= 0)
1941 return -1;
1942 }
1943
1944 btrfs_cpu_key_to_disk(&disk_key, new_key);
1945 btrfs_set_item_key(eb, &disk_key, slot);
1946 btrfs_mark_buffer_dirty(eb);
1947 if (slot == 0)
1948 fixup_low_keys(trans, root, path, &disk_key, 1);
1949 return 0;
1950}
1951
1952/*
Chris Mason74123bd2007-02-02 11:05:29 -05001953 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001954 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001955 *
1956 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1957 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001958 */
Chris Mason98ed5172008-01-03 10:01:48 -05001959static int push_node_left(struct btrfs_trans_handle *trans,
1960 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001961 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001963 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001964 int src_nritems;
1965 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001966 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 src_nritems = btrfs_header_nritems(src);
1969 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001970 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001971 WARN_ON(btrfs_header_generation(src) != trans->transid);
1972 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001973
Chris Masonbce4eae2008-04-24 14:42:46 -04001974 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001975 return 1;
1976
Chris Masond3977122009-01-05 21:25:51 -05001977 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001978 return 1;
1979
Chris Masonbce4eae2008-04-24 14:42:46 -04001980 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001981 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001982 if (push_items < src_nritems) {
1983 /* leave at least 8 pointers in the node if
1984 * we aren't going to empty it
1985 */
1986 if (src_nritems - push_items < 8) {
1987 if (push_items <= 8)
1988 return 1;
1989 push_items -= 8;
1990 }
1991 }
1992 } else
1993 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001994
Chris Mason5f39d392007-10-15 16:14:19 -04001995 copy_extent_buffer(dst, src,
1996 btrfs_node_key_ptr_offset(dst_nritems),
1997 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001998 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001999
Chris Masonbb803952007-03-01 12:04:21 -05002000 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002001 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2002 btrfs_node_key_ptr_offset(push_items),
2003 (src_nritems - push_items) *
2004 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002005 }
Chris Mason5f39d392007-10-15 16:14:19 -04002006 btrfs_set_header_nritems(src, src_nritems - push_items);
2007 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2008 btrfs_mark_buffer_dirty(src);
2009 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002010
2011 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
2012 BUG_ON(ret);
2013
Chris Masonbb803952007-03-01 12:04:21 -05002014 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002015}
2016
Chris Mason97571fd2007-02-24 13:39:08 -05002017/*
Chris Mason79f95c82007-03-01 15:16:26 -05002018 * try to push data from one node into the next node right in the
2019 * tree.
2020 *
2021 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2022 * error, and > 0 if there was no room in the right hand block.
2023 *
2024 * this will only push up to 1/2 the contents of the left node over
2025 */
Chris Mason5f39d392007-10-15 16:14:19 -04002026static int balance_node_right(struct btrfs_trans_handle *trans,
2027 struct btrfs_root *root,
2028 struct extent_buffer *dst,
2029 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002030{
Chris Mason79f95c82007-03-01 15:16:26 -05002031 int push_items = 0;
2032 int max_push;
2033 int src_nritems;
2034 int dst_nritems;
2035 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002036
Chris Mason7bb86312007-12-11 09:25:06 -05002037 WARN_ON(btrfs_header_generation(src) != trans->transid);
2038 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2039
Chris Mason5f39d392007-10-15 16:14:19 -04002040 src_nritems = btrfs_header_nritems(src);
2041 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002042 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002043 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002044 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002045
Chris Masond3977122009-01-05 21:25:51 -05002046 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002047 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002048
2049 max_push = src_nritems / 2 + 1;
2050 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002051 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002052 return 1;
Yan252c38f2007-08-29 09:11:44 -04002053
Chris Mason79f95c82007-03-01 15:16:26 -05002054 if (max_push < push_items)
2055 push_items = max_push;
2056
Chris Mason5f39d392007-10-15 16:14:19 -04002057 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2058 btrfs_node_key_ptr_offset(0),
2059 (dst_nritems) *
2060 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002061
Chris Mason5f39d392007-10-15 16:14:19 -04002062 copy_extent_buffer(dst, src,
2063 btrfs_node_key_ptr_offset(0),
2064 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002065 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002066
Chris Mason5f39d392007-10-15 16:14:19 -04002067 btrfs_set_header_nritems(src, src_nritems - push_items);
2068 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002069
Chris Mason5f39d392007-10-15 16:14:19 -04002070 btrfs_mark_buffer_dirty(src);
2071 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002072
2073 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2074 BUG_ON(ret);
2075
Chris Mason79f95c82007-03-01 15:16:26 -05002076 return ret;
2077}
2078
2079/*
Chris Mason97571fd2007-02-24 13:39:08 -05002080 * helper function to insert a new root level in the tree.
2081 * A new node is allocated, and a single item is inserted to
2082 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002083 *
2084 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002085 */
Chris Masond3977122009-01-05 21:25:51 -05002086static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002087 struct btrfs_root *root,
2088 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002089{
Chris Mason7bb86312007-12-11 09:25:06 -05002090 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002091 struct extent_buffer *lower;
2092 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002093 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002094 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002095 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002096
2097 BUG_ON(path->nodes[level]);
2098 BUG_ON(path->nodes[level-1] != root->node);
2099
Chris Mason7bb86312007-12-11 09:25:06 -05002100 lower = path->nodes[level-1];
2101 if (level == 1)
2102 btrfs_item_key(lower, &lower_key, 0);
2103 else
2104 btrfs_node_key(lower, &lower_key, 0);
2105
Zheng Yan31840ae2008-09-23 13:14:14 -04002106 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2107 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002108 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002109 if (IS_ERR(c))
2110 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002111
Chris Mason5f39d392007-10-15 16:14:19 -04002112 memset_extent_buffer(c, 0, 0, root->nodesize);
2113 btrfs_set_header_nritems(c, 1);
2114 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002115 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002116 btrfs_set_header_generation(c, trans->transid);
2117 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002118
Chris Mason5f39d392007-10-15 16:14:19 -04002119 write_extent_buffer(c, root->fs_info->fsid,
2120 (unsigned long)btrfs_header_fsid(c),
2121 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002122
2123 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2124 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2125 BTRFS_UUID_SIZE);
2126
Chris Mason5f39d392007-10-15 16:14:19 -04002127 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002128 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002129 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002130 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002131
2132 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002133
2134 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002135
Chris Mason925baed2008-06-25 16:01:30 -04002136 spin_lock(&root->node_lock);
2137 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002138 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002139 spin_unlock(&root->node_lock);
2140
Zheng Yan31840ae2008-09-23 13:14:14 -04002141 ret = btrfs_update_extent_ref(trans, root, lower->start,
2142 lower->start, c->start,
2143 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002144 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002145 BUG_ON(ret);
2146
Chris Mason925baed2008-06-25 16:01:30 -04002147 /* the super has an extra ref to root->node */
2148 free_extent_buffer(old);
2149
Chris Mason0b86a832008-03-24 15:01:56 -04002150 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002151 extent_buffer_get(c);
2152 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002153 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002154 path->slots[level] = 0;
2155 return 0;
2156}
2157
Chris Mason74123bd2007-02-02 11:05:29 -05002158/*
2159 * worker function to insert a single pointer in a node.
2160 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002161 *
Chris Mason74123bd2007-02-02 11:05:29 -05002162 * slot and level indicate where you want the key to go, and
2163 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002164 *
2165 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002166 */
Chris Masone089f052007-03-16 16:20:31 -04002167static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2168 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002169 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002170{
Chris Mason5f39d392007-10-15 16:14:19 -04002171 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002172 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002173
2174 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002175 lower = path->nodes[level];
2176 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002177 if (slot > nritems)
2178 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002179 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002180 BUG();
2181 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002182 memmove_extent_buffer(lower,
2183 btrfs_node_key_ptr_offset(slot + 1),
2184 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002185 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002186 }
Chris Mason5f39d392007-10-15 16:14:19 -04002187 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002188 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002189 WARN_ON(trans->transid == 0);
2190 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002191 btrfs_set_header_nritems(lower, nritems + 1);
2192 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002193 return 0;
2194}
2195
Chris Mason97571fd2007-02-24 13:39:08 -05002196/*
2197 * split the node at the specified level in path in two.
2198 * The path is corrected to point to the appropriate node after the split
2199 *
2200 * Before splitting this tries to make some room in the node by pushing
2201 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002202 *
2203 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002204 */
Chris Masone02119d2008-09-05 16:13:11 -04002205static noinline int split_node(struct btrfs_trans_handle *trans,
2206 struct btrfs_root *root,
2207 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002208{
Chris Mason5f39d392007-10-15 16:14:19 -04002209 struct extent_buffer *c;
2210 struct extent_buffer *split;
2211 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002212 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002213 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002214 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002215 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002216
Chris Mason5f39d392007-10-15 16:14:19 -04002217 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002218 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002219 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002220 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002221 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002222 if (ret)
2223 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002224 } else {
2225 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002226 c = path->nodes[level];
2227 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002228 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002229 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002230 if (ret < 0)
2231 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002232 }
Chris Masone66f7092007-04-20 13:16:02 -04002233
Chris Mason5f39d392007-10-15 16:14:19 -04002234 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002235
Chris Mason925baed2008-06-25 16:01:30 -04002236 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002237 path->nodes[level + 1]->start,
2238 root->root_key.objectid,
2239 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002240 if (IS_ERR(split))
2241 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002242
Chris Mason5f39d392007-10-15 16:14:19 -04002243 btrfs_set_header_flags(split, btrfs_header_flags(c));
2244 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002245 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002246 btrfs_set_header_generation(split, trans->transid);
2247 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002248 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002249 write_extent_buffer(split, root->fs_info->fsid,
2250 (unsigned long)btrfs_header_fsid(split),
2251 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002252 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2253 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2254 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002255
Chris Mason7518a232007-03-12 12:01:18 -04002256 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002257
2258 copy_extent_buffer(split, c,
2259 btrfs_node_key_ptr_offset(0),
2260 btrfs_node_key_ptr_offset(mid),
2261 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2262 btrfs_set_header_nritems(split, c_nritems - mid);
2263 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002264 ret = 0;
2265
Chris Mason5f39d392007-10-15 16:14:19 -04002266 btrfs_mark_buffer_dirty(c);
2267 btrfs_mark_buffer_dirty(split);
2268
2269 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002270 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002271 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002272 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002273 if (wret)
2274 ret = wret;
2275
Zheng Yan31840ae2008-09-23 13:14:14 -04002276 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2277 BUG_ON(ret);
2278
Chris Mason5de08d72007-02-24 06:24:44 -05002279 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002280 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002281 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002282 free_extent_buffer(c);
2283 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002284 path->slots[level + 1] += 1;
2285 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002286 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002287 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002288 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002289 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002290}
2291
Chris Mason74123bd2007-02-02 11:05:29 -05002292/*
2293 * how many bytes are required to store the items in a leaf. start
2294 * and nr indicate which items in the leaf to check. This totals up the
2295 * space used both by the item structs and the item data
2296 */
Chris Mason5f39d392007-10-15 16:14:19 -04002297static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002298{
2299 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002300 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002301 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002302
2303 if (!nr)
2304 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002305 data_len = btrfs_item_end_nr(l, start);
2306 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002307 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002308 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002309 return data_len;
2310}
2311
Chris Mason74123bd2007-02-02 11:05:29 -05002312/*
Chris Masond4dbff92007-04-04 14:08:15 -04002313 * The space between the end of the leaf items and
2314 * the start of the leaf data. IOW, how much room
2315 * the leaf has left for both items and data
2316 */
Chris Masond3977122009-01-05 21:25:51 -05002317noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002318 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002319{
Chris Mason5f39d392007-10-15 16:14:19 -04002320 int nritems = btrfs_header_nritems(leaf);
2321 int ret;
2322 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2323 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002324 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2325 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002326 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002327 leaf_space_used(leaf, 0, nritems), nritems);
2328 }
2329 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002330}
2331
2332/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002333 * push some data in the path leaf to the right, trying to free up at
2334 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002335 *
2336 * returns 1 if the push failed because the other node didn't have enough
2337 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 */
Chris Masone089f052007-03-16 16:20:31 -04002339static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002340 *root, struct btrfs_path *path, int data_size,
2341 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002342{
Chris Mason5f39d392007-10-15 16:14:19 -04002343 struct extent_buffer *left = path->nodes[0];
2344 struct extent_buffer *right;
2345 struct extent_buffer *upper;
2346 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002348 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002349 int free_space;
2350 int push_space = 0;
2351 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002352 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002353 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002354 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002355 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002356 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002357 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002358 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002359
2360 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002361 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002362 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002363
Chris Mason00ec4c52007-02-24 12:47:20 -05002364 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002365 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002366 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Chris Masonb9447ef82009-03-09 11:45:38 -04002368 btrfs_assert_tree_locked(path->nodes[1]);
Chris Masona2135012008-06-25 16:01:30 -04002369
Chris Masonca7a79a2008-05-12 12:59:19 -04002370 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002371 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002372 btrfs_set_lock_blocking(right);
2373
Chris Mason123abc82007-03-14 14:14:43 -04002374 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002375 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002376 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002377
Chris Mason5f39d392007-10-15 16:14:19 -04002378 /* cow and double check */
2379 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002380 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002381 if (ret)
2382 goto out_unlock;
2383
Chris Mason5f39d392007-10-15 16:14:19 -04002384 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002385 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002386 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002387
2388 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002389 if (left_nritems == 0)
2390 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002391
Chris Mason34a38212007-11-07 13:31:03 -05002392 if (empty)
2393 nr = 0;
2394 else
2395 nr = 1;
2396
Zheng Yan31840ae2008-09-23 13:14:14 -04002397 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002398 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002399
Chris Mason34a38212007-11-07 13:31:03 -05002400 i = left_nritems - 1;
2401 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002402 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002403
Zheng Yan31840ae2008-09-23 13:14:14 -04002404 if (!empty && push_items > 0) {
2405 if (path->slots[0] > i)
2406 break;
2407 if (path->slots[0] == i) {
2408 int space = btrfs_leaf_free_space(root, left);
2409 if (space + push_space * 2 > free_space)
2410 break;
2411 }
2412 }
2413
Chris Mason00ec4c52007-02-24 12:47:20 -05002414 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002415 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002416
2417 if (!left->map_token) {
2418 map_extent_buffer(left, (unsigned long)item,
2419 sizeof(struct btrfs_item),
2420 &left->map_token, &left->kaddr,
2421 &left->map_start, &left->map_len,
2422 KM_USER1);
2423 }
2424
2425 this_item_size = btrfs_item_size(left, item);
2426 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002427 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002428
Chris Mason00ec4c52007-02-24 12:47:20 -05002429 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002430 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002431 if (i == 0)
2432 break;
2433 i--;
Chris Masondb945352007-10-15 16:15:53 -04002434 }
2435 if (left->map_token) {
2436 unmap_extent_buffer(left, left->map_token, KM_USER1);
2437 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002438 }
Chris Mason5f39d392007-10-15 16:14:19 -04002439
Chris Mason925baed2008-06-25 16:01:30 -04002440 if (push_items == 0)
2441 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002442
Chris Mason34a38212007-11-07 13:31:03 -05002443 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002444 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002445
Chris Mason00ec4c52007-02-24 12:47:20 -05002446 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002447 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002448
Chris Mason5f39d392007-10-15 16:14:19 -04002449 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002450 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002451
Chris Mason00ec4c52007-02-24 12:47:20 -05002452 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002453 data_end = leaf_data_end(root, right);
2454 memmove_extent_buffer(right,
2455 btrfs_leaf_data(right) + data_end - push_space,
2456 btrfs_leaf_data(right) + data_end,
2457 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2458
Chris Mason00ec4c52007-02-24 12:47:20 -05002459 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002460 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002461 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2462 btrfs_leaf_data(left) + leaf_data_end(root, left),
2463 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002464
2465 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2466 btrfs_item_nr_offset(0),
2467 right_nritems * sizeof(struct btrfs_item));
2468
Chris Mason00ec4c52007-02-24 12:47:20 -05002469 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002470 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2471 btrfs_item_nr_offset(left_nritems - push_items),
2472 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002473
2474 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002475 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002476 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002477 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002478 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002479 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002480 if (!right->map_token) {
2481 map_extent_buffer(right, (unsigned long)item,
2482 sizeof(struct btrfs_item),
2483 &right->map_token, &right->kaddr,
2484 &right->map_start, &right->map_len,
2485 KM_USER1);
2486 }
2487 push_space -= btrfs_item_size(right, item);
2488 btrfs_set_item_offset(right, item, push_space);
2489 }
2490
2491 if (right->map_token) {
2492 unmap_extent_buffer(right, right->map_token, KM_USER1);
2493 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002494 }
Chris Mason7518a232007-03-12 12:01:18 -04002495 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002496 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002497
Chris Mason34a38212007-11-07 13:31:03 -05002498 if (left_nritems)
2499 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002500 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002501
Zheng Yan31840ae2008-09-23 13:14:14 -04002502 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2503 BUG_ON(ret);
2504
Chris Mason5f39d392007-10-15 16:14:19 -04002505 btrfs_item_key(right, &disk_key, 0);
2506 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002507 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002508
Chris Mason00ec4c52007-02-24 12:47:20 -05002509 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002510 if (path->slots[0] >= left_nritems) {
2511 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002512 if (btrfs_header_nritems(path->nodes[0]) == 0)
2513 clean_tree_block(trans, root, path->nodes[0]);
2514 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002515 free_extent_buffer(path->nodes[0]);
2516 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002517 path->slots[1] += 1;
2518 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002519 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002520 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002521 }
2522 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002523
2524out_unlock:
2525 btrfs_tree_unlock(right);
2526 free_extent_buffer(right);
2527 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002528}
Chris Mason925baed2008-06-25 16:01:30 -04002529
Chris Mason00ec4c52007-02-24 12:47:20 -05002530/*
Chris Mason74123bd2007-02-02 11:05:29 -05002531 * push some data in the path leaf to the left, trying to free up at
2532 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2533 */
Chris Masone089f052007-03-16 16:20:31 -04002534static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002535 *root, struct btrfs_path *path, int data_size,
2536 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002537{
Chris Mason5f39d392007-10-15 16:14:19 -04002538 struct btrfs_disk_key disk_key;
2539 struct extent_buffer *right = path->nodes[0];
2540 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002541 int slot;
2542 int i;
2543 int free_space;
2544 int push_space = 0;
2545 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002546 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002547 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002548 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002549 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002550 int ret = 0;
2551 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002552 u32 this_item_size;
2553 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554
2555 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002556 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002558 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002560
Chris Mason3685f792007-10-19 09:23:27 -04002561 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002562 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002563 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002564
Chris Masonb9447ef82009-03-09 11:45:38 -04002565 btrfs_assert_tree_locked(path->nodes[1]);
Chris Masona2135012008-06-25 16:01:30 -04002566
Chris Masonca7a79a2008-05-12 12:59:19 -04002567 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002568 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002569 btrfs_set_lock_blocking(left);
2570
Chris Mason123abc82007-03-14 14:14:43 -04002571 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002572 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002573 ret = 1;
2574 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002575 }
Chris Mason02217ed2007-03-02 16:08:05 -05002576
2577 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002578 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002579 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002580 if (ret) {
2581 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002582 ret = 1;
2583 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002584 }
Chris Mason3685f792007-10-19 09:23:27 -04002585
Chris Mason123abc82007-03-14 14:14:43 -04002586 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002587 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002588 ret = 1;
2589 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002590 }
2591
Chris Mason34a38212007-11-07 13:31:03 -05002592 if (empty)
2593 nr = right_nritems;
2594 else
2595 nr = right_nritems - 1;
2596
2597 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002598 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002599 if (!right->map_token) {
2600 map_extent_buffer(right, (unsigned long)item,
2601 sizeof(struct btrfs_item),
2602 &right->map_token, &right->kaddr,
2603 &right->map_start, &right->map_len,
2604 KM_USER1);
2605 }
2606
Zheng Yan31840ae2008-09-23 13:14:14 -04002607 if (!empty && push_items > 0) {
2608 if (path->slots[0] < i)
2609 break;
2610 if (path->slots[0] == i) {
2611 int space = btrfs_leaf_free_space(root, right);
2612 if (space + push_space * 2 > free_space)
2613 break;
2614 }
2615 }
2616
Chris Masonbe0e5c02007-01-26 15:51:26 -05002617 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002618 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002619
2620 this_item_size = btrfs_item_size(right, item);
2621 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002622 break;
Chris Masondb945352007-10-15 16:15:53 -04002623
Chris Masonbe0e5c02007-01-26 15:51:26 -05002624 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002625 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002626 }
Chris Masondb945352007-10-15 16:15:53 -04002627
2628 if (right->map_token) {
2629 unmap_extent_buffer(right, right->map_token, KM_USER1);
2630 right->map_token = NULL;
2631 }
2632
Chris Masonbe0e5c02007-01-26 15:51:26 -05002633 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002634 ret = 1;
2635 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002636 }
Chris Mason34a38212007-11-07 13:31:03 -05002637 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002638 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002639
Chris Masonbe0e5c02007-01-26 15:51:26 -05002640 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002641 copy_extent_buffer(left, right,
2642 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2643 btrfs_item_nr_offset(0),
2644 push_items * sizeof(struct btrfs_item));
2645
Chris Mason123abc82007-03-14 14:14:43 -04002646 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002647 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002648
2649 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002650 leaf_data_end(root, left) - push_space,
2651 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002652 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002653 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002654 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002655 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002656
Chris Masondb945352007-10-15 16:15:53 -04002657 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002658 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002659 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002660
Chris Mason5f39d392007-10-15 16:14:19 -04002661 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002662 if (!left->map_token) {
2663 map_extent_buffer(left, (unsigned long)item,
2664 sizeof(struct btrfs_item),
2665 &left->map_token, &left->kaddr,
2666 &left->map_start, &left->map_len,
2667 KM_USER1);
2668 }
2669
Chris Mason5f39d392007-10-15 16:14:19 -04002670 ioff = btrfs_item_offset(left, item);
2671 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002672 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002673 }
Chris Mason5f39d392007-10-15 16:14:19 -04002674 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002675 if (left->map_token) {
2676 unmap_extent_buffer(left, left->map_token, KM_USER1);
2677 left->map_token = NULL;
2678 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002679
2680 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002681 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002682 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2683 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002684 WARN_ON(1);
2685 }
Chris Mason5f39d392007-10-15 16:14:19 -04002686
Chris Mason34a38212007-11-07 13:31:03 -05002687 if (push_items < right_nritems) {
2688 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2689 leaf_data_end(root, right);
2690 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2691 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2692 btrfs_leaf_data(right) +
2693 leaf_data_end(root, right), push_space);
2694
2695 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002696 btrfs_item_nr_offset(push_items),
2697 (btrfs_header_nritems(right) - push_items) *
2698 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002699 }
Yaneef1c492007-11-26 10:58:13 -05002700 right_nritems -= push_items;
2701 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002702 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002703 for (i = 0; i < right_nritems; i++) {
2704 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002705
2706 if (!right->map_token) {
2707 map_extent_buffer(right, (unsigned long)item,
2708 sizeof(struct btrfs_item),
2709 &right->map_token, &right->kaddr,
2710 &right->map_start, &right->map_len,
2711 KM_USER1);
2712 }
2713
2714 push_space = push_space - btrfs_item_size(right, item);
2715 btrfs_set_item_offset(right, item, push_space);
2716 }
2717 if (right->map_token) {
2718 unmap_extent_buffer(right, right->map_token, KM_USER1);
2719 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002720 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002721
Chris Mason5f39d392007-10-15 16:14:19 -04002722 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002723 if (right_nritems)
2724 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002725
Zheng Yan31840ae2008-09-23 13:14:14 -04002726 ret = btrfs_update_ref(trans, root, right, left,
2727 old_left_nritems, push_items);
2728 BUG_ON(ret);
2729
Chris Mason5f39d392007-10-15 16:14:19 -04002730 btrfs_item_key(right, &disk_key, 0);
2731 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002732 if (wret)
2733 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002734
2735 /* then fixup the leaf pointer in the path */
2736 if (path->slots[0] < push_items) {
2737 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002738 if (btrfs_header_nritems(path->nodes[0]) == 0)
2739 clean_tree_block(trans, root, path->nodes[0]);
2740 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002741 free_extent_buffer(path->nodes[0]);
2742 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002743 path->slots[1] -= 1;
2744 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002745 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002746 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002747 path->slots[0] -= push_items;
2748 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002749 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002750 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002751out:
2752 btrfs_tree_unlock(left);
2753 free_extent_buffer(left);
2754 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002755}
2756
Chris Mason74123bd2007-02-02 11:05:29 -05002757/*
2758 * split the path's leaf in two, making sure there is at least data_size
2759 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002760 *
2761 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002762 */
Chris Masone02119d2008-09-05 16:13:11 -04002763static noinline int split_leaf(struct btrfs_trans_handle *trans,
2764 struct btrfs_root *root,
2765 struct btrfs_key *ins_key,
2766 struct btrfs_path *path, int data_size,
2767 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002768{
Chris Mason5f39d392007-10-15 16:14:19 -04002769 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002770 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002771 int mid;
2772 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002773 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002774 int data_copy_size;
2775 int rt_data_off;
2776 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002777 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002778 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002779 int double_split;
2780 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002781 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002782
Chris Mason40689472007-03-17 14:29:23 -04002783 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002784 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002785 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002786 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002787 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002788 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002789 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002790 if (wret < 0)
2791 return wret;
2792 }
2793 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002794
Chris Mason3685f792007-10-19 09:23:27 -04002795 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002796 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002797 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002798 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002799
Chris Mason5c680ed2007-02-22 11:39:13 -05002800 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002801 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002802 if (ret)
2803 return ret;
2804 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002805again:
2806 double_split = 0;
2807 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002808 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002809 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002810 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002811
Chris Mason925baed2008-06-25 16:01:30 -04002812 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002813 path->nodes[1]->start,
2814 root->root_key.objectid,
2815 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002816 if (IS_ERR(right)) {
2817 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002818 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002819 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002820
Chris Mason5f39d392007-10-15 16:14:19 -04002821 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002822 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 btrfs_set_header_generation(right, trans->transid);
2824 btrfs_set_header_owner(right, root->root_key.objectid);
2825 btrfs_set_header_level(right, 0);
2826 write_extent_buffer(right, root->fs_info->fsid,
2827 (unsigned long)btrfs_header_fsid(right),
2828 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002829
2830 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2831 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2832 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002833 if (mid <= slot) {
2834 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002835 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002836 BTRFS_LEAF_DATA_SIZE(root)) {
2837 if (slot >= nritems) {
2838 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002839 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002840 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002841 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002842 path->slots[1] + 1, 1);
2843 if (wret)
2844 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002845
2846 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002847 free_extent_buffer(path->nodes[0]);
2848 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002849 path->slots[0] = 0;
2850 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002851 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002852 return ret;
2853 }
2854 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002855 if (mid != nritems &&
2856 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002857 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002858 double_split = 1;
2859 }
Chris Masond4dbff92007-04-04 14:08:15 -04002860 }
2861 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002862 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002863 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002864 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002865 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002866 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002867 wret = insert_ptr(trans, root, path,
2868 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002869 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002870 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002871 if (wret)
2872 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002873 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002874 free_extent_buffer(path->nodes[0]);
2875 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002876 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002877 if (path->slots[1] == 0) {
2878 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002879 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002880 if (wret)
2881 ret = wret;
2882 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002883 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002884 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002885 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002886 mid = 1;
2887 } else {
2888 mid = slot;
2889 if (mid != nritems &&
2890 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002891 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002892 double_split = 1;
2893 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002894 }
Chris Masond4dbff92007-04-04 14:08:15 -04002895 }
2896 }
Chris Mason5f39d392007-10-15 16:14:19 -04002897 nritems = nritems - mid;
2898 btrfs_set_header_nritems(right, nritems);
2899 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2900
2901 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2902 btrfs_item_nr_offset(mid),
2903 nritems * sizeof(struct btrfs_item));
2904
2905 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002906 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2907 data_copy_size, btrfs_leaf_data(l) +
2908 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002909
Chris Mason5f39d392007-10-15 16:14:19 -04002910 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2911 btrfs_item_end_nr(l, mid);
2912
2913 for (i = 0; i < nritems; i++) {
2914 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002915 u32 ioff;
2916
2917 if (!right->map_token) {
2918 map_extent_buffer(right, (unsigned long)item,
2919 sizeof(struct btrfs_item),
2920 &right->map_token, &right->kaddr,
2921 &right->map_start, &right->map_len,
2922 KM_USER1);
2923 }
2924
2925 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002926 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002927 }
Chris Mason74123bd2007-02-02 11:05:29 -05002928
Chris Masondb945352007-10-15 16:15:53 -04002929 if (right->map_token) {
2930 unmap_extent_buffer(right, right->map_token, KM_USER1);
2931 right->map_token = NULL;
2932 }
2933
Chris Mason5f39d392007-10-15 16:14:19 -04002934 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002935 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002936 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002937 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2938 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002939 if (wret)
2940 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002941
2942 btrfs_mark_buffer_dirty(right);
2943 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002944 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002945
Zheng Yan31840ae2008-09-23 13:14:14 -04002946 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2947 BUG_ON(ret);
2948
Chris Masonbe0e5c02007-01-26 15:51:26 -05002949 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002950 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002951 free_extent_buffer(path->nodes[0]);
2952 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002953 path->slots[0] -= mid;
2954 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002955 } else {
2956 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002957 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002958 }
Chris Mason5f39d392007-10-15 16:14:19 -04002959
Chris Masoneb60cea2007-02-02 09:18:22 -05002960 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002961
Chris Masoncc0c5532007-10-25 15:42:57 -04002962 if (double_split) {
2963 BUG_ON(num_doubles != 0);
2964 num_doubles++;
2965 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002966 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002967 return ret;
2968}
2969
Chris Masond352ac62008-09-29 15:18:18 -04002970/*
Chris Mason459931e2008-12-10 09:10:46 -05002971 * This function splits a single item into two items,
2972 * giving 'new_key' to the new item and splitting the
2973 * old one at split_offset (from the start of the item).
2974 *
2975 * The path may be released by this operation. After
2976 * the split, the path is pointing to the old item. The
2977 * new item is going to be in the same node as the old one.
2978 *
2979 * Note, the item being split must be smaller enough to live alone on
2980 * a tree block with room for one extra struct btrfs_item
2981 *
2982 * This allows us to split the item in place, keeping a lock on the
2983 * leaf the entire time.
2984 */
2985int btrfs_split_item(struct btrfs_trans_handle *trans,
2986 struct btrfs_root *root,
2987 struct btrfs_path *path,
2988 struct btrfs_key *new_key,
2989 unsigned long split_offset)
2990{
2991 u32 item_size;
2992 struct extent_buffer *leaf;
2993 struct btrfs_key orig_key;
2994 struct btrfs_item *item;
2995 struct btrfs_item *new_item;
2996 int ret = 0;
2997 int slot;
2998 u32 nritems;
2999 u32 orig_offset;
3000 struct btrfs_disk_key disk_key;
3001 char *buf;
3002
3003 leaf = path->nodes[0];
3004 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
3005 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
3006 goto split;
3007
3008 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3009 btrfs_release_path(root, path);
3010
3011 path->search_for_split = 1;
3012 path->keep_locks = 1;
3013
3014 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
3015 path->search_for_split = 0;
3016
3017 /* if our item isn't there or got smaller, return now */
3018 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3019 path->slots[0])) {
3020 path->keep_locks = 0;
3021 return -EAGAIN;
3022 }
3023
Yan Zheng87b29b22008-12-17 10:21:48 -05003024 ret = split_leaf(trans, root, &orig_key, path,
3025 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003026 path->keep_locks = 0;
3027 BUG_ON(ret);
3028
Chris Masonb4ce94d2009-02-04 09:25:08 -05003029 /*
3030 * make sure any changes to the path from split_leaf leave it
3031 * in a blocking state
3032 */
3033 btrfs_set_path_blocking(path);
3034
Chris Mason459931e2008-12-10 09:10:46 -05003035 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05003036 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003037
3038split:
3039 item = btrfs_item_nr(leaf, path->slots[0]);
3040 orig_offset = btrfs_item_offset(leaf, item);
3041 item_size = btrfs_item_size(leaf, item);
3042
3043
3044 buf = kmalloc(item_size, GFP_NOFS);
3045 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3046 path->slots[0]), item_size);
3047 slot = path->slots[0] + 1;
3048 leaf = path->nodes[0];
3049
3050 nritems = btrfs_header_nritems(leaf);
3051
3052 if (slot != nritems) {
3053 /* shift the items */
3054 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3055 btrfs_item_nr_offset(slot),
3056 (nritems - slot) * sizeof(struct btrfs_item));
3057
3058 }
3059
3060 btrfs_cpu_key_to_disk(&disk_key, new_key);
3061 btrfs_set_item_key(leaf, &disk_key, slot);
3062
3063 new_item = btrfs_item_nr(leaf, slot);
3064
3065 btrfs_set_item_offset(leaf, new_item, orig_offset);
3066 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3067
3068 btrfs_set_item_offset(leaf, item,
3069 orig_offset + item_size - split_offset);
3070 btrfs_set_item_size(leaf, item, split_offset);
3071
3072 btrfs_set_header_nritems(leaf, nritems + 1);
3073
3074 /* write the data for the start of the original item */
3075 write_extent_buffer(leaf, buf,
3076 btrfs_item_ptr_offset(leaf, path->slots[0]),
3077 split_offset);
3078
3079 /* write the data for the new item */
3080 write_extent_buffer(leaf, buf + split_offset,
3081 btrfs_item_ptr_offset(leaf, slot),
3082 item_size - split_offset);
3083 btrfs_mark_buffer_dirty(leaf);
3084
3085 ret = 0;
3086 if (btrfs_leaf_free_space(root, leaf) < 0) {
3087 btrfs_print_leaf(root, leaf);
3088 BUG();
3089 }
3090 kfree(buf);
3091 return ret;
3092}
3093
3094/*
Chris Masond352ac62008-09-29 15:18:18 -04003095 * make the item pointed to by the path smaller. new_size indicates
3096 * how small to make it, and from_end tells us if we just chop bytes
3097 * off the end of the item or if we shift the item to chop bytes off
3098 * the front.
3099 */
Chris Masonb18c6682007-04-17 13:26:50 -04003100int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3101 struct btrfs_root *root,
3102 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003103 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003104{
3105 int ret = 0;
3106 int slot;
3107 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003108 struct extent_buffer *leaf;
3109 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003110 u32 nritems;
3111 unsigned int data_end;
3112 unsigned int old_data_start;
3113 unsigned int old_size;
3114 unsigned int size_diff;
3115 int i;
3116
3117 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003118 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003119 slot = path->slots[0];
3120
3121 old_size = btrfs_item_size_nr(leaf, slot);
3122 if (old_size == new_size)
3123 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003124
Chris Mason5f39d392007-10-15 16:14:19 -04003125 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003126 data_end = leaf_data_end(root, leaf);
3127
Chris Mason5f39d392007-10-15 16:14:19 -04003128 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003129
Chris Masonb18c6682007-04-17 13:26:50 -04003130 size_diff = old_size - new_size;
3131
3132 BUG_ON(slot < 0);
3133 BUG_ON(slot >= nritems);
3134
3135 /*
3136 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3137 */
3138 /* first correct the data pointers */
3139 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003140 u32 ioff;
3141 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003142
3143 if (!leaf->map_token) {
3144 map_extent_buffer(leaf, (unsigned long)item,
3145 sizeof(struct btrfs_item),
3146 &leaf->map_token, &leaf->kaddr,
3147 &leaf->map_start, &leaf->map_len,
3148 KM_USER1);
3149 }
3150
Chris Mason5f39d392007-10-15 16:14:19 -04003151 ioff = btrfs_item_offset(leaf, item);
3152 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003153 }
Chris Masondb945352007-10-15 16:15:53 -04003154
3155 if (leaf->map_token) {
3156 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3157 leaf->map_token = NULL;
3158 }
3159
Chris Masonb18c6682007-04-17 13:26:50 -04003160 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003161 if (from_end) {
3162 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3163 data_end + size_diff, btrfs_leaf_data(leaf) +
3164 data_end, old_data_start + new_size - data_end);
3165 } else {
3166 struct btrfs_disk_key disk_key;
3167 u64 offset;
3168
3169 btrfs_item_key(leaf, &disk_key, slot);
3170
3171 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3172 unsigned long ptr;
3173 struct btrfs_file_extent_item *fi;
3174
3175 fi = btrfs_item_ptr(leaf, slot,
3176 struct btrfs_file_extent_item);
3177 fi = (struct btrfs_file_extent_item *)(
3178 (unsigned long)fi - size_diff);
3179
3180 if (btrfs_file_extent_type(leaf, fi) ==
3181 BTRFS_FILE_EXTENT_INLINE) {
3182 ptr = btrfs_item_ptr_offset(leaf, slot);
3183 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003184 (unsigned long)fi,
3185 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003186 disk_bytenr));
3187 }
3188 }
3189
3190 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3191 data_end + size_diff, btrfs_leaf_data(leaf) +
3192 data_end, old_data_start - data_end);
3193
3194 offset = btrfs_disk_key_offset(&disk_key);
3195 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3196 btrfs_set_item_key(leaf, &disk_key, slot);
3197 if (slot == 0)
3198 fixup_low_keys(trans, root, path, &disk_key, 1);
3199 }
Chris Mason5f39d392007-10-15 16:14:19 -04003200
3201 item = btrfs_item_nr(leaf, slot);
3202 btrfs_set_item_size(leaf, item, new_size);
3203 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003204
3205 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003206 if (btrfs_leaf_free_space(root, leaf) < 0) {
3207 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003208 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003209 }
Chris Masonb18c6682007-04-17 13:26:50 -04003210 return ret;
3211}
3212
Chris Masond352ac62008-09-29 15:18:18 -04003213/*
3214 * make the item pointed to by the path bigger, data_size is the new size.
3215 */
Chris Mason5f39d392007-10-15 16:14:19 -04003216int btrfs_extend_item(struct btrfs_trans_handle *trans,
3217 struct btrfs_root *root, struct btrfs_path *path,
3218 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003219{
3220 int ret = 0;
3221 int slot;
3222 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003223 struct extent_buffer *leaf;
3224 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003225 u32 nritems;
3226 unsigned int data_end;
3227 unsigned int old_data;
3228 unsigned int old_size;
3229 int i;
3230
3231 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003232 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003233
Chris Mason5f39d392007-10-15 16:14:19 -04003234 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003235 data_end = leaf_data_end(root, leaf);
3236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3238 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003239 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003240 }
Chris Mason6567e832007-04-16 09:22:45 -04003241 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003242 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003243
3244 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003245 if (slot >= nritems) {
3246 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003247 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3248 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003249 BUG_ON(1);
3250 }
Chris Mason6567e832007-04-16 09:22:45 -04003251
3252 /*
3253 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3254 */
3255 /* first correct the data pointers */
3256 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003257 u32 ioff;
3258 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003259
3260 if (!leaf->map_token) {
3261 map_extent_buffer(leaf, (unsigned long)item,
3262 sizeof(struct btrfs_item),
3263 &leaf->map_token, &leaf->kaddr,
3264 &leaf->map_start, &leaf->map_len,
3265 KM_USER1);
3266 }
Chris Mason5f39d392007-10-15 16:14:19 -04003267 ioff = btrfs_item_offset(leaf, item);
3268 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003269 }
Chris Mason5f39d392007-10-15 16:14:19 -04003270
Chris Masondb945352007-10-15 16:15:53 -04003271 if (leaf->map_token) {
3272 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3273 leaf->map_token = NULL;
3274 }
3275
Chris Mason6567e832007-04-16 09:22:45 -04003276 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003277 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003278 data_end - data_size, btrfs_leaf_data(leaf) +
3279 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003280
Chris Mason6567e832007-04-16 09:22:45 -04003281 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003282 old_size = btrfs_item_size_nr(leaf, slot);
3283 item = btrfs_item_nr(leaf, slot);
3284 btrfs_set_item_size(leaf, item, old_size + data_size);
3285 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003286
3287 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003288 if (btrfs_leaf_free_space(root, leaf) < 0) {
3289 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003290 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003291 }
Chris Mason6567e832007-04-16 09:22:45 -04003292 return ret;
3293}
3294
Chris Mason74123bd2007-02-02 11:05:29 -05003295/*
Chris Masond352ac62008-09-29 15:18:18 -04003296 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003297 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003298 * Returns the number of keys that were inserted.
3299 */
3300int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3301 struct btrfs_root *root,
3302 struct btrfs_path *path,
3303 struct btrfs_key *cpu_key, u32 *data_size,
3304 int nr)
3305{
3306 struct extent_buffer *leaf;
3307 struct btrfs_item *item;
3308 int ret = 0;
3309 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003310 int i;
3311 u32 nritems;
3312 u32 total_data = 0;
3313 u32 total_size = 0;
3314 unsigned int data_end;
3315 struct btrfs_disk_key disk_key;
3316 struct btrfs_key found_key;
3317
Yan Zheng87b29b22008-12-17 10:21:48 -05003318 for (i = 0; i < nr; i++) {
3319 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3320 BTRFS_LEAF_DATA_SIZE(root)) {
3321 break;
3322 nr = i;
3323 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003324 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003325 total_size += data_size[i] + sizeof(struct btrfs_item);
3326 }
3327 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003328
Josef Bacikf3465ca2008-11-12 14:19:50 -05003329 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3330 if (ret == 0)
3331 return -EEXIST;
3332 if (ret < 0)
3333 goto out;
3334
Josef Bacikf3465ca2008-11-12 14:19:50 -05003335 leaf = path->nodes[0];
3336
3337 nritems = btrfs_header_nritems(leaf);
3338 data_end = leaf_data_end(root, leaf);
3339
3340 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3341 for (i = nr; i >= 0; i--) {
3342 total_data -= data_size[i];
3343 total_size -= data_size[i] + sizeof(struct btrfs_item);
3344 if (total_size < btrfs_leaf_free_space(root, leaf))
3345 break;
3346 }
3347 nr = i;
3348 }
3349
3350 slot = path->slots[0];
3351 BUG_ON(slot < 0);
3352
3353 if (slot != nritems) {
3354 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3355
3356 item = btrfs_item_nr(leaf, slot);
3357 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3358
3359 /* figure out how many keys we can insert in here */
3360 total_data = data_size[0];
3361 for (i = 1; i < nr; i++) {
3362 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3363 break;
3364 total_data += data_size[i];
3365 }
3366 nr = i;
3367
3368 if (old_data < data_end) {
3369 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003370 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003371 slot, old_data, data_end);
3372 BUG_ON(1);
3373 }
3374 /*
3375 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3376 */
3377 /* first correct the data pointers */
3378 WARN_ON(leaf->map_token);
3379 for (i = slot; i < nritems; i++) {
3380 u32 ioff;
3381
3382 item = btrfs_item_nr(leaf, i);
3383 if (!leaf->map_token) {
3384 map_extent_buffer(leaf, (unsigned long)item,
3385 sizeof(struct btrfs_item),
3386 &leaf->map_token, &leaf->kaddr,
3387 &leaf->map_start, &leaf->map_len,
3388 KM_USER1);
3389 }
3390
3391 ioff = btrfs_item_offset(leaf, item);
3392 btrfs_set_item_offset(leaf, item, ioff - total_data);
3393 }
3394 if (leaf->map_token) {
3395 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3396 leaf->map_token = NULL;
3397 }
3398
3399 /* shift the items */
3400 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3401 btrfs_item_nr_offset(slot),
3402 (nritems - slot) * sizeof(struct btrfs_item));
3403
3404 /* shift the data */
3405 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3406 data_end - total_data, btrfs_leaf_data(leaf) +
3407 data_end, old_data - data_end);
3408 data_end = old_data;
3409 } else {
3410 /*
3411 * this sucks but it has to be done, if we are inserting at
3412 * the end of the leaf only insert 1 of the items, since we
3413 * have no way of knowing whats on the next leaf and we'd have
3414 * to drop our current locks to figure it out
3415 */
3416 nr = 1;
3417 }
3418
3419 /* setup the item for the new data */
3420 for (i = 0; i < nr; i++) {
3421 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3422 btrfs_set_item_key(leaf, &disk_key, slot + i);
3423 item = btrfs_item_nr(leaf, slot + i);
3424 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3425 data_end -= data_size[i];
3426 btrfs_set_item_size(leaf, item, data_size[i]);
3427 }
3428 btrfs_set_header_nritems(leaf, nritems + nr);
3429 btrfs_mark_buffer_dirty(leaf);
3430
3431 ret = 0;
3432 if (slot == 0) {
3433 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3434 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3435 }
3436
3437 if (btrfs_leaf_free_space(root, leaf) < 0) {
3438 btrfs_print_leaf(root, leaf);
3439 BUG();
3440 }
3441out:
3442 if (!ret)
3443 ret = nr;
3444 return ret;
3445}
3446
3447/*
3448 * Given a key and some data, insert items into the tree.
3449 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003450 */
Chris Mason9c583092008-01-29 15:15:18 -05003451int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003452 struct btrfs_root *root,
3453 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003454 struct btrfs_key *cpu_key, u32 *data_size,
3455 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003456{
Chris Mason5f39d392007-10-15 16:14:19 -04003457 struct extent_buffer *leaf;
3458 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003459 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003460 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003461 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003462 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003463 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003464 u32 total_size = 0;
3465 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003466 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003467 struct btrfs_disk_key disk_key;
3468
Chris Masond3977122009-01-05 21:25:51 -05003469 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003470 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003471
Josef Bacik7b128762008-07-24 12:17:14 -04003472 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003473 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003474 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003475 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003476 if (ret < 0)
3477 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003478
Chris Mason62e27492007-03-15 12:56:47 -04003479 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003480 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003483 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003484
Chris Masonf25956c2008-09-12 15:32:53 -04003485 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003486 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003487 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003488 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003489 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003490 }
Chris Mason5f39d392007-10-15 16:14:19 -04003491
Chris Mason62e27492007-03-15 12:56:47 -04003492 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003493 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003494
Chris Masonbe0e5c02007-01-26 15:51:26 -05003495 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003496 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003497
Chris Mason5f39d392007-10-15 16:14:19 -04003498 if (old_data < data_end) {
3499 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003500 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003501 slot, old_data, data_end);
3502 BUG_ON(1);
3503 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003504 /*
3505 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3506 */
3507 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003508 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003509 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003510 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003511
Chris Mason5f39d392007-10-15 16:14:19 -04003512 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003513 if (!leaf->map_token) {
3514 map_extent_buffer(leaf, (unsigned long)item,
3515 sizeof(struct btrfs_item),
3516 &leaf->map_token, &leaf->kaddr,
3517 &leaf->map_start, &leaf->map_len,
3518 KM_USER1);
3519 }
3520
Chris Mason5f39d392007-10-15 16:14:19 -04003521 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003522 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003523 }
Chris Masondb945352007-10-15 16:15:53 -04003524 if (leaf->map_token) {
3525 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3526 leaf->map_token = NULL;
3527 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003528
3529 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003530 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003531 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003532 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003533
3534 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003535 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003536 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003537 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538 data_end = old_data;
3539 }
Chris Mason5f39d392007-10-15 16:14:19 -04003540
Chris Mason62e27492007-03-15 12:56:47 -04003541 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003542 for (i = 0; i < nr; i++) {
3543 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3544 btrfs_set_item_key(leaf, &disk_key, slot + i);
3545 item = btrfs_item_nr(leaf, slot + i);
3546 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3547 data_end -= data_size[i];
3548 btrfs_set_item_size(leaf, item, data_size[i]);
3549 }
3550 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003551 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003552
3553 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003554 if (slot == 0) {
3555 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003556 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003557 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003558
Chris Mason5f39d392007-10-15 16:14:19 -04003559 if (btrfs_leaf_free_space(root, leaf) < 0) {
3560 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003561 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003562 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003563out:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003564 btrfs_unlock_up_safe(path, 1);
Chris Mason62e27492007-03-15 12:56:47 -04003565 return ret;
3566}
3567
3568/*
3569 * Given a key and some data, insert an item into the tree.
3570 * This does all the path init required, making room in the tree if needed.
3571 */
Chris Masone089f052007-03-16 16:20:31 -04003572int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3573 *root, struct btrfs_key *cpu_key, void *data, u32
3574 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003575{
3576 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003577 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003578 struct extent_buffer *leaf;
3579 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003580
Chris Mason2c90e5d2007-04-02 10:50:19 -04003581 path = btrfs_alloc_path();
3582 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003583 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003584 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003585 leaf = path->nodes[0];
3586 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3587 write_extent_buffer(leaf, data, ptr, data_size);
3588 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003589 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003590 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003591 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003592}
3593
Chris Mason74123bd2007-02-02 11:05:29 -05003594/*
Chris Mason5de08d72007-02-24 06:24:44 -05003595 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003596 *
Chris Masond352ac62008-09-29 15:18:18 -04003597 * the tree should have been previously balanced so the deletion does not
3598 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003599 */
Chris Masone089f052007-03-16 16:20:31 -04003600static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3601 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003602{
Chris Mason5f39d392007-10-15 16:14:19 -04003603 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003604 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003605 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003606 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003607
Chris Mason5f39d392007-10-15 16:14:19 -04003608 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003609 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003610 memmove_extent_buffer(parent,
3611 btrfs_node_key_ptr_offset(slot),
3612 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003613 sizeof(struct btrfs_key_ptr) *
3614 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003615 }
Chris Mason7518a232007-03-12 12:01:18 -04003616 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003617 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003618 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003619 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003620 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003621 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003622 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003623 struct btrfs_disk_key disk_key;
3624
3625 btrfs_node_key(parent, &disk_key, 0);
3626 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003627 if (wret)
3628 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003629 }
Chris Masond6025572007-03-30 14:27:56 -04003630 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003631 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632}
3633
Chris Mason74123bd2007-02-02 11:05:29 -05003634/*
Chris Mason323ac952008-10-01 19:05:46 -04003635 * a helper function to delete the leaf pointed to by path->slots[1] and
3636 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3637 * already know it, it is faster to have them pass it down than to
3638 * read it out of the node again.
3639 *
3640 * This deletes the pointer in path->nodes[1] and frees the leaf
3641 * block extent. zero is returned if it all worked out, < 0 otherwise.
3642 *
3643 * The path must have already been setup for deleting the leaf, including
3644 * all the proper balancing. path->nodes[1] must be locked.
3645 */
3646noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root,
3648 struct btrfs_path *path, u64 bytenr)
3649{
3650 int ret;
3651 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003652 u64 parent_start = path->nodes[1]->start;
3653 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003654
3655 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3656 if (ret)
3657 return ret;
3658
Chris Mason4d081c42009-02-04 09:31:28 -05003659 /*
3660 * btrfs_free_extent is expensive, we want to make sure we
3661 * aren't holding any locks when we call it
3662 */
3663 btrfs_unlock_up_safe(path, 0);
3664
Chris Mason323ac952008-10-01 19:05:46 -04003665 ret = btrfs_free_extent(trans, root, bytenr,
3666 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003667 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003668 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003669 return ret;
3670}
3671/*
Chris Mason74123bd2007-02-02 11:05:29 -05003672 * delete the item at the leaf level in path. If that empties
3673 * the leaf, remove it from the tree
3674 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003675int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3676 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003677{
Chris Mason5f39d392007-10-15 16:14:19 -04003678 struct extent_buffer *leaf;
3679 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003680 int last_off;
3681 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003682 int ret = 0;
3683 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003684 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003685 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003686
Chris Mason5f39d392007-10-15 16:14:19 -04003687 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003688 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3689
3690 for (i = 0; i < nr; i++)
3691 dsize += btrfs_item_size_nr(leaf, slot + i);
3692
Chris Mason5f39d392007-10-15 16:14:19 -04003693 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003694
Chris Mason85e21ba2008-01-29 15:11:36 -05003695 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003696 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003697
3698 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003699 data_end + dsize,
3700 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003701 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003702
Chris Mason85e21ba2008-01-29 15:11:36 -05003703 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003704 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003705
Chris Mason5f39d392007-10-15 16:14:19 -04003706 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003707 if (!leaf->map_token) {
3708 map_extent_buffer(leaf, (unsigned long)item,
3709 sizeof(struct btrfs_item),
3710 &leaf->map_token, &leaf->kaddr,
3711 &leaf->map_start, &leaf->map_len,
3712 KM_USER1);
3713 }
Chris Mason5f39d392007-10-15 16:14:19 -04003714 ioff = btrfs_item_offset(leaf, item);
3715 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003716 }
Chris Masondb945352007-10-15 16:15:53 -04003717
3718 if (leaf->map_token) {
3719 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3720 leaf->map_token = NULL;
3721 }
3722
Chris Mason5f39d392007-10-15 16:14:19 -04003723 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003724 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003725 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003726 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003727 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003728 btrfs_set_header_nritems(leaf, nritems - nr);
3729 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003730
Chris Mason74123bd2007-02-02 11:05:29 -05003731 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003732 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003733 if (leaf == root->node) {
3734 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003735 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003736 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3737 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003738 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003739 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003740 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003741 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003742 struct btrfs_disk_key disk_key;
3743
3744 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003745 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003746 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003747 if (wret)
3748 ret = wret;
3749 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003750
Chris Mason74123bd2007-02-02 11:05:29 -05003751 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003752 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003753 /* push_leaf_left fixes the path.
3754 * make sure the path still points to our leaf
3755 * for possible call to del_ptr below
3756 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003757 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003758 extent_buffer_get(leaf);
3759
Chris Mason85e21ba2008-01-29 15:11:36 -05003760 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003761 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003762 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003763
3764 if (path->nodes[0] == leaf &&
3765 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003766 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003767 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003768 ret = wret;
3769 }
Chris Mason5f39d392007-10-15 16:14:19 -04003770
3771 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003772 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003773 ret = btrfs_del_leaf(trans, root, path,
3774 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003775 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003776 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003777 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003778 /* if we're still in the path, make sure
3779 * we're dirty. Otherwise, one of the
3780 * push_leaf functions must have already
3781 * dirtied this buffer
3782 */
3783 if (path->nodes[0] == leaf)
3784 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003785 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003786 }
Chris Masond5719762007-03-23 10:01:08 -04003787 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003788 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003789 }
3790 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003791 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003792}
3793
Chris Mason97571fd2007-02-24 13:39:08 -05003794/*
Chris Mason925baed2008-06-25 16:01:30 -04003795 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003796 * returns 0 if it found something or 1 if there are no lesser leaves.
3797 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003798 *
3799 * This may release the path, and so you may lose any locks held at the
3800 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003801 */
3802int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3803{
Chris Mason925baed2008-06-25 16:01:30 -04003804 struct btrfs_key key;
3805 struct btrfs_disk_key found_key;
3806 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003807
Chris Mason925baed2008-06-25 16:01:30 -04003808 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003809
Chris Mason925baed2008-06-25 16:01:30 -04003810 if (key.offset > 0)
3811 key.offset--;
3812 else if (key.type > 0)
3813 key.type--;
3814 else if (key.objectid > 0)
3815 key.objectid--;
3816 else
3817 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003818
Chris Mason925baed2008-06-25 16:01:30 -04003819 btrfs_release_path(root, path);
3820 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3821 if (ret < 0)
3822 return ret;
3823 btrfs_item_key(path->nodes[0], &found_key, 0);
3824 ret = comp_keys(&found_key, &key);
3825 if (ret < 0)
3826 return 0;
3827 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003828}
3829
Chris Mason3f157a22008-06-25 16:01:31 -04003830/*
3831 * A helper function to walk down the tree starting at min_key, and looking
3832 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003833 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003834 *
3835 * This does not cow, but it does stuff the starting key it finds back
3836 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3837 * key and get a writable path.
3838 *
3839 * This does lock as it descends, and path->keep_locks should be set
3840 * to 1 by the caller.
3841 *
3842 * This honors path->lowest_level to prevent descent past a given level
3843 * of the tree.
3844 *
Chris Masond352ac62008-09-29 15:18:18 -04003845 * min_trans indicates the oldest transaction that you are interested
3846 * in walking through. Any nodes or leaves older than min_trans are
3847 * skipped over (without reading them).
3848 *
Chris Mason3f157a22008-06-25 16:01:31 -04003849 * returns zero if something useful was found, < 0 on error and 1 if there
3850 * was nothing in the tree that matched the search criteria.
3851 */
3852int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003853 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003854 struct btrfs_path *path, int cache_only,
3855 u64 min_trans)
3856{
3857 struct extent_buffer *cur;
3858 struct btrfs_key found_key;
3859 int slot;
Yan96524802008-07-24 12:19:49 -04003860 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003861 u32 nritems;
3862 int level;
3863 int ret = 1;
3864
Chris Mason934d3752008-12-08 16:43:10 -05003865 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003866again:
3867 cur = btrfs_lock_root_node(root);
3868 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003869 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003870 path->nodes[level] = cur;
3871 path->locks[level] = 1;
3872
3873 if (btrfs_header_generation(cur) < min_trans) {
3874 ret = 1;
3875 goto out;
3876 }
Chris Masond3977122009-01-05 21:25:51 -05003877 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003878 nritems = btrfs_header_nritems(cur);
3879 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003880 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003881
Chris Mason323ac952008-10-01 19:05:46 -04003882 /* at the lowest level, we're done, setup the path and exit */
3883 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003884 if (slot >= nritems)
3885 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003886 ret = 0;
3887 path->slots[level] = slot;
3888 btrfs_item_key_to_cpu(cur, &found_key, slot);
3889 goto out;
3890 }
Yan96524802008-07-24 12:19:49 -04003891 if (sret && slot > 0)
3892 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003893 /*
3894 * check this node pointer against the cache_only and
3895 * min_trans parameters. If it isn't in cache or is too
3896 * old, skip to the next one.
3897 */
Chris Masond3977122009-01-05 21:25:51 -05003898 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003899 u64 blockptr;
3900 u64 gen;
3901 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003902 struct btrfs_disk_key disk_key;
3903
Chris Mason3f157a22008-06-25 16:01:31 -04003904 blockptr = btrfs_node_blockptr(cur, slot);
3905 gen = btrfs_node_ptr_generation(cur, slot);
3906 if (gen < min_trans) {
3907 slot++;
3908 continue;
3909 }
3910 if (!cache_only)
3911 break;
3912
Chris Masone02119d2008-09-05 16:13:11 -04003913 if (max_key) {
3914 btrfs_node_key(cur, &disk_key, slot);
3915 if (comp_keys(&disk_key, max_key) >= 0) {
3916 ret = 1;
3917 goto out;
3918 }
3919 }
3920
Chris Mason3f157a22008-06-25 16:01:31 -04003921 tmp = btrfs_find_tree_block(root, blockptr,
3922 btrfs_level_size(root, level - 1));
3923
3924 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3925 free_extent_buffer(tmp);
3926 break;
3927 }
3928 if (tmp)
3929 free_extent_buffer(tmp);
3930 slot++;
3931 }
Chris Masone02119d2008-09-05 16:13:11 -04003932find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003933 /*
3934 * we didn't find a candidate key in this node, walk forward
3935 * and find another one
3936 */
3937 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003938 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003939 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003940 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003941 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003942 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003943 btrfs_release_path(root, path);
3944 goto again;
3945 } else {
3946 goto out;
3947 }
3948 }
3949 /* save our key for returning back */
3950 btrfs_node_key_to_cpu(cur, &found_key, slot);
3951 path->slots[level] = slot;
3952 if (level == path->lowest_level) {
3953 ret = 0;
3954 unlock_up(path, level, 1);
3955 goto out;
3956 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003957 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003958 cur = read_node_slot(root, cur, slot);
3959
3960 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003961
Chris Mason3f157a22008-06-25 16:01:31 -04003962 path->locks[level - 1] = 1;
3963 path->nodes[level - 1] = cur;
3964 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05003965 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04003966 }
3967out:
3968 if (ret == 0)
3969 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05003970 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003971 return ret;
3972}
3973
3974/*
3975 * this is similar to btrfs_next_leaf, but does not try to preserve
3976 * and fixup the path. It looks for and returns the next key in the
3977 * tree based on the current path and the cache_only and min_trans
3978 * parameters.
3979 *
3980 * 0 is returned if another key is found, < 0 if there are any errors
3981 * and 1 is returned if there are no higher keys in the tree
3982 *
3983 * path->keep_locks should be set to 1 on the search made before
3984 * calling this function.
3985 */
Chris Masone7a84562008-06-25 16:01:31 -04003986int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003987 struct btrfs_key *key, int lowest_level,
3988 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003989{
3990 int level = lowest_level;
3991 int slot;
3992 struct extent_buffer *c;
3993
Chris Mason934d3752008-12-08 16:43:10 -05003994 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003995 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003996 if (!path->nodes[level])
3997 return 1;
3998
3999 slot = path->slots[level] + 1;
4000 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004001next:
Chris Masone7a84562008-06-25 16:01:31 -04004002 if (slot >= btrfs_header_nritems(c)) {
4003 level++;
Chris Masond3977122009-01-05 21:25:51 -05004004 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04004005 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04004006 continue;
4007 }
4008 if (level == 0)
4009 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004010 else {
4011 u64 blockptr = btrfs_node_blockptr(c, slot);
4012 u64 gen = btrfs_node_ptr_generation(c, slot);
4013
4014 if (cache_only) {
4015 struct extent_buffer *cur;
4016 cur = btrfs_find_tree_block(root, blockptr,
4017 btrfs_level_size(root, level - 1));
4018 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4019 slot++;
4020 if (cur)
4021 free_extent_buffer(cur);
4022 goto next;
4023 }
4024 free_extent_buffer(cur);
4025 }
4026 if (gen < min_trans) {
4027 slot++;
4028 goto next;
4029 }
Chris Masone7a84562008-06-25 16:01:31 -04004030 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004031 }
Chris Masone7a84562008-06-25 16:01:31 -04004032 return 0;
4033 }
4034 return 1;
4035}
4036
Chris Mason7bb86312007-12-11 09:25:06 -05004037/*
Chris Mason925baed2008-06-25 16:01:30 -04004038 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004039 * returns 0 if it found something or 1 if there are no greater leaves.
4040 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004041 */
Chris Mason234b63a2007-03-13 10:46:10 -04004042int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004043{
4044 int slot;
4045 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004046 struct extent_buffer *c;
4047 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004048 struct btrfs_key key;
4049 u32 nritems;
4050 int ret;
4051
4052 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004053 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004054 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004055
4056 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4057
Chris Mason925baed2008-06-25 16:01:30 -04004058 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04004059 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04004060 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4061 path->keep_locks = 0;
4062
4063 if (ret < 0)
4064 return ret;
4065
Chris Masonb4ce94d2009-02-04 09:25:08 -05004066 btrfs_set_path_blocking(path);
Chris Masona2135012008-06-25 16:01:30 -04004067 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004068 /*
4069 * by releasing the path above we dropped all our locks. A balance
4070 * could have added more items next to the key that used to be
4071 * at the very end of the block. So, check again here and
4072 * advance the path if there are now more items available.
4073 */
Chris Masona2135012008-06-25 16:01:30 -04004074 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004075 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04004076 goto done;
4077 }
Chris Masond97e63b2007-02-20 16:40:44 -05004078
Chris Masond3977122009-01-05 21:25:51 -05004079 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05004080 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05004081 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004082
Chris Masond97e63b2007-02-20 16:40:44 -05004083 slot = path->slots[level] + 1;
4084 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004085 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004086 level++;
Chris Masond3977122009-01-05 21:25:51 -05004087 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05004088 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004089 continue;
4090 }
Chris Mason5f39d392007-10-15 16:14:19 -04004091
Chris Mason925baed2008-06-25 16:01:30 -04004092 if (next) {
4093 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004094 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004095 }
Chris Mason5f39d392007-10-15 16:14:19 -04004096
Chris Masonb4ce94d2009-02-04 09:25:08 -05004097 /* the path was set to blocking above */
Chris Mason0bd40a72008-07-17 12:54:43 -04004098 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4099 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05004100 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004101
Chris Masonca7a79a2008-05-12 12:59:19 -04004102 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04004103 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004104 btrfs_assert_tree_locked(c);
Chris Mason5cd57b22008-06-25 16:01:30 -04004105 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004106 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004107 }
Chris Masond97e63b2007-02-20 16:40:44 -05004108 break;
4109 }
4110 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004111 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004112 level--;
4113 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004114 if (path->locks[level])
4115 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04004116 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004117 path->nodes[level] = next;
4118 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004119 if (!path->skip_locking)
4120 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004121 if (!level)
4122 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004123
4124 btrfs_set_path_blocking(path);
Chris Mason925baed2008-06-25 16:01:30 -04004125 if (level == 1 && path->locks[1] && path->reada)
4126 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04004127 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04004128 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004129 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5cd57b22008-06-25 16:01:30 -04004130 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004131 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004132 }
Chris Masond97e63b2007-02-20 16:40:44 -05004133 }
Chris Mason925baed2008-06-25 16:01:30 -04004134done:
4135 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05004136 return 0;
4137}
Chris Mason0b86a832008-03-24 15:01:56 -04004138
Chris Mason3f157a22008-06-25 16:01:31 -04004139/*
4140 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4141 * searching until it gets past min_objectid or finds an item of 'type'
4142 *
4143 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4144 */
Chris Mason0b86a832008-03-24 15:01:56 -04004145int btrfs_previous_item(struct btrfs_root *root,
4146 struct btrfs_path *path, u64 min_objectid,
4147 int type)
4148{
4149 struct btrfs_key found_key;
4150 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004151 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004152 int ret;
4153
Chris Masond3977122009-01-05 21:25:51 -05004154 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004155 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004156 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004157 ret = btrfs_prev_leaf(root, path);
4158 if (ret != 0)
4159 return ret;
4160 } else {
4161 path->slots[0]--;
4162 }
4163 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004164 nritems = btrfs_header_nritems(leaf);
4165 if (nritems == 0)
4166 return 1;
4167 if (path->slots[0] == nritems)
4168 path->slots[0]--;
4169
Chris Mason0b86a832008-03-24 15:01:56 -04004170 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4171 if (found_key.type == type)
4172 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004173 if (found_key.objectid < min_objectid)
4174 break;
4175 if (found_key.objectid == min_objectid &&
4176 found_key.type < type)
4177 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004178 }
4179 return 1;
4180}