blob: 0fe615e4ea387582acc06f60cac81366f23fe069 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040039static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040057 if (!p->nodes[i] || !p->locks[i])
58 continue;
59 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60 if (p->locks[i] == BTRFS_READ_LOCK)
61 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62 else if (p->locks[i] == BTRFS_WRITE_LOCK)
63 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050064 }
65}
66
67/*
68 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050069 *
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
73 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 */
Chris Mason4008c042009-02-12 14:09:45 -050075noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040076 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050077{
78 int i;
Chris Mason4008c042009-02-12 14:09:45 -050079
80#ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
85 * the path blocking.
86 */
Chris Masonbd681512011-07-16 15:23:14 -040087 if (held) {
88 btrfs_set_lock_blocking_rw(held, held_rw);
89 if (held_rw == BTRFS_WRITE_LOCK)
90 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91 else if (held_rw == BTRFS_READ_LOCK)
92 held_rw = BTRFS_READ_LOCK_BLOCKING;
93 }
Chris Mason4008c042009-02-12 14:09:45 -050094 btrfs_set_path_blocking(p);
95#endif
96
97 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040098 if (p->nodes[i] && p->locks[i]) {
99 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101 p->locks[i] = BTRFS_WRITE_LOCK;
102 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103 p->locks[i] = BTRFS_READ_LOCK;
104 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500105 }
Chris Mason4008c042009-02-12 14:09:45 -0500106
107#ifdef CONFIG_DEBUG_LOCK_ALLOC
108 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400109 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500110#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400114void btrfs_free_path(struct btrfs_path *p)
115{
Jesper Juhlff175d52010-12-25 21:22:30 +0000116 if (!p)
117 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200118 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400119 kmem_cache_free(btrfs_path_cachep, p);
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
125 *
126 * It is safe to call this on paths that no locks or extent buffers held.
127 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200128noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500129{
130 int i;
Chris Masona2135012008-06-25 16:01:30 -0400131
Chris Mason234b63a2007-03-13 10:46:10 -0400132 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400135 continue;
136 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400137 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400138 p->locks[i] = 0;
139 }
Chris Mason5f39d392007-10-15 16:14:19 -0400140 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400141 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 }
143}
144
Chris Masond352ac62008-09-29 15:18:18 -0400145/*
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
149 * looping required.
150 *
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400158
159 rcu_read_lock();
160 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400161 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400162 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400163 return eb;
164}
165
Chris Masond352ac62008-09-29 15:18:18 -0400166/* loop around taking references on and locking the root node of the
167 * tree until you end up with a lock on the root. A locked buffer
168 * is returned, with a reference held.
169 */
Chris Mason925baed2008-06-25 16:01:30 -0400170struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
171{
172 struct extent_buffer *eb;
173
Chris Masond3977122009-01-05 21:25:51 -0500174 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400175 eb = btrfs_root_node(root);
176 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400177 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400178 break;
Chris Mason925baed2008-06-25 16:01:30 -0400179 btrfs_tree_unlock(eb);
180 free_extent_buffer(eb);
181 }
182 return eb;
183}
184
Chris Masonbd681512011-07-16 15:23:14 -0400185/* loop around taking references on and locking the root node of the
186 * tree until you end up with a lock on the root. A locked buffer
187 * is returned, with a reference held.
188 */
189struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190{
191 struct extent_buffer *eb;
192
193 while (1) {
194 eb = btrfs_root_node(root);
195 btrfs_tree_read_lock(eb);
196 if (eb == root->node)
197 break;
198 btrfs_tree_read_unlock(eb);
199 free_extent_buffer(eb);
200 }
201 return eb;
202}
203
Chris Masond352ac62008-09-29 15:18:18 -0400204/* cowonly root (everything not a reference counted cow subvolume), just get
205 * put onto a simple dirty list. transaction.c walks this to make sure they
206 * get properly updated on disk.
207 */
Chris Mason0b86a832008-03-24 15:01:56 -0400208static void add_root_to_dirty_list(struct btrfs_root *root)
209{
210 if (root->track_dirty && list_empty(&root->dirty_list)) {
211 list_add(&root->dirty_list,
212 &root->fs_info->dirty_cowonly_roots);
213 }
214}
215
Chris Masond352ac62008-09-29 15:18:18 -0400216/*
217 * used by snapshot creation to make a copy of a root for a tree with
218 * a given objectid. The buffer with the new root node is returned in
219 * cow_ret, and this func returns zero on success or a negative error code.
220 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500221int btrfs_copy_root(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root,
223 struct extent_buffer *buf,
224 struct extent_buffer **cow_ret, u64 new_root_objectid)
225{
226 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 int ret = 0;
228 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400229 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500230
231 WARN_ON(root->ref_cows && trans->transid !=
232 root->fs_info->running_transaction->transid);
233 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
234
235 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400236 if (level == 0)
237 btrfs_item_key(buf, &disk_key, 0);
238 else
239 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400240
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400241 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
242 new_root_objectid, &disk_key, level,
243 buf->start, 0);
244 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 return PTR_ERR(cow);
246
247 copy_extent_buffer(cow, buf, 0, 0, cow->len);
248 btrfs_set_header_bytenr(cow, cow->start);
249 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400250 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
251 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
252 BTRFS_HEADER_FLAG_RELOC);
253 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
254 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
255 else
256 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500257
Yan Zheng2b820322008-11-17 21:11:30 -0500258 write_extent_buffer(cow, root->fs_info->fsid,
259 (unsigned long)btrfs_header_fsid(cow),
260 BTRFS_FSID_SIZE);
261
Chris Masonbe20aa92007-12-17 20:14:01 -0500262 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
264 ret = btrfs_inc_ref(trans, root, cow, 1);
265 else
266 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500267
Chris Masonbe20aa92007-12-17 20:14:01 -0500268 if (ret)
269 return ret;
270
271 btrfs_mark_buffer_dirty(cow);
272 *cow_ret = cow;
273 return 0;
274}
275
Chris Masond352ac62008-09-29 15:18:18 -0400276/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400277 * check if the tree block can be shared by multiple trees
278 */
279int btrfs_block_can_be_shared(struct btrfs_root *root,
280 struct extent_buffer *buf)
281{
282 /*
283 * Tree blocks not in refernece counted trees and tree roots
284 * are never shared. If a block was allocated after the last
285 * snapshot and the block was not allocated by tree relocation,
286 * we know the block is not shared.
287 */
288 if (root->ref_cows &&
289 buf != root->node && buf != root->commit_root &&
290 (btrfs_header_generation(buf) <=
291 btrfs_root_last_snapshot(&root->root_item) ||
292 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
293 return 1;
294#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
295 if (root->ref_cows &&
296 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
297 return 1;
298#endif
299 return 0;
300}
301
302static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
303 struct btrfs_root *root,
304 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400305 struct extent_buffer *cow,
306 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400307{
308 u64 refs;
309 u64 owner;
310 u64 flags;
311 u64 new_flags = 0;
312 int ret;
313
314 /*
315 * Backrefs update rules:
316 *
317 * Always use full backrefs for extent pointers in tree block
318 * allocated by tree relocation.
319 *
320 * If a shared tree block is no longer referenced by its owner
321 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
322 * use full backrefs for extent pointers in tree block.
323 *
324 * If a tree block is been relocating
325 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
326 * use full backrefs for extent pointers in tree block.
327 * The reason for this is some operations (such as drop tree)
328 * are only allowed for blocks use full backrefs.
329 */
330
331 if (btrfs_block_can_be_shared(root, buf)) {
332 ret = btrfs_lookup_extent_info(trans, root, buf->start,
333 buf->len, &refs, &flags);
334 BUG_ON(ret);
335 BUG_ON(refs == 0);
336 } else {
337 refs = 1;
338 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
339 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
340 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
341 else
342 flags = 0;
343 }
344
345 owner = btrfs_header_owner(buf);
346 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
347 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
348
349 if (refs > 1) {
350 if ((owner == root->root_key.objectid ||
351 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
352 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
353 ret = btrfs_inc_ref(trans, root, buf, 1);
354 BUG_ON(ret);
355
356 if (root->root_key.objectid ==
357 BTRFS_TREE_RELOC_OBJECTID) {
358 ret = btrfs_dec_ref(trans, root, buf, 0);
359 BUG_ON(ret);
360 ret = btrfs_inc_ref(trans, root, cow, 1);
361 BUG_ON(ret);
362 }
363 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
364 } else {
365
366 if (root->root_key.objectid ==
367 BTRFS_TREE_RELOC_OBJECTID)
368 ret = btrfs_inc_ref(trans, root, cow, 1);
369 else
370 ret = btrfs_inc_ref(trans, root, cow, 0);
371 BUG_ON(ret);
372 }
373 if (new_flags != 0) {
374 ret = btrfs_set_disk_extent_flags(trans, root,
375 buf->start,
376 buf->len,
377 new_flags, 0);
378 BUG_ON(ret);
379 }
380 } else {
381 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
382 if (root->root_key.objectid ==
383 BTRFS_TREE_RELOC_OBJECTID)
384 ret = btrfs_inc_ref(trans, root, cow, 1);
385 else
386 ret = btrfs_inc_ref(trans, root, cow, 0);
387 BUG_ON(ret);
388 ret = btrfs_dec_ref(trans, root, buf, 1);
389 BUG_ON(ret);
390 }
391 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400392 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400393 }
394 return 0;
395}
396
397/*
Chris Masond3977122009-01-05 21:25:51 -0500398 * does the dirty work in cow of a single block. The parent block (if
399 * supplied) is updated to point to the new cow copy. The new buffer is marked
400 * dirty and returned locked. If you modify the block it needs to be marked
401 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400402 *
403 * search_start -- an allocation hint for the new block
404 *
Chris Masond3977122009-01-05 21:25:51 -0500405 * empty_size -- a hint that you plan on doing more cow. This is the size in
406 * bytes the allocator should try to find free next to the block it returns.
407 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400408 */
Chris Masond3977122009-01-05 21:25:51 -0500409static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400410 struct btrfs_root *root,
411 struct extent_buffer *buf,
412 struct extent_buffer *parent, int parent_slot,
413 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400414 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400415{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400416 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400417 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500418 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400419 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400420 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400421 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400422
Chris Mason925baed2008-06-25 16:01:30 -0400423 if (*cow_ret == buf)
424 unlock_orig = 1;
425
Chris Masonb9447ef82009-03-09 11:45:38 -0400426 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400427
Chris Mason7bb86312007-12-11 09:25:06 -0500428 WARN_ON(root->ref_cows && trans->transid !=
429 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400430 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400431
Chris Mason7bb86312007-12-11 09:25:06 -0500432 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400433
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400434 if (level == 0)
435 btrfs_item_key(buf, &disk_key, 0);
436 else
437 btrfs_node_key(buf, &disk_key, 0);
438
439 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
440 if (parent)
441 parent_start = parent->start;
442 else
443 parent_start = 0;
444 } else
445 parent_start = 0;
446
447 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
448 root->root_key.objectid, &disk_key,
449 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400450 if (IS_ERR(cow))
451 return PTR_ERR(cow);
452
Chris Masonb4ce94d2009-02-04 09:25:08 -0500453 /* cow is set to blocking by btrfs_init_new_buffer */
454
Chris Mason5f39d392007-10-15 16:14:19 -0400455 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400456 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400457 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400458 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
459 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
460 BTRFS_HEADER_FLAG_RELOC);
461 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
462 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
463 else
464 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400465
Yan Zheng2b820322008-11-17 21:11:30 -0500466 write_extent_buffer(cow, root->fs_info->fsid,
467 (unsigned long)btrfs_header_fsid(cow),
468 BTRFS_FSID_SIZE);
469
Yan, Zhengf0486c62010-05-16 10:46:25 -0400470 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400471
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400472 if (root->ref_cows)
473 btrfs_reloc_cow_block(trans, root, buf, cow);
474
Chris Mason6702ed42007-08-07 16:15:09 -0400475 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400476 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
478 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
479 parent_start = buf->start;
480 else
481 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400482
Chris Mason5f39d392007-10-15 16:14:19 -0400483 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400484 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400485
Yan, Zhengf0486c62010-05-16 10:46:25 -0400486 btrfs_free_tree_block(trans, root, buf, parent_start,
487 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400488 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400489 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400490 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400491 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
492 parent_start = parent->start;
493 else
494 parent_start = 0;
495
496 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400497 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400498 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500499 btrfs_set_node_ptr_generation(parent, parent_slot,
500 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400501 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400502 btrfs_free_tree_block(trans, root, buf, parent_start,
503 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400504 }
Chris Mason925baed2008-06-25 16:01:30 -0400505 if (unlock_orig)
506 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400507 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400508 btrfs_mark_buffer_dirty(cow);
509 *cow_ret = cow;
510 return 0;
511}
512
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400513static inline int should_cow_block(struct btrfs_trans_handle *trans,
514 struct btrfs_root *root,
515 struct extent_buffer *buf)
516{
517 if (btrfs_header_generation(buf) == trans->transid &&
518 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
519 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
520 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
521 return 0;
522 return 1;
523}
524
Chris Masond352ac62008-09-29 15:18:18 -0400525/*
526 * cows a single block, see __btrfs_cow_block for the real work.
527 * This version of it has extra checks so that a block isn't cow'd more than
528 * once per transaction, as long as it hasn't been written yet
529 */
Chris Masond3977122009-01-05 21:25:51 -0500530noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400531 struct btrfs_root *root, struct extent_buffer *buf,
532 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400533 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500534{
Chris Mason6702ed42007-08-07 16:15:09 -0400535 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400536 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500537
Chris Masonccd467d2007-06-28 15:57:36 -0400538 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500539 printk(KERN_CRIT "trans %llu running %llu\n",
540 (unsigned long long)trans->transid,
541 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400542 root->fs_info->running_transaction->transid);
543 WARN_ON(1);
544 }
545 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500546 printk(KERN_CRIT "trans %llu running %llu\n",
547 (unsigned long long)trans->transid,
548 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400549 WARN_ON(1);
550 }
Chris Masondc17ff82008-01-08 15:46:30 -0500551
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400552 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500553 *cow_ret = buf;
554 return 0;
555 }
Chris Masonc4876852009-02-04 09:24:25 -0500556
Chris Mason0b86a832008-03-24 15:01:56 -0400557 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500558
559 if (parent)
560 btrfs_set_lock_blocking(parent);
561 btrfs_set_lock_blocking(buf);
562
Chris Masonf510cfe2007-10-15 16:14:48 -0400563 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400564 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000565
566 trace_btrfs_cow_block(root, buf, *cow_ret);
567
Chris Masonf510cfe2007-10-15 16:14:48 -0400568 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400569}
570
Chris Masond352ac62008-09-29 15:18:18 -0400571/*
572 * helper function for defrag to decide if two blocks pointed to by a
573 * node are actually close by
574 */
Chris Mason6b800532007-10-15 16:17:34 -0400575static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400576{
Chris Mason6b800532007-10-15 16:17:34 -0400577 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400578 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400579 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400580 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500581 return 0;
582}
583
Chris Mason081e9572007-11-06 10:26:24 -0500584/*
585 * compare two keys in a memcmp fashion
586 */
587static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
588{
589 struct btrfs_key k1;
590
591 btrfs_disk_key_to_cpu(&k1, disk);
592
Diego Calleja20736ab2009-07-24 11:06:52 -0400593 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500594}
595
Josef Bacikf3465ca2008-11-12 14:19:50 -0500596/*
597 * same as comp_keys only with two btrfs_key's
598 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400599int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500600{
601 if (k1->objectid > k2->objectid)
602 return 1;
603 if (k1->objectid < k2->objectid)
604 return -1;
605 if (k1->type > k2->type)
606 return 1;
607 if (k1->type < k2->type)
608 return -1;
609 if (k1->offset > k2->offset)
610 return 1;
611 if (k1->offset < k2->offset)
612 return -1;
613 return 0;
614}
Chris Mason081e9572007-11-06 10:26:24 -0500615
Chris Masond352ac62008-09-29 15:18:18 -0400616/*
617 * this is used by the defrag code to go through all the
618 * leaves pointed to by a node and reallocate them so that
619 * disk order is close to key order
620 */
Chris Mason6702ed42007-08-07 16:15:09 -0400621int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400622 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400623 int start_slot, int cache_only, u64 *last_ret,
624 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400625{
Chris Mason6b800532007-10-15 16:17:34 -0400626 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400627 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400628 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400629 u64 search_start = *last_ret;
630 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400631 u64 other;
632 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400633 int end_slot;
634 int i;
635 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400636 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400637 int uptodate;
638 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500639 int progress_passed = 0;
640 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400641
Chris Mason5708b952007-10-25 15:43:18 -0400642 parent_level = btrfs_header_level(parent);
643 if (cache_only && parent_level != 1)
644 return 0;
645
Chris Masond3977122009-01-05 21:25:51 -0500646 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400647 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500648 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400649 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400650
Chris Mason6b800532007-10-15 16:17:34 -0400651 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400652 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400653 end_slot = parent_nritems;
654
655 if (parent_nritems == 1)
656 return 0;
657
Chris Masonb4ce94d2009-02-04 09:25:08 -0500658 btrfs_set_lock_blocking(parent);
659
Chris Mason6702ed42007-08-07 16:15:09 -0400660 for (i = start_slot; i < end_slot; i++) {
661 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400662
Chris Mason081e9572007-11-06 10:26:24 -0500663 btrfs_node_key(parent, &disk_key, i);
664 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
665 continue;
666
667 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400668 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400669 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400670 if (last_block == 0)
671 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400672
Chris Mason6702ed42007-08-07 16:15:09 -0400673 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400674 other = btrfs_node_blockptr(parent, i - 1);
675 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400676 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400677 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400678 other = btrfs_node_blockptr(parent, i + 1);
679 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400680 }
Chris Masone9d0b132007-08-10 14:06:19 -0400681 if (close) {
682 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400683 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400684 }
Chris Mason6702ed42007-08-07 16:15:09 -0400685
Chris Mason6b800532007-10-15 16:17:34 -0400686 cur = btrfs_find_tree_block(root, blocknr, blocksize);
687 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400688 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400689 else
690 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400691 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400692 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400693 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400694 continue;
695 }
Chris Mason6b800532007-10-15 16:17:34 -0400696 if (!cur) {
697 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400698 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000699 if (!cur)
700 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400701 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400702 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400703 }
Chris Mason6702ed42007-08-07 16:15:09 -0400704 }
Chris Masone9d0b132007-08-10 14:06:19 -0400705 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400706 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400707
Chris Masone7a84562008-06-25 16:01:31 -0400708 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500709 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400710 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400711 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400712 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400713 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400714 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400715 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400716 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400717 break;
Yan252c38f2007-08-29 09:11:44 -0400718 }
Chris Masone7a84562008-06-25 16:01:31 -0400719 search_start = cur->start;
720 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400721 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400722 btrfs_tree_unlock(cur);
723 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400724 }
725 return err;
726}
727
Chris Mason74123bd2007-02-02 11:05:29 -0500728/*
729 * The leaf data grows from end-to-front in the node.
730 * this returns the address of the start of the last item,
731 * which is the stop of the leaf data stack
732 */
Chris Mason123abc82007-03-14 14:14:43 -0400733static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400734 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735{
Chris Mason5f39d392007-10-15 16:14:19 -0400736 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500737 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400738 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400739 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500740}
741
Chris Masonaa5d6be2007-02-28 16:35:06 -0500742
Chris Mason74123bd2007-02-02 11:05:29 -0500743/*
Chris Mason5f39d392007-10-15 16:14:19 -0400744 * search for key in the extent_buffer. The items start at offset p,
745 * and they are item_size apart. There are 'max' items in p.
746 *
Chris Mason74123bd2007-02-02 11:05:29 -0500747 * the slot in the array is returned via slot, and it points to
748 * the place where you would insert key if it is not found in
749 * the array.
750 *
751 * slot may point to max if the key is bigger than all of the keys
752 */
Chris Masone02119d2008-09-05 16:13:11 -0400753static noinline int generic_bin_search(struct extent_buffer *eb,
754 unsigned long p,
755 int item_size, struct btrfs_key *key,
756 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500757{
758 int low = 0;
759 int high = max;
760 int mid;
761 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400762 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400763 struct btrfs_disk_key unaligned;
764 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400765 char *kaddr = NULL;
766 unsigned long map_start = 0;
767 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400768 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500769
Chris Masond3977122009-01-05 21:25:51 -0500770 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500771 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400772 offset = p + mid * item_size;
773
Chris Masona6591712011-07-19 12:04:14 -0400774 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400775 (offset + sizeof(struct btrfs_disk_key)) >
776 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500777
778 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400779 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400780 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400781
Chris Mason479965d2007-10-15 16:14:27 -0400782 if (!err) {
783 tmp = (struct btrfs_disk_key *)(kaddr + offset -
784 map_start);
785 } else {
786 read_extent_buffer(eb, &unaligned,
787 offset, sizeof(unaligned));
788 tmp = &unaligned;
789 }
790
Chris Mason5f39d392007-10-15 16:14:19 -0400791 } else {
792 tmp = (struct btrfs_disk_key *)(kaddr + offset -
793 map_start);
794 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500795 ret = comp_keys(tmp, key);
796
797 if (ret < 0)
798 low = mid + 1;
799 else if (ret > 0)
800 high = mid;
801 else {
802 *slot = mid;
803 return 0;
804 }
805 }
806 *slot = low;
807 return 1;
808}
809
Chris Mason97571fd2007-02-24 13:39:08 -0500810/*
811 * simple bin_search frontend that does the right thing for
812 * leaves vs nodes
813 */
Chris Mason5f39d392007-10-15 16:14:19 -0400814static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
815 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500816{
Chris Mason5f39d392007-10-15 16:14:19 -0400817 if (level == 0) {
818 return generic_bin_search(eb,
819 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400820 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400821 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400822 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500823 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400824 return generic_bin_search(eb,
825 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400826 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400827 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400828 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500829 }
830 return -1;
831}
832
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400833int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
834 int level, int *slot)
835{
836 return bin_search(eb, key, level, slot);
837}
838
Yan, Zhengf0486c62010-05-16 10:46:25 -0400839static void root_add_used(struct btrfs_root *root, u32 size)
840{
841 spin_lock(&root->accounting_lock);
842 btrfs_set_root_used(&root->root_item,
843 btrfs_root_used(&root->root_item) + size);
844 spin_unlock(&root->accounting_lock);
845}
846
847static void root_sub_used(struct btrfs_root *root, u32 size)
848{
849 spin_lock(&root->accounting_lock);
850 btrfs_set_root_used(&root->root_item,
851 btrfs_root_used(&root->root_item) - size);
852 spin_unlock(&root->accounting_lock);
853}
854
Chris Masond352ac62008-09-29 15:18:18 -0400855/* given a node and slot number, this reads the blocks it points to. The
856 * extent buffer is returned with a reference taken (but unlocked).
857 * NULL is returned on error.
858 */
Chris Masone02119d2008-09-05 16:13:11 -0400859static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400860 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500861{
Chris Masonca7a79a2008-05-12 12:59:19 -0400862 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500863 if (slot < 0)
864 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400865 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500866 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400867
868 BUG_ON(level == 0);
869
Chris Masondb945352007-10-15 16:15:53 -0400870 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400871 btrfs_level_size(root, level - 1),
872 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500873}
874
Chris Masond352ac62008-09-29 15:18:18 -0400875/*
876 * node level balancing, used to make sure nodes are in proper order for
877 * item deletion. We balance from the top down, so we have to make sure
878 * that a deletion won't leave an node completely empty later on.
879 */
Chris Masone02119d2008-09-05 16:13:11 -0400880static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500881 struct btrfs_root *root,
882 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500883{
Chris Mason5f39d392007-10-15 16:14:19 -0400884 struct extent_buffer *right = NULL;
885 struct extent_buffer *mid;
886 struct extent_buffer *left = NULL;
887 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500888 int ret = 0;
889 int wret;
890 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500891 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500892 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500893
894 if (level == 0)
895 return 0;
896
Chris Mason5f39d392007-10-15 16:14:19 -0400897 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500898
Chris Masonbd681512011-07-16 15:23:14 -0400899 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
900 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500901 WARN_ON(btrfs_header_generation(mid) != trans->transid);
902
Chris Mason1d4f8a02007-03-13 09:28:32 -0400903 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500904
Li Zefana05a9bb2011-09-06 16:55:34 +0800905 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400906 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800907 pslot = path->slots[level + 1];
908 }
Chris Masonbb803952007-03-01 12:04:21 -0500909
Chris Mason40689472007-03-17 14:29:23 -0400910 /*
911 * deal with the case where there is only one pointer in the root
912 * by promoting the node below to a root
913 */
Chris Mason5f39d392007-10-15 16:14:19 -0400914 if (!parent) {
915 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500916
Chris Mason5f39d392007-10-15 16:14:19 -0400917 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500918 return 0;
919
920 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400921 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500922 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400923 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500924 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400925 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400926 if (ret) {
927 btrfs_tree_unlock(child);
928 free_extent_buffer(child);
929 goto enospc;
930 }
Yan2f375ab2008-02-01 14:58:07 -0500931
Chris Mason240f62c2011-03-23 14:54:42 -0400932 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400933
Chris Mason0b86a832008-03-24 15:01:56 -0400934 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400935 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500936
Chris Mason925baed2008-06-25 16:01:30 -0400937 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500938 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400939 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400940 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500941 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400942 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400943
944 root_sub_used(root, mid->len);
945 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500946 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400947 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400948 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500949 }
Chris Mason5f39d392007-10-15 16:14:19 -0400950 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400951 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500952 return 0;
953
Andi Kleen559af822010-10-29 15:14:37 -0400954 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400955
Chris Mason5f39d392007-10-15 16:14:19 -0400956 left = read_node_slot(root, parent, pslot - 1);
957 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400958 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500959 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400960 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400961 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400962 if (wret) {
963 ret = wret;
964 goto enospc;
965 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400966 }
Chris Mason5f39d392007-10-15 16:14:19 -0400967 right = read_node_slot(root, parent, pslot + 1);
968 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400969 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500970 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400971 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400972 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400973 if (wret) {
974 ret = wret;
975 goto enospc;
976 }
977 }
978
979 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400980 if (left) {
981 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400982 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500983 if (wret < 0)
984 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400985 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500986 }
Chris Mason79f95c82007-03-01 15:16:26 -0500987
988 /*
989 * then try to empty the right most buffer into the middle
990 */
Chris Mason5f39d392007-10-15 16:14:19 -0400991 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400992 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400993 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500994 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400995 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400996 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400997 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400998 wret = del_ptr(trans, root, path, level + 1, pslot +
999 1);
Chris Masonbb803952007-03-01 12:04:21 -05001000 if (wret)
1001 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001002 root_sub_used(root, right->len);
1003 btrfs_free_tree_block(trans, root, right, 0, 1);
1004 free_extent_buffer(right);
1005 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001006 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001007 struct btrfs_disk_key right_key;
1008 btrfs_node_key(right, &right_key, 0);
1009 btrfs_set_node_key(parent, &right_key, pslot + 1);
1010 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001011 }
1012 }
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001014 /*
1015 * we're not allowed to leave a node with one item in the
1016 * tree during a delete. A deletion from lower in the tree
1017 * could try to delete the only pointer in this node.
1018 * So, pull some keys from the left.
1019 * There has to be a left pointer at this point because
1020 * otherwise we would have pulled some pointers from the
1021 * right
1022 */
Chris Mason5f39d392007-10-15 16:14:19 -04001023 BUG_ON(!left);
1024 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001025 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001026 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001027 goto enospc;
1028 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001029 if (wret == 1) {
1030 wret = push_node_left(trans, root, left, mid, 1);
1031 if (wret < 0)
1032 ret = wret;
1033 }
Chris Mason79f95c82007-03-01 15:16:26 -05001034 BUG_ON(wret == 1);
1035 }
Chris Mason5f39d392007-10-15 16:14:19 -04001036 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001037 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001038 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001039 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001040 if (wret)
1041 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001042 root_sub_used(root, mid->len);
1043 btrfs_free_tree_block(trans, root, mid, 0, 1);
1044 free_extent_buffer(mid);
1045 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001046 } else {
1047 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001048 struct btrfs_disk_key mid_key;
1049 btrfs_node_key(mid, &mid_key, 0);
1050 btrfs_set_node_key(parent, &mid_key, pslot);
1051 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001052 }
Chris Masonbb803952007-03-01 12:04:21 -05001053
Chris Mason79f95c82007-03-01 15:16:26 -05001054 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001055 if (left) {
1056 if (btrfs_header_nritems(left) > orig_slot) {
1057 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001058 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001059 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001060 path->slots[level + 1] -= 1;
1061 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001062 if (mid) {
1063 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001064 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001065 }
Chris Masonbb803952007-03-01 12:04:21 -05001066 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001067 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001068 path->slots[level] = orig_slot;
1069 }
1070 }
Chris Mason79f95c82007-03-01 15:16:26 -05001071 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001072 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001073 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001074 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001075enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001076 if (right) {
1077 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001078 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001079 }
1080 if (left) {
1081 if (path->nodes[level] != left)
1082 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001083 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001084 }
Chris Masonbb803952007-03-01 12:04:21 -05001085 return ret;
1086}
1087
Chris Masond352ac62008-09-29 15:18:18 -04001088/* Node balancing for insertion. Here we only split or push nodes around
1089 * when they are completely full. This is also done top down, so we
1090 * have to be pessimistic.
1091 */
Chris Masond3977122009-01-05 21:25:51 -05001092static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001093 struct btrfs_root *root,
1094 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001095{
Chris Mason5f39d392007-10-15 16:14:19 -04001096 struct extent_buffer *right = NULL;
1097 struct extent_buffer *mid;
1098 struct extent_buffer *left = NULL;
1099 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001100 int ret = 0;
1101 int wret;
1102 int pslot;
1103 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001104
1105 if (level == 0)
1106 return 1;
1107
Chris Mason5f39d392007-10-15 16:14:19 -04001108 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001109 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001110
Li Zefana05a9bb2011-09-06 16:55:34 +08001111 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001112 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001113 pslot = path->slots[level + 1];
1114 }
Chris Masone66f7092007-04-20 13:16:02 -04001115
Chris Mason5f39d392007-10-15 16:14:19 -04001116 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001117 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001118
Chris Mason5f39d392007-10-15 16:14:19 -04001119 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001120
1121 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001122 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001123 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001124
1125 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001126 btrfs_set_lock_blocking(left);
1127
Chris Mason5f39d392007-10-15 16:14:19 -04001128 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001129 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1130 wret = 1;
1131 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001132 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001133 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001134 if (ret)
1135 wret = 1;
1136 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001137 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001138 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001139 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001140 }
Chris Masone66f7092007-04-20 13:16:02 -04001141 if (wret < 0)
1142 ret = wret;
1143 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001144 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001145 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001146 btrfs_node_key(mid, &disk_key, 0);
1147 btrfs_set_node_key(parent, &disk_key, pslot);
1148 btrfs_mark_buffer_dirty(parent);
1149 if (btrfs_header_nritems(left) > orig_slot) {
1150 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001151 path->slots[level + 1] -= 1;
1152 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001153 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001154 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001155 } else {
1156 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001157 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001158 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001159 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001160 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001161 }
Chris Masone66f7092007-04-20 13:16:02 -04001162 return 0;
1163 }
Chris Mason925baed2008-06-25 16:01:30 -04001164 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001165 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001166 }
Chris Mason925baed2008-06-25 16:01:30 -04001167 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001168
1169 /*
1170 * then try to empty the right most buffer into the middle
1171 */
Chris Mason5f39d392007-10-15 16:14:19 -04001172 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001173 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001174
Chris Mason925baed2008-06-25 16:01:30 -04001175 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001176 btrfs_set_lock_blocking(right);
1177
Chris Mason5f39d392007-10-15 16:14:19 -04001178 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001179 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1180 wret = 1;
1181 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001182 ret = btrfs_cow_block(trans, root, right,
1183 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001184 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001185 if (ret)
1186 wret = 1;
1187 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001188 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001189 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001190 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001191 }
Chris Masone66f7092007-04-20 13:16:02 -04001192 if (wret < 0)
1193 ret = wret;
1194 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001195 struct btrfs_disk_key disk_key;
1196
1197 btrfs_node_key(right, &disk_key, 0);
1198 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1199 btrfs_mark_buffer_dirty(parent);
1200
1201 if (btrfs_header_nritems(mid) <= orig_slot) {
1202 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001203 path->slots[level + 1] += 1;
1204 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001205 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001206 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001207 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001208 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001209 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001210 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001211 }
Chris Masone66f7092007-04-20 13:16:02 -04001212 return 0;
1213 }
Chris Mason925baed2008-06-25 16:01:30 -04001214 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001215 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001216 }
Chris Masone66f7092007-04-20 13:16:02 -04001217 return 1;
1218}
1219
Chris Mason74123bd2007-02-02 11:05:29 -05001220/*
Chris Masond352ac62008-09-29 15:18:18 -04001221 * readahead one full node of leaves, finding things that are close
1222 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001223 */
Chris Masonc8c42862009-04-03 10:14:18 -04001224static void reada_for_search(struct btrfs_root *root,
1225 struct btrfs_path *path,
1226 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001227{
Chris Mason5f39d392007-10-15 16:14:19 -04001228 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001229 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001230 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001231 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001232 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001233 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001234 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001235 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001236 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001237 u32 nr;
1238 u32 blocksize;
1239 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001240
Chris Masona6b6e752007-10-15 16:22:39 -04001241 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001242 return;
1243
Chris Mason6702ed42007-08-07 16:15:09 -04001244 if (!path->nodes[level])
1245 return;
1246
Chris Mason5f39d392007-10-15 16:14:19 -04001247 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001248
Chris Mason3c69fae2007-08-07 15:52:22 -04001249 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001250 blocksize = btrfs_level_size(root, level - 1);
1251 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001252 if (eb) {
1253 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001254 return;
1255 }
1256
Chris Masona7175312009-01-22 09:23:10 -05001257 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001258
Chris Mason5f39d392007-10-15 16:14:19 -04001259 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001260 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001261
Chris Masond3977122009-01-05 21:25:51 -05001262 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001263 if (direction < 0) {
1264 if (nr == 0)
1265 break;
1266 nr--;
1267 } else if (direction > 0) {
1268 nr++;
1269 if (nr >= nritems)
1270 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 }
Chris Mason01f46652007-12-21 16:24:26 -05001272 if (path->reada < 0 && objectid) {
1273 btrfs_node_key(node, &disk_key, nr);
1274 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1275 break;
1276 }
Chris Mason6b800532007-10-15 16:17:34 -04001277 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001278 if ((search <= target && target - search <= 65536) ||
1279 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001280 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001281 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001282 nread += blocksize;
1283 }
1284 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001285 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001286 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001287 }
1288}
Chris Mason925baed2008-06-25 16:01:30 -04001289
Chris Masond352ac62008-09-29 15:18:18 -04001290/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001291 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1292 * cache
1293 */
1294static noinline int reada_for_balance(struct btrfs_root *root,
1295 struct btrfs_path *path, int level)
1296{
1297 int slot;
1298 int nritems;
1299 struct extent_buffer *parent;
1300 struct extent_buffer *eb;
1301 u64 gen;
1302 u64 block1 = 0;
1303 u64 block2 = 0;
1304 int ret = 0;
1305 int blocksize;
1306
Chris Mason8c594ea2009-04-20 15:50:10 -04001307 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001308 if (!parent)
1309 return 0;
1310
1311 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001312 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001313 blocksize = btrfs_level_size(root, level);
1314
1315 if (slot > 0) {
1316 block1 = btrfs_node_blockptr(parent, slot - 1);
1317 gen = btrfs_node_ptr_generation(parent, slot - 1);
1318 eb = btrfs_find_tree_block(root, block1, blocksize);
1319 if (eb && btrfs_buffer_uptodate(eb, gen))
1320 block1 = 0;
1321 free_extent_buffer(eb);
1322 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001323 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001324 block2 = btrfs_node_blockptr(parent, slot + 1);
1325 gen = btrfs_node_ptr_generation(parent, slot + 1);
1326 eb = btrfs_find_tree_block(root, block2, blocksize);
1327 if (eb && btrfs_buffer_uptodate(eb, gen))
1328 block2 = 0;
1329 free_extent_buffer(eb);
1330 }
1331 if (block1 || block2) {
1332 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001333
1334 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001335 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001336
1337 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001338 if (block1)
1339 readahead_tree_block(root, block1, blocksize, 0);
1340 if (block2)
1341 readahead_tree_block(root, block2, blocksize, 0);
1342
1343 if (block1) {
1344 eb = read_tree_block(root, block1, blocksize, 0);
1345 free_extent_buffer(eb);
1346 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001347 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001348 eb = read_tree_block(root, block2, blocksize, 0);
1349 free_extent_buffer(eb);
1350 }
1351 }
1352 return ret;
1353}
1354
1355
1356/*
Chris Masond3977122009-01-05 21:25:51 -05001357 * when we walk down the tree, it is usually safe to unlock the higher layers
1358 * in the tree. The exceptions are when our path goes through slot 0, because
1359 * operations on the tree might require changing key pointers higher up in the
1360 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001361 *
Chris Masond3977122009-01-05 21:25:51 -05001362 * callers might also have set path->keep_locks, which tells this code to keep
1363 * the lock if the path points to the last slot in the block. This is part of
1364 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001365 *
Chris Masond3977122009-01-05 21:25:51 -05001366 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1367 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001368 */
Chris Masone02119d2008-09-05 16:13:11 -04001369static noinline void unlock_up(struct btrfs_path *path, int level,
1370 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001371{
1372 int i;
1373 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001374 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001375 struct extent_buffer *t;
1376
1377 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1378 if (!path->nodes[i])
1379 break;
1380 if (!path->locks[i])
1381 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001382 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001383 skip_level = i + 1;
1384 continue;
1385 }
Chris Mason051e1b92008-06-25 16:01:30 -04001386 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001387 u32 nritems;
1388 t = path->nodes[i];
1389 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001390 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001391 skip_level = i + 1;
1392 continue;
1393 }
1394 }
Chris Mason051e1b92008-06-25 16:01:30 -04001395 if (skip_level < i && i >= lowest_unlock)
1396 no_skips = 1;
1397
Chris Mason925baed2008-06-25 16:01:30 -04001398 t = path->nodes[i];
1399 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001400 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001401 path->locks[i] = 0;
1402 }
1403 }
1404}
1405
Chris Mason3c69fae2007-08-07 15:52:22 -04001406/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001407 * This releases any locks held in the path starting at level and
1408 * going all the way up to the root.
1409 *
1410 * btrfs_search_slot will keep the lock held on higher nodes in a few
1411 * corner cases, such as COW of the block at slot zero in the node. This
1412 * ignores those rules, and it should only be called when there are no
1413 * more updates to be done higher up in the tree.
1414 */
1415noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1416{
1417 int i;
1418
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001419 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001420 return;
1421
1422 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1423 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001424 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001425 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001426 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001427 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001428 path->locks[i] = 0;
1429 }
1430}
1431
1432/*
Chris Masonc8c42862009-04-03 10:14:18 -04001433 * helper function for btrfs_search_slot. The goal is to find a block
1434 * in cache without setting the path to blocking. If we find the block
1435 * we return zero and the path is unchanged.
1436 *
1437 * If we can't find the block, we set the path blocking and do some
1438 * reada. -EAGAIN is returned and the search must be repeated.
1439 */
1440static int
1441read_block_for_search(struct btrfs_trans_handle *trans,
1442 struct btrfs_root *root, struct btrfs_path *p,
1443 struct extent_buffer **eb_ret, int level, int slot,
1444 struct btrfs_key *key)
1445{
1446 u64 blocknr;
1447 u64 gen;
1448 u32 blocksize;
1449 struct extent_buffer *b = *eb_ret;
1450 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001451 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001452
1453 blocknr = btrfs_node_blockptr(b, slot);
1454 gen = btrfs_node_ptr_generation(b, slot);
1455 blocksize = btrfs_level_size(root, level - 1);
1456
1457 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001458 if (tmp) {
1459 if (btrfs_buffer_uptodate(tmp, 0)) {
1460 if (btrfs_buffer_uptodate(tmp, gen)) {
1461 /*
1462 * we found an up to date block without
1463 * sleeping, return
1464 * right away
1465 */
1466 *eb_ret = tmp;
1467 return 0;
1468 }
1469 /* the pages were up to date, but we failed
1470 * the generation number check. Do a full
1471 * read for the generation number that is correct.
1472 * We must do this without dropping locks so
1473 * we can trust our generation number
1474 */
1475 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001476 btrfs_set_path_blocking(p);
1477
Chris Masoncb449212010-10-24 11:01:27 -04001478 tmp = read_tree_block(root, blocknr, blocksize, gen);
1479 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1480 *eb_ret = tmp;
1481 return 0;
1482 }
1483 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001484 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001485 return -EIO;
1486 }
Chris Masonc8c42862009-04-03 10:14:18 -04001487 }
1488
1489 /*
1490 * reduce lock contention at high levels
1491 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001492 * we read. Don't release the lock on the current
1493 * level because we need to walk this node to figure
1494 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001495 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001496 btrfs_unlock_up_safe(p, level + 1);
1497 btrfs_set_path_blocking(p);
1498
Chris Masoncb449212010-10-24 11:01:27 -04001499 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001500 if (p->reada)
1501 reada_for_search(root, p, level, slot, key->objectid);
1502
David Sterbab3b4aa72011-04-21 01:20:15 +02001503 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001504
1505 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001506 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001507 if (tmp) {
1508 /*
1509 * If the read above didn't mark this buffer up to date,
1510 * it will never end up being up to date. Set ret to EIO now
1511 * and give up so that our caller doesn't loop forever
1512 * on our EAGAINs.
1513 */
1514 if (!btrfs_buffer_uptodate(tmp, 0))
1515 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001516 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001517 }
1518 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001519}
1520
1521/*
1522 * helper function for btrfs_search_slot. This does all of the checks
1523 * for node-level blocks and does any balancing required based on
1524 * the ins_len.
1525 *
1526 * If no extra work was required, zero is returned. If we had to
1527 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1528 * start over
1529 */
1530static int
1531setup_nodes_for_search(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001533 struct extent_buffer *b, int level, int ins_len,
1534 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001535{
1536 int ret;
1537 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1538 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1539 int sret;
1540
Chris Masonbd681512011-07-16 15:23:14 -04001541 if (*write_lock_level < level + 1) {
1542 *write_lock_level = level + 1;
1543 btrfs_release_path(p);
1544 goto again;
1545 }
1546
Chris Masonc8c42862009-04-03 10:14:18 -04001547 sret = reada_for_balance(root, p, level);
1548 if (sret)
1549 goto again;
1550
1551 btrfs_set_path_blocking(p);
1552 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001553 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001554
1555 BUG_ON(sret > 0);
1556 if (sret) {
1557 ret = sret;
1558 goto done;
1559 }
1560 b = p->nodes[level];
1561 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001562 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001563 int sret;
1564
Chris Masonbd681512011-07-16 15:23:14 -04001565 if (*write_lock_level < level + 1) {
1566 *write_lock_level = level + 1;
1567 btrfs_release_path(p);
1568 goto again;
1569 }
1570
Chris Masonc8c42862009-04-03 10:14:18 -04001571 sret = reada_for_balance(root, p, level);
1572 if (sret)
1573 goto again;
1574
1575 btrfs_set_path_blocking(p);
1576 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001577 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001578
1579 if (sret) {
1580 ret = sret;
1581 goto done;
1582 }
1583 b = p->nodes[level];
1584 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001585 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001586 goto again;
1587 }
1588 BUG_ON(btrfs_header_nritems(b) == 1);
1589 }
1590 return 0;
1591
1592again:
1593 ret = -EAGAIN;
1594done:
1595 return ret;
1596}
1597
1598/*
Chris Mason74123bd2007-02-02 11:05:29 -05001599 * look for key in the tree. path is filled in with nodes along the way
1600 * if key is found, we return zero and you can find the item in the leaf
1601 * level of the path (level 0)
1602 *
1603 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001604 * be inserted, and 1 is returned. If there are other errors during the
1605 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001606 *
1607 * if ins_len > 0, nodes and leaves will be split as we walk down the
1608 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1609 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001610 */
Chris Masone089f052007-03-16 16:20:31 -04001611int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1612 *root, struct btrfs_key *key, struct btrfs_path *p, int
1613 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001614{
Chris Mason5f39d392007-10-15 16:14:19 -04001615 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001616 int slot;
1617 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001618 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001619 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001620 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001621 int root_lock;
1622 /* everything at write_lock_level or lower must be write locked */
1623 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001624 u8 lowest_level = 0;
1625
Chris Mason6702ed42007-08-07 16:15:09 -04001626 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001627 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001628 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001629
Chris Masonbd681512011-07-16 15:23:14 -04001630 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001631 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001632
Chris Masonbd681512011-07-16 15:23:14 -04001633 /* when we are removing items, we might have to go up to level
1634 * two as we update tree pointers Make sure we keep write
1635 * for those levels as well
1636 */
1637 write_lock_level = 2;
1638 } else if (ins_len > 0) {
1639 /*
1640 * for inserting items, make sure we have a write lock on
1641 * level 1 so we can update keys
1642 */
1643 write_lock_level = 1;
1644 }
1645
1646 if (!cow)
1647 write_lock_level = -1;
1648
1649 if (cow && (p->keep_locks || p->lowest_level))
1650 write_lock_level = BTRFS_MAX_LEVEL;
1651
Chris Masonbb803952007-03-01 12:04:21 -05001652again:
Chris Masonbd681512011-07-16 15:23:14 -04001653 /*
1654 * we try very hard to do read locks on the root
1655 */
1656 root_lock = BTRFS_READ_LOCK;
1657 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001658 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001659 /*
1660 * the commit roots are read only
1661 * so we always do read locks
1662 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001663 b = root->commit_root;
1664 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001665 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001666 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001667 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001669 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001670 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001671 level = btrfs_header_level(b);
1672 } else {
1673 /* we don't know the level of the root node
1674 * until we actually have it read locked
1675 */
1676 b = btrfs_read_lock_root_node(root);
1677 level = btrfs_header_level(b);
1678 if (level <= write_lock_level) {
1679 /* whoops, must trade for write lock */
1680 btrfs_tree_read_unlock(b);
1681 free_extent_buffer(b);
1682 b = btrfs_lock_root_node(root);
1683 root_lock = BTRFS_WRITE_LOCK;
1684
1685 /* the level might have changed, check again */
1686 level = btrfs_header_level(b);
1687 }
1688 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689 }
Chris Masonbd681512011-07-16 15:23:14 -04001690 p->nodes[level] = b;
1691 if (!p->skip_locking)
1692 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001693
Chris Masoneb60cea2007-02-02 09:18:22 -05001694 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001695 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001696
1697 /*
1698 * setup the path here so we can release it under lock
1699 * contention with the cow code
1700 */
Chris Mason02217ed2007-03-02 16:08:05 -05001701 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001702 /*
1703 * if we don't really need to cow this block
1704 * then we don't want to set the path blocking,
1705 * so we test it here
1706 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001707 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001708 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709
Chris Masonb4ce94d2009-02-04 09:25:08 -05001710 btrfs_set_path_blocking(p);
1711
Chris Masonbd681512011-07-16 15:23:14 -04001712 /*
1713 * must have write locks on this node and the
1714 * parent
1715 */
1716 if (level + 1 > write_lock_level) {
1717 write_lock_level = level + 1;
1718 btrfs_release_path(p);
1719 goto again;
1720 }
1721
Yan Zheng33c66f42009-07-22 09:59:00 -04001722 err = btrfs_cow_block(trans, root, b,
1723 p->nodes[level + 1],
1724 p->slots[level + 1], &b);
1725 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001726 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001727 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001728 }
Chris Mason02217ed2007-03-02 16:08:05 -05001729 }
Chris Mason65b51a02008-08-01 15:11:20 -04001730cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001731 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001732
Chris Masoneb60cea2007-02-02 09:18:22 -05001733 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001734 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001735
1736 /*
1737 * we have a lock on b and as long as we aren't changing
1738 * the tree, there is no way to for the items in b to change.
1739 * It is safe to drop the lock on our parent before we
1740 * go through the expensive btree search on b.
1741 *
1742 * If cow is true, then we might be changing slot zero,
1743 * which may require changing the parent. So, we can't
1744 * drop the lock until after we know which slot we're
1745 * operating on.
1746 */
1747 if (!cow)
1748 btrfs_unlock_up_safe(p, level + 1);
1749
Chris Mason5f39d392007-10-15 16:14:19 -04001750 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001751
Chris Mason5f39d392007-10-15 16:14:19 -04001752 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001753 int dec = 0;
1754 if (ret && slot > 0) {
1755 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001756 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001757 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001758 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001759 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001760 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001761 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001762 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001763 if (err) {
1764 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001765 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001766 }
Chris Masonc8c42862009-04-03 10:14:18 -04001767 b = p->nodes[level];
1768 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001769
Chris Masonbd681512011-07-16 15:23:14 -04001770 /*
1771 * slot 0 is special, if we change the key
1772 * we have to update the parent pointer
1773 * which means we must have a write lock
1774 * on the parent
1775 */
1776 if (slot == 0 && cow &&
1777 write_lock_level < level + 1) {
1778 write_lock_level = level + 1;
1779 btrfs_release_path(p);
1780 goto again;
1781 }
1782
Chris Masonf9efa9c2008-06-25 16:14:04 -04001783 unlock_up(p, level, lowest_unlock);
1784
Chris Mason925baed2008-06-25 16:01:30 -04001785 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001786 if (dec)
1787 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001788 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001789 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001790
Yan Zheng33c66f42009-07-22 09:59:00 -04001791 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001792 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001793 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001794 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001795 if (err) {
1796 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001797 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001798 }
Chris Mason76a05b32009-05-14 13:24:30 -04001799
Chris Masonb4ce94d2009-02-04 09:25:08 -05001800 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001801 level = btrfs_header_level(b);
1802 if (level <= write_lock_level) {
1803 err = btrfs_try_tree_write_lock(b);
1804 if (!err) {
1805 btrfs_set_path_blocking(p);
1806 btrfs_tree_lock(b);
1807 btrfs_clear_path_blocking(p, b,
1808 BTRFS_WRITE_LOCK);
1809 }
1810 p->locks[level] = BTRFS_WRITE_LOCK;
1811 } else {
1812 err = btrfs_try_tree_read_lock(b);
1813 if (!err) {
1814 btrfs_set_path_blocking(p);
1815 btrfs_tree_read_lock(b);
1816 btrfs_clear_path_blocking(p, b,
1817 BTRFS_READ_LOCK);
1818 }
1819 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820 }
Chris Masonbd681512011-07-16 15:23:14 -04001821 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001822 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001823 } else {
1824 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001825 if (ins_len > 0 &&
1826 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001827 if (write_lock_level < 1) {
1828 write_lock_level = 1;
1829 btrfs_release_path(p);
1830 goto again;
1831 }
1832
Chris Masonb4ce94d2009-02-04 09:25:08 -05001833 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001834 err = split_leaf(trans, root, key,
1835 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001836 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001837
Yan Zheng33c66f42009-07-22 09:59:00 -04001838 BUG_ON(err > 0);
1839 if (err) {
1840 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001841 goto done;
1842 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001843 }
Chris Mason459931e2008-12-10 09:10:46 -05001844 if (!p->search_for_split)
1845 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001846 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001847 }
1848 }
Chris Mason65b51a02008-08-01 15:11:20 -04001849 ret = 1;
1850done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001851 /*
1852 * we don't really know what they plan on doing with the path
1853 * from here on, so for now just mark it as blocking
1854 */
Chris Masonb9473432009-03-13 11:00:37 -04001855 if (!p->leave_spinning)
1856 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001857 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001858 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001859 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001860}
1861
Chris Mason74123bd2007-02-02 11:05:29 -05001862/*
1863 * adjust the pointers going up the tree, starting at level
1864 * making sure the right key of each node is points to 'key'.
1865 * This is used after shifting pointers to the left, so it stops
1866 * fixing up pointers when a given leaf/node is not in slot 0 of the
1867 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001868 *
1869 * If this fails to write a tree block, it returns -1, but continues
1870 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001871 */
Chris Mason5f39d392007-10-15 16:14:19 -04001872static int fixup_low_keys(struct btrfs_trans_handle *trans,
1873 struct btrfs_root *root, struct btrfs_path *path,
1874 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001875{
1876 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001877 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001878 struct extent_buffer *t;
1879
Chris Mason234b63a2007-03-13 10:46:10 -04001880 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001881 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001882 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001884 t = path->nodes[i];
1885 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001886 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001887 if (tslot != 0)
1888 break;
1889 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001890 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891}
1892
Chris Mason74123bd2007-02-02 11:05:29 -05001893/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001894 * update item key.
1895 *
1896 * This function isn't completely safe. It's the caller's responsibility
1897 * that the new key won't break the order
1898 */
1899int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1900 struct btrfs_root *root, struct btrfs_path *path,
1901 struct btrfs_key *new_key)
1902{
1903 struct btrfs_disk_key disk_key;
1904 struct extent_buffer *eb;
1905 int slot;
1906
1907 eb = path->nodes[0];
1908 slot = path->slots[0];
1909 if (slot > 0) {
1910 btrfs_item_key(eb, &disk_key, slot - 1);
1911 if (comp_keys(&disk_key, new_key) >= 0)
1912 return -1;
1913 }
1914 if (slot < btrfs_header_nritems(eb) - 1) {
1915 btrfs_item_key(eb, &disk_key, slot + 1);
1916 if (comp_keys(&disk_key, new_key) <= 0)
1917 return -1;
1918 }
1919
1920 btrfs_cpu_key_to_disk(&disk_key, new_key);
1921 btrfs_set_item_key(eb, &disk_key, slot);
1922 btrfs_mark_buffer_dirty(eb);
1923 if (slot == 0)
1924 fixup_low_keys(trans, root, path, &disk_key, 1);
1925 return 0;
1926}
1927
1928/*
Chris Mason74123bd2007-02-02 11:05:29 -05001929 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001930 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001931 *
1932 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1933 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001934 */
Chris Mason98ed5172008-01-03 10:01:48 -05001935static int push_node_left(struct btrfs_trans_handle *trans,
1936 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001937 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001939 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001940 int src_nritems;
1941 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001942 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001943
Chris Mason5f39d392007-10-15 16:14:19 -04001944 src_nritems = btrfs_header_nritems(src);
1945 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001946 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001947 WARN_ON(btrfs_header_generation(src) != trans->transid);
1948 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001949
Chris Masonbce4eae2008-04-24 14:42:46 -04001950 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001951 return 1;
1952
Chris Masond3977122009-01-05 21:25:51 -05001953 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001954 return 1;
1955
Chris Masonbce4eae2008-04-24 14:42:46 -04001956 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001957 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001958 if (push_items < src_nritems) {
1959 /* leave at least 8 pointers in the node if
1960 * we aren't going to empty it
1961 */
1962 if (src_nritems - push_items < 8) {
1963 if (push_items <= 8)
1964 return 1;
1965 push_items -= 8;
1966 }
1967 }
1968 } else
1969 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 copy_extent_buffer(dst, src,
1972 btrfs_node_key_ptr_offset(dst_nritems),
1973 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001974 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001975
Chris Masonbb803952007-03-01 12:04:21 -05001976 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001977 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1978 btrfs_node_key_ptr_offset(push_items),
1979 (src_nritems - push_items) *
1980 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001981 }
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_set_header_nritems(src, src_nritems - push_items);
1983 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1984 btrfs_mark_buffer_dirty(src);
1985 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001986
Chris Masonbb803952007-03-01 12:04:21 -05001987 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001988}
1989
Chris Mason97571fd2007-02-24 13:39:08 -05001990/*
Chris Mason79f95c82007-03-01 15:16:26 -05001991 * try to push data from one node into the next node right in the
1992 * tree.
1993 *
1994 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1995 * error, and > 0 if there was no room in the right hand block.
1996 *
1997 * this will only push up to 1/2 the contents of the left node over
1998 */
Chris Mason5f39d392007-10-15 16:14:19 -04001999static int balance_node_right(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root,
2001 struct extent_buffer *dst,
2002 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002003{
Chris Mason79f95c82007-03-01 15:16:26 -05002004 int push_items = 0;
2005 int max_push;
2006 int src_nritems;
2007 int dst_nritems;
2008 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002009
Chris Mason7bb86312007-12-11 09:25:06 -05002010 WARN_ON(btrfs_header_generation(src) != trans->transid);
2011 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2012
Chris Mason5f39d392007-10-15 16:14:19 -04002013 src_nritems = btrfs_header_nritems(src);
2014 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002015 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002016 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002017 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002018
Chris Masond3977122009-01-05 21:25:51 -05002019 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002020 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002021
2022 max_push = src_nritems / 2 + 1;
2023 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002024 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002025 return 1;
Yan252c38f2007-08-29 09:11:44 -04002026
Chris Mason79f95c82007-03-01 15:16:26 -05002027 if (max_push < push_items)
2028 push_items = max_push;
2029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2031 btrfs_node_key_ptr_offset(0),
2032 (dst_nritems) *
2033 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002034
Chris Mason5f39d392007-10-15 16:14:19 -04002035 copy_extent_buffer(dst, src,
2036 btrfs_node_key_ptr_offset(0),
2037 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002038 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002039
Chris Mason5f39d392007-10-15 16:14:19 -04002040 btrfs_set_header_nritems(src, src_nritems - push_items);
2041 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 btrfs_mark_buffer_dirty(src);
2044 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002045
Chris Mason79f95c82007-03-01 15:16:26 -05002046 return ret;
2047}
2048
2049/*
Chris Mason97571fd2007-02-24 13:39:08 -05002050 * helper function to insert a new root level in the tree.
2051 * A new node is allocated, and a single item is inserted to
2052 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002053 *
2054 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002055 */
Chris Masond3977122009-01-05 21:25:51 -05002056static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002057 struct btrfs_root *root,
2058 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002059{
Chris Mason7bb86312007-12-11 09:25:06 -05002060 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002061 struct extent_buffer *lower;
2062 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002063 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002064 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002065
2066 BUG_ON(path->nodes[level]);
2067 BUG_ON(path->nodes[level-1] != root->node);
2068
Chris Mason7bb86312007-12-11 09:25:06 -05002069 lower = path->nodes[level-1];
2070 if (level == 1)
2071 btrfs_item_key(lower, &lower_key, 0);
2072 else
2073 btrfs_node_key(lower, &lower_key, 0);
2074
Zheng Yan31840ae2008-09-23 13:14:14 -04002075 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002077 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002078 if (IS_ERR(c))
2079 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002080
Yan, Zhengf0486c62010-05-16 10:46:25 -04002081 root_add_used(root, root->nodesize);
2082
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002083 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002084 btrfs_set_header_nritems(c, 1);
2085 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002086 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002087 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002088 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 write_extent_buffer(c, root->fs_info->fsid,
2092 (unsigned long)btrfs_header_fsid(c),
2093 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002094
2095 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2096 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2097 BTRFS_UUID_SIZE);
2098
Chris Mason5f39d392007-10-15 16:14:19 -04002099 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002100 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002101 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002102 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002103
2104 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002105
2106 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002107
Chris Mason925baed2008-06-25 16:01:30 -04002108 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002109 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002110
2111 /* the super has an extra ref to root->node */
2112 free_extent_buffer(old);
2113
Chris Mason0b86a832008-03-24 15:01:56 -04002114 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002115 extent_buffer_get(c);
2116 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002117 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002118 path->slots[level] = 0;
2119 return 0;
2120}
2121
Chris Mason74123bd2007-02-02 11:05:29 -05002122/*
2123 * worker function to insert a single pointer in a node.
2124 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002125 *
Chris Mason74123bd2007-02-02 11:05:29 -05002126 * slot and level indicate where you want the key to go, and
2127 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002128 *
2129 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002130 */
Chris Masone089f052007-03-16 16:20:31 -04002131static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2132 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002133 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002134{
Chris Mason5f39d392007-10-15 16:14:19 -04002135 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002136 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002137
2138 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002139 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002140 lower = path->nodes[level];
2141 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002142 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002143 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002144 BUG();
2145 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002146 memmove_extent_buffer(lower,
2147 btrfs_node_key_ptr_offset(slot + 1),
2148 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002149 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002150 }
Chris Mason5f39d392007-10-15 16:14:19 -04002151 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002152 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002153 WARN_ON(trans->transid == 0);
2154 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002155 btrfs_set_header_nritems(lower, nritems + 1);
2156 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002157 return 0;
2158}
2159
Chris Mason97571fd2007-02-24 13:39:08 -05002160/*
2161 * split the node at the specified level in path in two.
2162 * The path is corrected to point to the appropriate node after the split
2163 *
2164 * Before splitting this tries to make some room in the node by pushing
2165 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002166 *
2167 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002168 */
Chris Masone02119d2008-09-05 16:13:11 -04002169static noinline int split_node(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002172{
Chris Mason5f39d392007-10-15 16:14:19 -04002173 struct extent_buffer *c;
2174 struct extent_buffer *split;
2175 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002176 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002177 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002178 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002179 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002180
Chris Mason5f39d392007-10-15 16:14:19 -04002181 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002182 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002183 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002184 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002185 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002186 if (ret)
2187 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002188 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002189 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002190 c = path->nodes[level];
2191 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002192 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002193 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002194 if (ret < 0)
2195 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002196 }
Chris Masone66f7092007-04-20 13:16:02 -04002197
Chris Mason5f39d392007-10-15 16:14:19 -04002198 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002199 mid = (c_nritems + 1) / 2;
2200 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002201
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002203 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002205 if (IS_ERR(split))
2206 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002207
Yan, Zhengf0486c62010-05-16 10:46:25 -04002208 root_add_used(root, root->nodesize);
2209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002210 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002211 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002212 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002213 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002214 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 btrfs_set_header_owner(split, root->root_key.objectid);
2216 write_extent_buffer(split, root->fs_info->fsid,
2217 (unsigned long)btrfs_header_fsid(split),
2218 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002219 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2220 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2221 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002222
Chris Mason5f39d392007-10-15 16:14:19 -04002223
2224 copy_extent_buffer(split, c,
2225 btrfs_node_key_ptr_offset(0),
2226 btrfs_node_key_ptr_offset(mid),
2227 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2228 btrfs_set_header_nritems(split, c_nritems - mid);
2229 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002230 ret = 0;
2231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 btrfs_mark_buffer_dirty(c);
2233 btrfs_mark_buffer_dirty(split);
2234
Chris Masondb945352007-10-15 16:15:53 -04002235 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002236 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002237 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002238 if (wret)
2239 ret = wret;
2240
Chris Mason5de08d72007-02-24 06:24:44 -05002241 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002242 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002243 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002244 free_extent_buffer(c);
2245 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002246 path->slots[level + 1] += 1;
2247 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002248 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002249 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002250 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002251 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002252}
2253
Chris Mason74123bd2007-02-02 11:05:29 -05002254/*
2255 * how many bytes are required to store the items in a leaf. start
2256 * and nr indicate which items in the leaf to check. This totals up the
2257 * space used both by the item structs and the item data
2258 */
Chris Mason5f39d392007-10-15 16:14:19 -04002259static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002260{
2261 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002262 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002263 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002264
2265 if (!nr)
2266 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002267 data_len = btrfs_item_end_nr(l, start);
2268 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002269 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002270 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002271 return data_len;
2272}
2273
Chris Mason74123bd2007-02-02 11:05:29 -05002274/*
Chris Masond4dbff92007-04-04 14:08:15 -04002275 * The space between the end of the leaf items and
2276 * the start of the leaf data. IOW, how much room
2277 * the leaf has left for both items and data
2278 */
Chris Masond3977122009-01-05 21:25:51 -05002279noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002280 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002281{
Chris Mason5f39d392007-10-15 16:14:19 -04002282 int nritems = btrfs_header_nritems(leaf);
2283 int ret;
2284 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2285 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002286 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2287 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002288 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002289 leaf_space_used(leaf, 0, nritems), nritems);
2290 }
2291 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002292}
2293
Chris Mason99d8f832010-07-07 10:51:48 -04002294/*
2295 * min slot controls the lowest index we're willing to push to the
2296 * right. We'll push up to and including min_slot, but no lower
2297 */
Chris Mason44871b12009-03-13 10:04:31 -04002298static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2299 struct btrfs_root *root,
2300 struct btrfs_path *path,
2301 int data_size, int empty,
2302 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002303 int free_space, u32 left_nritems,
2304 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002305{
Chris Mason5f39d392007-10-15 16:14:19 -04002306 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002307 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002308 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002309 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002310 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002311 int push_space = 0;
2312 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002313 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002314 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002315 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002316 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002317 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002318
Chris Mason34a38212007-11-07 13:31:03 -05002319 if (empty)
2320 nr = 0;
2321 else
Chris Mason99d8f832010-07-07 10:51:48 -04002322 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002323
Zheng Yan31840ae2008-09-23 13:14:14 -04002324 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002325 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002326
Chris Mason44871b12009-03-13 10:04:31 -04002327 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002328 i = left_nritems - 1;
2329 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002330 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002331
Zheng Yan31840ae2008-09-23 13:14:14 -04002332 if (!empty && push_items > 0) {
2333 if (path->slots[0] > i)
2334 break;
2335 if (path->slots[0] == i) {
2336 int space = btrfs_leaf_free_space(root, left);
2337 if (space + push_space * 2 > free_space)
2338 break;
2339 }
2340 }
2341
Chris Mason00ec4c52007-02-24 12:47:20 -05002342 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002343 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002344
Chris Masondb945352007-10-15 16:15:53 -04002345 this_item_size = btrfs_item_size(left, item);
2346 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002348
Chris Mason00ec4c52007-02-24 12:47:20 -05002349 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002350 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002351 if (i == 0)
2352 break;
2353 i--;
Chris Masondb945352007-10-15 16:15:53 -04002354 }
Chris Mason5f39d392007-10-15 16:14:19 -04002355
Chris Mason925baed2008-06-25 16:01:30 -04002356 if (push_items == 0)
2357 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002358
Chris Mason34a38212007-11-07 13:31:03 -05002359 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002360 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002361
Chris Mason00ec4c52007-02-24 12:47:20 -05002362 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002363 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002364
Chris Mason5f39d392007-10-15 16:14:19 -04002365 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002366 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Chris Mason00ec4c52007-02-24 12:47:20 -05002368 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002369 data_end = leaf_data_end(root, right);
2370 memmove_extent_buffer(right,
2371 btrfs_leaf_data(right) + data_end - push_space,
2372 btrfs_leaf_data(right) + data_end,
2373 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2374
Chris Mason00ec4c52007-02-24 12:47:20 -05002375 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002376 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002377 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2378 btrfs_leaf_data(left) + leaf_data_end(root, left),
2379 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002380
2381 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2382 btrfs_item_nr_offset(0),
2383 right_nritems * sizeof(struct btrfs_item));
2384
Chris Mason00ec4c52007-02-24 12:47:20 -05002385 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002386 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2387 btrfs_item_nr_offset(left_nritems - push_items),
2388 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002389
2390 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002391 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002392 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002393 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002394 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002395 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002396 push_space -= btrfs_item_size(right, item);
2397 btrfs_set_item_offset(right, item, push_space);
2398 }
2399
Chris Mason7518a232007-03-12 12:01:18 -04002400 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002401 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002402
Chris Mason34a38212007-11-07 13:31:03 -05002403 if (left_nritems)
2404 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002405 else
2406 clean_tree_block(trans, root, left);
2407
Chris Mason5f39d392007-10-15 16:14:19 -04002408 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002409
Chris Mason5f39d392007-10-15 16:14:19 -04002410 btrfs_item_key(right, &disk_key, 0);
2411 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002412 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002413
Chris Mason00ec4c52007-02-24 12:47:20 -05002414 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002415 if (path->slots[0] >= left_nritems) {
2416 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002417 if (btrfs_header_nritems(path->nodes[0]) == 0)
2418 clean_tree_block(trans, root, path->nodes[0]);
2419 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002420 free_extent_buffer(path->nodes[0]);
2421 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002422 path->slots[1] += 1;
2423 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002424 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002425 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002426 }
2427 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002428
2429out_unlock:
2430 btrfs_tree_unlock(right);
2431 free_extent_buffer(right);
2432 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002433}
Chris Mason925baed2008-06-25 16:01:30 -04002434
Chris Mason00ec4c52007-02-24 12:47:20 -05002435/*
Chris Mason44871b12009-03-13 10:04:31 -04002436 * push some data in the path leaf to the right, trying to free up at
2437 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2438 *
2439 * returns 1 if the push failed because the other node didn't have enough
2440 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002441 *
2442 * this will push starting from min_slot to the end of the leaf. It won't
2443 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002444 */
2445static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002446 *root, struct btrfs_path *path,
2447 int min_data_size, int data_size,
2448 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002449{
2450 struct extent_buffer *left = path->nodes[0];
2451 struct extent_buffer *right;
2452 struct extent_buffer *upper;
2453 int slot;
2454 int free_space;
2455 u32 left_nritems;
2456 int ret;
2457
2458 if (!path->nodes[1])
2459 return 1;
2460
2461 slot = path->slots[1];
2462 upper = path->nodes[1];
2463 if (slot >= btrfs_header_nritems(upper) - 1)
2464 return 1;
2465
2466 btrfs_assert_tree_locked(path->nodes[1]);
2467
2468 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002469 if (right == NULL)
2470 return 1;
2471
Chris Mason44871b12009-03-13 10:04:31 -04002472 btrfs_tree_lock(right);
2473 btrfs_set_lock_blocking(right);
2474
2475 free_space = btrfs_leaf_free_space(root, right);
2476 if (free_space < data_size)
2477 goto out_unlock;
2478
2479 /* cow and double check */
2480 ret = btrfs_cow_block(trans, root, right, upper,
2481 slot + 1, &right);
2482 if (ret)
2483 goto out_unlock;
2484
2485 free_space = btrfs_leaf_free_space(root, right);
2486 if (free_space < data_size)
2487 goto out_unlock;
2488
2489 left_nritems = btrfs_header_nritems(left);
2490 if (left_nritems == 0)
2491 goto out_unlock;
2492
Chris Mason99d8f832010-07-07 10:51:48 -04002493 return __push_leaf_right(trans, root, path, min_data_size, empty,
2494 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002495out_unlock:
2496 btrfs_tree_unlock(right);
2497 free_extent_buffer(right);
2498 return 1;
2499}
2500
2501/*
Chris Mason74123bd2007-02-02 11:05:29 -05002502 * push some data in the path leaf to the left, trying to free up at
2503 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002504 *
2505 * max_slot can put a limit on how far into the leaf we'll push items. The
2506 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2507 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002508 */
Chris Mason44871b12009-03-13 10:04:31 -04002509static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2510 struct btrfs_root *root,
2511 struct btrfs_path *path, int data_size,
2512 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002513 int free_space, u32 right_nritems,
2514 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515{
Chris Mason5f39d392007-10-15 16:14:19 -04002516 struct btrfs_disk_key disk_key;
2517 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002519 int push_space = 0;
2520 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002521 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002522 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002523 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002524 int ret = 0;
2525 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002526 u32 this_item_size;
2527 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002528
Chris Mason34a38212007-11-07 13:31:03 -05002529 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002530 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002531 else
Chris Mason99d8f832010-07-07 10:51:48 -04002532 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002533
2534 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002535 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002536
Zheng Yan31840ae2008-09-23 13:14:14 -04002537 if (!empty && push_items > 0) {
2538 if (path->slots[0] < i)
2539 break;
2540 if (path->slots[0] == i) {
2541 int space = btrfs_leaf_free_space(root, right);
2542 if (space + push_space * 2 > free_space)
2543 break;
2544 }
2545 }
2546
Chris Masonbe0e5c02007-01-26 15:51:26 -05002547 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002548 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002549
2550 this_item_size = btrfs_item_size(right, item);
2551 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002552 break;
Chris Masondb945352007-10-15 16:15:53 -04002553
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002555 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556 }
Chris Masondb945352007-10-15 16:15:53 -04002557
Chris Masonbe0e5c02007-01-26 15:51:26 -05002558 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002559 ret = 1;
2560 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 }
Chris Mason34a38212007-11-07 13:31:03 -05002562 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002563 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002564
Chris Masonbe0e5c02007-01-26 15:51:26 -05002565 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002566 copy_extent_buffer(left, right,
2567 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2568 btrfs_item_nr_offset(0),
2569 push_items * sizeof(struct btrfs_item));
2570
Chris Mason123abc82007-03-14 14:14:43 -04002571 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002572 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002573
2574 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002575 leaf_data_end(root, left) - push_space,
2576 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002577 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002578 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002579 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002580 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002581
Chris Masondb945352007-10-15 16:15:53 -04002582 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002583 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002584 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002585
Chris Mason5f39d392007-10-15 16:14:19 -04002586 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002587
Chris Mason5f39d392007-10-15 16:14:19 -04002588 ioff = btrfs_item_offset(left, item);
2589 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002590 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002591 }
Chris Mason5f39d392007-10-15 16:14:19 -04002592 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593
2594 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002595 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002596 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2597 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002598 WARN_ON(1);
2599 }
Chris Mason5f39d392007-10-15 16:14:19 -04002600
Chris Mason34a38212007-11-07 13:31:03 -05002601 if (push_items < right_nritems) {
2602 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2603 leaf_data_end(root, right);
2604 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2605 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2606 btrfs_leaf_data(right) +
2607 leaf_data_end(root, right), push_space);
2608
2609 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002610 btrfs_item_nr_offset(push_items),
2611 (btrfs_header_nritems(right) - push_items) *
2612 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002613 }
Yaneef1c492007-11-26 10:58:13 -05002614 right_nritems -= push_items;
2615 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002616 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002617 for (i = 0; i < right_nritems; i++) {
2618 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002619
Chris Masondb945352007-10-15 16:15:53 -04002620 push_space = push_space - btrfs_item_size(right, item);
2621 btrfs_set_item_offset(right, item, push_space);
2622 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002623
Chris Mason5f39d392007-10-15 16:14:19 -04002624 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002625 if (right_nritems)
2626 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002627 else
2628 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002629
Chris Mason5f39d392007-10-15 16:14:19 -04002630 btrfs_item_key(right, &disk_key, 0);
2631 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002632 if (wret)
2633 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002634
2635 /* then fixup the leaf pointer in the path */
2636 if (path->slots[0] < push_items) {
2637 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002638 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002639 free_extent_buffer(path->nodes[0]);
2640 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002641 path->slots[1] -= 1;
2642 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002643 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002644 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645 path->slots[0] -= push_items;
2646 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002647 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002648 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002649out:
2650 btrfs_tree_unlock(left);
2651 free_extent_buffer(left);
2652 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002653}
2654
Chris Mason74123bd2007-02-02 11:05:29 -05002655/*
Chris Mason44871b12009-03-13 10:04:31 -04002656 * push some data in the path leaf to the left, trying to free up at
2657 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002658 *
2659 * max_slot can put a limit on how far into the leaf we'll push items. The
2660 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2661 * items
Chris Mason44871b12009-03-13 10:04:31 -04002662 */
2663static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002664 *root, struct btrfs_path *path, int min_data_size,
2665 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002666{
2667 struct extent_buffer *right = path->nodes[0];
2668 struct extent_buffer *left;
2669 int slot;
2670 int free_space;
2671 u32 right_nritems;
2672 int ret = 0;
2673
2674 slot = path->slots[1];
2675 if (slot == 0)
2676 return 1;
2677 if (!path->nodes[1])
2678 return 1;
2679
2680 right_nritems = btrfs_header_nritems(right);
2681 if (right_nritems == 0)
2682 return 1;
2683
2684 btrfs_assert_tree_locked(path->nodes[1]);
2685
2686 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002687 if (left == NULL)
2688 return 1;
2689
Chris Mason44871b12009-03-13 10:04:31 -04002690 btrfs_tree_lock(left);
2691 btrfs_set_lock_blocking(left);
2692
2693 free_space = btrfs_leaf_free_space(root, left);
2694 if (free_space < data_size) {
2695 ret = 1;
2696 goto out;
2697 }
2698
2699 /* cow and double check */
2700 ret = btrfs_cow_block(trans, root, left,
2701 path->nodes[1], slot - 1, &left);
2702 if (ret) {
2703 /* we hit -ENOSPC, but it isn't fatal here */
2704 ret = 1;
2705 goto out;
2706 }
2707
2708 free_space = btrfs_leaf_free_space(root, left);
2709 if (free_space < data_size) {
2710 ret = 1;
2711 goto out;
2712 }
2713
Chris Mason99d8f832010-07-07 10:51:48 -04002714 return __push_leaf_left(trans, root, path, min_data_size,
2715 empty, left, free_space, right_nritems,
2716 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002717out:
2718 btrfs_tree_unlock(left);
2719 free_extent_buffer(left);
2720 return ret;
2721}
2722
2723/*
Chris Mason74123bd2007-02-02 11:05:29 -05002724 * split the path's leaf in two, making sure there is at least data_size
2725 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002726 *
2727 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002728 */
Chris Mason44871b12009-03-13 10:04:31 -04002729static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002730 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002731 struct btrfs_path *path,
2732 struct extent_buffer *l,
2733 struct extent_buffer *right,
2734 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002735{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002736 int data_copy_size;
2737 int rt_data_off;
2738 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002739 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002740 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002741 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002742
Chris Mason5f39d392007-10-15 16:14:19 -04002743 nritems = nritems - mid;
2744 btrfs_set_header_nritems(right, nritems);
2745 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2746
2747 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2748 btrfs_item_nr_offset(mid),
2749 nritems * sizeof(struct btrfs_item));
2750
2751 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002752 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2753 data_copy_size, btrfs_leaf_data(l) +
2754 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002755
Chris Mason5f39d392007-10-15 16:14:19 -04002756 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2757 btrfs_item_end_nr(l, mid);
2758
2759 for (i = 0; i < nritems; i++) {
2760 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002761 u32 ioff;
2762
Chris Masondb945352007-10-15 16:15:53 -04002763 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002764 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002765 }
Chris Mason74123bd2007-02-02 11:05:29 -05002766
Chris Mason5f39d392007-10-15 16:14:19 -04002767 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002768 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002769 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002770 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2771 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002772 if (wret)
2773 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002774
2775 btrfs_mark_buffer_dirty(right);
2776 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002777 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002778
Chris Masonbe0e5c02007-01-26 15:51:26 -05002779 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002780 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002781 free_extent_buffer(path->nodes[0]);
2782 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002783 path->slots[0] -= mid;
2784 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002785 } else {
2786 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002787 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002788 }
Chris Mason5f39d392007-10-15 16:14:19 -04002789
Chris Masoneb60cea2007-02-02 09:18:22 -05002790 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002791
Chris Mason44871b12009-03-13 10:04:31 -04002792 return ret;
2793}
2794
2795/*
Chris Mason99d8f832010-07-07 10:51:48 -04002796 * double splits happen when we need to insert a big item in the middle
2797 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2798 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2799 * A B C
2800 *
2801 * We avoid this by trying to push the items on either side of our target
2802 * into the adjacent leaves. If all goes well we can avoid the double split
2803 * completely.
2804 */
2805static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2806 struct btrfs_root *root,
2807 struct btrfs_path *path,
2808 int data_size)
2809{
2810 int ret;
2811 int progress = 0;
2812 int slot;
2813 u32 nritems;
2814
2815 slot = path->slots[0];
2816
2817 /*
2818 * try to push all the items after our slot into the
2819 * right leaf
2820 */
2821 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2822 if (ret < 0)
2823 return ret;
2824
2825 if (ret == 0)
2826 progress++;
2827
2828 nritems = btrfs_header_nritems(path->nodes[0]);
2829 /*
2830 * our goal is to get our slot at the start or end of a leaf. If
2831 * we've done so we're done
2832 */
2833 if (path->slots[0] == 0 || path->slots[0] == nritems)
2834 return 0;
2835
2836 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2837 return 0;
2838
2839 /* try to push all the items before our slot into the next leaf */
2840 slot = path->slots[0];
2841 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2842 if (ret < 0)
2843 return ret;
2844
2845 if (ret == 0)
2846 progress++;
2847
2848 if (progress)
2849 return 0;
2850 return 1;
2851}
2852
2853/*
Chris Mason44871b12009-03-13 10:04:31 -04002854 * split the path's leaf in two, making sure there is at least data_size
2855 * available for the resulting leaf level of the path.
2856 *
2857 * returns 0 if all went well and < 0 on failure.
2858 */
2859static noinline int split_leaf(struct btrfs_trans_handle *trans,
2860 struct btrfs_root *root,
2861 struct btrfs_key *ins_key,
2862 struct btrfs_path *path, int data_size,
2863 int extend)
2864{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002865 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002866 struct extent_buffer *l;
2867 u32 nritems;
2868 int mid;
2869 int slot;
2870 struct extent_buffer *right;
2871 int ret = 0;
2872 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002873 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002874 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002875 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002876
Yan, Zhenga5719522009-09-24 09:17:31 -04002877 l = path->nodes[0];
2878 slot = path->slots[0];
2879 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2880 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2881 return -EOVERFLOW;
2882
Chris Mason44871b12009-03-13 10:04:31 -04002883 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002884 if (data_size) {
2885 wret = push_leaf_right(trans, root, path, data_size,
2886 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002887 if (wret < 0)
2888 return wret;
2889 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002890 wret = push_leaf_left(trans, root, path, data_size,
2891 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002892 if (wret < 0)
2893 return wret;
2894 }
2895 l = path->nodes[0];
2896
2897 /* did the pushes work? */
2898 if (btrfs_leaf_free_space(root, l) >= data_size)
2899 return 0;
2900 }
2901
2902 if (!path->nodes[1]) {
2903 ret = insert_new_root(trans, root, path, 1);
2904 if (ret)
2905 return ret;
2906 }
2907again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002908 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002909 l = path->nodes[0];
2910 slot = path->slots[0];
2911 nritems = btrfs_header_nritems(l);
2912 mid = (nritems + 1) / 2;
2913
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002914 if (mid <= slot) {
2915 if (nritems == 1 ||
2916 leaf_space_used(l, mid, nritems - mid) + data_size >
2917 BTRFS_LEAF_DATA_SIZE(root)) {
2918 if (slot >= nritems) {
2919 split = 0;
2920 } else {
2921 mid = slot;
2922 if (mid != nritems &&
2923 leaf_space_used(l, mid, nritems - mid) +
2924 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002925 if (data_size && !tried_avoid_double)
2926 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002927 split = 2;
2928 }
2929 }
2930 }
2931 } else {
2932 if (leaf_space_used(l, 0, mid) + data_size >
2933 BTRFS_LEAF_DATA_SIZE(root)) {
2934 if (!extend && data_size && slot == 0) {
2935 split = 0;
2936 } else if ((extend || !data_size) && slot == 0) {
2937 mid = 1;
2938 } else {
2939 mid = slot;
2940 if (mid != nritems &&
2941 leaf_space_used(l, mid, nritems - mid) +
2942 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002943 if (data_size && !tried_avoid_double)
2944 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002945 split = 2 ;
2946 }
2947 }
2948 }
2949 }
2950
2951 if (split == 0)
2952 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2953 else
2954 btrfs_item_key(l, &disk_key, mid);
2955
2956 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002957 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002958 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002959 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002960 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002961
2962 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002963
2964 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2965 btrfs_set_header_bytenr(right, right->start);
2966 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002967 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002968 btrfs_set_header_owner(right, root->root_key.objectid);
2969 btrfs_set_header_level(right, 0);
2970 write_extent_buffer(right, root->fs_info->fsid,
2971 (unsigned long)btrfs_header_fsid(right),
2972 BTRFS_FSID_SIZE);
2973
2974 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2975 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2976 BTRFS_UUID_SIZE);
2977
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002978 if (split == 0) {
2979 if (mid <= slot) {
2980 btrfs_set_header_nritems(right, 0);
2981 wret = insert_ptr(trans, root, path,
2982 &disk_key, right->start,
2983 path->slots[1] + 1, 1);
2984 if (wret)
2985 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002986
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 btrfs_tree_unlock(path->nodes[0]);
2988 free_extent_buffer(path->nodes[0]);
2989 path->nodes[0] = right;
2990 path->slots[0] = 0;
2991 path->slots[1] += 1;
2992 } else {
2993 btrfs_set_header_nritems(right, 0);
2994 wret = insert_ptr(trans, root, path,
2995 &disk_key,
2996 right->start,
2997 path->slots[1], 1);
2998 if (wret)
2999 ret = wret;
3000 btrfs_tree_unlock(path->nodes[0]);
3001 free_extent_buffer(path->nodes[0]);
3002 path->nodes[0] = right;
3003 path->slots[0] = 0;
3004 if (path->slots[1] == 0) {
3005 wret = fixup_low_keys(trans, root,
3006 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003007 if (wret)
3008 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003009 }
3010 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003011 btrfs_mark_buffer_dirty(right);
3012 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003013 }
3014
3015 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3016 BUG_ON(ret);
3017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003019 BUG_ON(num_doubles != 0);
3020 num_doubles++;
3021 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003022 }
Chris Mason44871b12009-03-13 10:04:31 -04003023
Chris Masonbe0e5c02007-01-26 15:51:26 -05003024 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003025
3026push_for_double:
3027 push_for_double_split(trans, root, path, data_size);
3028 tried_avoid_double = 1;
3029 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3030 return 0;
3031 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003032}
3033
Yan, Zhengad48fd752009-11-12 09:33:58 +00003034static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3035 struct btrfs_root *root,
3036 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003037{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003038 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003039 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003040 struct btrfs_file_extent_item *fi;
3041 u64 extent_len = 0;
3042 u32 item_size;
3043 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003044
3045 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003046 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3047
3048 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3049 key.type != BTRFS_EXTENT_CSUM_KEY);
3050
3051 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3052 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003053
3054 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003055 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3056 fi = btrfs_item_ptr(leaf, path->slots[0],
3057 struct btrfs_file_extent_item);
3058 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3059 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003060 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003061
Chris Mason459931e2008-12-10 09:10:46 -05003062 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003063 path->search_for_split = 1;
3064 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003065 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003066 if (ret < 0)
3067 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003068
Yan, Zhengad48fd752009-11-12 09:33:58 +00003069 ret = -EAGAIN;
3070 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003071 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003072 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3073 goto err;
3074
Chris Mason109f6ae2010-04-02 09:20:18 -04003075 /* the leaf has changed, it now has room. return now */
3076 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3077 goto err;
3078
Yan, Zhengad48fd752009-11-12 09:33:58 +00003079 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3080 fi = btrfs_item_ptr(leaf, path->slots[0],
3081 struct btrfs_file_extent_item);
3082 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3083 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003084 }
3085
Chris Masonb9473432009-03-13 11:00:37 -04003086 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003087 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003088 if (ret)
3089 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003090
Yan, Zhengad48fd752009-11-12 09:33:58 +00003091 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003092 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003093 return 0;
3094err:
3095 path->keep_locks = 0;
3096 return ret;
3097}
3098
3099static noinline int split_item(struct btrfs_trans_handle *trans,
3100 struct btrfs_root *root,
3101 struct btrfs_path *path,
3102 struct btrfs_key *new_key,
3103 unsigned long split_offset)
3104{
3105 struct extent_buffer *leaf;
3106 struct btrfs_item *item;
3107 struct btrfs_item *new_item;
3108 int slot;
3109 char *buf;
3110 u32 nritems;
3111 u32 item_size;
3112 u32 orig_offset;
3113 struct btrfs_disk_key disk_key;
3114
Chris Masonb9473432009-03-13 11:00:37 -04003115 leaf = path->nodes[0];
3116 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3117
Chris Masonb4ce94d2009-02-04 09:25:08 -05003118 btrfs_set_path_blocking(path);
3119
Chris Mason459931e2008-12-10 09:10:46 -05003120 item = btrfs_item_nr(leaf, path->slots[0]);
3121 orig_offset = btrfs_item_offset(leaf, item);
3122 item_size = btrfs_item_size(leaf, item);
3123
Chris Mason459931e2008-12-10 09:10:46 -05003124 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003125 if (!buf)
3126 return -ENOMEM;
3127
Chris Mason459931e2008-12-10 09:10:46 -05003128 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3129 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003130
Chris Mason459931e2008-12-10 09:10:46 -05003131 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003132 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003133 if (slot != nritems) {
3134 /* shift the items */
3135 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003136 btrfs_item_nr_offset(slot),
3137 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003138 }
3139
3140 btrfs_cpu_key_to_disk(&disk_key, new_key);
3141 btrfs_set_item_key(leaf, &disk_key, slot);
3142
3143 new_item = btrfs_item_nr(leaf, slot);
3144
3145 btrfs_set_item_offset(leaf, new_item, orig_offset);
3146 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3147
3148 btrfs_set_item_offset(leaf, item,
3149 orig_offset + item_size - split_offset);
3150 btrfs_set_item_size(leaf, item, split_offset);
3151
3152 btrfs_set_header_nritems(leaf, nritems + 1);
3153
3154 /* write the data for the start of the original item */
3155 write_extent_buffer(leaf, buf,
3156 btrfs_item_ptr_offset(leaf, path->slots[0]),
3157 split_offset);
3158
3159 /* write the data for the new item */
3160 write_extent_buffer(leaf, buf + split_offset,
3161 btrfs_item_ptr_offset(leaf, slot),
3162 item_size - split_offset);
3163 btrfs_mark_buffer_dirty(leaf);
3164
Yan, Zhengad48fd752009-11-12 09:33:58 +00003165 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003166 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003167 return 0;
3168}
3169
3170/*
3171 * This function splits a single item into two items,
3172 * giving 'new_key' to the new item and splitting the
3173 * old one at split_offset (from the start of the item).
3174 *
3175 * The path may be released by this operation. After
3176 * the split, the path is pointing to the old item. The
3177 * new item is going to be in the same node as the old one.
3178 *
3179 * Note, the item being split must be smaller enough to live alone on
3180 * a tree block with room for one extra struct btrfs_item
3181 *
3182 * This allows us to split the item in place, keeping a lock on the
3183 * leaf the entire time.
3184 */
3185int btrfs_split_item(struct btrfs_trans_handle *trans,
3186 struct btrfs_root *root,
3187 struct btrfs_path *path,
3188 struct btrfs_key *new_key,
3189 unsigned long split_offset)
3190{
3191 int ret;
3192 ret = setup_leaf_for_split(trans, root, path,
3193 sizeof(struct btrfs_item));
3194 if (ret)
3195 return ret;
3196
3197 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003198 return ret;
3199}
3200
3201/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003202 * This function duplicate a item, giving 'new_key' to the new item.
3203 * It guarantees both items live in the same tree leaf and the new item
3204 * is contiguous with the original item.
3205 *
3206 * This allows us to split file extent in place, keeping a lock on the
3207 * leaf the entire time.
3208 */
3209int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3210 struct btrfs_root *root,
3211 struct btrfs_path *path,
3212 struct btrfs_key *new_key)
3213{
3214 struct extent_buffer *leaf;
3215 int ret;
3216 u32 item_size;
3217
3218 leaf = path->nodes[0];
3219 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3220 ret = setup_leaf_for_split(trans, root, path,
3221 item_size + sizeof(struct btrfs_item));
3222 if (ret)
3223 return ret;
3224
3225 path->slots[0]++;
3226 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3227 item_size, item_size +
3228 sizeof(struct btrfs_item), 1);
3229 BUG_ON(ret);
3230
3231 leaf = path->nodes[0];
3232 memcpy_extent_buffer(leaf,
3233 btrfs_item_ptr_offset(leaf, path->slots[0]),
3234 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3235 item_size);
3236 return 0;
3237}
3238
3239/*
Chris Masond352ac62008-09-29 15:18:18 -04003240 * make the item pointed to by the path smaller. new_size indicates
3241 * how small to make it, and from_end tells us if we just chop bytes
3242 * off the end of the item or if we shift the item to chop bytes off
3243 * the front.
3244 */
Chris Masonb18c6682007-04-17 13:26:50 -04003245int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3246 struct btrfs_root *root,
3247 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003248 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003249{
Chris Masonb18c6682007-04-17 13:26:50 -04003250 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003251 struct extent_buffer *leaf;
3252 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003253 u32 nritems;
3254 unsigned int data_end;
3255 unsigned int old_data_start;
3256 unsigned int old_size;
3257 unsigned int size_diff;
3258 int i;
3259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003261 slot = path->slots[0];
3262
3263 old_size = btrfs_item_size_nr(leaf, slot);
3264 if (old_size == new_size)
3265 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003266
Chris Mason5f39d392007-10-15 16:14:19 -04003267 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003268 data_end = leaf_data_end(root, leaf);
3269
Chris Mason5f39d392007-10-15 16:14:19 -04003270 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003271
Chris Masonb18c6682007-04-17 13:26:50 -04003272 size_diff = old_size - new_size;
3273
3274 BUG_ON(slot < 0);
3275 BUG_ON(slot >= nritems);
3276
3277 /*
3278 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3279 */
3280 /* first correct the data pointers */
3281 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003282 u32 ioff;
3283 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003284
Chris Mason5f39d392007-10-15 16:14:19 -04003285 ioff = btrfs_item_offset(leaf, item);
3286 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003287 }
Chris Masondb945352007-10-15 16:15:53 -04003288
Chris Masonb18c6682007-04-17 13:26:50 -04003289 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003290 if (from_end) {
3291 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3292 data_end + size_diff, btrfs_leaf_data(leaf) +
3293 data_end, old_data_start + new_size - data_end);
3294 } else {
3295 struct btrfs_disk_key disk_key;
3296 u64 offset;
3297
3298 btrfs_item_key(leaf, &disk_key, slot);
3299
3300 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3301 unsigned long ptr;
3302 struct btrfs_file_extent_item *fi;
3303
3304 fi = btrfs_item_ptr(leaf, slot,
3305 struct btrfs_file_extent_item);
3306 fi = (struct btrfs_file_extent_item *)(
3307 (unsigned long)fi - size_diff);
3308
3309 if (btrfs_file_extent_type(leaf, fi) ==
3310 BTRFS_FILE_EXTENT_INLINE) {
3311 ptr = btrfs_item_ptr_offset(leaf, slot);
3312 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003313 (unsigned long)fi,
3314 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003315 disk_bytenr));
3316 }
3317 }
3318
3319 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3320 data_end + size_diff, btrfs_leaf_data(leaf) +
3321 data_end, old_data_start - data_end);
3322
3323 offset = btrfs_disk_key_offset(&disk_key);
3324 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3325 btrfs_set_item_key(leaf, &disk_key, slot);
3326 if (slot == 0)
3327 fixup_low_keys(trans, root, path, &disk_key, 1);
3328 }
Chris Mason5f39d392007-10-15 16:14:19 -04003329
3330 item = btrfs_item_nr(leaf, slot);
3331 btrfs_set_item_size(leaf, item, new_size);
3332 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003333
Chris Mason5f39d392007-10-15 16:14:19 -04003334 if (btrfs_leaf_free_space(root, leaf) < 0) {
3335 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003336 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003337 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003338 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003339}
3340
Chris Masond352ac62008-09-29 15:18:18 -04003341/*
3342 * make the item pointed to by the path bigger, data_size is the new size.
3343 */
Chris Mason5f39d392007-10-15 16:14:19 -04003344int btrfs_extend_item(struct btrfs_trans_handle *trans,
3345 struct btrfs_root *root, struct btrfs_path *path,
3346 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003347{
Chris Mason6567e832007-04-16 09:22:45 -04003348 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct extent_buffer *leaf;
3350 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003351 u32 nritems;
3352 unsigned int data_end;
3353 unsigned int old_data;
3354 unsigned int old_size;
3355 int i;
3356
Chris Mason5f39d392007-10-15 16:14:19 -04003357 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003358
Chris Mason5f39d392007-10-15 16:14:19 -04003359 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003360 data_end = leaf_data_end(root, leaf);
3361
Chris Mason5f39d392007-10-15 16:14:19 -04003362 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3363 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003364 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003365 }
Chris Mason6567e832007-04-16 09:22:45 -04003366 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003367 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003368
3369 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003370 if (slot >= nritems) {
3371 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003372 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3373 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003374 BUG_ON(1);
3375 }
Chris Mason6567e832007-04-16 09:22:45 -04003376
3377 /*
3378 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3379 */
3380 /* first correct the data pointers */
3381 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003382 u32 ioff;
3383 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003384
Chris Mason5f39d392007-10-15 16:14:19 -04003385 ioff = btrfs_item_offset(leaf, item);
3386 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003387 }
Chris Mason5f39d392007-10-15 16:14:19 -04003388
Chris Mason6567e832007-04-16 09:22:45 -04003389 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003390 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003391 data_end - data_size, btrfs_leaf_data(leaf) +
3392 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003393
Chris Mason6567e832007-04-16 09:22:45 -04003394 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003395 old_size = btrfs_item_size_nr(leaf, slot);
3396 item = btrfs_item_nr(leaf, slot);
3397 btrfs_set_item_size(leaf, item, old_size + data_size);
3398 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003399
Chris Mason5f39d392007-10-15 16:14:19 -04003400 if (btrfs_leaf_free_space(root, leaf) < 0) {
3401 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003402 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003403 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003404 return 0;
Chris Mason6567e832007-04-16 09:22:45 -04003405}
3406
Chris Mason74123bd2007-02-02 11:05:29 -05003407/*
Chris Masond352ac62008-09-29 15:18:18 -04003408 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003409 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003410 * Returns the number of keys that were inserted.
3411 */
3412int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3413 struct btrfs_root *root,
3414 struct btrfs_path *path,
3415 struct btrfs_key *cpu_key, u32 *data_size,
3416 int nr)
3417{
3418 struct extent_buffer *leaf;
3419 struct btrfs_item *item;
3420 int ret = 0;
3421 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003422 int i;
3423 u32 nritems;
3424 u32 total_data = 0;
3425 u32 total_size = 0;
3426 unsigned int data_end;
3427 struct btrfs_disk_key disk_key;
3428 struct btrfs_key found_key;
3429
Yan Zheng87b29b22008-12-17 10:21:48 -05003430 for (i = 0; i < nr; i++) {
3431 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3432 BTRFS_LEAF_DATA_SIZE(root)) {
3433 break;
3434 nr = i;
3435 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003436 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003437 total_size += data_size[i] + sizeof(struct btrfs_item);
3438 }
3439 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003440
Josef Bacikf3465ca2008-11-12 14:19:50 -05003441 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3442 if (ret == 0)
3443 return -EEXIST;
3444 if (ret < 0)
3445 goto out;
3446
Josef Bacikf3465ca2008-11-12 14:19:50 -05003447 leaf = path->nodes[0];
3448
3449 nritems = btrfs_header_nritems(leaf);
3450 data_end = leaf_data_end(root, leaf);
3451
3452 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3453 for (i = nr; i >= 0; i--) {
3454 total_data -= data_size[i];
3455 total_size -= data_size[i] + sizeof(struct btrfs_item);
3456 if (total_size < btrfs_leaf_free_space(root, leaf))
3457 break;
3458 }
3459 nr = i;
3460 }
3461
3462 slot = path->slots[0];
3463 BUG_ON(slot < 0);
3464
3465 if (slot != nritems) {
3466 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3467
3468 item = btrfs_item_nr(leaf, slot);
3469 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3470
3471 /* figure out how many keys we can insert in here */
3472 total_data = data_size[0];
3473 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003474 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003475 break;
3476 total_data += data_size[i];
3477 }
3478 nr = i;
3479
3480 if (old_data < data_end) {
3481 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003482 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003483 slot, old_data, data_end);
3484 BUG_ON(1);
3485 }
3486 /*
3487 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3488 */
3489 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003490 for (i = slot; i < nritems; i++) {
3491 u32 ioff;
3492
3493 item = btrfs_item_nr(leaf, i);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003494 ioff = btrfs_item_offset(leaf, item);
3495 btrfs_set_item_offset(leaf, item, ioff - total_data);
3496 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003497 /* shift the items */
3498 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3499 btrfs_item_nr_offset(slot),
3500 (nritems - slot) * sizeof(struct btrfs_item));
3501
3502 /* shift the data */
3503 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3504 data_end - total_data, btrfs_leaf_data(leaf) +
3505 data_end, old_data - data_end);
3506 data_end = old_data;
3507 } else {
3508 /*
3509 * this sucks but it has to be done, if we are inserting at
3510 * the end of the leaf only insert 1 of the items, since we
3511 * have no way of knowing whats on the next leaf and we'd have
3512 * to drop our current locks to figure it out
3513 */
3514 nr = 1;
3515 }
3516
3517 /* setup the item for the new data */
3518 for (i = 0; i < nr; i++) {
3519 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3520 btrfs_set_item_key(leaf, &disk_key, slot + i);
3521 item = btrfs_item_nr(leaf, slot + i);
3522 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3523 data_end -= data_size[i];
3524 btrfs_set_item_size(leaf, item, data_size[i]);
3525 }
3526 btrfs_set_header_nritems(leaf, nritems + nr);
3527 btrfs_mark_buffer_dirty(leaf);
3528
3529 ret = 0;
3530 if (slot == 0) {
3531 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3532 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3533 }
3534
3535 if (btrfs_leaf_free_space(root, leaf) < 0) {
3536 btrfs_print_leaf(root, leaf);
3537 BUG();
3538 }
3539out:
3540 if (!ret)
3541 ret = nr;
3542 return ret;
3543}
3544
3545/*
Chris Mason44871b12009-03-13 10:04:31 -04003546 * this is a helper for btrfs_insert_empty_items, the main goal here is
3547 * to save stack depth by doing the bulk of the work in a function
3548 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003549 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003550int setup_items_for_insert(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root, struct btrfs_path *path,
3552 struct btrfs_key *cpu_key, u32 *data_size,
3553 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003554{
Chris Mason5f39d392007-10-15 16:14:19 -04003555 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003556 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003557 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003558 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003559 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003560 int ret;
3561 struct extent_buffer *leaf;
3562 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003563
Chris Mason5f39d392007-10-15 16:14:19 -04003564 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003565 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003566
Chris Mason5f39d392007-10-15 16:14:19 -04003567 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003568 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003569
Chris Masonf25956c2008-09-12 15:32:53 -04003570 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003571 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003572 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003573 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003575 }
Chris Mason5f39d392007-10-15 16:14:19 -04003576
Chris Masonbe0e5c02007-01-26 15:51:26 -05003577 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003578 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003579
Chris Mason5f39d392007-10-15 16:14:19 -04003580 if (old_data < data_end) {
3581 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003582 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003583 slot, old_data, data_end);
3584 BUG_ON(1);
3585 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586 /*
3587 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3588 */
3589 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003590 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003591 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003592
Chris Mason5f39d392007-10-15 16:14:19 -04003593 item = btrfs_item_nr(leaf, i);
3594 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003595 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003596 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003598 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003599 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003600 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003601
3602 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003603 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003604 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003605 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003606 data_end = old_data;
3607 }
Chris Mason5f39d392007-10-15 16:14:19 -04003608
Chris Mason62e27492007-03-15 12:56:47 -04003609 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003610 for (i = 0; i < nr; i++) {
3611 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3612 btrfs_set_item_key(leaf, &disk_key, slot + i);
3613 item = btrfs_item_nr(leaf, slot + i);
3614 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3615 data_end -= data_size[i];
3616 btrfs_set_item_size(leaf, item, data_size[i]);
3617 }
Chris Mason44871b12009-03-13 10:04:31 -04003618
Chris Mason9c583092008-01-29 15:15:18 -05003619 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003620
3621 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003622 if (slot == 0) {
3623 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003624 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003625 }
Chris Masonb9473432009-03-13 11:00:37 -04003626 btrfs_unlock_up_safe(path, 1);
3627 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003628
Chris Mason5f39d392007-10-15 16:14:19 -04003629 if (btrfs_leaf_free_space(root, leaf) < 0) {
3630 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003631 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003632 }
Chris Mason44871b12009-03-13 10:04:31 -04003633 return ret;
3634}
3635
3636/*
3637 * Given a key and some data, insert items into the tree.
3638 * This does all the path init required, making room in the tree if needed.
3639 */
3640int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3641 struct btrfs_root *root,
3642 struct btrfs_path *path,
3643 struct btrfs_key *cpu_key, u32 *data_size,
3644 int nr)
3645{
Chris Mason44871b12009-03-13 10:04:31 -04003646 int ret = 0;
3647 int slot;
3648 int i;
3649 u32 total_size = 0;
3650 u32 total_data = 0;
3651
3652 for (i = 0; i < nr; i++)
3653 total_data += data_size[i];
3654
3655 total_size = total_data + (nr * sizeof(struct btrfs_item));
3656 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3657 if (ret == 0)
3658 return -EEXIST;
3659 if (ret < 0)
3660 goto out;
3661
Chris Mason44871b12009-03-13 10:04:31 -04003662 slot = path->slots[0];
3663 BUG_ON(slot < 0);
3664
3665 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3666 total_data, total_size, nr);
3667
Chris Masoned2ff2c2007-03-01 18:59:40 -05003668out:
Chris Mason62e27492007-03-15 12:56:47 -04003669 return ret;
3670}
3671
3672/*
3673 * Given a key and some data, insert an item into the tree.
3674 * This does all the path init required, making room in the tree if needed.
3675 */
Chris Masone089f052007-03-16 16:20:31 -04003676int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3677 *root, struct btrfs_key *cpu_key, void *data, u32
3678 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003679{
3680 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003681 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003682 struct extent_buffer *leaf;
3683 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003684
Chris Mason2c90e5d2007-04-02 10:50:19 -04003685 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003686 if (!path)
3687 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003688 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003689 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003690 leaf = path->nodes[0];
3691 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3692 write_extent_buffer(leaf, data, ptr, data_size);
3693 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003694 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003695 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003696 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003697}
3698
Chris Mason74123bd2007-02-02 11:05:29 -05003699/*
Chris Mason5de08d72007-02-24 06:24:44 -05003700 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003701 *
Chris Masond352ac62008-09-29 15:18:18 -04003702 * the tree should have been previously balanced so the deletion does not
3703 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003704 */
Chris Masone089f052007-03-16 16:20:31 -04003705static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3706 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003707{
Chris Mason5f39d392007-10-15 16:14:19 -04003708 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003709 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003710 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003711 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003712
Chris Mason5f39d392007-10-15 16:14:19 -04003713 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003714 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003715 memmove_extent_buffer(parent,
3716 btrfs_node_key_ptr_offset(slot),
3717 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003718 sizeof(struct btrfs_key_ptr) *
3719 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003720 }
Chris Mason7518a232007-03-12 12:01:18 -04003721 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003722 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003723 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003724 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003725 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003726 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003727 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003728 struct btrfs_disk_key disk_key;
3729
3730 btrfs_node_key(parent, &disk_key, 0);
3731 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003732 if (wret)
3733 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003734 }
Chris Masond6025572007-03-30 14:27:56 -04003735 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003736 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003737}
3738
Chris Mason74123bd2007-02-02 11:05:29 -05003739/*
Chris Mason323ac952008-10-01 19:05:46 -04003740 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003741 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003742 *
3743 * This deletes the pointer in path->nodes[1] and frees the leaf
3744 * block extent. zero is returned if it all worked out, < 0 otherwise.
3745 *
3746 * The path must have already been setup for deleting the leaf, including
3747 * all the proper balancing. path->nodes[1] must be locked.
3748 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003749static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3750 struct btrfs_root *root,
3751 struct btrfs_path *path,
3752 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003753{
3754 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003755
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003756 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003757 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3758 if (ret)
3759 return ret;
3760
Chris Mason4d081c42009-02-04 09:31:28 -05003761 /*
3762 * btrfs_free_extent is expensive, we want to make sure we
3763 * aren't holding any locks when we call it
3764 */
3765 btrfs_unlock_up_safe(path, 0);
3766
Yan, Zhengf0486c62010-05-16 10:46:25 -04003767 root_sub_used(root, leaf->len);
3768
3769 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3770 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003771}
3772/*
Chris Mason74123bd2007-02-02 11:05:29 -05003773 * delete the item at the leaf level in path. If that empties
3774 * the leaf, remove it from the tree
3775 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003776int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3777 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003778{
Chris Mason5f39d392007-10-15 16:14:19 -04003779 struct extent_buffer *leaf;
3780 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003781 int last_off;
3782 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003783 int ret = 0;
3784 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003785 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003786 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003787
Chris Mason5f39d392007-10-15 16:14:19 -04003788 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003789 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3790
3791 for (i = 0; i < nr; i++)
3792 dsize += btrfs_item_size_nr(leaf, slot + i);
3793
Chris Mason5f39d392007-10-15 16:14:19 -04003794 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003795
Chris Mason85e21ba2008-01-29 15:11:36 -05003796 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003797 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003798
3799 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003800 data_end + dsize,
3801 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003802 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Chris Mason85e21ba2008-01-29 15:11:36 -05003804 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003805 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003806
Chris Mason5f39d392007-10-15 16:14:19 -04003807 item = btrfs_item_nr(leaf, i);
3808 ioff = btrfs_item_offset(leaf, item);
3809 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003810 }
Chris Masondb945352007-10-15 16:15:53 -04003811
Chris Mason5f39d392007-10-15 16:14:19 -04003812 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003813 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003814 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003815 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003816 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003817 btrfs_set_header_nritems(leaf, nritems - nr);
3818 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003819
Chris Mason74123bd2007-02-02 11:05:29 -05003820 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003821 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003822 if (leaf == root->node) {
3823 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003824 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003825 btrfs_set_path_blocking(path);
3826 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003827 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003828 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003829 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003830 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003831 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003832 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003833 struct btrfs_disk_key disk_key;
3834
3835 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003836 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003837 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003838 if (wret)
3839 ret = wret;
3840 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003841
Chris Mason74123bd2007-02-02 11:05:29 -05003842 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003843 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003844 /* push_leaf_left fixes the path.
3845 * make sure the path still points to our leaf
3846 * for possible call to del_ptr below
3847 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003848 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003849 extent_buffer_get(leaf);
3850
Chris Masonb9473432009-03-13 11:00:37 -04003851 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003852 wret = push_leaf_left(trans, root, path, 1, 1,
3853 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003854 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003855 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003856
3857 if (path->nodes[0] == leaf &&
3858 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003859 wret = push_leaf_right(trans, root, path, 1,
3860 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003861 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003862 ret = wret;
3863 }
Chris Mason5f39d392007-10-15 16:14:19 -04003864
3865 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003866 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003868 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003869 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003870 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003871 /* if we're still in the path, make sure
3872 * we're dirty. Otherwise, one of the
3873 * push_leaf functions must have already
3874 * dirtied this buffer
3875 */
3876 if (path->nodes[0] == leaf)
3877 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003878 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003879 }
Chris Masond5719762007-03-23 10:01:08 -04003880 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003881 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003882 }
3883 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003884 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885}
3886
Chris Mason97571fd2007-02-24 13:39:08 -05003887/*
Chris Mason925baed2008-06-25 16:01:30 -04003888 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003889 * returns 0 if it found something or 1 if there are no lesser leaves.
3890 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003891 *
3892 * This may release the path, and so you may lose any locks held at the
3893 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003894 */
3895int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3896{
Chris Mason925baed2008-06-25 16:01:30 -04003897 struct btrfs_key key;
3898 struct btrfs_disk_key found_key;
3899 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003900
Chris Mason925baed2008-06-25 16:01:30 -04003901 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003902
Chris Mason925baed2008-06-25 16:01:30 -04003903 if (key.offset > 0)
3904 key.offset--;
3905 else if (key.type > 0)
3906 key.type--;
3907 else if (key.objectid > 0)
3908 key.objectid--;
3909 else
3910 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003911
David Sterbab3b4aa72011-04-21 01:20:15 +02003912 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003913 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3914 if (ret < 0)
3915 return ret;
3916 btrfs_item_key(path->nodes[0], &found_key, 0);
3917 ret = comp_keys(&found_key, &key);
3918 if (ret < 0)
3919 return 0;
3920 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003921}
3922
Chris Mason3f157a22008-06-25 16:01:31 -04003923/*
3924 * A helper function to walk down the tree starting at min_key, and looking
3925 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003926 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003927 *
3928 * This does not cow, but it does stuff the starting key it finds back
3929 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3930 * key and get a writable path.
3931 *
3932 * This does lock as it descends, and path->keep_locks should be set
3933 * to 1 by the caller.
3934 *
3935 * This honors path->lowest_level to prevent descent past a given level
3936 * of the tree.
3937 *
Chris Masond352ac62008-09-29 15:18:18 -04003938 * min_trans indicates the oldest transaction that you are interested
3939 * in walking through. Any nodes or leaves older than min_trans are
3940 * skipped over (without reading them).
3941 *
Chris Mason3f157a22008-06-25 16:01:31 -04003942 * returns zero if something useful was found, < 0 on error and 1 if there
3943 * was nothing in the tree that matched the search criteria.
3944 */
3945int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003946 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003947 struct btrfs_path *path, int cache_only,
3948 u64 min_trans)
3949{
3950 struct extent_buffer *cur;
3951 struct btrfs_key found_key;
3952 int slot;
Yan96524802008-07-24 12:19:49 -04003953 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003954 u32 nritems;
3955 int level;
3956 int ret = 1;
3957
Chris Mason934d3752008-12-08 16:43:10 -05003958 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003959again:
Chris Masonbd681512011-07-16 15:23:14 -04003960 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003961 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003962 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003963 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003964 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003965
3966 if (btrfs_header_generation(cur) < min_trans) {
3967 ret = 1;
3968 goto out;
3969 }
Chris Masond3977122009-01-05 21:25:51 -05003970 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003971 nritems = btrfs_header_nritems(cur);
3972 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003973 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003974
Chris Mason323ac952008-10-01 19:05:46 -04003975 /* at the lowest level, we're done, setup the path and exit */
3976 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003977 if (slot >= nritems)
3978 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003979 ret = 0;
3980 path->slots[level] = slot;
3981 btrfs_item_key_to_cpu(cur, &found_key, slot);
3982 goto out;
3983 }
Yan96524802008-07-24 12:19:49 -04003984 if (sret && slot > 0)
3985 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003986 /*
3987 * check this node pointer against the cache_only and
3988 * min_trans parameters. If it isn't in cache or is too
3989 * old, skip to the next one.
3990 */
Chris Masond3977122009-01-05 21:25:51 -05003991 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003992 u64 blockptr;
3993 u64 gen;
3994 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003995 struct btrfs_disk_key disk_key;
3996
Chris Mason3f157a22008-06-25 16:01:31 -04003997 blockptr = btrfs_node_blockptr(cur, slot);
3998 gen = btrfs_node_ptr_generation(cur, slot);
3999 if (gen < min_trans) {
4000 slot++;
4001 continue;
4002 }
4003 if (!cache_only)
4004 break;
4005
Chris Masone02119d2008-09-05 16:13:11 -04004006 if (max_key) {
4007 btrfs_node_key(cur, &disk_key, slot);
4008 if (comp_keys(&disk_key, max_key) >= 0) {
4009 ret = 1;
4010 goto out;
4011 }
4012 }
4013
Chris Mason3f157a22008-06-25 16:01:31 -04004014 tmp = btrfs_find_tree_block(root, blockptr,
4015 btrfs_level_size(root, level - 1));
4016
4017 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4018 free_extent_buffer(tmp);
4019 break;
4020 }
4021 if (tmp)
4022 free_extent_buffer(tmp);
4023 slot++;
4024 }
Chris Masone02119d2008-09-05 16:13:11 -04004025find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004026 /*
4027 * we didn't find a candidate key in this node, walk forward
4028 * and find another one
4029 */
4030 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004031 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004032 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004033 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004034 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004035 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004036 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004037 goto again;
4038 } else {
4039 goto out;
4040 }
4041 }
4042 /* save our key for returning back */
4043 btrfs_node_key_to_cpu(cur, &found_key, slot);
4044 path->slots[level] = slot;
4045 if (level == path->lowest_level) {
4046 ret = 0;
4047 unlock_up(path, level, 1);
4048 goto out;
4049 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004050 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004051 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004052 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004053
Chris Masonbd681512011-07-16 15:23:14 -04004054 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004055
Chris Masonbd681512011-07-16 15:23:14 -04004056 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004057 path->nodes[level - 1] = cur;
4058 unlock_up(path, level, 1);
Chris Masonbd681512011-07-16 15:23:14 -04004059 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004060 }
4061out:
4062 if (ret == 0)
4063 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004064 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004065 return ret;
4066}
4067
4068/*
4069 * this is similar to btrfs_next_leaf, but does not try to preserve
4070 * and fixup the path. It looks for and returns the next key in the
4071 * tree based on the current path and the cache_only and min_trans
4072 * parameters.
4073 *
4074 * 0 is returned if another key is found, < 0 if there are any errors
4075 * and 1 is returned if there are no higher keys in the tree
4076 *
4077 * path->keep_locks should be set to 1 on the search made before
4078 * calling this function.
4079 */
Chris Masone7a84562008-06-25 16:01:31 -04004080int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004081 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004082 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004083{
Chris Masone7a84562008-06-25 16:01:31 -04004084 int slot;
4085 struct extent_buffer *c;
4086
Chris Mason934d3752008-12-08 16:43:10 -05004087 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004088 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004089 if (!path->nodes[level])
4090 return 1;
4091
4092 slot = path->slots[level] + 1;
4093 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004094next:
Chris Masone7a84562008-06-25 16:01:31 -04004095 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004096 int ret;
4097 int orig_lowest;
4098 struct btrfs_key cur_key;
4099 if (level + 1 >= BTRFS_MAX_LEVEL ||
4100 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004101 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004102
4103 if (path->locks[level + 1]) {
4104 level++;
4105 continue;
4106 }
4107
4108 slot = btrfs_header_nritems(c) - 1;
4109 if (level == 0)
4110 btrfs_item_key_to_cpu(c, &cur_key, slot);
4111 else
4112 btrfs_node_key_to_cpu(c, &cur_key, slot);
4113
4114 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004115 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004116 path->lowest_level = level;
4117 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4118 0, 0);
4119 path->lowest_level = orig_lowest;
4120 if (ret < 0)
4121 return ret;
4122
4123 c = path->nodes[level];
4124 slot = path->slots[level];
4125 if (ret == 0)
4126 slot++;
4127 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004128 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004129
Chris Masone7a84562008-06-25 16:01:31 -04004130 if (level == 0)
4131 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004132 else {
4133 u64 blockptr = btrfs_node_blockptr(c, slot);
4134 u64 gen = btrfs_node_ptr_generation(c, slot);
4135
4136 if (cache_only) {
4137 struct extent_buffer *cur;
4138 cur = btrfs_find_tree_block(root, blockptr,
4139 btrfs_level_size(root, level - 1));
4140 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4141 slot++;
4142 if (cur)
4143 free_extent_buffer(cur);
4144 goto next;
4145 }
4146 free_extent_buffer(cur);
4147 }
4148 if (gen < min_trans) {
4149 slot++;
4150 goto next;
4151 }
Chris Masone7a84562008-06-25 16:01:31 -04004152 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004153 }
Chris Masone7a84562008-06-25 16:01:31 -04004154 return 0;
4155 }
4156 return 1;
4157}
4158
Chris Mason7bb86312007-12-11 09:25:06 -05004159/*
Chris Mason925baed2008-06-25 16:01:30 -04004160 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004161 * returns 0 if it found something or 1 if there are no greater leaves.
4162 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004163 */
Chris Mason234b63a2007-03-13 10:46:10 -04004164int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004165{
4166 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004167 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004168 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004169 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004170 struct btrfs_key key;
4171 u32 nritems;
4172 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004173 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004174 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004175
4176 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004177 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004178 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004179
Chris Mason8e73f272009-04-03 10:14:18 -04004180 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4181again:
4182 level = 1;
4183 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004184 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004185 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004186
Chris Masona2135012008-06-25 16:01:30 -04004187 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004188 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004189
Chris Mason925baed2008-06-25 16:01:30 -04004190 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4191 path->keep_locks = 0;
4192
4193 if (ret < 0)
4194 return ret;
4195
Chris Masona2135012008-06-25 16:01:30 -04004196 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004197 /*
4198 * by releasing the path above we dropped all our locks. A balance
4199 * could have added more items next to the key that used to be
4200 * at the very end of the block. So, check again here and
4201 * advance the path if there are now more items available.
4202 */
Chris Masona2135012008-06-25 16:01:30 -04004203 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004204 if (ret == 0)
4205 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004206 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004207 goto done;
4208 }
Chris Masond97e63b2007-02-20 16:40:44 -05004209
Chris Masond3977122009-01-05 21:25:51 -05004210 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004211 if (!path->nodes[level]) {
4212 ret = 1;
4213 goto done;
4214 }
Chris Mason5f39d392007-10-15 16:14:19 -04004215
Chris Masond97e63b2007-02-20 16:40:44 -05004216 slot = path->slots[level] + 1;
4217 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004218 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004219 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004220 if (level == BTRFS_MAX_LEVEL) {
4221 ret = 1;
4222 goto done;
4223 }
Chris Masond97e63b2007-02-20 16:40:44 -05004224 continue;
4225 }
Chris Mason5f39d392007-10-15 16:14:19 -04004226
Chris Mason925baed2008-06-25 16:01:30 -04004227 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004228 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004229 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004230 }
Chris Mason5f39d392007-10-15 16:14:19 -04004231
Chris Mason8e73f272009-04-03 10:14:18 -04004232 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004233 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004234 ret = read_block_for_search(NULL, root, path, &next, level,
4235 slot, &key);
4236 if (ret == -EAGAIN)
4237 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004238
Chris Mason76a05b32009-05-14 13:24:30 -04004239 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004240 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004241 goto done;
4242 }
4243
Chris Mason5cd57b22008-06-25 16:01:30 -04004244 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004245 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004246 if (!ret) {
4247 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004248 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004249 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004250 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004251 }
Chris Mason31533fb2011-07-26 16:01:59 -04004252 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004253 }
Chris Masond97e63b2007-02-20 16:40:44 -05004254 break;
4255 }
4256 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004257 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004258 level--;
4259 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004260 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004261 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004262
Chris Mason5f39d392007-10-15 16:14:19 -04004263 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004264 path->nodes[level] = next;
4265 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004266 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004267 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004268 if (!level)
4269 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004270
Chris Mason8e73f272009-04-03 10:14:18 -04004271 ret = read_block_for_search(NULL, root, path, &next, level,
4272 0, &key);
4273 if (ret == -EAGAIN)
4274 goto again;
4275
Chris Mason76a05b32009-05-14 13:24:30 -04004276 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004277 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004278 goto done;
4279 }
4280
Chris Mason5cd57b22008-06-25 16:01:30 -04004281 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004282 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004283 if (!ret) {
4284 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004285 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004286 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004287 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004288 }
Chris Mason31533fb2011-07-26 16:01:59 -04004289 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004290 }
Chris Masond97e63b2007-02-20 16:40:44 -05004291 }
Chris Mason8e73f272009-04-03 10:14:18 -04004292 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004293done:
4294 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004295 path->leave_spinning = old_spinning;
4296 if (!old_spinning)
4297 btrfs_set_path_blocking(path);
4298
4299 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004300}
Chris Mason0b86a832008-03-24 15:01:56 -04004301
Chris Mason3f157a22008-06-25 16:01:31 -04004302/*
4303 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4304 * searching until it gets past min_objectid or finds an item of 'type'
4305 *
4306 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4307 */
Chris Mason0b86a832008-03-24 15:01:56 -04004308int btrfs_previous_item(struct btrfs_root *root,
4309 struct btrfs_path *path, u64 min_objectid,
4310 int type)
4311{
4312 struct btrfs_key found_key;
4313 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004314 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004315 int ret;
4316
Chris Masond3977122009-01-05 21:25:51 -05004317 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004318 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004319 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004320 ret = btrfs_prev_leaf(root, path);
4321 if (ret != 0)
4322 return ret;
4323 } else {
4324 path->slots[0]--;
4325 }
4326 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004327 nritems = btrfs_header_nritems(leaf);
4328 if (nritems == 0)
4329 return 1;
4330 if (path->slots[0] == nritems)
4331 path->slots[0]--;
4332
Chris Mason0b86a832008-03-24 15:01:56 -04004333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004334 if (found_key.objectid < min_objectid)
4335 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004336 if (found_key.type == type)
4337 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004338 if (found_key.objectid == min_objectid &&
4339 found_key.type < type)
4340 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004341 }
4342 return 1;
4343}