blob: 3b767d2b68e833275030627341695d911b6960ac [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);
Jeff Mahoney143bede2012-03-01 14:56:26 +010039static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone089f052007-03-16 16:20:31 -040040 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,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200243 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400244 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)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200264 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200266 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
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);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700334 if (ret)
335 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700336 if (refs == 0) {
337 ret = -EROFS;
338 btrfs_std_error(root->fs_info, ret);
339 return ret;
340 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400341 } else {
342 refs = 1;
343 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
344 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
345 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
346 else
347 flags = 0;
348 }
349
350 owner = btrfs_header_owner(buf);
351 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
352 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
353
354 if (refs > 1) {
355 if ((owner == root->root_key.objectid ||
356 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
357 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200358 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 BUG_ON(ret);
360
361 if (root->root_key.objectid ==
362 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200363 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400364 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200365 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400366 BUG_ON(ret);
367 }
368 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
369 } else {
370
371 if (root->root_key.objectid ==
372 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200373 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400374 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200375 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400376 BUG_ON(ret);
377 }
378 if (new_flags != 0) {
379 ret = btrfs_set_disk_extent_flags(trans, root,
380 buf->start,
381 buf->len,
382 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700383 if (ret)
384 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400385 }
386 } else {
387 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
388 if (root->root_key.objectid ==
389 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200390 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400391 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200392 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400393 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200394 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400395 BUG_ON(ret);
396 }
397 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400398 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400399 }
400 return 0;
401}
402
403/*
Chris Masond3977122009-01-05 21:25:51 -0500404 * does the dirty work in cow of a single block. The parent block (if
405 * supplied) is updated to point to the new cow copy. The new buffer is marked
406 * dirty and returned locked. If you modify the block it needs to be marked
407 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400408 *
409 * search_start -- an allocation hint for the new block
410 *
Chris Masond3977122009-01-05 21:25:51 -0500411 * empty_size -- a hint that you plan on doing more cow. This is the size in
412 * bytes the allocator should try to find free next to the block it returns.
413 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400414 */
Chris Masond3977122009-01-05 21:25:51 -0500415static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400416 struct btrfs_root *root,
417 struct extent_buffer *buf,
418 struct extent_buffer *parent, int parent_slot,
419 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400420 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400421{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400422 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400423 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700424 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400425 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400426 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400427 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400428
Chris Mason925baed2008-06-25 16:01:30 -0400429 if (*cow_ret == buf)
430 unlock_orig = 1;
431
Chris Masonb9447ef2009-03-09 11:45:38 -0400432 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400433
Chris Mason7bb86312007-12-11 09:25:06 -0500434 WARN_ON(root->ref_cows && trans->transid !=
435 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400436 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400437
Chris Mason7bb86312007-12-11 09:25:06 -0500438 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400439
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400440 if (level == 0)
441 btrfs_item_key(buf, &disk_key, 0);
442 else
443 btrfs_node_key(buf, &disk_key, 0);
444
445 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
446 if (parent)
447 parent_start = parent->start;
448 else
449 parent_start = 0;
450 } else
451 parent_start = 0;
452
453 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
454 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200455 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400456 if (IS_ERR(cow))
457 return PTR_ERR(cow);
458
Chris Masonb4ce94d2009-02-04 09:25:08 -0500459 /* cow is set to blocking by btrfs_init_new_buffer */
460
Chris Mason5f39d392007-10-15 16:14:19 -0400461 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400462 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400464 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
465 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
466 BTRFS_HEADER_FLAG_RELOC);
467 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
468 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
469 else
470 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400471
Yan Zheng2b820322008-11-17 21:11:30 -0500472 write_extent_buffer(cow, root->fs_info->fsid,
473 (unsigned long)btrfs_header_fsid(cow),
474 BTRFS_FSID_SIZE);
475
Mark Fashehbe1a5562011-08-08 13:20:18 -0700476 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
477 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400478
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400479 if (root->ref_cows)
480 btrfs_reloc_cow_block(trans, root, buf, cow);
481
Chris Mason6702ed42007-08-07 16:15:09 -0400482 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400483 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400484 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
485 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
486 parent_start = buf->start;
487 else
488 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400489
Chris Mason5f39d392007-10-15 16:14:19 -0400490 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400491 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400492
Yan, Zhengf0486c62010-05-16 10:46:25 -0400493 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200494 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400495 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400496 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400497 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400498 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
499 parent_start = parent->start;
500 else
501 parent_start = 0;
502
503 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400504 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400505 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500506 btrfs_set_node_ptr_generation(parent, parent_slot,
507 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400508 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400509 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200510 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400511 }
Chris Mason925baed2008-06-25 16:01:30 -0400512 if (unlock_orig)
513 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400514 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400515 btrfs_mark_buffer_dirty(cow);
516 *cow_ret = cow;
517 return 0;
518}
519
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400520static inline int should_cow_block(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct extent_buffer *buf)
523{
Liu Bof1ebcc72011-11-14 20:48:06 -0500524 /* ensure we can see the force_cow */
525 smp_rmb();
526
527 /*
528 * We do not need to cow a block if
529 * 1) this block is not created or changed in this transaction;
530 * 2) this block does not belong to TREE_RELOC tree;
531 * 3) the root is not forced COW.
532 *
533 * What is forced COW:
534 * when we create snapshot during commiting the transaction,
535 * after we've finished coping src root, we must COW the shared
536 * block to ensure the metadata consistency.
537 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400538 if (btrfs_header_generation(buf) == trans->transid &&
539 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
540 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500541 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
542 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400543 return 0;
544 return 1;
545}
546
Chris Masond352ac62008-09-29 15:18:18 -0400547/*
548 * cows a single block, see __btrfs_cow_block for the real work.
549 * This version of it has extra checks so that a block isn't cow'd more than
550 * once per transaction, as long as it hasn't been written yet
551 */
Chris Masond3977122009-01-05 21:25:51 -0500552noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400553 struct btrfs_root *root, struct extent_buffer *buf,
554 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400555 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500556{
Chris Mason6702ed42007-08-07 16:15:09 -0400557 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400558 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500559
Chris Masonccd467d2007-06-28 15:57:36 -0400560 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500561 printk(KERN_CRIT "trans %llu running %llu\n",
562 (unsigned long long)trans->transid,
563 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400564 root->fs_info->running_transaction->transid);
565 WARN_ON(1);
566 }
567 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500568 printk(KERN_CRIT "trans %llu running %llu\n",
569 (unsigned long long)trans->transid,
570 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400571 WARN_ON(1);
572 }
Chris Masondc17ff82008-01-08 15:46:30 -0500573
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400574 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500575 *cow_ret = buf;
576 return 0;
577 }
Chris Masonc4876852009-02-04 09:24:25 -0500578
Chris Mason0b86a832008-03-24 15:01:56 -0400579 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500580
581 if (parent)
582 btrfs_set_lock_blocking(parent);
583 btrfs_set_lock_blocking(buf);
584
Chris Masonf510cfe2007-10-15 16:14:48 -0400585 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400586 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000587
588 trace_btrfs_cow_block(root, buf, *cow_ret);
589
Chris Masonf510cfe2007-10-15 16:14:48 -0400590 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400591}
592
Chris Masond352ac62008-09-29 15:18:18 -0400593/*
594 * helper function for defrag to decide if two blocks pointed to by a
595 * node are actually close by
596 */
Chris Mason6b800532007-10-15 16:17:34 -0400597static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400598{
Chris Mason6b800532007-10-15 16:17:34 -0400599 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400600 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400601 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400602 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500603 return 0;
604}
605
Chris Mason081e9572007-11-06 10:26:24 -0500606/*
607 * compare two keys in a memcmp fashion
608 */
609static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
610{
611 struct btrfs_key k1;
612
613 btrfs_disk_key_to_cpu(&k1, disk);
614
Diego Calleja20736ab2009-07-24 11:06:52 -0400615 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500616}
617
Josef Bacikf3465ca2008-11-12 14:19:50 -0500618/*
619 * same as comp_keys only with two btrfs_key's
620 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400621int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500622{
623 if (k1->objectid > k2->objectid)
624 return 1;
625 if (k1->objectid < k2->objectid)
626 return -1;
627 if (k1->type > k2->type)
628 return 1;
629 if (k1->type < k2->type)
630 return -1;
631 if (k1->offset > k2->offset)
632 return 1;
633 if (k1->offset < k2->offset)
634 return -1;
635 return 0;
636}
Chris Mason081e9572007-11-06 10:26:24 -0500637
Chris Masond352ac62008-09-29 15:18:18 -0400638/*
639 * this is used by the defrag code to go through all the
640 * leaves pointed to by a node and reallocate them so that
641 * disk order is close to key order
642 */
Chris Mason6702ed42007-08-07 16:15:09 -0400643int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400644 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400645 int start_slot, int cache_only, u64 *last_ret,
646 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400647{
Chris Mason6b800532007-10-15 16:17:34 -0400648 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400649 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400650 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400651 u64 search_start = *last_ret;
652 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400653 u64 other;
654 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400655 int end_slot;
656 int i;
657 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400658 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400659 int uptodate;
660 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500661 int progress_passed = 0;
662 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400663
Chris Mason5708b952007-10-25 15:43:18 -0400664 parent_level = btrfs_header_level(parent);
665 if (cache_only && parent_level != 1)
666 return 0;
667
Chris Masond3977122009-01-05 21:25:51 -0500668 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400669 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500670 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400671 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400672
Chris Mason6b800532007-10-15 16:17:34 -0400673 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400674 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400675 end_slot = parent_nritems;
676
677 if (parent_nritems == 1)
678 return 0;
679
Chris Masonb4ce94d2009-02-04 09:25:08 -0500680 btrfs_set_lock_blocking(parent);
681
Chris Mason6702ed42007-08-07 16:15:09 -0400682 for (i = start_slot; i < end_slot; i++) {
683 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400684
Chris Mason081e9572007-11-06 10:26:24 -0500685 btrfs_node_key(parent, &disk_key, i);
686 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
687 continue;
688
689 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400690 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400691 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400692 if (last_block == 0)
693 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400694
Chris Mason6702ed42007-08-07 16:15:09 -0400695 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400696 other = btrfs_node_blockptr(parent, i - 1);
697 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400698 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400699 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400700 other = btrfs_node_blockptr(parent, i + 1);
701 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400702 }
Chris Masone9d0b132007-08-10 14:06:19 -0400703 if (close) {
704 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400705 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400706 }
Chris Mason6702ed42007-08-07 16:15:09 -0400707
Chris Mason6b800532007-10-15 16:17:34 -0400708 cur = btrfs_find_tree_block(root, blocknr, blocksize);
709 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400710 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400711 else
712 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400713 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400714 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400715 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400716 continue;
717 }
Chris Mason6b800532007-10-15 16:17:34 -0400718 if (!cur) {
719 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400720 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000721 if (!cur)
722 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400723 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400724 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400725 }
Chris Mason6702ed42007-08-07 16:15:09 -0400726 }
Chris Masone9d0b132007-08-10 14:06:19 -0400727 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400728 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400729
Chris Masone7a84562008-06-25 16:01:31 -0400730 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500731 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400732 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400733 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400734 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400735 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400736 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400737 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400738 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400739 break;
Yan252c38f2007-08-29 09:11:44 -0400740 }
Chris Masone7a84562008-06-25 16:01:31 -0400741 search_start = cur->start;
742 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400743 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400744 btrfs_tree_unlock(cur);
745 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400746 }
747 return err;
748}
749
Chris Mason74123bd2007-02-02 11:05:29 -0500750/*
751 * The leaf data grows from end-to-front in the node.
752 * this returns the address of the start of the last item,
753 * which is the stop of the leaf data stack
754 */
Chris Mason123abc82007-03-14 14:14:43 -0400755static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400756 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500757{
Chris Mason5f39d392007-10-15 16:14:19 -0400758 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500759 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400760 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400761 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500762}
763
Chris Masonaa5d6be2007-02-28 16:35:06 -0500764
Chris Mason74123bd2007-02-02 11:05:29 -0500765/*
Chris Mason5f39d392007-10-15 16:14:19 -0400766 * search for key in the extent_buffer. The items start at offset p,
767 * and they are item_size apart. There are 'max' items in p.
768 *
Chris Mason74123bd2007-02-02 11:05:29 -0500769 * the slot in the array is returned via slot, and it points to
770 * the place where you would insert key if it is not found in
771 * the array.
772 *
773 * slot may point to max if the key is bigger than all of the keys
774 */
Chris Masone02119d2008-09-05 16:13:11 -0400775static noinline int generic_bin_search(struct extent_buffer *eb,
776 unsigned long p,
777 int item_size, struct btrfs_key *key,
778 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500779{
780 int low = 0;
781 int high = max;
782 int mid;
783 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400784 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400785 struct btrfs_disk_key unaligned;
786 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400787 char *kaddr = NULL;
788 unsigned long map_start = 0;
789 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400790 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500791
Chris Masond3977122009-01-05 21:25:51 -0500792 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500793 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400794 offset = p + mid * item_size;
795
Chris Masona6591712011-07-19 12:04:14 -0400796 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400797 (offset + sizeof(struct btrfs_disk_key)) >
798 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500799
800 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400801 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400802 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400803
Chris Mason479965d2007-10-15 16:14:27 -0400804 if (!err) {
805 tmp = (struct btrfs_disk_key *)(kaddr + offset -
806 map_start);
807 } else {
808 read_extent_buffer(eb, &unaligned,
809 offset, sizeof(unaligned));
810 tmp = &unaligned;
811 }
812
Chris Mason5f39d392007-10-15 16:14:19 -0400813 } else {
814 tmp = (struct btrfs_disk_key *)(kaddr + offset -
815 map_start);
816 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817 ret = comp_keys(tmp, key);
818
819 if (ret < 0)
820 low = mid + 1;
821 else if (ret > 0)
822 high = mid;
823 else {
824 *slot = mid;
825 return 0;
826 }
827 }
828 *slot = low;
829 return 1;
830}
831
Chris Mason97571fd2007-02-24 13:39:08 -0500832/*
833 * simple bin_search frontend that does the right thing for
834 * leaves vs nodes
835 */
Chris Mason5f39d392007-10-15 16:14:19 -0400836static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
837 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500838{
Chris Mason5f39d392007-10-15 16:14:19 -0400839 if (level == 0) {
840 return generic_bin_search(eb,
841 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400842 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400843 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400844 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500845 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400846 return generic_bin_search(eb,
847 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400848 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400849 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400850 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500851 }
852 return -1;
853}
854
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400855int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
856 int level, int *slot)
857{
858 return bin_search(eb, key, level, slot);
859}
860
Yan, Zhengf0486c62010-05-16 10:46:25 -0400861static void root_add_used(struct btrfs_root *root, u32 size)
862{
863 spin_lock(&root->accounting_lock);
864 btrfs_set_root_used(&root->root_item,
865 btrfs_root_used(&root->root_item) + size);
866 spin_unlock(&root->accounting_lock);
867}
868
869static void root_sub_used(struct btrfs_root *root, u32 size)
870{
871 spin_lock(&root->accounting_lock);
872 btrfs_set_root_used(&root->root_item,
873 btrfs_root_used(&root->root_item) - size);
874 spin_unlock(&root->accounting_lock);
875}
876
Chris Masond352ac62008-09-29 15:18:18 -0400877/* given a node and slot number, this reads the blocks it points to. The
878 * extent buffer is returned with a reference taken (but unlocked).
879 * NULL is returned on error.
880 */
Chris Masone02119d2008-09-05 16:13:11 -0400881static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500883{
Chris Masonca7a79a2008-05-12 12:59:19 -0400884 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500885 if (slot < 0)
886 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400887 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500888 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400889
890 BUG_ON(level == 0);
891
Chris Masondb945352007-10-15 16:15:53 -0400892 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400893 btrfs_level_size(root, level - 1),
894 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500895}
896
Chris Masond352ac62008-09-29 15:18:18 -0400897/*
898 * node level balancing, used to make sure nodes are in proper order for
899 * item deletion. We balance from the top down, so we have to make sure
900 * that a deletion won't leave an node completely empty later on.
901 */
Chris Masone02119d2008-09-05 16:13:11 -0400902static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500903 struct btrfs_root *root,
904 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500905{
Chris Mason5f39d392007-10-15 16:14:19 -0400906 struct extent_buffer *right = NULL;
907 struct extent_buffer *mid;
908 struct extent_buffer *left = NULL;
909 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500910 int ret = 0;
911 int wret;
912 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500913 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500914 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500915
916 if (level == 0)
917 return 0;
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500920
Chris Masonbd681512011-07-16 15:23:14 -0400921 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
922 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500923 WARN_ON(btrfs_header_generation(mid) != trans->transid);
924
Chris Mason1d4f8a02007-03-13 09:28:32 -0400925 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500926
Li Zefana05a9bb2011-09-06 16:55:34 +0800927 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400928 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800929 pslot = path->slots[level + 1];
930 }
Chris Masonbb803952007-03-01 12:04:21 -0500931
Chris Mason40689472007-03-17 14:29:23 -0400932 /*
933 * deal with the case where there is only one pointer in the root
934 * by promoting the node below to a root
935 */
Chris Mason5f39d392007-10-15 16:14:19 -0400936 if (!parent) {
937 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500938
Chris Mason5f39d392007-10-15 16:14:19 -0400939 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500940 return 0;
941
942 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400943 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500944 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400945 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500946 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400947 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400948 if (ret) {
949 btrfs_tree_unlock(child);
950 free_extent_buffer(child);
951 goto enospc;
952 }
Yan2f375ab2008-02-01 14:58:07 -0500953
Chris Mason240f62c2011-03-23 14:54:42 -0400954 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400955
Chris Mason0b86a832008-03-24 15:01:56 -0400956 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400957 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500958
Chris Mason925baed2008-06-25 16:01:30 -0400959 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500960 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400961 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400962 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500963 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400964 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400965
966 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200967 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500968 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400969 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400970 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500971 }
Chris Mason5f39d392007-10-15 16:14:19 -0400972 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400973 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500974 return 0;
975
Andi Kleen559af822010-10-29 15:14:37 -0400976 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400977
Chris Mason5f39d392007-10-15 16:14:19 -0400978 left = read_node_slot(root, parent, pslot - 1);
979 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400980 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500981 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400982 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400983 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400984 if (wret) {
985 ret = wret;
986 goto enospc;
987 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400988 }
Chris Mason5f39d392007-10-15 16:14:19 -0400989 right = read_node_slot(root, parent, pslot + 1);
990 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400991 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500992 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400993 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400994 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400995 if (wret) {
996 ret = wret;
997 goto enospc;
998 }
999 }
1000
1001 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001002 if (left) {
1003 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001004 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001005 if (wret < 0)
1006 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001007 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001008 }
Chris Mason79f95c82007-03-01 15:16:26 -05001009
1010 /*
1011 * then try to empty the right most buffer into the middle
1012 */
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001014 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001015 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001016 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001017 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001018 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001019 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001020 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001021 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001022 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001023 free_extent_buffer(right);
1024 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001025 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001026 struct btrfs_disk_key right_key;
1027 btrfs_node_key(right, &right_key, 0);
1028 btrfs_set_node_key(parent, &right_key, pslot + 1);
1029 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001030 }
1031 }
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001033 /*
1034 * we're not allowed to leave a node with one item in the
1035 * tree during a delete. A deletion from lower in the tree
1036 * could try to delete the only pointer in this node.
1037 * So, pull some keys from the left.
1038 * There has to be a left pointer at this point because
1039 * otherwise we would have pulled some pointers from the
1040 * right
1041 */
Chris Mason5f39d392007-10-15 16:14:19 -04001042 BUG_ON(!left);
1043 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001044 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001045 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001046 goto enospc;
1047 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001048 if (wret == 1) {
1049 wret = push_node_left(trans, root, left, mid, 1);
1050 if (wret < 0)
1051 ret = wret;
1052 }
Chris Mason79f95c82007-03-01 15:16:26 -05001053 BUG_ON(wret == 1);
1054 }
Chris Mason5f39d392007-10-15 16:14:19 -04001055 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001056 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001057 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001058 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001059 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001060 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001061 free_extent_buffer(mid);
1062 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001063 } else {
1064 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001065 struct btrfs_disk_key mid_key;
1066 btrfs_node_key(mid, &mid_key, 0);
1067 btrfs_set_node_key(parent, &mid_key, pslot);
1068 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001069 }
Chris Masonbb803952007-03-01 12:04:21 -05001070
Chris Mason79f95c82007-03-01 15:16:26 -05001071 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001072 if (left) {
1073 if (btrfs_header_nritems(left) > orig_slot) {
1074 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001075 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001076 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001077 path->slots[level + 1] -= 1;
1078 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001079 if (mid) {
1080 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001081 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001082 }
Chris Masonbb803952007-03-01 12:04:21 -05001083 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001084 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001085 path->slots[level] = orig_slot;
1086 }
1087 }
Chris Mason79f95c82007-03-01 15:16:26 -05001088 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001089 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001090 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001091 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001092enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001093 if (right) {
1094 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001095 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001096 }
1097 if (left) {
1098 if (path->nodes[level] != left)
1099 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001100 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001101 }
Chris Masonbb803952007-03-01 12:04:21 -05001102 return ret;
1103}
1104
Chris Masond352ac62008-09-29 15:18:18 -04001105/* Node balancing for insertion. Here we only split or push nodes around
1106 * when they are completely full. This is also done top down, so we
1107 * have to be pessimistic.
1108 */
Chris Masond3977122009-01-05 21:25:51 -05001109static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001110 struct btrfs_root *root,
1111 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001112{
Chris Mason5f39d392007-10-15 16:14:19 -04001113 struct extent_buffer *right = NULL;
1114 struct extent_buffer *mid;
1115 struct extent_buffer *left = NULL;
1116 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001117 int ret = 0;
1118 int wret;
1119 int pslot;
1120 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001121
1122 if (level == 0)
1123 return 1;
1124
Chris Mason5f39d392007-10-15 16:14:19 -04001125 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001126 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001127
Li Zefana05a9bb2011-09-06 16:55:34 +08001128 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001129 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001130 pslot = path->slots[level + 1];
1131 }
Chris Masone66f7092007-04-20 13:16:02 -04001132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001134 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001135
Chris Mason5f39d392007-10-15 16:14:19 -04001136 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001137
1138 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001139 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001140 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001141
1142 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001143 btrfs_set_lock_blocking(left);
1144
Chris Mason5f39d392007-10-15 16:14:19 -04001145 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001146 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1147 wret = 1;
1148 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001149 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001150 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001151 if (ret)
1152 wret = 1;
1153 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001154 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001155 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001156 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001157 }
Chris Masone66f7092007-04-20 13:16:02 -04001158 if (wret < 0)
1159 ret = wret;
1160 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001161 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001162 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001163 btrfs_node_key(mid, &disk_key, 0);
1164 btrfs_set_node_key(parent, &disk_key, pslot);
1165 btrfs_mark_buffer_dirty(parent);
1166 if (btrfs_header_nritems(left) > orig_slot) {
1167 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001168 path->slots[level + 1] -= 1;
1169 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001170 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001171 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001172 } else {
1173 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001174 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001175 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001176 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001177 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001178 }
Chris Masone66f7092007-04-20 13:16:02 -04001179 return 0;
1180 }
Chris Mason925baed2008-06-25 16:01:30 -04001181 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001182 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001183 }
Chris Mason925baed2008-06-25 16:01:30 -04001184 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001185
1186 /*
1187 * then try to empty the right most buffer into the middle
1188 */
Chris Mason5f39d392007-10-15 16:14:19 -04001189 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001190 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001191
Chris Mason925baed2008-06-25 16:01:30 -04001192 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001193 btrfs_set_lock_blocking(right);
1194
Chris Mason5f39d392007-10-15 16:14:19 -04001195 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001196 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1197 wret = 1;
1198 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001199 ret = btrfs_cow_block(trans, root, right,
1200 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001201 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001202 if (ret)
1203 wret = 1;
1204 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001205 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001206 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001207 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001208 }
Chris Masone66f7092007-04-20 13:16:02 -04001209 if (wret < 0)
1210 ret = wret;
1211 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001212 struct btrfs_disk_key disk_key;
1213
1214 btrfs_node_key(right, &disk_key, 0);
1215 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1216 btrfs_mark_buffer_dirty(parent);
1217
1218 if (btrfs_header_nritems(mid) <= orig_slot) {
1219 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001220 path->slots[level + 1] += 1;
1221 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001222 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001223 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001224 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001225 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001226 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001227 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001228 }
Chris Masone66f7092007-04-20 13:16:02 -04001229 return 0;
1230 }
Chris Mason925baed2008-06-25 16:01:30 -04001231 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001232 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001233 }
Chris Masone66f7092007-04-20 13:16:02 -04001234 return 1;
1235}
1236
Chris Mason74123bd2007-02-02 11:05:29 -05001237/*
Chris Masond352ac62008-09-29 15:18:18 -04001238 * readahead one full node of leaves, finding things that are close
1239 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001240 */
Chris Masonc8c42862009-04-03 10:14:18 -04001241static void reada_for_search(struct btrfs_root *root,
1242 struct btrfs_path *path,
1243 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001244{
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001246 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001247 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001248 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001249 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001250 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001251 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001252 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001253 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001254 u32 nr;
1255 u32 blocksize;
1256 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001257
Chris Masona6b6e752007-10-15 16:22:39 -04001258 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001259 return;
1260
Chris Mason6702ed42007-08-07 16:15:09 -04001261 if (!path->nodes[level])
1262 return;
1263
Chris Mason5f39d392007-10-15 16:14:19 -04001264 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001265
Chris Mason3c69fae2007-08-07 15:52:22 -04001266 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001267 blocksize = btrfs_level_size(root, level - 1);
1268 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001269 if (eb) {
1270 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 return;
1272 }
1273
Chris Masona7175312009-01-22 09:23:10 -05001274 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001275
Chris Mason5f39d392007-10-15 16:14:19 -04001276 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001277 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001278
Chris Masond3977122009-01-05 21:25:51 -05001279 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001280 if (direction < 0) {
1281 if (nr == 0)
1282 break;
1283 nr--;
1284 } else if (direction > 0) {
1285 nr++;
1286 if (nr >= nritems)
1287 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001288 }
Chris Mason01f46652007-12-21 16:24:26 -05001289 if (path->reada < 0 && objectid) {
1290 btrfs_node_key(node, &disk_key, nr);
1291 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1292 break;
1293 }
Chris Mason6b800532007-10-15 16:17:34 -04001294 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001295 if ((search <= target && target - search <= 65536) ||
1296 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001297 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001298 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001299 nread += blocksize;
1300 }
1301 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001302 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001303 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001304 }
1305}
Chris Mason925baed2008-06-25 16:01:30 -04001306
Chris Masond352ac62008-09-29 15:18:18 -04001307/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001308 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1309 * cache
1310 */
1311static noinline int reada_for_balance(struct btrfs_root *root,
1312 struct btrfs_path *path, int level)
1313{
1314 int slot;
1315 int nritems;
1316 struct extent_buffer *parent;
1317 struct extent_buffer *eb;
1318 u64 gen;
1319 u64 block1 = 0;
1320 u64 block2 = 0;
1321 int ret = 0;
1322 int blocksize;
1323
Chris Mason8c594ea2009-04-20 15:50:10 -04001324 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001325 if (!parent)
1326 return 0;
1327
1328 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001329 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001330 blocksize = btrfs_level_size(root, level);
1331
1332 if (slot > 0) {
1333 block1 = btrfs_node_blockptr(parent, slot - 1);
1334 gen = btrfs_node_ptr_generation(parent, slot - 1);
1335 eb = btrfs_find_tree_block(root, block1, blocksize);
1336 if (eb && btrfs_buffer_uptodate(eb, gen))
1337 block1 = 0;
1338 free_extent_buffer(eb);
1339 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001340 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001341 block2 = btrfs_node_blockptr(parent, slot + 1);
1342 gen = btrfs_node_ptr_generation(parent, slot + 1);
1343 eb = btrfs_find_tree_block(root, block2, blocksize);
1344 if (eb && btrfs_buffer_uptodate(eb, gen))
1345 block2 = 0;
1346 free_extent_buffer(eb);
1347 }
1348 if (block1 || block2) {
1349 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001350
1351 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001352 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001353
1354 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001355 if (block1)
1356 readahead_tree_block(root, block1, blocksize, 0);
1357 if (block2)
1358 readahead_tree_block(root, block2, blocksize, 0);
1359
1360 if (block1) {
1361 eb = read_tree_block(root, block1, blocksize, 0);
1362 free_extent_buffer(eb);
1363 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001364 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001365 eb = read_tree_block(root, block2, blocksize, 0);
1366 free_extent_buffer(eb);
1367 }
1368 }
1369 return ret;
1370}
1371
1372
1373/*
Chris Masond3977122009-01-05 21:25:51 -05001374 * when we walk down the tree, it is usually safe to unlock the higher layers
1375 * in the tree. The exceptions are when our path goes through slot 0, because
1376 * operations on the tree might require changing key pointers higher up in the
1377 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001378 *
Chris Masond3977122009-01-05 21:25:51 -05001379 * callers might also have set path->keep_locks, which tells this code to keep
1380 * the lock if the path points to the last slot in the block. This is part of
1381 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001382 *
Chris Masond3977122009-01-05 21:25:51 -05001383 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1384 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001385 */
Chris Masone02119d2008-09-05 16:13:11 -04001386static noinline void unlock_up(struct btrfs_path *path, int level,
1387 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001388{
1389 int i;
1390 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001391 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001392 struct extent_buffer *t;
1393
1394 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1395 if (!path->nodes[i])
1396 break;
1397 if (!path->locks[i])
1398 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001399 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001400 skip_level = i + 1;
1401 continue;
1402 }
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001404 u32 nritems;
1405 t = path->nodes[i];
1406 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001407 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001408 skip_level = i + 1;
1409 continue;
1410 }
1411 }
Chris Mason051e1b92008-06-25 16:01:30 -04001412 if (skip_level < i && i >= lowest_unlock)
1413 no_skips = 1;
1414
Chris Mason925baed2008-06-25 16:01:30 -04001415 t = path->nodes[i];
1416 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001417 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001418 path->locks[i] = 0;
1419 }
1420 }
1421}
1422
Chris Mason3c69fae2007-08-07 15:52:22 -04001423/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001424 * This releases any locks held in the path starting at level and
1425 * going all the way up to the root.
1426 *
1427 * btrfs_search_slot will keep the lock held on higher nodes in a few
1428 * corner cases, such as COW of the block at slot zero in the node. This
1429 * ignores those rules, and it should only be called when there are no
1430 * more updates to be done higher up in the tree.
1431 */
1432noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1433{
1434 int i;
1435
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001436 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001437 return;
1438
1439 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1440 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001441 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001442 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001443 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001444 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001445 path->locks[i] = 0;
1446 }
1447}
1448
1449/*
Chris Masonc8c42862009-04-03 10:14:18 -04001450 * helper function for btrfs_search_slot. The goal is to find a block
1451 * in cache without setting the path to blocking. If we find the block
1452 * we return zero and the path is unchanged.
1453 *
1454 * If we can't find the block, we set the path blocking and do some
1455 * reada. -EAGAIN is returned and the search must be repeated.
1456 */
1457static int
1458read_block_for_search(struct btrfs_trans_handle *trans,
1459 struct btrfs_root *root, struct btrfs_path *p,
1460 struct extent_buffer **eb_ret, int level, int slot,
1461 struct btrfs_key *key)
1462{
1463 u64 blocknr;
1464 u64 gen;
1465 u32 blocksize;
1466 struct extent_buffer *b = *eb_ret;
1467 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001468 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001469
1470 blocknr = btrfs_node_blockptr(b, slot);
1471 gen = btrfs_node_ptr_generation(b, slot);
1472 blocksize = btrfs_level_size(root, level - 1);
1473
1474 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001475 if (tmp) {
1476 if (btrfs_buffer_uptodate(tmp, 0)) {
1477 if (btrfs_buffer_uptodate(tmp, gen)) {
1478 /*
1479 * we found an up to date block without
1480 * sleeping, return
1481 * right away
1482 */
1483 *eb_ret = tmp;
1484 return 0;
1485 }
1486 /* the pages were up to date, but we failed
1487 * the generation number check. Do a full
1488 * read for the generation number that is correct.
1489 * We must do this without dropping locks so
1490 * we can trust our generation number
1491 */
1492 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001493 btrfs_set_path_blocking(p);
1494
Chris Masoncb449212010-10-24 11:01:27 -04001495 tmp = read_tree_block(root, blocknr, blocksize, gen);
1496 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1497 *eb_ret = tmp;
1498 return 0;
1499 }
1500 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001501 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001502 return -EIO;
1503 }
Chris Masonc8c42862009-04-03 10:14:18 -04001504 }
1505
1506 /*
1507 * reduce lock contention at high levels
1508 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001509 * we read. Don't release the lock on the current
1510 * level because we need to walk this node to figure
1511 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001512 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001513 btrfs_unlock_up_safe(p, level + 1);
1514 btrfs_set_path_blocking(p);
1515
Chris Masoncb449212010-10-24 11:01:27 -04001516 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001517 if (p->reada)
1518 reada_for_search(root, p, level, slot, key->objectid);
1519
David Sterbab3b4aa72011-04-21 01:20:15 +02001520 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001521
1522 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001523 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001524 if (tmp) {
1525 /*
1526 * If the read above didn't mark this buffer up to date,
1527 * it will never end up being up to date. Set ret to EIO now
1528 * and give up so that our caller doesn't loop forever
1529 * on our EAGAINs.
1530 */
1531 if (!btrfs_buffer_uptodate(tmp, 0))
1532 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001533 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001534 }
1535 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001536}
1537
1538/*
1539 * helper function for btrfs_search_slot. This does all of the checks
1540 * for node-level blocks and does any balancing required based on
1541 * the ins_len.
1542 *
1543 * If no extra work was required, zero is returned. If we had to
1544 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1545 * start over
1546 */
1547static int
1548setup_nodes_for_search(struct btrfs_trans_handle *trans,
1549 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001550 struct extent_buffer *b, int level, int ins_len,
1551 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001552{
1553 int ret;
1554 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1555 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1556 int sret;
1557
Chris Masonbd681512011-07-16 15:23:14 -04001558 if (*write_lock_level < level + 1) {
1559 *write_lock_level = level + 1;
1560 btrfs_release_path(p);
1561 goto again;
1562 }
1563
Chris Masonc8c42862009-04-03 10:14:18 -04001564 sret = reada_for_balance(root, p, level);
1565 if (sret)
1566 goto again;
1567
1568 btrfs_set_path_blocking(p);
1569 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001570 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001571
1572 BUG_ON(sret > 0);
1573 if (sret) {
1574 ret = sret;
1575 goto done;
1576 }
1577 b = p->nodes[level];
1578 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001579 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001580 int sret;
1581
Chris Masonbd681512011-07-16 15:23:14 -04001582 if (*write_lock_level < level + 1) {
1583 *write_lock_level = level + 1;
1584 btrfs_release_path(p);
1585 goto again;
1586 }
1587
Chris Masonc8c42862009-04-03 10:14:18 -04001588 sret = reada_for_balance(root, p, level);
1589 if (sret)
1590 goto again;
1591
1592 btrfs_set_path_blocking(p);
1593 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001594 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001595
1596 if (sret) {
1597 ret = sret;
1598 goto done;
1599 }
1600 b = p->nodes[level];
1601 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001602 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001603 goto again;
1604 }
1605 BUG_ON(btrfs_header_nritems(b) == 1);
1606 }
1607 return 0;
1608
1609again:
1610 ret = -EAGAIN;
1611done:
1612 return ret;
1613}
1614
1615/*
Chris Mason74123bd2007-02-02 11:05:29 -05001616 * look for key in the tree. path is filled in with nodes along the way
1617 * if key is found, we return zero and you can find the item in the leaf
1618 * level of the path (level 0)
1619 *
1620 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001621 * be inserted, and 1 is returned. If there are other errors during the
1622 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001623 *
1624 * if ins_len > 0, nodes and leaves will be split as we walk down the
1625 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1626 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001627 */
Chris Masone089f052007-03-16 16:20:31 -04001628int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1629 *root, struct btrfs_key *key, struct btrfs_path *p, int
1630 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001631{
Chris Mason5f39d392007-10-15 16:14:19 -04001632 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001633 int slot;
1634 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001635 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001636 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001637 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001638 int root_lock;
1639 /* everything at write_lock_level or lower must be write locked */
1640 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001641 u8 lowest_level = 0;
1642
Chris Mason6702ed42007-08-07 16:15:09 -04001643 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001644 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001645 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001646
Chris Masonbd681512011-07-16 15:23:14 -04001647 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001648 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001649
Chris Masonbd681512011-07-16 15:23:14 -04001650 /* when we are removing items, we might have to go up to level
1651 * two as we update tree pointers Make sure we keep write
1652 * for those levels as well
1653 */
1654 write_lock_level = 2;
1655 } else if (ins_len > 0) {
1656 /*
1657 * for inserting items, make sure we have a write lock on
1658 * level 1 so we can update keys
1659 */
1660 write_lock_level = 1;
1661 }
1662
1663 if (!cow)
1664 write_lock_level = -1;
1665
1666 if (cow && (p->keep_locks || p->lowest_level))
1667 write_lock_level = BTRFS_MAX_LEVEL;
1668
Chris Masonbb803952007-03-01 12:04:21 -05001669again:
Chris Masonbd681512011-07-16 15:23:14 -04001670 /*
1671 * we try very hard to do read locks on the root
1672 */
1673 root_lock = BTRFS_READ_LOCK;
1674 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001675 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001676 /*
1677 * the commit roots are read only
1678 * so we always do read locks
1679 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680 b = root->commit_root;
1681 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001682 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001684 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001685 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001686 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001687 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001688 level = btrfs_header_level(b);
1689 } else {
1690 /* we don't know the level of the root node
1691 * until we actually have it read locked
1692 */
1693 b = btrfs_read_lock_root_node(root);
1694 level = btrfs_header_level(b);
1695 if (level <= write_lock_level) {
1696 /* whoops, must trade for write lock */
1697 btrfs_tree_read_unlock(b);
1698 free_extent_buffer(b);
1699 b = btrfs_lock_root_node(root);
1700 root_lock = BTRFS_WRITE_LOCK;
1701
1702 /* the level might have changed, check again */
1703 level = btrfs_header_level(b);
1704 }
1705 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706 }
Chris Masonbd681512011-07-16 15:23:14 -04001707 p->nodes[level] = b;
1708 if (!p->skip_locking)
1709 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001710
Chris Masoneb60cea2007-02-02 09:18:22 -05001711 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001712 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001713
1714 /*
1715 * setup the path here so we can release it under lock
1716 * contention with the cow code
1717 */
Chris Mason02217ed2007-03-02 16:08:05 -05001718 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001719 /*
1720 * if we don't really need to cow this block
1721 * then we don't want to set the path blocking,
1722 * so we test it here
1723 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001724 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001725 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726
Chris Masonb4ce94d2009-02-04 09:25:08 -05001727 btrfs_set_path_blocking(p);
1728
Chris Masonbd681512011-07-16 15:23:14 -04001729 /*
1730 * must have write locks on this node and the
1731 * parent
1732 */
1733 if (level + 1 > write_lock_level) {
1734 write_lock_level = level + 1;
1735 btrfs_release_path(p);
1736 goto again;
1737 }
1738
Yan Zheng33c66f42009-07-22 09:59:00 -04001739 err = btrfs_cow_block(trans, root, b,
1740 p->nodes[level + 1],
1741 p->slots[level + 1], &b);
1742 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001743 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001744 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001745 }
Chris Mason02217ed2007-03-02 16:08:05 -05001746 }
Chris Mason65b51a02008-08-01 15:11:20 -04001747cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001748 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001749
Chris Masoneb60cea2007-02-02 09:18:22 -05001750 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001751 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001752
1753 /*
1754 * we have a lock on b and as long as we aren't changing
1755 * the tree, there is no way to for the items in b to change.
1756 * It is safe to drop the lock on our parent before we
1757 * go through the expensive btree search on b.
1758 *
1759 * If cow is true, then we might be changing slot zero,
1760 * which may require changing the parent. So, we can't
1761 * drop the lock until after we know which slot we're
1762 * operating on.
1763 */
1764 if (!cow)
1765 btrfs_unlock_up_safe(p, level + 1);
1766
Chris Mason5f39d392007-10-15 16:14:19 -04001767 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768
Chris Mason5f39d392007-10-15 16:14:19 -04001769 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001770 int dec = 0;
1771 if (ret && slot > 0) {
1772 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001774 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001775 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001776 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001777 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001778 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001779 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001780 if (err) {
1781 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001782 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001783 }
Chris Masonc8c42862009-04-03 10:14:18 -04001784 b = p->nodes[level];
1785 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001786
Chris Masonbd681512011-07-16 15:23:14 -04001787 /*
1788 * slot 0 is special, if we change the key
1789 * we have to update the parent pointer
1790 * which means we must have a write lock
1791 * on the parent
1792 */
1793 if (slot == 0 && cow &&
1794 write_lock_level < level + 1) {
1795 write_lock_level = level + 1;
1796 btrfs_release_path(p);
1797 goto again;
1798 }
1799
Chris Masonf9efa9c2008-06-25 16:14:04 -04001800 unlock_up(p, level, lowest_unlock);
1801
Chris Mason925baed2008-06-25 16:01:30 -04001802 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001803 if (dec)
1804 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001805 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001806 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001807
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001809 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001810 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001811 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 if (err) {
1813 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001814 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001815 }
Chris Mason76a05b32009-05-14 13:24:30 -04001816
Chris Masonb4ce94d2009-02-04 09:25:08 -05001817 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001818 level = btrfs_header_level(b);
1819 if (level <= write_lock_level) {
1820 err = btrfs_try_tree_write_lock(b);
1821 if (!err) {
1822 btrfs_set_path_blocking(p);
1823 btrfs_tree_lock(b);
1824 btrfs_clear_path_blocking(p, b,
1825 BTRFS_WRITE_LOCK);
1826 }
1827 p->locks[level] = BTRFS_WRITE_LOCK;
1828 } else {
1829 err = btrfs_try_tree_read_lock(b);
1830 if (!err) {
1831 btrfs_set_path_blocking(p);
1832 btrfs_tree_read_lock(b);
1833 btrfs_clear_path_blocking(p, b,
1834 BTRFS_READ_LOCK);
1835 }
1836 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001837 }
Chris Masonbd681512011-07-16 15:23:14 -04001838 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001839 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001840 } else {
1841 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001842 if (ins_len > 0 &&
1843 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001844 if (write_lock_level < 1) {
1845 write_lock_level = 1;
1846 btrfs_release_path(p);
1847 goto again;
1848 }
1849
Chris Masonb4ce94d2009-02-04 09:25:08 -05001850 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001851 err = split_leaf(trans, root, key,
1852 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001853 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001854
Yan Zheng33c66f42009-07-22 09:59:00 -04001855 BUG_ON(err > 0);
1856 if (err) {
1857 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001858 goto done;
1859 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001860 }
Chris Mason459931e2008-12-10 09:10:46 -05001861 if (!p->search_for_split)
1862 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001863 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001864 }
1865 }
Chris Mason65b51a02008-08-01 15:11:20 -04001866 ret = 1;
1867done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001868 /*
1869 * we don't really know what they plan on doing with the path
1870 * from here on, so for now just mark it as blocking
1871 */
Chris Masonb9473432009-03-13 11:00:37 -04001872 if (!p->leave_spinning)
1873 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001874 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001875 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001876 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001877}
1878
Chris Mason74123bd2007-02-02 11:05:29 -05001879/*
1880 * adjust the pointers going up the tree, starting at level
1881 * making sure the right key of each node is points to 'key'.
1882 * This is used after shifting pointers to the left, so it stops
1883 * fixing up pointers when a given leaf/node is not in slot 0 of the
1884 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001885 *
Chris Mason74123bd2007-02-02 11:05:29 -05001886 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001887static void fixup_low_keys(struct btrfs_trans_handle *trans,
1888 struct btrfs_root *root, struct btrfs_path *path,
1889 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001890{
1891 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001892 struct extent_buffer *t;
1893
Chris Mason234b63a2007-03-13 10:46:10 -04001894 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001895 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001896 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001898 t = path->nodes[i];
1899 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001900 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001901 if (tslot != 0)
1902 break;
1903 }
1904}
1905
Chris Mason74123bd2007-02-02 11:05:29 -05001906/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001907 * update item key.
1908 *
1909 * This function isn't completely safe. It's the caller's responsibility
1910 * that the new key won't break the order
1911 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001912void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1913 struct btrfs_root *root, struct btrfs_path *path,
1914 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001915{
1916 struct btrfs_disk_key disk_key;
1917 struct extent_buffer *eb;
1918 int slot;
1919
1920 eb = path->nodes[0];
1921 slot = path->slots[0];
1922 if (slot > 0) {
1923 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001924 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001925 }
1926 if (slot < btrfs_header_nritems(eb) - 1) {
1927 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001928 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001929 }
1930
1931 btrfs_cpu_key_to_disk(&disk_key, new_key);
1932 btrfs_set_item_key(eb, &disk_key, slot);
1933 btrfs_mark_buffer_dirty(eb);
1934 if (slot == 0)
1935 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001936}
1937
1938/*
Chris Mason74123bd2007-02-02 11:05:29 -05001939 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001940 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001941 *
1942 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1943 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001944 */
Chris Mason98ed5172008-01-03 10:01:48 -05001945static int push_node_left(struct btrfs_trans_handle *trans,
1946 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001947 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001948{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001949 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001950 int src_nritems;
1951 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001952 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001953
Chris Mason5f39d392007-10-15 16:14:19 -04001954 src_nritems = btrfs_header_nritems(src);
1955 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001956 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001957 WARN_ON(btrfs_header_generation(src) != trans->transid);
1958 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001959
Chris Masonbce4eae2008-04-24 14:42:46 -04001960 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001961 return 1;
1962
Chris Masond3977122009-01-05 21:25:51 -05001963 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001964 return 1;
1965
Chris Masonbce4eae2008-04-24 14:42:46 -04001966 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001967 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001968 if (push_items < src_nritems) {
1969 /* leave at least 8 pointers in the node if
1970 * we aren't going to empty it
1971 */
1972 if (src_nritems - push_items < 8) {
1973 if (push_items <= 8)
1974 return 1;
1975 push_items -= 8;
1976 }
1977 }
1978 } else
1979 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 copy_extent_buffer(dst, src,
1982 btrfs_node_key_ptr_offset(dst_nritems),
1983 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001984 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001985
Chris Masonbb803952007-03-01 12:04:21 -05001986 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001987 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1988 btrfs_node_key_ptr_offset(push_items),
1989 (src_nritems - push_items) *
1990 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001991 }
Chris Mason5f39d392007-10-15 16:14:19 -04001992 btrfs_set_header_nritems(src, src_nritems - push_items);
1993 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1994 btrfs_mark_buffer_dirty(src);
1995 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001996
Chris Masonbb803952007-03-01 12:04:21 -05001997 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998}
1999
Chris Mason97571fd2007-02-24 13:39:08 -05002000/*
Chris Mason79f95c82007-03-01 15:16:26 -05002001 * try to push data from one node into the next node right in the
2002 * tree.
2003 *
2004 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2005 * error, and > 0 if there was no room in the right hand block.
2006 *
2007 * this will only push up to 1/2 the contents of the left node over
2008 */
Chris Mason5f39d392007-10-15 16:14:19 -04002009static int balance_node_right(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct extent_buffer *dst,
2012 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002013{
Chris Mason79f95c82007-03-01 15:16:26 -05002014 int push_items = 0;
2015 int max_push;
2016 int src_nritems;
2017 int dst_nritems;
2018 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002019
Chris Mason7bb86312007-12-11 09:25:06 -05002020 WARN_ON(btrfs_header_generation(src) != trans->transid);
2021 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2022
Chris Mason5f39d392007-10-15 16:14:19 -04002023 src_nritems = btrfs_header_nritems(src);
2024 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002025 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002026 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002027 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002028
Chris Masond3977122009-01-05 21:25:51 -05002029 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002030 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002031
2032 max_push = src_nritems / 2 + 1;
2033 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002034 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002035 return 1;
Yan252c38f2007-08-29 09:11:44 -04002036
Chris Mason79f95c82007-03-01 15:16:26 -05002037 if (max_push < push_items)
2038 push_items = max_push;
2039
Chris Mason5f39d392007-10-15 16:14:19 -04002040 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2041 btrfs_node_key_ptr_offset(0),
2042 (dst_nritems) *
2043 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002044
Chris Mason5f39d392007-10-15 16:14:19 -04002045 copy_extent_buffer(dst, src,
2046 btrfs_node_key_ptr_offset(0),
2047 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002048 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002049
Chris Mason5f39d392007-10-15 16:14:19 -04002050 btrfs_set_header_nritems(src, src_nritems - push_items);
2051 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002052
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_mark_buffer_dirty(src);
2054 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002055
Chris Mason79f95c82007-03-01 15:16:26 -05002056 return ret;
2057}
2058
2059/*
Chris Mason97571fd2007-02-24 13:39:08 -05002060 * helper function to insert a new root level in the tree.
2061 * A new node is allocated, and a single item is inserted to
2062 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002063 *
2064 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002065 */
Chris Masond3977122009-01-05 21:25:51 -05002066static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002067 struct btrfs_root *root,
2068 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002069{
Chris Mason7bb86312007-12-11 09:25:06 -05002070 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002071 struct extent_buffer *lower;
2072 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002073 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002074 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002075
2076 BUG_ON(path->nodes[level]);
2077 BUG_ON(path->nodes[level-1] != root->node);
2078
Chris Mason7bb86312007-12-11 09:25:06 -05002079 lower = path->nodes[level-1];
2080 if (level == 1)
2081 btrfs_item_key(lower, &lower_key, 0);
2082 else
2083 btrfs_node_key(lower, &lower_key, 0);
2084
Zheng Yan31840ae2008-09-23 13:14:14 -04002085 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002087 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 if (IS_ERR(c))
2089 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002090
Yan, Zhengf0486c62010-05-16 10:46:25 -04002091 root_add_used(root, root->nodesize);
2092
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002093 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002094 btrfs_set_header_nritems(c, 1);
2095 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002096 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002098 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002100
Chris Mason5f39d392007-10-15 16:14:19 -04002101 write_extent_buffer(c, root->fs_info->fsid,
2102 (unsigned long)btrfs_header_fsid(c),
2103 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002104
2105 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2106 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2107 BTRFS_UUID_SIZE);
2108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002110 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002111 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002112 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002113
2114 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002115
2116 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002117
Chris Mason925baed2008-06-25 16:01:30 -04002118 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002119 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002120
2121 /* the super has an extra ref to root->node */
2122 free_extent_buffer(old);
2123
Chris Mason0b86a832008-03-24 15:01:56 -04002124 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002125 extent_buffer_get(c);
2126 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002127 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002128 path->slots[level] = 0;
2129 return 0;
2130}
2131
Chris Mason74123bd2007-02-02 11:05:29 -05002132/*
2133 * worker function to insert a single pointer in a node.
2134 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002135 *
Chris Mason74123bd2007-02-02 11:05:29 -05002136 * slot and level indicate where you want the key to go, and
2137 * blocknr is the block the key points to.
2138 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002139static void insert_ptr(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root, struct btrfs_path *path,
2141 struct btrfs_disk_key *key, u64 bytenr,
2142 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002143{
Chris Mason5f39d392007-10-15 16:14:19 -04002144 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002145 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002146
2147 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002148 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 lower = path->nodes[level];
2150 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002151 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002152 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002153 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002154 memmove_extent_buffer(lower,
2155 btrfs_node_key_ptr_offset(slot + 1),
2156 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002157 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002158 }
Chris Mason5f39d392007-10-15 16:14:19 -04002159 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002160 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002161 WARN_ON(trans->transid == 0);
2162 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002163 btrfs_set_header_nritems(lower, nritems + 1);
2164 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002165}
2166
Chris Mason97571fd2007-02-24 13:39:08 -05002167/*
2168 * split the node at the specified level in path in two.
2169 * The path is corrected to point to the appropriate node after the split
2170 *
2171 * Before splitting this tries to make some room in the node by pushing
2172 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002173 *
2174 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002175 */
Chris Masone02119d2008-09-05 16:13:11 -04002176static noinline int split_node(struct btrfs_trans_handle *trans,
2177 struct btrfs_root *root,
2178 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002179{
Chris Mason5f39d392007-10-15 16:14:19 -04002180 struct extent_buffer *c;
2181 struct extent_buffer *split;
2182 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002183 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002184 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002185 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002186
Chris Mason5f39d392007-10-15 16:14:19 -04002187 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002188 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002190 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002191 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002192 if (ret)
2193 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002194 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002195 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002196 c = path->nodes[level];
2197 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002198 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002199 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002200 if (ret < 0)
2201 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002202 }
Chris Masone66f7092007-04-20 13:16:02 -04002203
Chris Mason5f39d392007-10-15 16:14:19 -04002204 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002205 mid = (c_nritems + 1) / 2;
2206 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002207
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002208 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002209 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002210 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002211 if (IS_ERR(split))
2212 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002213
Yan, Zhengf0486c62010-05-16 10:46:25 -04002214 root_add_used(root, root->nodesize);
2215
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002217 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002218 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002219 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002221 btrfs_set_header_owner(split, root->root_key.objectid);
2222 write_extent_buffer(split, root->fs_info->fsid,
2223 (unsigned long)btrfs_header_fsid(split),
2224 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002225 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2226 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2227 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002228
Chris Mason5f39d392007-10-15 16:14:19 -04002229
2230 copy_extent_buffer(split, c,
2231 btrfs_node_key_ptr_offset(0),
2232 btrfs_node_key_ptr_offset(mid),
2233 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2234 btrfs_set_header_nritems(split, c_nritems - mid);
2235 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002236 ret = 0;
2237
Chris Mason5f39d392007-10-15 16:14:19 -04002238 btrfs_mark_buffer_dirty(c);
2239 btrfs_mark_buffer_dirty(split);
2240
Jeff Mahoney143bede2012-03-01 14:56:26 +01002241 insert_ptr(trans, root, path, &disk_key, split->start,
2242 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002243
Chris Mason5de08d72007-02-24 06:24:44 -05002244 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002245 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002246 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002247 free_extent_buffer(c);
2248 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002249 path->slots[level + 1] += 1;
2250 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002251 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002252 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002253 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002254 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002255}
2256
Chris Mason74123bd2007-02-02 11:05:29 -05002257/*
2258 * how many bytes are required to store the items in a leaf. start
2259 * and nr indicate which items in the leaf to check. This totals up the
2260 * space used both by the item structs and the item data
2261 */
Chris Mason5f39d392007-10-15 16:14:19 -04002262static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002263{
2264 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002265 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002266 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002267
2268 if (!nr)
2269 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002270 data_len = btrfs_item_end_nr(l, start);
2271 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002272 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002273 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002274 return data_len;
2275}
2276
Chris Mason74123bd2007-02-02 11:05:29 -05002277/*
Chris Masond4dbff92007-04-04 14:08:15 -04002278 * The space between the end of the leaf items and
2279 * the start of the leaf data. IOW, how much room
2280 * the leaf has left for both items and data
2281 */
Chris Masond3977122009-01-05 21:25:51 -05002282noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002283 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002284{
Chris Mason5f39d392007-10-15 16:14:19 -04002285 int nritems = btrfs_header_nritems(leaf);
2286 int ret;
2287 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2288 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002289 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2290 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002291 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002292 leaf_space_used(leaf, 0, nritems), nritems);
2293 }
2294 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002295}
2296
Chris Mason99d8f832010-07-07 10:51:48 -04002297/*
2298 * min slot controls the lowest index we're willing to push to the
2299 * right. We'll push up to and including min_slot, but no lower
2300 */
Chris Mason44871b12009-03-13 10:04:31 -04002301static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2302 struct btrfs_root *root,
2303 struct btrfs_path *path,
2304 int data_size, int empty,
2305 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002306 int free_space, u32 left_nritems,
2307 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002308{
Chris Mason5f39d392007-10-15 16:14:19 -04002309 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002310 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002311 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002312 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002313 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002314 int push_space = 0;
2315 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002316 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002317 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002318 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002319 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002320 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002321
Chris Mason34a38212007-11-07 13:31:03 -05002322 if (empty)
2323 nr = 0;
2324 else
Chris Mason99d8f832010-07-07 10:51:48 -04002325 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002326
Zheng Yan31840ae2008-09-23 13:14:14 -04002327 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002328 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002329
Chris Mason44871b12009-03-13 10:04:31 -04002330 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002331 i = left_nritems - 1;
2332 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002333 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002334
Zheng Yan31840ae2008-09-23 13:14:14 -04002335 if (!empty && push_items > 0) {
2336 if (path->slots[0] > i)
2337 break;
2338 if (path->slots[0] == i) {
2339 int space = btrfs_leaf_free_space(root, left);
2340 if (space + push_space * 2 > free_space)
2341 break;
2342 }
2343 }
2344
Chris Mason00ec4c52007-02-24 12:47:20 -05002345 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002346 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002347
Chris Masondb945352007-10-15 16:15:53 -04002348 this_item_size = btrfs_item_size(left, item);
2349 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002351
Chris Mason00ec4c52007-02-24 12:47:20 -05002352 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002353 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002354 if (i == 0)
2355 break;
2356 i--;
Chris Masondb945352007-10-15 16:15:53 -04002357 }
Chris Mason5f39d392007-10-15 16:14:19 -04002358
Chris Mason925baed2008-06-25 16:01:30 -04002359 if (push_items == 0)
2360 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002361
Chris Mason34a38212007-11-07 13:31:03 -05002362 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002363 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002364
Chris Mason00ec4c52007-02-24 12:47:20 -05002365 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002366 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002367
Chris Mason5f39d392007-10-15 16:14:19 -04002368 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002369 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Mason00ec4c52007-02-24 12:47:20 -05002371 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002372 data_end = leaf_data_end(root, right);
2373 memmove_extent_buffer(right,
2374 btrfs_leaf_data(right) + data_end - push_space,
2375 btrfs_leaf_data(right) + data_end,
2376 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2377
Chris Mason00ec4c52007-02-24 12:47:20 -05002378 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002379 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002380 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2381 btrfs_leaf_data(left) + leaf_data_end(root, left),
2382 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002383
2384 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2385 btrfs_item_nr_offset(0),
2386 right_nritems * sizeof(struct btrfs_item));
2387
Chris Mason00ec4c52007-02-24 12:47:20 -05002388 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002389 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2390 btrfs_item_nr_offset(left_nritems - push_items),
2391 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002392
2393 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002394 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002395 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002396 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002397 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002398 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002399 push_space -= btrfs_item_size(right, item);
2400 btrfs_set_item_offset(right, item, push_space);
2401 }
2402
Chris Mason7518a232007-03-12 12:01:18 -04002403 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002404 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002405
Chris Mason34a38212007-11-07 13:31:03 -05002406 if (left_nritems)
2407 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002408 else
2409 clean_tree_block(trans, root, left);
2410
Chris Mason5f39d392007-10-15 16:14:19 -04002411 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002412
Chris Mason5f39d392007-10-15 16:14:19 -04002413 btrfs_item_key(right, &disk_key, 0);
2414 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002415 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002416
Chris Mason00ec4c52007-02-24 12:47:20 -05002417 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002418 if (path->slots[0] >= left_nritems) {
2419 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002420 if (btrfs_header_nritems(path->nodes[0]) == 0)
2421 clean_tree_block(trans, root, path->nodes[0]);
2422 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002423 free_extent_buffer(path->nodes[0]);
2424 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002425 path->slots[1] += 1;
2426 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002427 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002428 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002429 }
2430 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002431
2432out_unlock:
2433 btrfs_tree_unlock(right);
2434 free_extent_buffer(right);
2435 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002436}
Chris Mason925baed2008-06-25 16:01:30 -04002437
Chris Mason00ec4c52007-02-24 12:47:20 -05002438/*
Chris Mason44871b12009-03-13 10:04:31 -04002439 * push some data in the path leaf to the right, trying to free up at
2440 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2441 *
2442 * returns 1 if the push failed because the other node didn't have enough
2443 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002444 *
2445 * this will push starting from min_slot to the end of the leaf. It won't
2446 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002447 */
2448static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002449 *root, struct btrfs_path *path,
2450 int min_data_size, int data_size,
2451 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002452{
2453 struct extent_buffer *left = path->nodes[0];
2454 struct extent_buffer *right;
2455 struct extent_buffer *upper;
2456 int slot;
2457 int free_space;
2458 u32 left_nritems;
2459 int ret;
2460
2461 if (!path->nodes[1])
2462 return 1;
2463
2464 slot = path->slots[1];
2465 upper = path->nodes[1];
2466 if (slot >= btrfs_header_nritems(upper) - 1)
2467 return 1;
2468
2469 btrfs_assert_tree_locked(path->nodes[1]);
2470
2471 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002472 if (right == NULL)
2473 return 1;
2474
Chris Mason44871b12009-03-13 10:04:31 -04002475 btrfs_tree_lock(right);
2476 btrfs_set_lock_blocking(right);
2477
2478 free_space = btrfs_leaf_free_space(root, right);
2479 if (free_space < data_size)
2480 goto out_unlock;
2481
2482 /* cow and double check */
2483 ret = btrfs_cow_block(trans, root, right, upper,
2484 slot + 1, &right);
2485 if (ret)
2486 goto out_unlock;
2487
2488 free_space = btrfs_leaf_free_space(root, right);
2489 if (free_space < data_size)
2490 goto out_unlock;
2491
2492 left_nritems = btrfs_header_nritems(left);
2493 if (left_nritems == 0)
2494 goto out_unlock;
2495
Chris Mason99d8f832010-07-07 10:51:48 -04002496 return __push_leaf_right(trans, root, path, min_data_size, empty,
2497 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002498out_unlock:
2499 btrfs_tree_unlock(right);
2500 free_extent_buffer(right);
2501 return 1;
2502}
2503
2504/*
Chris Mason74123bd2007-02-02 11:05:29 -05002505 * push some data in the path leaf to the left, trying to free up at
2506 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002507 *
2508 * max_slot can put a limit on how far into the leaf we'll push items. The
2509 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2510 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002511 */
Chris Mason44871b12009-03-13 10:04:31 -04002512static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2513 struct btrfs_root *root,
2514 struct btrfs_path *path, int data_size,
2515 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002516 int free_space, u32 right_nritems,
2517 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518{
Chris Mason5f39d392007-10-15 16:14:19 -04002519 struct btrfs_disk_key disk_key;
2520 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002521 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002522 int push_space = 0;
2523 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002524 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002525 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002526 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002527 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002528 u32 this_item_size;
2529 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002530
Chris Mason34a38212007-11-07 13:31:03 -05002531 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002532 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002533 else
Chris Mason99d8f832010-07-07 10:51:48 -04002534 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002535
2536 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002537 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002538
Zheng Yan31840ae2008-09-23 13:14:14 -04002539 if (!empty && push_items > 0) {
2540 if (path->slots[0] < i)
2541 break;
2542 if (path->slots[0] == i) {
2543 int space = btrfs_leaf_free_space(root, right);
2544 if (space + push_space * 2 > free_space)
2545 break;
2546 }
2547 }
2548
Chris Masonbe0e5c02007-01-26 15:51:26 -05002549 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002550 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002551
2552 this_item_size = btrfs_item_size(right, item);
2553 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 break;
Chris Masondb945352007-10-15 16:15:53 -04002555
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002557 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002558 }
Chris Masondb945352007-10-15 16:15:53 -04002559
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002561 ret = 1;
2562 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 }
Chris Mason34a38212007-11-07 13:31:03 -05002564 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002565 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002566
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002568 copy_extent_buffer(left, right,
2569 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2570 btrfs_item_nr_offset(0),
2571 push_items * sizeof(struct btrfs_item));
2572
Chris Mason123abc82007-03-14 14:14:43 -04002573 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002574 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002575
2576 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002577 leaf_data_end(root, left) - push_space,
2578 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002579 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002580 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002581 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002582 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002583
Chris Masondb945352007-10-15 16:15:53 -04002584 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002585 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002586 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002587
Chris Mason5f39d392007-10-15 16:14:19 -04002588 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002589
Chris Mason5f39d392007-10-15 16:14:19 -04002590 ioff = btrfs_item_offset(left, item);
2591 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002592 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593 }
Chris Mason5f39d392007-10-15 16:14:19 -04002594 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595
2596 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002597 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002598 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2599 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002600 WARN_ON(1);
2601 }
Chris Mason5f39d392007-10-15 16:14:19 -04002602
Chris Mason34a38212007-11-07 13:31:03 -05002603 if (push_items < right_nritems) {
2604 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2605 leaf_data_end(root, right);
2606 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2607 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2608 btrfs_leaf_data(right) +
2609 leaf_data_end(root, right), push_space);
2610
2611 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002612 btrfs_item_nr_offset(push_items),
2613 (btrfs_header_nritems(right) - push_items) *
2614 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002615 }
Yaneef1c492007-11-26 10:58:13 -05002616 right_nritems -= push_items;
2617 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002618 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 for (i = 0; i < right_nritems; i++) {
2620 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002621
Chris Masondb945352007-10-15 16:15:53 -04002622 push_space = push_space - btrfs_item_size(right, item);
2623 btrfs_set_item_offset(right, item, push_space);
2624 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002625
Chris Mason5f39d392007-10-15 16:14:19 -04002626 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002627 if (right_nritems)
2628 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002629 else
2630 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002631
Chris Mason5f39d392007-10-15 16:14:19 -04002632 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002633 fixup_low_keys(trans, root, path, &disk_key, 1);
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.
2726 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002727static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2728 struct btrfs_root *root,
2729 struct btrfs_path *path,
2730 struct extent_buffer *l,
2731 struct extent_buffer *right,
2732 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002733{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002734 int data_copy_size;
2735 int rt_data_off;
2736 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002737 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002738
Chris Mason5f39d392007-10-15 16:14:19 -04002739 nritems = nritems - mid;
2740 btrfs_set_header_nritems(right, nritems);
2741 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2742
2743 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2744 btrfs_item_nr_offset(mid),
2745 nritems * sizeof(struct btrfs_item));
2746
2747 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002748 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2749 data_copy_size, btrfs_leaf_data(l) +
2750 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002751
Chris Mason5f39d392007-10-15 16:14:19 -04002752 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2753 btrfs_item_end_nr(l, mid);
2754
2755 for (i = 0; i < nritems; i++) {
2756 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002757 u32 ioff;
2758
Chris Masondb945352007-10-15 16:15:53 -04002759 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002760 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002761 }
Chris Mason74123bd2007-02-02 11:05:29 -05002762
Chris Mason5f39d392007-10-15 16:14:19 -04002763 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002764 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002765 insert_ptr(trans, root, path, &disk_key, right->start,
2766 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002767
2768 btrfs_mark_buffer_dirty(right);
2769 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002770 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002771
Chris Masonbe0e5c02007-01-26 15:51:26 -05002772 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002773 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002774 free_extent_buffer(path->nodes[0]);
2775 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002776 path->slots[0] -= mid;
2777 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002778 } else {
2779 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002780 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002781 }
Chris Mason5f39d392007-10-15 16:14:19 -04002782
Chris Masoneb60cea2007-02-02 09:18:22 -05002783 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002784}
2785
2786/*
Chris Mason99d8f832010-07-07 10:51:48 -04002787 * double splits happen when we need to insert a big item in the middle
2788 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2789 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2790 * A B C
2791 *
2792 * We avoid this by trying to push the items on either side of our target
2793 * into the adjacent leaves. If all goes well we can avoid the double split
2794 * completely.
2795 */
2796static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root,
2798 struct btrfs_path *path,
2799 int data_size)
2800{
2801 int ret;
2802 int progress = 0;
2803 int slot;
2804 u32 nritems;
2805
2806 slot = path->slots[0];
2807
2808 /*
2809 * try to push all the items after our slot into the
2810 * right leaf
2811 */
2812 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2813 if (ret < 0)
2814 return ret;
2815
2816 if (ret == 0)
2817 progress++;
2818
2819 nritems = btrfs_header_nritems(path->nodes[0]);
2820 /*
2821 * our goal is to get our slot at the start or end of a leaf. If
2822 * we've done so we're done
2823 */
2824 if (path->slots[0] == 0 || path->slots[0] == nritems)
2825 return 0;
2826
2827 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2828 return 0;
2829
2830 /* try to push all the items before our slot into the next leaf */
2831 slot = path->slots[0];
2832 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2833 if (ret < 0)
2834 return ret;
2835
2836 if (ret == 0)
2837 progress++;
2838
2839 if (progress)
2840 return 0;
2841 return 1;
2842}
2843
2844/*
Chris Mason44871b12009-03-13 10:04:31 -04002845 * split the path's leaf in two, making sure there is at least data_size
2846 * available for the resulting leaf level of the path.
2847 *
2848 * returns 0 if all went well and < 0 on failure.
2849 */
2850static noinline int split_leaf(struct btrfs_trans_handle *trans,
2851 struct btrfs_root *root,
2852 struct btrfs_key *ins_key,
2853 struct btrfs_path *path, int data_size,
2854 int extend)
2855{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002856 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002857 struct extent_buffer *l;
2858 u32 nritems;
2859 int mid;
2860 int slot;
2861 struct extent_buffer *right;
2862 int ret = 0;
2863 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002864 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002865 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002866 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002867
Yan, Zhenga5719522009-09-24 09:17:31 -04002868 l = path->nodes[0];
2869 slot = path->slots[0];
2870 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2871 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2872 return -EOVERFLOW;
2873
Chris Mason44871b12009-03-13 10:04:31 -04002874 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002875 if (data_size) {
2876 wret = push_leaf_right(trans, root, path, data_size,
2877 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002878 if (wret < 0)
2879 return wret;
2880 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002881 wret = push_leaf_left(trans, root, path, data_size,
2882 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002883 if (wret < 0)
2884 return wret;
2885 }
2886 l = path->nodes[0];
2887
2888 /* did the pushes work? */
2889 if (btrfs_leaf_free_space(root, l) >= data_size)
2890 return 0;
2891 }
2892
2893 if (!path->nodes[1]) {
2894 ret = insert_new_root(trans, root, path, 1);
2895 if (ret)
2896 return ret;
2897 }
2898again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002899 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002900 l = path->nodes[0];
2901 slot = path->slots[0];
2902 nritems = btrfs_header_nritems(l);
2903 mid = (nritems + 1) / 2;
2904
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905 if (mid <= slot) {
2906 if (nritems == 1 ||
2907 leaf_space_used(l, mid, nritems - mid) + data_size >
2908 BTRFS_LEAF_DATA_SIZE(root)) {
2909 if (slot >= nritems) {
2910 split = 0;
2911 } else {
2912 mid = slot;
2913 if (mid != nritems &&
2914 leaf_space_used(l, mid, nritems - mid) +
2915 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002916 if (data_size && !tried_avoid_double)
2917 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918 split = 2;
2919 }
2920 }
2921 }
2922 } else {
2923 if (leaf_space_used(l, 0, mid) + data_size >
2924 BTRFS_LEAF_DATA_SIZE(root)) {
2925 if (!extend && data_size && slot == 0) {
2926 split = 0;
2927 } else if ((extend || !data_size) && slot == 0) {
2928 mid = 1;
2929 } else {
2930 mid = slot;
2931 if (mid != nritems &&
2932 leaf_space_used(l, mid, nritems - mid) +
2933 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002934 if (data_size && !tried_avoid_double)
2935 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002936 split = 2 ;
2937 }
2938 }
2939 }
2940 }
2941
2942 if (split == 0)
2943 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2944 else
2945 btrfs_item_key(l, &disk_key, mid);
2946
2947 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002948 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002949 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002950 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002951 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002952
2953 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002954
2955 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2956 btrfs_set_header_bytenr(right, right->start);
2957 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002958 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002959 btrfs_set_header_owner(right, root->root_key.objectid);
2960 btrfs_set_header_level(right, 0);
2961 write_extent_buffer(right, root->fs_info->fsid,
2962 (unsigned long)btrfs_header_fsid(right),
2963 BTRFS_FSID_SIZE);
2964
2965 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2966 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2967 BTRFS_UUID_SIZE);
2968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969 if (split == 0) {
2970 if (mid <= slot) {
2971 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002972 insert_ptr(trans, root, path, &disk_key, right->start,
2973 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974 btrfs_tree_unlock(path->nodes[0]);
2975 free_extent_buffer(path->nodes[0]);
2976 path->nodes[0] = right;
2977 path->slots[0] = 0;
2978 path->slots[1] += 1;
2979 } else {
2980 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002981 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002982 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983 btrfs_tree_unlock(path->nodes[0]);
2984 free_extent_buffer(path->nodes[0]);
2985 path->nodes[0] = right;
2986 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01002987 if (path->slots[1] == 0)
2988 fixup_low_keys(trans, root, path,
2989 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002990 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002991 btrfs_mark_buffer_dirty(right);
2992 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002993 }
2994
Jeff Mahoney143bede2012-03-01 14:56:26 +01002995 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04002996
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002997 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002998 BUG_ON(num_doubles != 0);
2999 num_doubles++;
3000 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003001 }
Chris Mason44871b12009-03-13 10:04:31 -04003002
Jeff Mahoney143bede2012-03-01 14:56:26 +01003003 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003004
3005push_for_double:
3006 push_for_double_split(trans, root, path, data_size);
3007 tried_avoid_double = 1;
3008 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3009 return 0;
3010 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011}
3012
Yan, Zhengad48fd752009-11-12 09:33:58 +00003013static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3014 struct btrfs_root *root,
3015 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003016{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003017 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003018 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003019 struct btrfs_file_extent_item *fi;
3020 u64 extent_len = 0;
3021 u32 item_size;
3022 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003023
3024 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003025 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3026
3027 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3028 key.type != BTRFS_EXTENT_CSUM_KEY);
3029
3030 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3031 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003032
3033 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003034 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3035 fi = btrfs_item_ptr(leaf, path->slots[0],
3036 struct btrfs_file_extent_item);
3037 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3038 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003039 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003040
Chris Mason459931e2008-12-10 09:10:46 -05003041 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003042 path->search_for_split = 1;
3043 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003044 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003045 if (ret < 0)
3046 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003047
Yan, Zhengad48fd752009-11-12 09:33:58 +00003048 ret = -EAGAIN;
3049 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003050 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003051 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3052 goto err;
3053
Chris Mason109f6ae2010-04-02 09:20:18 -04003054 /* the leaf has changed, it now has room. return now */
3055 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3056 goto err;
3057
Yan, Zhengad48fd752009-11-12 09:33:58 +00003058 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3059 fi = btrfs_item_ptr(leaf, path->slots[0],
3060 struct btrfs_file_extent_item);
3061 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3062 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003063 }
3064
Chris Masonb9473432009-03-13 11:00:37 -04003065 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003066 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003067 if (ret)
3068 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003069
Yan, Zhengad48fd752009-11-12 09:33:58 +00003070 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003071 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003072 return 0;
3073err:
3074 path->keep_locks = 0;
3075 return ret;
3076}
3077
3078static noinline int split_item(struct btrfs_trans_handle *trans,
3079 struct btrfs_root *root,
3080 struct btrfs_path *path,
3081 struct btrfs_key *new_key,
3082 unsigned long split_offset)
3083{
3084 struct extent_buffer *leaf;
3085 struct btrfs_item *item;
3086 struct btrfs_item *new_item;
3087 int slot;
3088 char *buf;
3089 u32 nritems;
3090 u32 item_size;
3091 u32 orig_offset;
3092 struct btrfs_disk_key disk_key;
3093
Chris Masonb9473432009-03-13 11:00:37 -04003094 leaf = path->nodes[0];
3095 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3096
Chris Masonb4ce94d2009-02-04 09:25:08 -05003097 btrfs_set_path_blocking(path);
3098
Chris Mason459931e2008-12-10 09:10:46 -05003099 item = btrfs_item_nr(leaf, path->slots[0]);
3100 orig_offset = btrfs_item_offset(leaf, item);
3101 item_size = btrfs_item_size(leaf, item);
3102
Chris Mason459931e2008-12-10 09:10:46 -05003103 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003104 if (!buf)
3105 return -ENOMEM;
3106
Chris Mason459931e2008-12-10 09:10:46 -05003107 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3108 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003109
Chris Mason459931e2008-12-10 09:10:46 -05003110 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003111 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003112 if (slot != nritems) {
3113 /* shift the items */
3114 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003115 btrfs_item_nr_offset(slot),
3116 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003117 }
3118
3119 btrfs_cpu_key_to_disk(&disk_key, new_key);
3120 btrfs_set_item_key(leaf, &disk_key, slot);
3121
3122 new_item = btrfs_item_nr(leaf, slot);
3123
3124 btrfs_set_item_offset(leaf, new_item, orig_offset);
3125 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3126
3127 btrfs_set_item_offset(leaf, item,
3128 orig_offset + item_size - split_offset);
3129 btrfs_set_item_size(leaf, item, split_offset);
3130
3131 btrfs_set_header_nritems(leaf, nritems + 1);
3132
3133 /* write the data for the start of the original item */
3134 write_extent_buffer(leaf, buf,
3135 btrfs_item_ptr_offset(leaf, path->slots[0]),
3136 split_offset);
3137
3138 /* write the data for the new item */
3139 write_extent_buffer(leaf, buf + split_offset,
3140 btrfs_item_ptr_offset(leaf, slot),
3141 item_size - split_offset);
3142 btrfs_mark_buffer_dirty(leaf);
3143
Yan, Zhengad48fd752009-11-12 09:33:58 +00003144 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003145 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003146 return 0;
3147}
3148
3149/*
3150 * This function splits a single item into two items,
3151 * giving 'new_key' to the new item and splitting the
3152 * old one at split_offset (from the start of the item).
3153 *
3154 * The path may be released by this operation. After
3155 * the split, the path is pointing to the old item. The
3156 * new item is going to be in the same node as the old one.
3157 *
3158 * Note, the item being split must be smaller enough to live alone on
3159 * a tree block with room for one extra struct btrfs_item
3160 *
3161 * This allows us to split the item in place, keeping a lock on the
3162 * leaf the entire time.
3163 */
3164int btrfs_split_item(struct btrfs_trans_handle *trans,
3165 struct btrfs_root *root,
3166 struct btrfs_path *path,
3167 struct btrfs_key *new_key,
3168 unsigned long split_offset)
3169{
3170 int ret;
3171 ret = setup_leaf_for_split(trans, root, path,
3172 sizeof(struct btrfs_item));
3173 if (ret)
3174 return ret;
3175
3176 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003177 return ret;
3178}
3179
3180/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003181 * This function duplicate a item, giving 'new_key' to the new item.
3182 * It guarantees both items live in the same tree leaf and the new item
3183 * is contiguous with the original item.
3184 *
3185 * This allows us to split file extent in place, keeping a lock on the
3186 * leaf the entire time.
3187 */
3188int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3189 struct btrfs_root *root,
3190 struct btrfs_path *path,
3191 struct btrfs_key *new_key)
3192{
3193 struct extent_buffer *leaf;
3194 int ret;
3195 u32 item_size;
3196
3197 leaf = path->nodes[0];
3198 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3199 ret = setup_leaf_for_split(trans, root, path,
3200 item_size + sizeof(struct btrfs_item));
3201 if (ret)
3202 return ret;
3203
3204 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003205 setup_items_for_insert(trans, root, path, new_key, &item_size,
3206 item_size, item_size +
3207 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003208 leaf = path->nodes[0];
3209 memcpy_extent_buffer(leaf,
3210 btrfs_item_ptr_offset(leaf, path->slots[0]),
3211 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3212 item_size);
3213 return 0;
3214}
3215
3216/*
Chris Masond352ac62008-09-29 15:18:18 -04003217 * make the item pointed to by the path smaller. new_size indicates
3218 * how small to make it, and from_end tells us if we just chop bytes
3219 * off the end of the item or if we shift the item to chop bytes off
3220 * the front.
3221 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003222void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3223 struct btrfs_root *root,
3224 struct btrfs_path *path,
3225 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003226{
Chris Masonb18c6682007-04-17 13:26:50 -04003227 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003228 struct extent_buffer *leaf;
3229 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003230 u32 nritems;
3231 unsigned int data_end;
3232 unsigned int old_data_start;
3233 unsigned int old_size;
3234 unsigned int size_diff;
3235 int i;
3236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003238 slot = path->slots[0];
3239
3240 old_size = btrfs_item_size_nr(leaf, slot);
3241 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003242 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003243
Chris Mason5f39d392007-10-15 16:14:19 -04003244 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003245 data_end = leaf_data_end(root, leaf);
3246
Chris Mason5f39d392007-10-15 16:14:19 -04003247 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003248
Chris Masonb18c6682007-04-17 13:26:50 -04003249 size_diff = old_size - new_size;
3250
3251 BUG_ON(slot < 0);
3252 BUG_ON(slot >= nritems);
3253
3254 /*
3255 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3256 */
3257 /* first correct the data pointers */
3258 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003259 u32 ioff;
3260 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003261
Chris Mason5f39d392007-10-15 16:14:19 -04003262 ioff = btrfs_item_offset(leaf, item);
3263 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003264 }
Chris Masondb945352007-10-15 16:15:53 -04003265
Chris Masonb18c6682007-04-17 13:26:50 -04003266 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003267 if (from_end) {
3268 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3269 data_end + size_diff, btrfs_leaf_data(leaf) +
3270 data_end, old_data_start + new_size - data_end);
3271 } else {
3272 struct btrfs_disk_key disk_key;
3273 u64 offset;
3274
3275 btrfs_item_key(leaf, &disk_key, slot);
3276
3277 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3278 unsigned long ptr;
3279 struct btrfs_file_extent_item *fi;
3280
3281 fi = btrfs_item_ptr(leaf, slot,
3282 struct btrfs_file_extent_item);
3283 fi = (struct btrfs_file_extent_item *)(
3284 (unsigned long)fi - size_diff);
3285
3286 if (btrfs_file_extent_type(leaf, fi) ==
3287 BTRFS_FILE_EXTENT_INLINE) {
3288 ptr = btrfs_item_ptr_offset(leaf, slot);
3289 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003290 (unsigned long)fi,
3291 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003292 disk_bytenr));
3293 }
3294 }
3295
3296 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3297 data_end + size_diff, btrfs_leaf_data(leaf) +
3298 data_end, old_data_start - data_end);
3299
3300 offset = btrfs_disk_key_offset(&disk_key);
3301 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3302 btrfs_set_item_key(leaf, &disk_key, slot);
3303 if (slot == 0)
3304 fixup_low_keys(trans, root, path, &disk_key, 1);
3305 }
Chris Mason5f39d392007-10-15 16:14:19 -04003306
3307 item = btrfs_item_nr(leaf, slot);
3308 btrfs_set_item_size(leaf, item, new_size);
3309 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003310
Chris Mason5f39d392007-10-15 16:14:19 -04003311 if (btrfs_leaf_free_space(root, leaf) < 0) {
3312 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003313 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003314 }
Chris Masonb18c6682007-04-17 13:26:50 -04003315}
3316
Chris Masond352ac62008-09-29 15:18:18 -04003317/*
3318 * make the item pointed to by the path bigger, data_size is the new size.
3319 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003320void btrfs_extend_item(struct btrfs_trans_handle *trans,
3321 struct btrfs_root *root, struct btrfs_path *path,
3322 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003323{
Chris Mason6567e832007-04-16 09:22:45 -04003324 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003325 struct extent_buffer *leaf;
3326 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003327 u32 nritems;
3328 unsigned int data_end;
3329 unsigned int old_data;
3330 unsigned int old_size;
3331 int i;
3332
Chris Mason5f39d392007-10-15 16:14:19 -04003333 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003334
Chris Mason5f39d392007-10-15 16:14:19 -04003335 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003336 data_end = leaf_data_end(root, leaf);
3337
Chris Mason5f39d392007-10-15 16:14:19 -04003338 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3339 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003340 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003341 }
Chris Mason6567e832007-04-16 09:22:45 -04003342 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003343 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003344
3345 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003346 if (slot >= nritems) {
3347 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003348 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3349 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003350 BUG_ON(1);
3351 }
Chris Mason6567e832007-04-16 09:22:45 -04003352
3353 /*
3354 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3355 */
3356 /* first correct the data pointers */
3357 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003358 u32 ioff;
3359 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003360
Chris Mason5f39d392007-10-15 16:14:19 -04003361 ioff = btrfs_item_offset(leaf, item);
3362 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003363 }
Chris Mason5f39d392007-10-15 16:14:19 -04003364
Chris Mason6567e832007-04-16 09:22:45 -04003365 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003366 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003367 data_end - data_size, btrfs_leaf_data(leaf) +
3368 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003369
Chris Mason6567e832007-04-16 09:22:45 -04003370 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003371 old_size = btrfs_item_size_nr(leaf, slot);
3372 item = btrfs_item_nr(leaf, slot);
3373 btrfs_set_item_size(leaf, item, old_size + data_size);
3374 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003375
Chris Mason5f39d392007-10-15 16:14:19 -04003376 if (btrfs_leaf_free_space(root, leaf) < 0) {
3377 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003378 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003379 }
Chris Mason6567e832007-04-16 09:22:45 -04003380}
3381
Chris Mason74123bd2007-02-02 11:05:29 -05003382/*
Chris Masond352ac62008-09-29 15:18:18 -04003383 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003384 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003385 * Returns the number of keys that were inserted.
3386 */
3387int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3388 struct btrfs_root *root,
3389 struct btrfs_path *path,
3390 struct btrfs_key *cpu_key, u32 *data_size,
3391 int nr)
3392{
3393 struct extent_buffer *leaf;
3394 struct btrfs_item *item;
3395 int ret = 0;
3396 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003397 int i;
3398 u32 nritems;
3399 u32 total_data = 0;
3400 u32 total_size = 0;
3401 unsigned int data_end;
3402 struct btrfs_disk_key disk_key;
3403 struct btrfs_key found_key;
3404
Yan Zheng87b29b22008-12-17 10:21:48 -05003405 for (i = 0; i < nr; i++) {
3406 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3407 BTRFS_LEAF_DATA_SIZE(root)) {
3408 break;
3409 nr = i;
3410 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003411 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003412 total_size += data_size[i] + sizeof(struct btrfs_item);
3413 }
3414 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003415
Josef Bacikf3465ca2008-11-12 14:19:50 -05003416 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3417 if (ret == 0)
3418 return -EEXIST;
3419 if (ret < 0)
3420 goto out;
3421
Josef Bacikf3465ca2008-11-12 14:19:50 -05003422 leaf = path->nodes[0];
3423
3424 nritems = btrfs_header_nritems(leaf);
3425 data_end = leaf_data_end(root, leaf);
3426
3427 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3428 for (i = nr; i >= 0; i--) {
3429 total_data -= data_size[i];
3430 total_size -= data_size[i] + sizeof(struct btrfs_item);
3431 if (total_size < btrfs_leaf_free_space(root, leaf))
3432 break;
3433 }
3434 nr = i;
3435 }
3436
3437 slot = path->slots[0];
3438 BUG_ON(slot < 0);
3439
3440 if (slot != nritems) {
3441 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3442
3443 item = btrfs_item_nr(leaf, slot);
3444 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3445
3446 /* figure out how many keys we can insert in here */
3447 total_data = data_size[0];
3448 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003449 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003450 break;
3451 total_data += data_size[i];
3452 }
3453 nr = i;
3454
3455 if (old_data < data_end) {
3456 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003457 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003458 slot, old_data, data_end);
3459 BUG_ON(1);
3460 }
3461 /*
3462 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3463 */
3464 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003465 for (i = slot; i < nritems; i++) {
3466 u32 ioff;
3467
3468 item = btrfs_item_nr(leaf, i);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003469 ioff = btrfs_item_offset(leaf, item);
3470 btrfs_set_item_offset(leaf, item, ioff - total_data);
3471 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003472 /* shift the items */
3473 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3474 btrfs_item_nr_offset(slot),
3475 (nritems - slot) * sizeof(struct btrfs_item));
3476
3477 /* shift the data */
3478 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3479 data_end - total_data, btrfs_leaf_data(leaf) +
3480 data_end, old_data - data_end);
3481 data_end = old_data;
3482 } else {
3483 /*
3484 * this sucks but it has to be done, if we are inserting at
3485 * the end of the leaf only insert 1 of the items, since we
3486 * have no way of knowing whats on the next leaf and we'd have
3487 * to drop our current locks to figure it out
3488 */
3489 nr = 1;
3490 }
3491
3492 /* setup the item for the new data */
3493 for (i = 0; i < nr; i++) {
3494 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3495 btrfs_set_item_key(leaf, &disk_key, slot + i);
3496 item = btrfs_item_nr(leaf, slot + i);
3497 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3498 data_end -= data_size[i];
3499 btrfs_set_item_size(leaf, item, data_size[i]);
3500 }
3501 btrfs_set_header_nritems(leaf, nritems + nr);
3502 btrfs_mark_buffer_dirty(leaf);
3503
3504 ret = 0;
3505 if (slot == 0) {
3506 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003507 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003508 }
3509
3510 if (btrfs_leaf_free_space(root, leaf) < 0) {
3511 btrfs_print_leaf(root, leaf);
3512 BUG();
3513 }
3514out:
3515 if (!ret)
3516 ret = nr;
3517 return ret;
3518}
3519
3520/*
Chris Mason44871b12009-03-13 10:04:31 -04003521 * this is a helper for btrfs_insert_empty_items, the main goal here is
3522 * to save stack depth by doing the bulk of the work in a function
3523 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003524 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003525void setup_items_for_insert(struct btrfs_trans_handle *trans,
3526 struct btrfs_root *root, struct btrfs_path *path,
3527 struct btrfs_key *cpu_key, u32 *data_size,
3528 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003529{
Chris Mason5f39d392007-10-15 16:14:19 -04003530 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003531 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003532 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003533 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003534 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003535 struct extent_buffer *leaf;
3536 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003537
Chris Mason5f39d392007-10-15 16:14:19 -04003538 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003539 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003540
Chris Mason5f39d392007-10-15 16:14:19 -04003541 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003542 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003543
Chris Masonf25956c2008-09-12 15:32:53 -04003544 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003545 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003546 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003547 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003548 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003549 }
Chris Mason5f39d392007-10-15 16:14:19 -04003550
Chris Masonbe0e5c02007-01-26 15:51:26 -05003551 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003552 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003553
Chris Mason5f39d392007-10-15 16:14:19 -04003554 if (old_data < data_end) {
3555 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003556 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003557 slot, old_data, data_end);
3558 BUG_ON(1);
3559 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003560 /*
3561 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3562 */
3563 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003564 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003565 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003566
Chris Mason5f39d392007-10-15 16:14:19 -04003567 item = btrfs_item_nr(leaf, i);
3568 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003569 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003570 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003572 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003573 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003574 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575
3576 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003577 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003578 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003579 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003580 data_end = old_data;
3581 }
Chris Mason5f39d392007-10-15 16:14:19 -04003582
Chris Mason62e27492007-03-15 12:56:47 -04003583 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003584 for (i = 0; i < nr; i++) {
3585 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3586 btrfs_set_item_key(leaf, &disk_key, slot + i);
3587 item = btrfs_item_nr(leaf, slot + i);
3588 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3589 data_end -= data_size[i];
3590 btrfs_set_item_size(leaf, item, data_size[i]);
3591 }
Chris Mason44871b12009-03-13 10:04:31 -04003592
Chris Mason9c583092008-01-29 15:15:18 -05003593 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003594
Chris Mason5a01a2e2008-01-30 11:43:54 -05003595 if (slot == 0) {
3596 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003597 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003598 }
Chris Masonb9473432009-03-13 11:00:37 -04003599 btrfs_unlock_up_safe(path, 1);
3600 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003601
Chris Mason5f39d392007-10-15 16:14:19 -04003602 if (btrfs_leaf_free_space(root, leaf) < 0) {
3603 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003604 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003605 }
Chris Mason44871b12009-03-13 10:04:31 -04003606}
3607
3608/*
3609 * Given a key and some data, insert items into the tree.
3610 * This does all the path init required, making room in the tree if needed.
3611 */
3612int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3613 struct btrfs_root *root,
3614 struct btrfs_path *path,
3615 struct btrfs_key *cpu_key, u32 *data_size,
3616 int nr)
3617{
Chris Mason44871b12009-03-13 10:04:31 -04003618 int ret = 0;
3619 int slot;
3620 int i;
3621 u32 total_size = 0;
3622 u32 total_data = 0;
3623
3624 for (i = 0; i < nr; i++)
3625 total_data += data_size[i];
3626
3627 total_size = total_data + (nr * sizeof(struct btrfs_item));
3628 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3629 if (ret == 0)
3630 return -EEXIST;
3631 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003632 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003633
Chris Mason44871b12009-03-13 10:04:31 -04003634 slot = path->slots[0];
3635 BUG_ON(slot < 0);
3636
Jeff Mahoney143bede2012-03-01 14:56:26 +01003637 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003638 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003639 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003640}
3641
3642/*
3643 * Given a key and some data, insert an item into the tree.
3644 * This does all the path init required, making room in the tree if needed.
3645 */
Chris Masone089f052007-03-16 16:20:31 -04003646int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3647 *root, struct btrfs_key *cpu_key, void *data, u32
3648 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003649{
3650 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003651 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003652 struct extent_buffer *leaf;
3653 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003654
Chris Mason2c90e5d2007-04-02 10:50:19 -04003655 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003656 if (!path)
3657 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003658 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003659 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003660 leaf = path->nodes[0];
3661 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3662 write_extent_buffer(leaf, data, ptr, data_size);
3663 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003664 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003665 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003666 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003667}
3668
Chris Mason74123bd2007-02-02 11:05:29 -05003669/*
Chris Mason5de08d72007-02-24 06:24:44 -05003670 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003671 *
Chris Masond352ac62008-09-29 15:18:18 -04003672 * the tree should have been previously balanced so the deletion does not
3673 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003674 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003675static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3676 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003677{
Chris Mason5f39d392007-10-15 16:14:19 -04003678 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003679 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003680
Chris Mason5f39d392007-10-15 16:14:19 -04003681 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003682 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003683 memmove_extent_buffer(parent,
3684 btrfs_node_key_ptr_offset(slot),
3685 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003686 sizeof(struct btrfs_key_ptr) *
3687 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003688 }
Chris Mason7518a232007-03-12 12:01:18 -04003689 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003690 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003691 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003692 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003693 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003694 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003695 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003696 struct btrfs_disk_key disk_key;
3697
3698 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003699 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003700 }
Chris Masond6025572007-03-30 14:27:56 -04003701 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003702}
3703
Chris Mason74123bd2007-02-02 11:05:29 -05003704/*
Chris Mason323ac952008-10-01 19:05:46 -04003705 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003706 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003707 *
3708 * This deletes the pointer in path->nodes[1] and frees the leaf
3709 * block extent. zero is returned if it all worked out, < 0 otherwise.
3710 *
3711 * The path must have already been setup for deleting the leaf, including
3712 * all the proper balancing. path->nodes[1] must be locked.
3713 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003714static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3715 struct btrfs_root *root,
3716 struct btrfs_path *path,
3717 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003718{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003719 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003720 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003721
Chris Mason4d081c42009-02-04 09:31:28 -05003722 /*
3723 * btrfs_free_extent is expensive, we want to make sure we
3724 * aren't holding any locks when we call it
3725 */
3726 btrfs_unlock_up_safe(path, 0);
3727
Yan, Zhengf0486c62010-05-16 10:46:25 -04003728 root_sub_used(root, leaf->len);
3729
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003730 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003731}
3732/*
Chris Mason74123bd2007-02-02 11:05:29 -05003733 * delete the item at the leaf level in path. If that empties
3734 * the leaf, remove it from the tree
3735 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003736int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3737 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003738{
Chris Mason5f39d392007-10-15 16:14:19 -04003739 struct extent_buffer *leaf;
3740 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003741 int last_off;
3742 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003743 int ret = 0;
3744 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003745 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003746 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003747
Chris Mason5f39d392007-10-15 16:14:19 -04003748 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003749 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3750
3751 for (i = 0; i < nr; i++)
3752 dsize += btrfs_item_size_nr(leaf, slot + i);
3753
Chris Mason5f39d392007-10-15 16:14:19 -04003754 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003755
Chris Mason85e21ba2008-01-29 15:11:36 -05003756 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003757 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003758
3759 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003760 data_end + dsize,
3761 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003762 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003763
Chris Mason85e21ba2008-01-29 15:11:36 -05003764 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003765 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003766
Chris Mason5f39d392007-10-15 16:14:19 -04003767 item = btrfs_item_nr(leaf, i);
3768 ioff = btrfs_item_offset(leaf, item);
3769 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003770 }
Chris Masondb945352007-10-15 16:15:53 -04003771
Chris Mason5f39d392007-10-15 16:14:19 -04003772 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003773 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003774 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003775 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003776 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003777 btrfs_set_header_nritems(leaf, nritems - nr);
3778 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003779
Chris Mason74123bd2007-02-02 11:05:29 -05003780 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003781 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003782 if (leaf == root->node) {
3783 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003784 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003785 btrfs_set_path_blocking(path);
3786 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003787 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003788 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003789 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003790 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003791 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003792 struct btrfs_disk_key disk_key;
3793
3794 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003795 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003796 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003797
Chris Mason74123bd2007-02-02 11:05:29 -05003798 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003799 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003800 /* push_leaf_left fixes the path.
3801 * make sure the path still points to our leaf
3802 * for possible call to del_ptr below
3803 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003804 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003805 extent_buffer_get(leaf);
3806
Chris Masonb9473432009-03-13 11:00:37 -04003807 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003808 wret = push_leaf_left(trans, root, path, 1, 1,
3809 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003810 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003811 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003812
3813 if (path->nodes[0] == leaf &&
3814 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003815 wret = push_leaf_right(trans, root, path, 1,
3816 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003817 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003818 ret = wret;
3819 }
Chris Mason5f39d392007-10-15 16:14:19 -04003820
3821 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003822 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003823 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003824 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003825 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003826 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003827 /* if we're still in the path, make sure
3828 * we're dirty. Otherwise, one of the
3829 * push_leaf functions must have already
3830 * dirtied this buffer
3831 */
3832 if (path->nodes[0] == leaf)
3833 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003834 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003835 }
Chris Masond5719762007-03-23 10:01:08 -04003836 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003837 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003838 }
3839 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003840 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003841}
3842
Chris Mason97571fd2007-02-24 13:39:08 -05003843/*
Chris Mason925baed2008-06-25 16:01:30 -04003844 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003845 * returns 0 if it found something or 1 if there are no lesser leaves.
3846 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003847 *
3848 * This may release the path, and so you may lose any locks held at the
3849 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003850 */
3851int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3852{
Chris Mason925baed2008-06-25 16:01:30 -04003853 struct btrfs_key key;
3854 struct btrfs_disk_key found_key;
3855 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003856
Chris Mason925baed2008-06-25 16:01:30 -04003857 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003858
Chris Mason925baed2008-06-25 16:01:30 -04003859 if (key.offset > 0)
3860 key.offset--;
3861 else if (key.type > 0)
3862 key.type--;
3863 else if (key.objectid > 0)
3864 key.objectid--;
3865 else
3866 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003867
David Sterbab3b4aa72011-04-21 01:20:15 +02003868 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003869 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3870 if (ret < 0)
3871 return ret;
3872 btrfs_item_key(path->nodes[0], &found_key, 0);
3873 ret = comp_keys(&found_key, &key);
3874 if (ret < 0)
3875 return 0;
3876 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003877}
3878
Chris Mason3f157a22008-06-25 16:01:31 -04003879/*
3880 * A helper function to walk down the tree starting at min_key, and looking
3881 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003882 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003883 *
3884 * This does not cow, but it does stuff the starting key it finds back
3885 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3886 * key and get a writable path.
3887 *
3888 * This does lock as it descends, and path->keep_locks should be set
3889 * to 1 by the caller.
3890 *
3891 * This honors path->lowest_level to prevent descent past a given level
3892 * of the tree.
3893 *
Chris Masond352ac62008-09-29 15:18:18 -04003894 * min_trans indicates the oldest transaction that you are interested
3895 * in walking through. Any nodes or leaves older than min_trans are
3896 * skipped over (without reading them).
3897 *
Chris Mason3f157a22008-06-25 16:01:31 -04003898 * returns zero if something useful was found, < 0 on error and 1 if there
3899 * was nothing in the tree that matched the search criteria.
3900 */
3901int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003902 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003903 struct btrfs_path *path, int cache_only,
3904 u64 min_trans)
3905{
3906 struct extent_buffer *cur;
3907 struct btrfs_key found_key;
3908 int slot;
Yan96524802008-07-24 12:19:49 -04003909 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003910 u32 nritems;
3911 int level;
3912 int ret = 1;
3913
Chris Mason934d3752008-12-08 16:43:10 -05003914 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003915again:
Chris Masonbd681512011-07-16 15:23:14 -04003916 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003917 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003918 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003919 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003920 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003921
3922 if (btrfs_header_generation(cur) < min_trans) {
3923 ret = 1;
3924 goto out;
3925 }
Chris Masond3977122009-01-05 21:25:51 -05003926 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003927 nritems = btrfs_header_nritems(cur);
3928 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003929 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003930
Chris Mason323ac952008-10-01 19:05:46 -04003931 /* at the lowest level, we're done, setup the path and exit */
3932 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003933 if (slot >= nritems)
3934 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003935 ret = 0;
3936 path->slots[level] = slot;
3937 btrfs_item_key_to_cpu(cur, &found_key, slot);
3938 goto out;
3939 }
Yan96524802008-07-24 12:19:49 -04003940 if (sret && slot > 0)
3941 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003942 /*
3943 * check this node pointer against the cache_only and
3944 * min_trans parameters. If it isn't in cache or is too
3945 * old, skip to the next one.
3946 */
Chris Masond3977122009-01-05 21:25:51 -05003947 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003948 u64 blockptr;
3949 u64 gen;
3950 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003951 struct btrfs_disk_key disk_key;
3952
Chris Mason3f157a22008-06-25 16:01:31 -04003953 blockptr = btrfs_node_blockptr(cur, slot);
3954 gen = btrfs_node_ptr_generation(cur, slot);
3955 if (gen < min_trans) {
3956 slot++;
3957 continue;
3958 }
3959 if (!cache_only)
3960 break;
3961
Chris Masone02119d2008-09-05 16:13:11 -04003962 if (max_key) {
3963 btrfs_node_key(cur, &disk_key, slot);
3964 if (comp_keys(&disk_key, max_key) >= 0) {
3965 ret = 1;
3966 goto out;
3967 }
3968 }
3969
Chris Mason3f157a22008-06-25 16:01:31 -04003970 tmp = btrfs_find_tree_block(root, blockptr,
3971 btrfs_level_size(root, level - 1));
3972
3973 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3974 free_extent_buffer(tmp);
3975 break;
3976 }
3977 if (tmp)
3978 free_extent_buffer(tmp);
3979 slot++;
3980 }
Chris Masone02119d2008-09-05 16:13:11 -04003981find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003982 /*
3983 * we didn't find a candidate key in this node, walk forward
3984 * and find another one
3985 */
3986 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003987 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003988 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003989 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003990 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003991 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003992 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003993 goto again;
3994 } else {
3995 goto out;
3996 }
3997 }
3998 /* save our key for returning back */
3999 btrfs_node_key_to_cpu(cur, &found_key, slot);
4000 path->slots[level] = slot;
4001 if (level == path->lowest_level) {
4002 ret = 0;
4003 unlock_up(path, level, 1);
4004 goto out;
4005 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004006 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004007 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004008 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004009
Chris Masonbd681512011-07-16 15:23:14 -04004010 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004011
Chris Masonbd681512011-07-16 15:23:14 -04004012 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004013 path->nodes[level - 1] = cur;
4014 unlock_up(path, level, 1);
Chris Masonbd681512011-07-16 15:23:14 -04004015 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004016 }
4017out:
4018 if (ret == 0)
4019 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004020 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004021 return ret;
4022}
4023
4024/*
4025 * this is similar to btrfs_next_leaf, but does not try to preserve
4026 * and fixup the path. It looks for and returns the next key in the
4027 * tree based on the current path and the cache_only and min_trans
4028 * parameters.
4029 *
4030 * 0 is returned if another key is found, < 0 if there are any errors
4031 * and 1 is returned if there are no higher keys in the tree
4032 *
4033 * path->keep_locks should be set to 1 on the search made before
4034 * calling this function.
4035 */
Chris Masone7a84562008-06-25 16:01:31 -04004036int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004037 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004038 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004039{
Chris Masone7a84562008-06-25 16:01:31 -04004040 int slot;
4041 struct extent_buffer *c;
4042
Chris Mason934d3752008-12-08 16:43:10 -05004043 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004044 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004045 if (!path->nodes[level])
4046 return 1;
4047
4048 slot = path->slots[level] + 1;
4049 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004050next:
Chris Masone7a84562008-06-25 16:01:31 -04004051 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004052 int ret;
4053 int orig_lowest;
4054 struct btrfs_key cur_key;
4055 if (level + 1 >= BTRFS_MAX_LEVEL ||
4056 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004057 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004058
4059 if (path->locks[level + 1]) {
4060 level++;
4061 continue;
4062 }
4063
4064 slot = btrfs_header_nritems(c) - 1;
4065 if (level == 0)
4066 btrfs_item_key_to_cpu(c, &cur_key, slot);
4067 else
4068 btrfs_node_key_to_cpu(c, &cur_key, slot);
4069
4070 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004071 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004072 path->lowest_level = level;
4073 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4074 0, 0);
4075 path->lowest_level = orig_lowest;
4076 if (ret < 0)
4077 return ret;
4078
4079 c = path->nodes[level];
4080 slot = path->slots[level];
4081 if (ret == 0)
4082 slot++;
4083 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004084 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004085
Chris Masone7a84562008-06-25 16:01:31 -04004086 if (level == 0)
4087 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004088 else {
4089 u64 blockptr = btrfs_node_blockptr(c, slot);
4090 u64 gen = btrfs_node_ptr_generation(c, slot);
4091
4092 if (cache_only) {
4093 struct extent_buffer *cur;
4094 cur = btrfs_find_tree_block(root, blockptr,
4095 btrfs_level_size(root, level - 1));
4096 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4097 slot++;
4098 if (cur)
4099 free_extent_buffer(cur);
4100 goto next;
4101 }
4102 free_extent_buffer(cur);
4103 }
4104 if (gen < min_trans) {
4105 slot++;
4106 goto next;
4107 }
Chris Masone7a84562008-06-25 16:01:31 -04004108 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004109 }
Chris Masone7a84562008-06-25 16:01:31 -04004110 return 0;
4111 }
4112 return 1;
4113}
4114
Chris Mason7bb86312007-12-11 09:25:06 -05004115/*
Chris Mason925baed2008-06-25 16:01:30 -04004116 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004117 * returns 0 if it found something or 1 if there are no greater leaves.
4118 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004119 */
Chris Mason234b63a2007-03-13 10:46:10 -04004120int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004121{
4122 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004123 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004124 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004125 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004126 struct btrfs_key key;
4127 u32 nritems;
4128 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004129 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004130 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004131
4132 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004133 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004134 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004135
Chris Mason8e73f272009-04-03 10:14:18 -04004136 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4137again:
4138 level = 1;
4139 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004140 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004141 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004142
Chris Masona2135012008-06-25 16:01:30 -04004143 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004144 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004145
Chris Mason925baed2008-06-25 16:01:30 -04004146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4147 path->keep_locks = 0;
4148
4149 if (ret < 0)
4150 return ret;
4151
Chris Masona2135012008-06-25 16:01:30 -04004152 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004153 /*
4154 * by releasing the path above we dropped all our locks. A balance
4155 * could have added more items next to the key that used to be
4156 * at the very end of the block. So, check again here and
4157 * advance the path if there are now more items available.
4158 */
Chris Masona2135012008-06-25 16:01:30 -04004159 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004160 if (ret == 0)
4161 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004162 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004163 goto done;
4164 }
Chris Masond97e63b2007-02-20 16:40:44 -05004165
Chris Masond3977122009-01-05 21:25:51 -05004166 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004167 if (!path->nodes[level]) {
4168 ret = 1;
4169 goto done;
4170 }
Chris Mason5f39d392007-10-15 16:14:19 -04004171
Chris Masond97e63b2007-02-20 16:40:44 -05004172 slot = path->slots[level] + 1;
4173 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004174 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004175 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004176 if (level == BTRFS_MAX_LEVEL) {
4177 ret = 1;
4178 goto done;
4179 }
Chris Masond97e63b2007-02-20 16:40:44 -05004180 continue;
4181 }
Chris Mason5f39d392007-10-15 16:14:19 -04004182
Chris Mason925baed2008-06-25 16:01:30 -04004183 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004184 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004185 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004186 }
Chris Mason5f39d392007-10-15 16:14:19 -04004187
Chris Mason8e73f272009-04-03 10:14:18 -04004188 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004189 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004190 ret = read_block_for_search(NULL, root, path, &next, level,
4191 slot, &key);
4192 if (ret == -EAGAIN)
4193 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004194
Chris Mason76a05b32009-05-14 13:24:30 -04004195 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004196 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004197 goto done;
4198 }
4199
Chris Mason5cd57b22008-06-25 16:01:30 -04004200 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004201 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004202 if (!ret) {
4203 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004204 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004205 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004206 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004207 }
Chris Mason31533fb2011-07-26 16:01:59 -04004208 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004209 }
Chris Masond97e63b2007-02-20 16:40:44 -05004210 break;
4211 }
4212 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004213 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004214 level--;
4215 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004216 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004217 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004218
Chris Mason5f39d392007-10-15 16:14:19 -04004219 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004220 path->nodes[level] = next;
4221 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004222 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004223 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004224 if (!level)
4225 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004226
Chris Mason8e73f272009-04-03 10:14:18 -04004227 ret = read_block_for_search(NULL, root, path, &next, level,
4228 0, &key);
4229 if (ret == -EAGAIN)
4230 goto again;
4231
Chris Mason76a05b32009-05-14 13:24:30 -04004232 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004233 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004234 goto done;
4235 }
4236
Chris Mason5cd57b22008-06-25 16:01:30 -04004237 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004238 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004239 if (!ret) {
4240 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004241 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004242 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004243 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004244 }
Chris Mason31533fb2011-07-26 16:01:59 -04004245 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004246 }
Chris Masond97e63b2007-02-20 16:40:44 -05004247 }
Chris Mason8e73f272009-04-03 10:14:18 -04004248 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004249done:
4250 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004251 path->leave_spinning = old_spinning;
4252 if (!old_spinning)
4253 btrfs_set_path_blocking(path);
4254
4255 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004256}
Chris Mason0b86a832008-03-24 15:01:56 -04004257
Chris Mason3f157a22008-06-25 16:01:31 -04004258/*
4259 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4260 * searching until it gets past min_objectid or finds an item of 'type'
4261 *
4262 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4263 */
Chris Mason0b86a832008-03-24 15:01:56 -04004264int btrfs_previous_item(struct btrfs_root *root,
4265 struct btrfs_path *path, u64 min_objectid,
4266 int type)
4267{
4268 struct btrfs_key found_key;
4269 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004270 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004271 int ret;
4272
Chris Masond3977122009-01-05 21:25:51 -05004273 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004274 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004275 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004276 ret = btrfs_prev_leaf(root, path);
4277 if (ret != 0)
4278 return ret;
4279 } else {
4280 path->slots[0]--;
4281 }
4282 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004283 nritems = btrfs_header_nritems(leaf);
4284 if (nritems == 0)
4285 return 1;
4286 if (path->slots[0] == nritems)
4287 path->slots[0]--;
4288
Chris Mason0b86a832008-03-24 15:01:56 -04004289 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004290 if (found_key.objectid < min_objectid)
4291 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004292 if (found_key.type == type)
4293 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004294 if (found_key.objectid == min_objectid &&
4295 found_key.type < type)
4296 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004297 }
4298 return 1;
4299}