blob: 9d472c2a8de85b2abb335cf8af44e94a8f0a93e0 [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);
334 BUG_ON(ret);
335 BUG_ON(refs == 0);
336 } else {
337 refs = 1;
338 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
339 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
340 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
341 else
342 flags = 0;
343 }
344
345 owner = btrfs_header_owner(buf);
346 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
347 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
348
349 if (refs > 1) {
350 if ((owner == root->root_key.objectid ||
351 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
352 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200353 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 BUG_ON(ret);
355
356 if (root->root_key.objectid ==
357 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200358 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200360 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400361 BUG_ON(ret);
362 }
363 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
364 } else {
365
366 if (root->root_key.objectid ==
367 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200368 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400369 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200370 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400371 BUG_ON(ret);
372 }
373 if (new_flags != 0) {
374 ret = btrfs_set_disk_extent_flags(trans, root,
375 buf->start,
376 buf->len,
377 new_flags, 0);
378 BUG_ON(ret);
379 }
380 } else {
381 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
382 if (root->root_key.objectid ==
383 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200384 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400385 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200386 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200388 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 BUG_ON(ret);
390 }
391 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400392 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400393 }
394 return 0;
395}
396
397/*
Chris Masond3977122009-01-05 21:25:51 -0500398 * does the dirty work in cow of a single block. The parent block (if
399 * supplied) is updated to point to the new cow copy. The new buffer is marked
400 * dirty and returned locked. If you modify the block it needs to be marked
401 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400402 *
403 * search_start -- an allocation hint for the new block
404 *
Chris Masond3977122009-01-05 21:25:51 -0500405 * empty_size -- a hint that you plan on doing more cow. This is the size in
406 * bytes the allocator should try to find free next to the block it returns.
407 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400408 */
Chris Masond3977122009-01-05 21:25:51 -0500409static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400410 struct btrfs_root *root,
411 struct extent_buffer *buf,
412 struct extent_buffer *parent, int parent_slot,
413 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400414 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400415{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400416 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400417 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500418 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400419 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400420 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400421 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400422
Chris Mason925baed2008-06-25 16:01:30 -0400423 if (*cow_ret == buf)
424 unlock_orig = 1;
425
Chris Masonb9447ef2009-03-09 11:45:38 -0400426 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400427
Chris Mason7bb86312007-12-11 09:25:06 -0500428 WARN_ON(root->ref_cows && trans->transid !=
429 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400430 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400431
Chris Mason7bb86312007-12-11 09:25:06 -0500432 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400433
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400434 if (level == 0)
435 btrfs_item_key(buf, &disk_key, 0);
436 else
437 btrfs_node_key(buf, &disk_key, 0);
438
439 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
440 if (parent)
441 parent_start = parent->start;
442 else
443 parent_start = 0;
444 } else
445 parent_start = 0;
446
447 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
448 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200449 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400450 if (IS_ERR(cow))
451 return PTR_ERR(cow);
452
Chris Masonb4ce94d2009-02-04 09:25:08 -0500453 /* cow is set to blocking by btrfs_init_new_buffer */
454
Chris Mason5f39d392007-10-15 16:14:19 -0400455 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400456 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400457 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400458 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
459 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
460 BTRFS_HEADER_FLAG_RELOC);
461 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
462 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
463 else
464 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400465
Yan Zheng2b820322008-11-17 21:11:30 -0500466 write_extent_buffer(cow, root->fs_info->fsid,
467 (unsigned long)btrfs_header_fsid(cow),
468 BTRFS_FSID_SIZE);
469
Yan, Zhengf0486c62010-05-16 10:46:25 -0400470 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400471
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400472 if (root->ref_cows)
473 btrfs_reloc_cow_block(trans, root, buf, cow);
474
Chris Mason6702ed42007-08-07 16:15:09 -0400475 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400476 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
478 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
479 parent_start = buf->start;
480 else
481 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400482
Chris Mason5f39d392007-10-15 16:14:19 -0400483 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400484 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400485
Yan, Zhengf0486c62010-05-16 10:46:25 -0400486 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200487 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400488 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400489 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400490 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400491 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
492 parent_start = parent->start;
493 else
494 parent_start = 0;
495
496 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400497 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400498 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500499 btrfs_set_node_ptr_generation(parent, parent_slot,
500 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400501 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400502 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200503 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400504 }
Chris Mason925baed2008-06-25 16:01:30 -0400505 if (unlock_orig)
506 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400507 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400508 btrfs_mark_buffer_dirty(cow);
509 *cow_ret = cow;
510 return 0;
511}
512
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400513static inline int should_cow_block(struct btrfs_trans_handle *trans,
514 struct btrfs_root *root,
515 struct extent_buffer *buf)
516{
Liu Bof1ebcc72011-11-14 20:48:06 -0500517 /* ensure we can see the force_cow */
518 smp_rmb();
519
520 /*
521 * We do not need to cow a block if
522 * 1) this block is not created or changed in this transaction;
523 * 2) this block does not belong to TREE_RELOC tree;
524 * 3) the root is not forced COW.
525 *
526 * What is forced COW:
527 * when we create snapshot during commiting the transaction,
528 * after we've finished coping src root, we must COW the shared
529 * block to ensure the metadata consistency.
530 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400531 if (btrfs_header_generation(buf) == trans->transid &&
532 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
533 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500534 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
535 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400536 return 0;
537 return 1;
538}
539
Chris Masond352ac62008-09-29 15:18:18 -0400540/*
541 * cows a single block, see __btrfs_cow_block for the real work.
542 * This version of it has extra checks so that a block isn't cow'd more than
543 * once per transaction, as long as it hasn't been written yet
544 */
Chris Masond3977122009-01-05 21:25:51 -0500545noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400546 struct btrfs_root *root, struct extent_buffer *buf,
547 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400548 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500549{
Chris Mason6702ed42007-08-07 16:15:09 -0400550 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400551 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500552
Chris Masonccd467d2007-06-28 15:57:36 -0400553 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500554 printk(KERN_CRIT "trans %llu running %llu\n",
555 (unsigned long long)trans->transid,
556 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400557 root->fs_info->running_transaction->transid);
558 WARN_ON(1);
559 }
560 if (trans->transid != root->fs_info->generation) {
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)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400564 WARN_ON(1);
565 }
Chris Masondc17ff82008-01-08 15:46:30 -0500566
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400567 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500568 *cow_ret = buf;
569 return 0;
570 }
Chris Masonc4876852009-02-04 09:24:25 -0500571
Chris Mason0b86a832008-03-24 15:01:56 -0400572 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500573
574 if (parent)
575 btrfs_set_lock_blocking(parent);
576 btrfs_set_lock_blocking(buf);
577
Chris Masonf510cfe2007-10-15 16:14:48 -0400578 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400579 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000580
581 trace_btrfs_cow_block(root, buf, *cow_ret);
582
Chris Masonf510cfe2007-10-15 16:14:48 -0400583 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400584}
585
Chris Masond352ac62008-09-29 15:18:18 -0400586/*
587 * helper function for defrag to decide if two blocks pointed to by a
588 * node are actually close by
589 */
Chris Mason6b800532007-10-15 16:17:34 -0400590static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400591{
Chris Mason6b800532007-10-15 16:17:34 -0400592 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400593 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400594 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400595 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500596 return 0;
597}
598
Chris Mason081e9572007-11-06 10:26:24 -0500599/*
600 * compare two keys in a memcmp fashion
601 */
602static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
603{
604 struct btrfs_key k1;
605
606 btrfs_disk_key_to_cpu(&k1, disk);
607
Diego Calleja20736ab2009-07-24 11:06:52 -0400608 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500609}
610
Josef Bacikf3465ca2008-11-12 14:19:50 -0500611/*
612 * same as comp_keys only with two btrfs_key's
613 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400614int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500615{
616 if (k1->objectid > k2->objectid)
617 return 1;
618 if (k1->objectid < k2->objectid)
619 return -1;
620 if (k1->type > k2->type)
621 return 1;
622 if (k1->type < k2->type)
623 return -1;
624 if (k1->offset > k2->offset)
625 return 1;
626 if (k1->offset < k2->offset)
627 return -1;
628 return 0;
629}
Chris Mason081e9572007-11-06 10:26:24 -0500630
Chris Masond352ac62008-09-29 15:18:18 -0400631/*
632 * this is used by the defrag code to go through all the
633 * leaves pointed to by a node and reallocate them so that
634 * disk order is close to key order
635 */
Chris Mason6702ed42007-08-07 16:15:09 -0400636int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400637 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400638 int start_slot, int cache_only, u64 *last_ret,
639 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400640{
Chris Mason6b800532007-10-15 16:17:34 -0400641 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400642 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400643 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400644 u64 search_start = *last_ret;
645 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400646 u64 other;
647 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400648 int end_slot;
649 int i;
650 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400651 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400652 int uptodate;
653 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500654 int progress_passed = 0;
655 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400656
Chris Mason5708b952007-10-25 15:43:18 -0400657 parent_level = btrfs_header_level(parent);
658 if (cache_only && parent_level != 1)
659 return 0;
660
Chris Masond3977122009-01-05 21:25:51 -0500661 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400662 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500663 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400664 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400665
Chris Mason6b800532007-10-15 16:17:34 -0400666 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400667 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400668 end_slot = parent_nritems;
669
670 if (parent_nritems == 1)
671 return 0;
672
Chris Masonb4ce94d2009-02-04 09:25:08 -0500673 btrfs_set_lock_blocking(parent);
674
Chris Mason6702ed42007-08-07 16:15:09 -0400675 for (i = start_slot; i < end_slot; i++) {
676 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400677
Chris Mason081e9572007-11-06 10:26:24 -0500678 btrfs_node_key(parent, &disk_key, i);
679 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
680 continue;
681
682 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400683 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400684 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400685 if (last_block == 0)
686 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400687
Chris Mason6702ed42007-08-07 16:15:09 -0400688 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400689 other = btrfs_node_blockptr(parent, i - 1);
690 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400691 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400692 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400693 other = btrfs_node_blockptr(parent, i + 1);
694 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400695 }
Chris Masone9d0b132007-08-10 14:06:19 -0400696 if (close) {
697 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400698 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400699 }
Chris Mason6702ed42007-08-07 16:15:09 -0400700
Chris Mason6b800532007-10-15 16:17:34 -0400701 cur = btrfs_find_tree_block(root, blocknr, blocksize);
702 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400703 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400704 else
705 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400706 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400707 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400708 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400709 continue;
710 }
Chris Mason6b800532007-10-15 16:17:34 -0400711 if (!cur) {
712 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400713 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000714 if (!cur)
715 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400716 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400717 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400718 }
Chris Mason6702ed42007-08-07 16:15:09 -0400719 }
Chris Masone9d0b132007-08-10 14:06:19 -0400720 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400721 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400722
Chris Masone7a84562008-06-25 16:01:31 -0400723 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500724 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400725 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400726 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400727 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400728 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400729 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400730 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400731 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400732 break;
Yan252c38f2007-08-29 09:11:44 -0400733 }
Chris Masone7a84562008-06-25 16:01:31 -0400734 search_start = cur->start;
735 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400736 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400737 btrfs_tree_unlock(cur);
738 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400739 }
740 return err;
741}
742
Chris Mason74123bd2007-02-02 11:05:29 -0500743/*
744 * The leaf data grows from end-to-front in the node.
745 * this returns the address of the start of the last item,
746 * which is the stop of the leaf data stack
747 */
Chris Mason123abc82007-03-14 14:14:43 -0400748static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400749 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500750{
Chris Mason5f39d392007-10-15 16:14:19 -0400751 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500752 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400753 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400754 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500755}
756
Chris Masonaa5d6be2007-02-28 16:35:06 -0500757
Chris Mason74123bd2007-02-02 11:05:29 -0500758/*
Chris Mason5f39d392007-10-15 16:14:19 -0400759 * search for key in the extent_buffer. The items start at offset p,
760 * and they are item_size apart. There are 'max' items in p.
761 *
Chris Mason74123bd2007-02-02 11:05:29 -0500762 * the slot in the array is returned via slot, and it points to
763 * the place where you would insert key if it is not found in
764 * the array.
765 *
766 * slot may point to max if the key is bigger than all of the keys
767 */
Chris Masone02119d2008-09-05 16:13:11 -0400768static noinline int generic_bin_search(struct extent_buffer *eb,
769 unsigned long p,
770 int item_size, struct btrfs_key *key,
771 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500772{
773 int low = 0;
774 int high = max;
775 int mid;
776 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400777 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400778 struct btrfs_disk_key unaligned;
779 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400780 char *kaddr = NULL;
781 unsigned long map_start = 0;
782 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400783 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500784
Chris Masond3977122009-01-05 21:25:51 -0500785 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500786 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400787 offset = p + mid * item_size;
788
Chris Masona6591712011-07-19 12:04:14 -0400789 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400790 (offset + sizeof(struct btrfs_disk_key)) >
791 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500792
793 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400794 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400795 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400796
Chris Mason479965d2007-10-15 16:14:27 -0400797 if (!err) {
798 tmp = (struct btrfs_disk_key *)(kaddr + offset -
799 map_start);
800 } else {
801 read_extent_buffer(eb, &unaligned,
802 offset, sizeof(unaligned));
803 tmp = &unaligned;
804 }
805
Chris Mason5f39d392007-10-15 16:14:19 -0400806 } else {
807 tmp = (struct btrfs_disk_key *)(kaddr + offset -
808 map_start);
809 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500810 ret = comp_keys(tmp, key);
811
812 if (ret < 0)
813 low = mid + 1;
814 else if (ret > 0)
815 high = mid;
816 else {
817 *slot = mid;
818 return 0;
819 }
820 }
821 *slot = low;
822 return 1;
823}
824
Chris Mason97571fd2007-02-24 13:39:08 -0500825/*
826 * simple bin_search frontend that does the right thing for
827 * leaves vs nodes
828 */
Chris Mason5f39d392007-10-15 16:14:19 -0400829static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
830 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500831{
Chris Mason5f39d392007-10-15 16:14:19 -0400832 if (level == 0) {
833 return generic_bin_search(eb,
834 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400835 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400836 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400837 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500838 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400839 return generic_bin_search(eb,
840 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400841 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400842 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400843 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500844 }
845 return -1;
846}
847
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
849 int level, int *slot)
850{
851 return bin_search(eb, key, level, slot);
852}
853
Yan, Zhengf0486c62010-05-16 10:46:25 -0400854static void root_add_used(struct btrfs_root *root, u32 size)
855{
856 spin_lock(&root->accounting_lock);
857 btrfs_set_root_used(&root->root_item,
858 btrfs_root_used(&root->root_item) + size);
859 spin_unlock(&root->accounting_lock);
860}
861
862static void root_sub_used(struct btrfs_root *root, u32 size)
863{
864 spin_lock(&root->accounting_lock);
865 btrfs_set_root_used(&root->root_item,
866 btrfs_root_used(&root->root_item) - size);
867 spin_unlock(&root->accounting_lock);
868}
869
Chris Masond352ac62008-09-29 15:18:18 -0400870/* given a node and slot number, this reads the blocks it points to. The
871 * extent buffer is returned with a reference taken (but unlocked).
872 * NULL is returned on error.
873 */
Chris Masone02119d2008-09-05 16:13:11 -0400874static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400875 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500876{
Chris Masonca7a79a2008-05-12 12:59:19 -0400877 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500878 if (slot < 0)
879 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400880 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500881 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400882
883 BUG_ON(level == 0);
884
Chris Masondb945352007-10-15 16:15:53 -0400885 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400886 btrfs_level_size(root, level - 1),
887 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500888}
889
Chris Masond352ac62008-09-29 15:18:18 -0400890/*
891 * node level balancing, used to make sure nodes are in proper order for
892 * item deletion. We balance from the top down, so we have to make sure
893 * that a deletion won't leave an node completely empty later on.
894 */
Chris Masone02119d2008-09-05 16:13:11 -0400895static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500896 struct btrfs_root *root,
897 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500898{
Chris Mason5f39d392007-10-15 16:14:19 -0400899 struct extent_buffer *right = NULL;
900 struct extent_buffer *mid;
901 struct extent_buffer *left = NULL;
902 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500903 int ret = 0;
904 int wret;
905 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500906 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500907 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500908
909 if (level == 0)
910 return 0;
911
Chris Mason5f39d392007-10-15 16:14:19 -0400912 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500913
Chris Masonbd681512011-07-16 15:23:14 -0400914 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
915 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500916 WARN_ON(btrfs_header_generation(mid) != trans->transid);
917
Chris Mason1d4f8a02007-03-13 09:28:32 -0400918 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500919
Li Zefana05a9bb2011-09-06 16:55:34 +0800920 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400921 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800922 pslot = path->slots[level + 1];
923 }
Chris Masonbb803952007-03-01 12:04:21 -0500924
Chris Mason40689472007-03-17 14:29:23 -0400925 /*
926 * deal with the case where there is only one pointer in the root
927 * by promoting the node below to a root
928 */
Chris Mason5f39d392007-10-15 16:14:19 -0400929 if (!parent) {
930 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500931
Chris Mason5f39d392007-10-15 16:14:19 -0400932 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500933 return 0;
934
935 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400936 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500937 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400938 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500939 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400940 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400941 if (ret) {
942 btrfs_tree_unlock(child);
943 free_extent_buffer(child);
944 goto enospc;
945 }
Yan2f375ab2008-02-01 14:58:07 -0500946
Chris Mason240f62c2011-03-23 14:54:42 -0400947 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400948
Chris Mason0b86a832008-03-24 15:01:56 -0400949 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400950 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500951
Chris Mason925baed2008-06-25 16:01:30 -0400952 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500953 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400954 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400955 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500956 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400957 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400958
959 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200960 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500961 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400962 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400963 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500964 }
Chris Mason5f39d392007-10-15 16:14:19 -0400965 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400966 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500967 return 0;
968
Andi Kleen559af822010-10-29 15:14:37 -0400969 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400970
Chris Mason5f39d392007-10-15 16:14:19 -0400971 left = read_node_slot(root, parent, pslot - 1);
972 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400973 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500974 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400975 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400976 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400977 if (wret) {
978 ret = wret;
979 goto enospc;
980 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400981 }
Chris Mason5f39d392007-10-15 16:14:19 -0400982 right = read_node_slot(root, parent, pslot + 1);
983 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400984 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500985 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400986 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400987 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400988 if (wret) {
989 ret = wret;
990 goto enospc;
991 }
992 }
993
994 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400995 if (left) {
996 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400997 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500998 if (wret < 0)
999 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001000 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001001 }
Chris Mason79f95c82007-03-01 15:16:26 -05001002
1003 /*
1004 * then try to empty the right most buffer into the middle
1005 */
Chris Mason5f39d392007-10-15 16:14:19 -04001006 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001007 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001008 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001009 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001010 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001011 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001012 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001013 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001014 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001015 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001016 free_extent_buffer(right);
1017 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001018 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001019 struct btrfs_disk_key right_key;
1020 btrfs_node_key(right, &right_key, 0);
1021 btrfs_set_node_key(parent, &right_key, pslot + 1);
1022 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001023 }
1024 }
Chris Mason5f39d392007-10-15 16:14:19 -04001025 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001026 /*
1027 * we're not allowed to leave a node with one item in the
1028 * tree during a delete. A deletion from lower in the tree
1029 * could try to delete the only pointer in this node.
1030 * So, pull some keys from the left.
1031 * There has to be a left pointer at this point because
1032 * otherwise we would have pulled some pointers from the
1033 * right
1034 */
Chris Mason5f39d392007-10-15 16:14:19 -04001035 BUG_ON(!left);
1036 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001037 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001038 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001039 goto enospc;
1040 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001041 if (wret == 1) {
1042 wret = push_node_left(trans, root, left, mid, 1);
1043 if (wret < 0)
1044 ret = wret;
1045 }
Chris Mason79f95c82007-03-01 15:16:26 -05001046 BUG_ON(wret == 1);
1047 }
Chris Mason5f39d392007-10-15 16:14:19 -04001048 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001049 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001050 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001051 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001052 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001053 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001054 free_extent_buffer(mid);
1055 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001056 } else {
1057 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001058 struct btrfs_disk_key mid_key;
1059 btrfs_node_key(mid, &mid_key, 0);
1060 btrfs_set_node_key(parent, &mid_key, pslot);
1061 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001062 }
Chris Masonbb803952007-03-01 12:04:21 -05001063
Chris Mason79f95c82007-03-01 15:16:26 -05001064 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001065 if (left) {
1066 if (btrfs_header_nritems(left) > orig_slot) {
1067 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001068 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001069 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001070 path->slots[level + 1] -= 1;
1071 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001072 if (mid) {
1073 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001074 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001075 }
Chris Masonbb803952007-03-01 12:04:21 -05001076 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001077 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001078 path->slots[level] = orig_slot;
1079 }
1080 }
Chris Mason79f95c82007-03-01 15:16:26 -05001081 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001082 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001083 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001084 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001085enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001086 if (right) {
1087 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001088 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001089 }
1090 if (left) {
1091 if (path->nodes[level] != left)
1092 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001093 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001094 }
Chris Masonbb803952007-03-01 12:04:21 -05001095 return ret;
1096}
1097
Chris Masond352ac62008-09-29 15:18:18 -04001098/* Node balancing for insertion. Here we only split or push nodes around
1099 * when they are completely full. This is also done top down, so we
1100 * have to be pessimistic.
1101 */
Chris Masond3977122009-01-05 21:25:51 -05001102static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001103 struct btrfs_root *root,
1104 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001105{
Chris Mason5f39d392007-10-15 16:14:19 -04001106 struct extent_buffer *right = NULL;
1107 struct extent_buffer *mid;
1108 struct extent_buffer *left = NULL;
1109 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001110 int ret = 0;
1111 int wret;
1112 int pslot;
1113 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001114
1115 if (level == 0)
1116 return 1;
1117
Chris Mason5f39d392007-10-15 16:14:19 -04001118 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001119 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001120
Li Zefana05a9bb2011-09-06 16:55:34 +08001121 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001122 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001123 pslot = path->slots[level + 1];
1124 }
Chris Masone66f7092007-04-20 13:16:02 -04001125
Chris Mason5f39d392007-10-15 16:14:19 -04001126 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001127 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001128
Chris Mason5f39d392007-10-15 16:14:19 -04001129 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001130
1131 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001132 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001133 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001134
1135 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001136 btrfs_set_lock_blocking(left);
1137
Chris Mason5f39d392007-10-15 16:14:19 -04001138 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001139 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1140 wret = 1;
1141 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001142 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001143 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001144 if (ret)
1145 wret = 1;
1146 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001147 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001148 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001149 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001150 }
Chris Masone66f7092007-04-20 13:16:02 -04001151 if (wret < 0)
1152 ret = wret;
1153 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001154 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001155 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001156 btrfs_node_key(mid, &disk_key, 0);
1157 btrfs_set_node_key(parent, &disk_key, pslot);
1158 btrfs_mark_buffer_dirty(parent);
1159 if (btrfs_header_nritems(left) > orig_slot) {
1160 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001161 path->slots[level + 1] -= 1;
1162 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001163 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001164 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001165 } else {
1166 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001167 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001168 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001169 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001170 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001171 }
Chris Masone66f7092007-04-20 13:16:02 -04001172 return 0;
1173 }
Chris Mason925baed2008-06-25 16:01:30 -04001174 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001175 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001176 }
Chris Mason925baed2008-06-25 16:01:30 -04001177 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001178
1179 /*
1180 * then try to empty the right most buffer into the middle
1181 */
Chris Mason5f39d392007-10-15 16:14:19 -04001182 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001183 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001184
Chris Mason925baed2008-06-25 16:01:30 -04001185 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001186 btrfs_set_lock_blocking(right);
1187
Chris Mason5f39d392007-10-15 16:14:19 -04001188 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001189 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1190 wret = 1;
1191 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001192 ret = btrfs_cow_block(trans, root, right,
1193 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001194 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001195 if (ret)
1196 wret = 1;
1197 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001198 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001199 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001200 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001201 }
Chris Masone66f7092007-04-20 13:16:02 -04001202 if (wret < 0)
1203 ret = wret;
1204 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001205 struct btrfs_disk_key disk_key;
1206
1207 btrfs_node_key(right, &disk_key, 0);
1208 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1209 btrfs_mark_buffer_dirty(parent);
1210
1211 if (btrfs_header_nritems(mid) <= orig_slot) {
1212 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001213 path->slots[level + 1] += 1;
1214 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001215 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001216 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001217 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001218 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001219 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001220 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001221 }
Chris Masone66f7092007-04-20 13:16:02 -04001222 return 0;
1223 }
Chris Mason925baed2008-06-25 16:01:30 -04001224 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001225 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001226 }
Chris Masone66f7092007-04-20 13:16:02 -04001227 return 1;
1228}
1229
Chris Mason74123bd2007-02-02 11:05:29 -05001230/*
Chris Masond352ac62008-09-29 15:18:18 -04001231 * readahead one full node of leaves, finding things that are close
1232 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001233 */
Chris Masonc8c42862009-04-03 10:14:18 -04001234static void reada_for_search(struct btrfs_root *root,
1235 struct btrfs_path *path,
1236 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001237{
Chris Mason5f39d392007-10-15 16:14:19 -04001238 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001239 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001240 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001241 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001242 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001243 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001244 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001245 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001246 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001247 u32 nr;
1248 u32 blocksize;
1249 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001250
Chris Masona6b6e752007-10-15 16:22:39 -04001251 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001252 return;
1253
Chris Mason6702ed42007-08-07 16:15:09 -04001254 if (!path->nodes[level])
1255 return;
1256
Chris Mason5f39d392007-10-15 16:14:19 -04001257 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001258
Chris Mason3c69fae2007-08-07 15:52:22 -04001259 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001260 blocksize = btrfs_level_size(root, level - 1);
1261 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001262 if (eb) {
1263 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001264 return;
1265 }
1266
Chris Masona7175312009-01-22 09:23:10 -05001267 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001268
Chris Mason5f39d392007-10-15 16:14:19 -04001269 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001270 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001271
Chris Masond3977122009-01-05 21:25:51 -05001272 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001273 if (direction < 0) {
1274 if (nr == 0)
1275 break;
1276 nr--;
1277 } else if (direction > 0) {
1278 nr++;
1279 if (nr >= nritems)
1280 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001281 }
Chris Mason01f46652007-12-21 16:24:26 -05001282 if (path->reada < 0 && objectid) {
1283 btrfs_node_key(node, &disk_key, nr);
1284 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1285 break;
1286 }
Chris Mason6b800532007-10-15 16:17:34 -04001287 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001288 if ((search <= target && target - search <= 65536) ||
1289 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001290 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001291 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001292 nread += blocksize;
1293 }
1294 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001295 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001296 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001297 }
1298}
Chris Mason925baed2008-06-25 16:01:30 -04001299
Chris Masond352ac62008-09-29 15:18:18 -04001300/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001301 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1302 * cache
1303 */
1304static noinline int reada_for_balance(struct btrfs_root *root,
1305 struct btrfs_path *path, int level)
1306{
1307 int slot;
1308 int nritems;
1309 struct extent_buffer *parent;
1310 struct extent_buffer *eb;
1311 u64 gen;
1312 u64 block1 = 0;
1313 u64 block2 = 0;
1314 int ret = 0;
1315 int blocksize;
1316
Chris Mason8c594ea2009-04-20 15:50:10 -04001317 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001318 if (!parent)
1319 return 0;
1320
1321 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001322 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001323 blocksize = btrfs_level_size(root, level);
1324
1325 if (slot > 0) {
1326 block1 = btrfs_node_blockptr(parent, slot - 1);
1327 gen = btrfs_node_ptr_generation(parent, slot - 1);
1328 eb = btrfs_find_tree_block(root, block1, blocksize);
1329 if (eb && btrfs_buffer_uptodate(eb, gen))
1330 block1 = 0;
1331 free_extent_buffer(eb);
1332 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001333 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001334 block2 = btrfs_node_blockptr(parent, slot + 1);
1335 gen = btrfs_node_ptr_generation(parent, slot + 1);
1336 eb = btrfs_find_tree_block(root, block2, blocksize);
1337 if (eb && btrfs_buffer_uptodate(eb, gen))
1338 block2 = 0;
1339 free_extent_buffer(eb);
1340 }
1341 if (block1 || block2) {
1342 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001343
1344 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001345 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001346
1347 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001348 if (block1)
1349 readahead_tree_block(root, block1, blocksize, 0);
1350 if (block2)
1351 readahead_tree_block(root, block2, blocksize, 0);
1352
1353 if (block1) {
1354 eb = read_tree_block(root, block1, blocksize, 0);
1355 free_extent_buffer(eb);
1356 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001357 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001358 eb = read_tree_block(root, block2, blocksize, 0);
1359 free_extent_buffer(eb);
1360 }
1361 }
1362 return ret;
1363}
1364
1365
1366/*
Chris Masond3977122009-01-05 21:25:51 -05001367 * when we walk down the tree, it is usually safe to unlock the higher layers
1368 * in the tree. The exceptions are when our path goes through slot 0, because
1369 * operations on the tree might require changing key pointers higher up in the
1370 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001371 *
Chris Masond3977122009-01-05 21:25:51 -05001372 * callers might also have set path->keep_locks, which tells this code to keep
1373 * the lock if the path points to the last slot in the block. This is part of
1374 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001375 *
Chris Masond3977122009-01-05 21:25:51 -05001376 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1377 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001378 */
Chris Masone02119d2008-09-05 16:13:11 -04001379static noinline void unlock_up(struct btrfs_path *path, int level,
1380 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001381{
1382 int i;
1383 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001384 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001385 struct extent_buffer *t;
1386
1387 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1388 if (!path->nodes[i])
1389 break;
1390 if (!path->locks[i])
1391 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001392 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001393 skip_level = i + 1;
1394 continue;
1395 }
Chris Mason051e1b92008-06-25 16:01:30 -04001396 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001397 u32 nritems;
1398 t = path->nodes[i];
1399 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001400 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001401 skip_level = i + 1;
1402 continue;
1403 }
1404 }
Chris Mason051e1b92008-06-25 16:01:30 -04001405 if (skip_level < i && i >= lowest_unlock)
1406 no_skips = 1;
1407
Chris Mason925baed2008-06-25 16:01:30 -04001408 t = path->nodes[i];
1409 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001410 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001411 path->locks[i] = 0;
1412 }
1413 }
1414}
1415
Chris Mason3c69fae2007-08-07 15:52:22 -04001416/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001417 * This releases any locks held in the path starting at level and
1418 * going all the way up to the root.
1419 *
1420 * btrfs_search_slot will keep the lock held on higher nodes in a few
1421 * corner cases, such as COW of the block at slot zero in the node. This
1422 * ignores those rules, and it should only be called when there are no
1423 * more updates to be done higher up in the tree.
1424 */
1425noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1426{
1427 int i;
1428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001430 return;
1431
1432 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1433 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001434 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001436 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001437 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001438 path->locks[i] = 0;
1439 }
1440}
1441
1442/*
Chris Masonc8c42862009-04-03 10:14:18 -04001443 * helper function for btrfs_search_slot. The goal is to find a block
1444 * in cache without setting the path to blocking. If we find the block
1445 * we return zero and the path is unchanged.
1446 *
1447 * If we can't find the block, we set the path blocking and do some
1448 * reada. -EAGAIN is returned and the search must be repeated.
1449 */
1450static int
1451read_block_for_search(struct btrfs_trans_handle *trans,
1452 struct btrfs_root *root, struct btrfs_path *p,
1453 struct extent_buffer **eb_ret, int level, int slot,
1454 struct btrfs_key *key)
1455{
1456 u64 blocknr;
1457 u64 gen;
1458 u32 blocksize;
1459 struct extent_buffer *b = *eb_ret;
1460 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001461 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001462
1463 blocknr = btrfs_node_blockptr(b, slot);
1464 gen = btrfs_node_ptr_generation(b, slot);
1465 blocksize = btrfs_level_size(root, level - 1);
1466
1467 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001468 if (tmp) {
1469 if (btrfs_buffer_uptodate(tmp, 0)) {
1470 if (btrfs_buffer_uptodate(tmp, gen)) {
1471 /*
1472 * we found an up to date block without
1473 * sleeping, return
1474 * right away
1475 */
1476 *eb_ret = tmp;
1477 return 0;
1478 }
1479 /* the pages were up to date, but we failed
1480 * the generation number check. Do a full
1481 * read for the generation number that is correct.
1482 * We must do this without dropping locks so
1483 * we can trust our generation number
1484 */
1485 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001486 btrfs_set_path_blocking(p);
1487
Chris Masoncb449212010-10-24 11:01:27 -04001488 tmp = read_tree_block(root, blocknr, blocksize, gen);
1489 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1490 *eb_ret = tmp;
1491 return 0;
1492 }
1493 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001494 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001495 return -EIO;
1496 }
Chris Masonc8c42862009-04-03 10:14:18 -04001497 }
1498
1499 /*
1500 * reduce lock contention at high levels
1501 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001502 * we read. Don't release the lock on the current
1503 * level because we need to walk this node to figure
1504 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001505 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001506 btrfs_unlock_up_safe(p, level + 1);
1507 btrfs_set_path_blocking(p);
1508
Chris Masoncb449212010-10-24 11:01:27 -04001509 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001510 if (p->reada)
1511 reada_for_search(root, p, level, slot, key->objectid);
1512
David Sterbab3b4aa72011-04-21 01:20:15 +02001513 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001514
1515 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001516 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001517 if (tmp) {
1518 /*
1519 * If the read above didn't mark this buffer up to date,
1520 * it will never end up being up to date. Set ret to EIO now
1521 * and give up so that our caller doesn't loop forever
1522 * on our EAGAINs.
1523 */
1524 if (!btrfs_buffer_uptodate(tmp, 0))
1525 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001526 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001527 }
1528 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001529}
1530
1531/*
1532 * helper function for btrfs_search_slot. This does all of the checks
1533 * for node-level blocks and does any balancing required based on
1534 * the ins_len.
1535 *
1536 * If no extra work was required, zero is returned. If we had to
1537 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1538 * start over
1539 */
1540static int
1541setup_nodes_for_search(struct btrfs_trans_handle *trans,
1542 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001543 struct extent_buffer *b, int level, int ins_len,
1544 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001545{
1546 int ret;
1547 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1548 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1549 int sret;
1550
Chris Masonbd681512011-07-16 15:23:14 -04001551 if (*write_lock_level < level + 1) {
1552 *write_lock_level = level + 1;
1553 btrfs_release_path(p);
1554 goto again;
1555 }
1556
Chris Masonc8c42862009-04-03 10:14:18 -04001557 sret = reada_for_balance(root, p, level);
1558 if (sret)
1559 goto again;
1560
1561 btrfs_set_path_blocking(p);
1562 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001563 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001564
1565 BUG_ON(sret > 0);
1566 if (sret) {
1567 ret = sret;
1568 goto done;
1569 }
1570 b = p->nodes[level];
1571 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001572 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001573 int sret;
1574
Chris Masonbd681512011-07-16 15:23:14 -04001575 if (*write_lock_level < level + 1) {
1576 *write_lock_level = level + 1;
1577 btrfs_release_path(p);
1578 goto again;
1579 }
1580
Chris Masonc8c42862009-04-03 10:14:18 -04001581 sret = reada_for_balance(root, p, level);
1582 if (sret)
1583 goto again;
1584
1585 btrfs_set_path_blocking(p);
1586 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001587 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001588
1589 if (sret) {
1590 ret = sret;
1591 goto done;
1592 }
1593 b = p->nodes[level];
1594 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001595 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001596 goto again;
1597 }
1598 BUG_ON(btrfs_header_nritems(b) == 1);
1599 }
1600 return 0;
1601
1602again:
1603 ret = -EAGAIN;
1604done:
1605 return ret;
1606}
1607
1608/*
Chris Mason74123bd2007-02-02 11:05:29 -05001609 * look for key in the tree. path is filled in with nodes along the way
1610 * if key is found, we return zero and you can find the item in the leaf
1611 * level of the path (level 0)
1612 *
1613 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001614 * be inserted, and 1 is returned. If there are other errors during the
1615 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001616 *
1617 * if ins_len > 0, nodes and leaves will be split as we walk down the
1618 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1619 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001620 */
Chris Masone089f052007-03-16 16:20:31 -04001621int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1622 *root, struct btrfs_key *key, struct btrfs_path *p, int
1623 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001624{
Chris Mason5f39d392007-10-15 16:14:19 -04001625 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001626 int slot;
1627 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001628 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001629 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001630 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001631 int root_lock;
1632 /* everything at write_lock_level or lower must be write locked */
1633 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001634 u8 lowest_level = 0;
1635
Chris Mason6702ed42007-08-07 16:15:09 -04001636 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001637 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001638 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001639
Chris Masonbd681512011-07-16 15:23:14 -04001640 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001641 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001642
Chris Masonbd681512011-07-16 15:23:14 -04001643 /* when we are removing items, we might have to go up to level
1644 * two as we update tree pointers Make sure we keep write
1645 * for those levels as well
1646 */
1647 write_lock_level = 2;
1648 } else if (ins_len > 0) {
1649 /*
1650 * for inserting items, make sure we have a write lock on
1651 * level 1 so we can update keys
1652 */
1653 write_lock_level = 1;
1654 }
1655
1656 if (!cow)
1657 write_lock_level = -1;
1658
1659 if (cow && (p->keep_locks || p->lowest_level))
1660 write_lock_level = BTRFS_MAX_LEVEL;
1661
Chris Masonbb803952007-03-01 12:04:21 -05001662again:
Chris Masonbd681512011-07-16 15:23:14 -04001663 /*
1664 * we try very hard to do read locks on the root
1665 */
1666 root_lock = BTRFS_READ_LOCK;
1667 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001669 /*
1670 * the commit roots are read only
1671 * so we always do read locks
1672 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673 b = root->commit_root;
1674 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001675 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001676 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001677 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001679 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001681 level = btrfs_header_level(b);
1682 } else {
1683 /* we don't know the level of the root node
1684 * until we actually have it read locked
1685 */
1686 b = btrfs_read_lock_root_node(root);
1687 level = btrfs_header_level(b);
1688 if (level <= write_lock_level) {
1689 /* whoops, must trade for write lock */
1690 btrfs_tree_read_unlock(b);
1691 free_extent_buffer(b);
1692 b = btrfs_lock_root_node(root);
1693 root_lock = BTRFS_WRITE_LOCK;
1694
1695 /* the level might have changed, check again */
1696 level = btrfs_header_level(b);
1697 }
1698 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001699 }
Chris Masonbd681512011-07-16 15:23:14 -04001700 p->nodes[level] = b;
1701 if (!p->skip_locking)
1702 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001703
Chris Masoneb60cea2007-02-02 09:18:22 -05001704 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001705 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001706
1707 /*
1708 * setup the path here so we can release it under lock
1709 * contention with the cow code
1710 */
Chris Mason02217ed2007-03-02 16:08:05 -05001711 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001712 /*
1713 * if we don't really need to cow this block
1714 * then we don't want to set the path blocking,
1715 * so we test it here
1716 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001718 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719
Chris Masonb4ce94d2009-02-04 09:25:08 -05001720 btrfs_set_path_blocking(p);
1721
Chris Masonbd681512011-07-16 15:23:14 -04001722 /*
1723 * must have write locks on this node and the
1724 * parent
1725 */
1726 if (level + 1 > write_lock_level) {
1727 write_lock_level = level + 1;
1728 btrfs_release_path(p);
1729 goto again;
1730 }
1731
Yan Zheng33c66f42009-07-22 09:59:00 -04001732 err = btrfs_cow_block(trans, root, b,
1733 p->nodes[level + 1],
1734 p->slots[level + 1], &b);
1735 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001736 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001737 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001738 }
Chris Mason02217ed2007-03-02 16:08:05 -05001739 }
Chris Mason65b51a02008-08-01 15:11:20 -04001740cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001741 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001742
Chris Masoneb60cea2007-02-02 09:18:22 -05001743 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001744 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001745
1746 /*
1747 * we have a lock on b and as long as we aren't changing
1748 * the tree, there is no way to for the items in b to change.
1749 * It is safe to drop the lock on our parent before we
1750 * go through the expensive btree search on b.
1751 *
1752 * If cow is true, then we might be changing slot zero,
1753 * which may require changing the parent. So, we can't
1754 * drop the lock until after we know which slot we're
1755 * operating on.
1756 */
1757 if (!cow)
1758 btrfs_unlock_up_safe(p, level + 1);
1759
Chris Mason5f39d392007-10-15 16:14:19 -04001760 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001761
Chris Mason5f39d392007-10-15 16:14:19 -04001762 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001763 int dec = 0;
1764 if (ret && slot > 0) {
1765 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001766 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001767 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001768 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001769 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001770 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001771 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001772 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001773 if (err) {
1774 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001775 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001776 }
Chris Masonc8c42862009-04-03 10:14:18 -04001777 b = p->nodes[level];
1778 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001779
Chris Masonbd681512011-07-16 15:23:14 -04001780 /*
1781 * slot 0 is special, if we change the key
1782 * we have to update the parent pointer
1783 * which means we must have a write lock
1784 * on the parent
1785 */
1786 if (slot == 0 && cow &&
1787 write_lock_level < level + 1) {
1788 write_lock_level = level + 1;
1789 btrfs_release_path(p);
1790 goto again;
1791 }
1792
Chris Masonf9efa9c2008-06-25 16:14:04 -04001793 unlock_up(p, level, lowest_unlock);
1794
Chris Mason925baed2008-06-25 16:01:30 -04001795 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001796 if (dec)
1797 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001798 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001799 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001800
Yan Zheng33c66f42009-07-22 09:59:00 -04001801 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001802 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001803 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001804 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001805 if (err) {
1806 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001807 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 }
Chris Mason76a05b32009-05-14 13:24:30 -04001809
Chris Masonb4ce94d2009-02-04 09:25:08 -05001810 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001811 level = btrfs_header_level(b);
1812 if (level <= write_lock_level) {
1813 err = btrfs_try_tree_write_lock(b);
1814 if (!err) {
1815 btrfs_set_path_blocking(p);
1816 btrfs_tree_lock(b);
1817 btrfs_clear_path_blocking(p, b,
1818 BTRFS_WRITE_LOCK);
1819 }
1820 p->locks[level] = BTRFS_WRITE_LOCK;
1821 } else {
1822 err = btrfs_try_tree_read_lock(b);
1823 if (!err) {
1824 btrfs_set_path_blocking(p);
1825 btrfs_tree_read_lock(b);
1826 btrfs_clear_path_blocking(p, b,
1827 BTRFS_READ_LOCK);
1828 }
1829 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001830 }
Chris Masonbd681512011-07-16 15:23:14 -04001831 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001832 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001833 } else {
1834 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001835 if (ins_len > 0 &&
1836 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001837 if (write_lock_level < 1) {
1838 write_lock_level = 1;
1839 btrfs_release_path(p);
1840 goto again;
1841 }
1842
Chris Masonb4ce94d2009-02-04 09:25:08 -05001843 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001844 err = split_leaf(trans, root, key,
1845 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001846 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001847
Yan Zheng33c66f42009-07-22 09:59:00 -04001848 BUG_ON(err > 0);
1849 if (err) {
1850 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001851 goto done;
1852 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001853 }
Chris Mason459931e2008-12-10 09:10:46 -05001854 if (!p->search_for_split)
1855 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001856 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001857 }
1858 }
Chris Mason65b51a02008-08-01 15:11:20 -04001859 ret = 1;
1860done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001861 /*
1862 * we don't really know what they plan on doing with the path
1863 * from here on, so for now just mark it as blocking
1864 */
Chris Masonb9473432009-03-13 11:00:37 -04001865 if (!p->leave_spinning)
1866 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001867 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001868 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001869 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001870}
1871
Chris Mason74123bd2007-02-02 11:05:29 -05001872/*
1873 * adjust the pointers going up the tree, starting at level
1874 * making sure the right key of each node is points to 'key'.
1875 * This is used after shifting pointers to the left, so it stops
1876 * fixing up pointers when a given leaf/node is not in slot 0 of the
1877 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001878 *
Chris Mason74123bd2007-02-02 11:05:29 -05001879 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001880static void fixup_low_keys(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *root, struct btrfs_path *path,
1882 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883{
1884 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001885 struct extent_buffer *t;
1886
Chris Mason234b63a2007-03-13 10:46:10 -04001887 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001888 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001889 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001890 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001891 t = path->nodes[i];
1892 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001893 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001894 if (tslot != 0)
1895 break;
1896 }
1897}
1898
Chris Mason74123bd2007-02-02 11:05:29 -05001899/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001900 * update item key.
1901 *
1902 * This function isn't completely safe. It's the caller's responsibility
1903 * that the new key won't break the order
1904 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001905void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1906 struct btrfs_root *root, struct btrfs_path *path,
1907 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001908{
1909 struct btrfs_disk_key disk_key;
1910 struct extent_buffer *eb;
1911 int slot;
1912
1913 eb = path->nodes[0];
1914 slot = path->slots[0];
1915 if (slot > 0) {
1916 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001917 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001918 }
1919 if (slot < btrfs_header_nritems(eb) - 1) {
1920 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001921 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001922 }
1923
1924 btrfs_cpu_key_to_disk(&disk_key, new_key);
1925 btrfs_set_item_key(eb, &disk_key, slot);
1926 btrfs_mark_buffer_dirty(eb);
1927 if (slot == 0)
1928 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001929}
1930
1931/*
Chris Mason74123bd2007-02-02 11:05:29 -05001932 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001933 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001934 *
1935 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1936 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001937 */
Chris Mason98ed5172008-01-03 10:01:48 -05001938static int push_node_left(struct btrfs_trans_handle *trans,
1939 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001940 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001941{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001942 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001943 int src_nritems;
1944 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001945 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946
Chris Mason5f39d392007-10-15 16:14:19 -04001947 src_nritems = btrfs_header_nritems(src);
1948 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001949 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001950 WARN_ON(btrfs_header_generation(src) != trans->transid);
1951 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001952
Chris Masonbce4eae2008-04-24 14:42:46 -04001953 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001954 return 1;
1955
Chris Masond3977122009-01-05 21:25:51 -05001956 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001957 return 1;
1958
Chris Masonbce4eae2008-04-24 14:42:46 -04001959 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001960 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001961 if (push_items < src_nritems) {
1962 /* leave at least 8 pointers in the node if
1963 * we aren't going to empty it
1964 */
1965 if (src_nritems - push_items < 8) {
1966 if (push_items <= 8)
1967 return 1;
1968 push_items -= 8;
1969 }
1970 }
1971 } else
1972 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001973
Chris Mason5f39d392007-10-15 16:14:19 -04001974 copy_extent_buffer(dst, src,
1975 btrfs_node_key_ptr_offset(dst_nritems),
1976 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001977 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001978
Chris Masonbb803952007-03-01 12:04:21 -05001979 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001980 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1981 btrfs_node_key_ptr_offset(push_items),
1982 (src_nritems - push_items) *
1983 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001984 }
Chris Mason5f39d392007-10-15 16:14:19 -04001985 btrfs_set_header_nritems(src, src_nritems - push_items);
1986 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1987 btrfs_mark_buffer_dirty(src);
1988 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001989
Chris Masonbb803952007-03-01 12:04:21 -05001990 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001991}
1992
Chris Mason97571fd2007-02-24 13:39:08 -05001993/*
Chris Mason79f95c82007-03-01 15:16:26 -05001994 * try to push data from one node into the next node right in the
1995 * tree.
1996 *
1997 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1998 * error, and > 0 if there was no room in the right hand block.
1999 *
2000 * this will only push up to 1/2 the contents of the left node over
2001 */
Chris Mason5f39d392007-10-15 16:14:19 -04002002static int balance_node_right(struct btrfs_trans_handle *trans,
2003 struct btrfs_root *root,
2004 struct extent_buffer *dst,
2005 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002006{
Chris Mason79f95c82007-03-01 15:16:26 -05002007 int push_items = 0;
2008 int max_push;
2009 int src_nritems;
2010 int dst_nritems;
2011 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002012
Chris Mason7bb86312007-12-11 09:25:06 -05002013 WARN_ON(btrfs_header_generation(src) != trans->transid);
2014 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2015
Chris Mason5f39d392007-10-15 16:14:19 -04002016 src_nritems = btrfs_header_nritems(src);
2017 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002018 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002019 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002020 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002021
Chris Masond3977122009-01-05 21:25:51 -05002022 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002023 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002024
2025 max_push = src_nritems / 2 + 1;
2026 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002027 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002028 return 1;
Yan252c38f2007-08-29 09:11:44 -04002029
Chris Mason79f95c82007-03-01 15:16:26 -05002030 if (max_push < push_items)
2031 push_items = max_push;
2032
Chris Mason5f39d392007-10-15 16:14:19 -04002033 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2034 btrfs_node_key_ptr_offset(0),
2035 (dst_nritems) *
2036 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 copy_extent_buffer(dst, src,
2039 btrfs_node_key_ptr_offset(0),
2040 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002041 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 btrfs_set_header_nritems(src, src_nritems - push_items);
2044 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_mark_buffer_dirty(src);
2047 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002048
Chris Mason79f95c82007-03-01 15:16:26 -05002049 return ret;
2050}
2051
2052/*
Chris Mason97571fd2007-02-24 13:39:08 -05002053 * helper function to insert a new root level in the tree.
2054 * A new node is allocated, and a single item is inserted to
2055 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002056 *
2057 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002058 */
Chris Masond3977122009-01-05 21:25:51 -05002059static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002060 struct btrfs_root *root,
2061 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002062{
Chris Mason7bb86312007-12-11 09:25:06 -05002063 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002064 struct extent_buffer *lower;
2065 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002066 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002067 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002068
2069 BUG_ON(path->nodes[level]);
2070 BUG_ON(path->nodes[level-1] != root->node);
2071
Chris Mason7bb86312007-12-11 09:25:06 -05002072 lower = path->nodes[level-1];
2073 if (level == 1)
2074 btrfs_item_key(lower, &lower_key, 0);
2075 else
2076 btrfs_node_key(lower, &lower_key, 0);
2077
Zheng Yan31840ae2008-09-23 13:14:14 -04002078 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002079 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002080 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 if (IS_ERR(c))
2082 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002083
Yan, Zhengf0486c62010-05-16 10:46:25 -04002084 root_add_used(root, root->nodesize);
2085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002087 btrfs_set_header_nritems(c, 1);
2088 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002089 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002091 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002093
Chris Mason5f39d392007-10-15 16:14:19 -04002094 write_extent_buffer(c, root->fs_info->fsid,
2095 (unsigned long)btrfs_header_fsid(c),
2096 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002097
2098 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2099 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2100 BTRFS_UUID_SIZE);
2101
Chris Mason5f39d392007-10-15 16:14:19 -04002102 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002103 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002104 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002105 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002106
2107 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002108
2109 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002110
Chris Mason925baed2008-06-25 16:01:30 -04002111 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002112 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002113
2114 /* the super has an extra ref to root->node */
2115 free_extent_buffer(old);
2116
Chris Mason0b86a832008-03-24 15:01:56 -04002117 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002118 extent_buffer_get(c);
2119 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002120 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002121 path->slots[level] = 0;
2122 return 0;
2123}
2124
Chris Mason74123bd2007-02-02 11:05:29 -05002125/*
2126 * worker function to insert a single pointer in a node.
2127 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002128 *
Chris Mason74123bd2007-02-02 11:05:29 -05002129 * slot and level indicate where you want the key to go, and
2130 * blocknr is the block the key points to.
2131 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002132static void insert_ptr(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *root, struct btrfs_path *path,
2134 struct btrfs_disk_key *key, u64 bytenr,
2135 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002136{
Chris Mason5f39d392007-10-15 16:14:19 -04002137 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002138 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002139
2140 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002141 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 lower = path->nodes[level];
2143 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002144 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002145 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002146 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002147 memmove_extent_buffer(lower,
2148 btrfs_node_key_ptr_offset(slot + 1),
2149 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002150 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002151 }
Chris Mason5f39d392007-10-15 16:14:19 -04002152 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002153 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002154 WARN_ON(trans->transid == 0);
2155 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002156 btrfs_set_header_nritems(lower, nritems + 1);
2157 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002158}
2159
Chris Mason97571fd2007-02-24 13:39:08 -05002160/*
2161 * split the node at the specified level in path in two.
2162 * The path is corrected to point to the appropriate node after the split
2163 *
2164 * Before splitting this tries to make some room in the node by pushing
2165 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002166 *
2167 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002168 */
Chris Masone02119d2008-09-05 16:13:11 -04002169static noinline int split_node(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002172{
Chris Mason5f39d392007-10-15 16:14:19 -04002173 struct extent_buffer *c;
2174 struct extent_buffer *split;
2175 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002176 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002177 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002178 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002179
Chris Mason5f39d392007-10-15 16:14:19 -04002180 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002181 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002183 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002184 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002185 if (ret)
2186 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002187 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002188 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 c = path->nodes[level];
2190 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002191 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002192 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002193 if (ret < 0)
2194 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002195 }
Chris Masone66f7092007-04-20 13:16:02 -04002196
Chris Mason5f39d392007-10-15 16:14:19 -04002197 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 mid = (c_nritems + 1) / 2;
2199 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002202 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002203 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002204 if (IS_ERR(split))
2205 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002206
Yan, Zhengf0486c62010-05-16 10:46:25 -04002207 root_add_used(root, root->nodesize);
2208
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002210 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002211 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002212 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 btrfs_set_header_owner(split, root->root_key.objectid);
2215 write_extent_buffer(split, root->fs_info->fsid,
2216 (unsigned long)btrfs_header_fsid(split),
2217 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002218 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2219 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2220 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002221
Chris Mason5f39d392007-10-15 16:14:19 -04002222
2223 copy_extent_buffer(split, c,
2224 btrfs_node_key_ptr_offset(0),
2225 btrfs_node_key_ptr_offset(mid),
2226 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2227 btrfs_set_header_nritems(split, c_nritems - mid);
2228 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002229 ret = 0;
2230
Chris Mason5f39d392007-10-15 16:14:19 -04002231 btrfs_mark_buffer_dirty(c);
2232 btrfs_mark_buffer_dirty(split);
2233
Jeff Mahoney143bede2012-03-01 14:56:26 +01002234 insert_ptr(trans, root, path, &disk_key, split->start,
2235 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002236
Chris Mason5de08d72007-02-24 06:24:44 -05002237 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002238 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002239 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002240 free_extent_buffer(c);
2241 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002242 path->slots[level + 1] += 1;
2243 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002244 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002245 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002246 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002247 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002248}
2249
Chris Mason74123bd2007-02-02 11:05:29 -05002250/*
2251 * how many bytes are required to store the items in a leaf. start
2252 * and nr indicate which items in the leaf to check. This totals up the
2253 * space used both by the item structs and the item data
2254 */
Chris Mason5f39d392007-10-15 16:14:19 -04002255static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002256{
2257 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002258 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002259 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002260
2261 if (!nr)
2262 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002263 data_len = btrfs_item_end_nr(l, start);
2264 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002265 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002266 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002267 return data_len;
2268}
2269
Chris Mason74123bd2007-02-02 11:05:29 -05002270/*
Chris Masond4dbff92007-04-04 14:08:15 -04002271 * The space between the end of the leaf items and
2272 * the start of the leaf data. IOW, how much room
2273 * the leaf has left for both items and data
2274 */
Chris Masond3977122009-01-05 21:25:51 -05002275noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002276 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002277{
Chris Mason5f39d392007-10-15 16:14:19 -04002278 int nritems = btrfs_header_nritems(leaf);
2279 int ret;
2280 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2281 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002282 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2283 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002284 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002285 leaf_space_used(leaf, 0, nritems), nritems);
2286 }
2287 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002288}
2289
Chris Mason99d8f832010-07-07 10:51:48 -04002290/*
2291 * min slot controls the lowest index we're willing to push to the
2292 * right. We'll push up to and including min_slot, but no lower
2293 */
Chris Mason44871b12009-03-13 10:04:31 -04002294static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2295 struct btrfs_root *root,
2296 struct btrfs_path *path,
2297 int data_size, int empty,
2298 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002299 int free_space, u32 left_nritems,
2300 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002301{
Chris Mason5f39d392007-10-15 16:14:19 -04002302 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002303 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002304 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002305 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002306 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002307 int push_space = 0;
2308 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002309 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002310 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002311 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002312 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002313 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002314
Chris Mason34a38212007-11-07 13:31:03 -05002315 if (empty)
2316 nr = 0;
2317 else
Chris Mason99d8f832010-07-07 10:51:48 -04002318 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002319
Zheng Yan31840ae2008-09-23 13:14:14 -04002320 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002321 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002322
Chris Mason44871b12009-03-13 10:04:31 -04002323 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002324 i = left_nritems - 1;
2325 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002326 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002327
Zheng Yan31840ae2008-09-23 13:14:14 -04002328 if (!empty && push_items > 0) {
2329 if (path->slots[0] > i)
2330 break;
2331 if (path->slots[0] == i) {
2332 int space = btrfs_leaf_free_space(root, left);
2333 if (space + push_space * 2 > free_space)
2334 break;
2335 }
2336 }
2337
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002339 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002340
Chris Masondb945352007-10-15 16:15:53 -04002341 this_item_size = btrfs_item_size(left, item);
2342 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002343 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002344
Chris Mason00ec4c52007-02-24 12:47:20 -05002345 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002346 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002347 if (i == 0)
2348 break;
2349 i--;
Chris Masondb945352007-10-15 16:15:53 -04002350 }
Chris Mason5f39d392007-10-15 16:14:19 -04002351
Chris Mason925baed2008-06-25 16:01:30 -04002352 if (push_items == 0)
2353 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002354
Chris Mason34a38212007-11-07 13:31:03 -05002355 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002356 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002357
Chris Mason00ec4c52007-02-24 12:47:20 -05002358 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002359 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002360
Chris Mason5f39d392007-10-15 16:14:19 -04002361 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002362 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002363
Chris Mason00ec4c52007-02-24 12:47:20 -05002364 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002365 data_end = leaf_data_end(root, right);
2366 memmove_extent_buffer(right,
2367 btrfs_leaf_data(right) + data_end - push_space,
2368 btrfs_leaf_data(right) + data_end,
2369 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2370
Chris Mason00ec4c52007-02-24 12:47:20 -05002371 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002372 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002373 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2374 btrfs_leaf_data(left) + leaf_data_end(root, left),
2375 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002376
2377 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2378 btrfs_item_nr_offset(0),
2379 right_nritems * sizeof(struct btrfs_item));
2380
Chris Mason00ec4c52007-02-24 12:47:20 -05002381 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002382 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2383 btrfs_item_nr_offset(left_nritems - push_items),
2384 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002385
2386 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002387 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002388 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002389 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002390 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002391 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002392 push_space -= btrfs_item_size(right, item);
2393 btrfs_set_item_offset(right, item, push_space);
2394 }
2395
Chris Mason7518a232007-03-12 12:01:18 -04002396 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002397 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002398
Chris Mason34a38212007-11-07 13:31:03 -05002399 if (left_nritems)
2400 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002401 else
2402 clean_tree_block(trans, root, left);
2403
Chris Mason5f39d392007-10-15 16:14:19 -04002404 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002405
Chris Mason5f39d392007-10-15 16:14:19 -04002406 btrfs_item_key(right, &disk_key, 0);
2407 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002408 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002409
Chris Mason00ec4c52007-02-24 12:47:20 -05002410 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002411 if (path->slots[0] >= left_nritems) {
2412 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002413 if (btrfs_header_nritems(path->nodes[0]) == 0)
2414 clean_tree_block(trans, root, path->nodes[0]);
2415 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002416 free_extent_buffer(path->nodes[0]);
2417 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002418 path->slots[1] += 1;
2419 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002420 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002421 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002422 }
2423 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002424
2425out_unlock:
2426 btrfs_tree_unlock(right);
2427 free_extent_buffer(right);
2428 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002429}
Chris Mason925baed2008-06-25 16:01:30 -04002430
Chris Mason00ec4c52007-02-24 12:47:20 -05002431/*
Chris Mason44871b12009-03-13 10:04:31 -04002432 * push some data in the path leaf to the right, trying to free up at
2433 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2434 *
2435 * returns 1 if the push failed because the other node didn't have enough
2436 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002437 *
2438 * this will push starting from min_slot to the end of the leaf. It won't
2439 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002440 */
2441static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002442 *root, struct btrfs_path *path,
2443 int min_data_size, int data_size,
2444 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002445{
2446 struct extent_buffer *left = path->nodes[0];
2447 struct extent_buffer *right;
2448 struct extent_buffer *upper;
2449 int slot;
2450 int free_space;
2451 u32 left_nritems;
2452 int ret;
2453
2454 if (!path->nodes[1])
2455 return 1;
2456
2457 slot = path->slots[1];
2458 upper = path->nodes[1];
2459 if (slot >= btrfs_header_nritems(upper) - 1)
2460 return 1;
2461
2462 btrfs_assert_tree_locked(path->nodes[1]);
2463
2464 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002465 if (right == NULL)
2466 return 1;
2467
Chris Mason44871b12009-03-13 10:04:31 -04002468 btrfs_tree_lock(right);
2469 btrfs_set_lock_blocking(right);
2470
2471 free_space = btrfs_leaf_free_space(root, right);
2472 if (free_space < data_size)
2473 goto out_unlock;
2474
2475 /* cow and double check */
2476 ret = btrfs_cow_block(trans, root, right, upper,
2477 slot + 1, &right);
2478 if (ret)
2479 goto out_unlock;
2480
2481 free_space = btrfs_leaf_free_space(root, right);
2482 if (free_space < data_size)
2483 goto out_unlock;
2484
2485 left_nritems = btrfs_header_nritems(left);
2486 if (left_nritems == 0)
2487 goto out_unlock;
2488
Chris Mason99d8f832010-07-07 10:51:48 -04002489 return __push_leaf_right(trans, root, path, min_data_size, empty,
2490 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002491out_unlock:
2492 btrfs_tree_unlock(right);
2493 free_extent_buffer(right);
2494 return 1;
2495}
2496
2497/*
Chris Mason74123bd2007-02-02 11:05:29 -05002498 * push some data in the path leaf to the left, trying to free up at
2499 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002500 *
2501 * max_slot can put a limit on how far into the leaf we'll push items. The
2502 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2503 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002504 */
Chris Mason44871b12009-03-13 10:04:31 -04002505static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2506 struct btrfs_root *root,
2507 struct btrfs_path *path, int data_size,
2508 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002509 int free_space, u32 right_nritems,
2510 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002511{
Chris Mason5f39d392007-10-15 16:14:19 -04002512 struct btrfs_disk_key disk_key;
2513 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002514 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515 int push_space = 0;
2516 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002517 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002518 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002519 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002520 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002521 u32 this_item_size;
2522 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002523
Chris Mason34a38212007-11-07 13:31:03 -05002524 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002525 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002526 else
Chris Mason99d8f832010-07-07 10:51:48 -04002527 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002528
2529 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002530 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002531
Zheng Yan31840ae2008-09-23 13:14:14 -04002532 if (!empty && push_items > 0) {
2533 if (path->slots[0] < i)
2534 break;
2535 if (path->slots[0] == i) {
2536 int space = btrfs_leaf_free_space(root, right);
2537 if (space + push_space * 2 > free_space)
2538 break;
2539 }
2540 }
2541
Chris Masonbe0e5c02007-01-26 15:51:26 -05002542 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002543 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002544
2545 this_item_size = btrfs_item_size(right, item);
2546 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002547 break;
Chris Masondb945352007-10-15 16:15:53 -04002548
Chris Masonbe0e5c02007-01-26 15:51:26 -05002549 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002550 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002551 }
Chris Masondb945352007-10-15 16:15:53 -04002552
Chris Masonbe0e5c02007-01-26 15:51:26 -05002553 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002554 ret = 1;
2555 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556 }
Chris Mason34a38212007-11-07 13:31:03 -05002557 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002558 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002559
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002561 copy_extent_buffer(left, right,
2562 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2563 btrfs_item_nr_offset(0),
2564 push_items * sizeof(struct btrfs_item));
2565
Chris Mason123abc82007-03-14 14:14:43 -04002566 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002567 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002568
2569 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002570 leaf_data_end(root, left) - push_space,
2571 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002572 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002573 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002574 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002575 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002576
Chris Masondb945352007-10-15 16:15:53 -04002577 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002578 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002579 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002580
Chris Mason5f39d392007-10-15 16:14:19 -04002581 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002582
Chris Mason5f39d392007-10-15 16:14:19 -04002583 ioff = btrfs_item_offset(left, item);
2584 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002585 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 }
Chris Mason5f39d392007-10-15 16:14:19 -04002587 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588
2589 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002590 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002591 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2592 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002593 WARN_ON(1);
2594 }
Chris Mason5f39d392007-10-15 16:14:19 -04002595
Chris Mason34a38212007-11-07 13:31:03 -05002596 if (push_items < right_nritems) {
2597 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2598 leaf_data_end(root, right);
2599 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2600 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2601 btrfs_leaf_data(right) +
2602 leaf_data_end(root, right), push_space);
2603
2604 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002605 btrfs_item_nr_offset(push_items),
2606 (btrfs_header_nritems(right) - push_items) *
2607 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002608 }
Yaneef1c492007-11-26 10:58:13 -05002609 right_nritems -= push_items;
2610 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002611 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002612 for (i = 0; i < right_nritems; i++) {
2613 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002614
Chris Masondb945352007-10-15 16:15:53 -04002615 push_space = push_space - btrfs_item_size(right, item);
2616 btrfs_set_item_offset(right, item, push_space);
2617 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002618
Chris Mason5f39d392007-10-15 16:14:19 -04002619 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002620 if (right_nritems)
2621 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002622 else
2623 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002624
Chris Mason5f39d392007-10-15 16:14:19 -04002625 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002626 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002627
2628 /* then fixup the leaf pointer in the path */
2629 if (path->slots[0] < push_items) {
2630 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002631 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002632 free_extent_buffer(path->nodes[0]);
2633 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002634 path->slots[1] -= 1;
2635 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002636 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002637 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002638 path->slots[0] -= push_items;
2639 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002640 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002641 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002642out:
2643 btrfs_tree_unlock(left);
2644 free_extent_buffer(left);
2645 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002646}
2647
Chris Mason74123bd2007-02-02 11:05:29 -05002648/*
Chris Mason44871b12009-03-13 10:04:31 -04002649 * push some data in the path leaf to the left, trying to free up at
2650 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002651 *
2652 * max_slot can put a limit on how far into the leaf we'll push items. The
2653 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2654 * items
Chris Mason44871b12009-03-13 10:04:31 -04002655 */
2656static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002657 *root, struct btrfs_path *path, int min_data_size,
2658 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002659{
2660 struct extent_buffer *right = path->nodes[0];
2661 struct extent_buffer *left;
2662 int slot;
2663 int free_space;
2664 u32 right_nritems;
2665 int ret = 0;
2666
2667 slot = path->slots[1];
2668 if (slot == 0)
2669 return 1;
2670 if (!path->nodes[1])
2671 return 1;
2672
2673 right_nritems = btrfs_header_nritems(right);
2674 if (right_nritems == 0)
2675 return 1;
2676
2677 btrfs_assert_tree_locked(path->nodes[1]);
2678
2679 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002680 if (left == NULL)
2681 return 1;
2682
Chris Mason44871b12009-03-13 10:04:31 -04002683 btrfs_tree_lock(left);
2684 btrfs_set_lock_blocking(left);
2685
2686 free_space = btrfs_leaf_free_space(root, left);
2687 if (free_space < data_size) {
2688 ret = 1;
2689 goto out;
2690 }
2691
2692 /* cow and double check */
2693 ret = btrfs_cow_block(trans, root, left,
2694 path->nodes[1], slot - 1, &left);
2695 if (ret) {
2696 /* we hit -ENOSPC, but it isn't fatal here */
2697 ret = 1;
2698 goto out;
2699 }
2700
2701 free_space = btrfs_leaf_free_space(root, left);
2702 if (free_space < data_size) {
2703 ret = 1;
2704 goto out;
2705 }
2706
Chris Mason99d8f832010-07-07 10:51:48 -04002707 return __push_leaf_left(trans, root, path, min_data_size,
2708 empty, left, free_space, right_nritems,
2709 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002710out:
2711 btrfs_tree_unlock(left);
2712 free_extent_buffer(left);
2713 return ret;
2714}
2715
2716/*
Chris Mason74123bd2007-02-02 11:05:29 -05002717 * split the path's leaf in two, making sure there is at least data_size
2718 * available for the resulting leaf level of the path.
2719 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002720static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2721 struct btrfs_root *root,
2722 struct btrfs_path *path,
2723 struct extent_buffer *l,
2724 struct extent_buffer *right,
2725 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002726{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002727 int data_copy_size;
2728 int rt_data_off;
2729 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002730 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002731
Chris Mason5f39d392007-10-15 16:14:19 -04002732 nritems = nritems - mid;
2733 btrfs_set_header_nritems(right, nritems);
2734 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2735
2736 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2737 btrfs_item_nr_offset(mid),
2738 nritems * sizeof(struct btrfs_item));
2739
2740 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002741 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2742 data_copy_size, btrfs_leaf_data(l) +
2743 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002744
Chris Mason5f39d392007-10-15 16:14:19 -04002745 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2746 btrfs_item_end_nr(l, mid);
2747
2748 for (i = 0; i < nritems; i++) {
2749 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002750 u32 ioff;
2751
Chris Masondb945352007-10-15 16:15:53 -04002752 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002753 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002754 }
Chris Mason74123bd2007-02-02 11:05:29 -05002755
Chris Mason5f39d392007-10-15 16:14:19 -04002756 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002757 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002758 insert_ptr(trans, root, path, &disk_key, right->start,
2759 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002760
2761 btrfs_mark_buffer_dirty(right);
2762 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002763 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002764
Chris Masonbe0e5c02007-01-26 15:51:26 -05002765 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002766 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002767 free_extent_buffer(path->nodes[0]);
2768 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002769 path->slots[0] -= mid;
2770 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002771 } else {
2772 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002773 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002774 }
Chris Mason5f39d392007-10-15 16:14:19 -04002775
Chris Masoneb60cea2007-02-02 09:18:22 -05002776 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002777}
2778
2779/*
Chris Mason99d8f832010-07-07 10:51:48 -04002780 * double splits happen when we need to insert a big item in the middle
2781 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2782 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2783 * A B C
2784 *
2785 * We avoid this by trying to push the items on either side of our target
2786 * into the adjacent leaves. If all goes well we can avoid the double split
2787 * completely.
2788 */
2789static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2790 struct btrfs_root *root,
2791 struct btrfs_path *path,
2792 int data_size)
2793{
2794 int ret;
2795 int progress = 0;
2796 int slot;
2797 u32 nritems;
2798
2799 slot = path->slots[0];
2800
2801 /*
2802 * try to push all the items after our slot into the
2803 * right leaf
2804 */
2805 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2806 if (ret < 0)
2807 return ret;
2808
2809 if (ret == 0)
2810 progress++;
2811
2812 nritems = btrfs_header_nritems(path->nodes[0]);
2813 /*
2814 * our goal is to get our slot at the start or end of a leaf. If
2815 * we've done so we're done
2816 */
2817 if (path->slots[0] == 0 || path->slots[0] == nritems)
2818 return 0;
2819
2820 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2821 return 0;
2822
2823 /* try to push all the items before our slot into the next leaf */
2824 slot = path->slots[0];
2825 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2826 if (ret < 0)
2827 return ret;
2828
2829 if (ret == 0)
2830 progress++;
2831
2832 if (progress)
2833 return 0;
2834 return 1;
2835}
2836
2837/*
Chris Mason44871b12009-03-13 10:04:31 -04002838 * split the path's leaf in two, making sure there is at least data_size
2839 * available for the resulting leaf level of the path.
2840 *
2841 * returns 0 if all went well and < 0 on failure.
2842 */
2843static noinline int split_leaf(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *root,
2845 struct btrfs_key *ins_key,
2846 struct btrfs_path *path, int data_size,
2847 int extend)
2848{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002849 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002850 struct extent_buffer *l;
2851 u32 nritems;
2852 int mid;
2853 int slot;
2854 struct extent_buffer *right;
2855 int ret = 0;
2856 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002857 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002858 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002859 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002860
Yan, Zhenga5719522009-09-24 09:17:31 -04002861 l = path->nodes[0];
2862 slot = path->slots[0];
2863 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2864 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2865 return -EOVERFLOW;
2866
Chris Mason44871b12009-03-13 10:04:31 -04002867 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002868 if (data_size) {
2869 wret = push_leaf_right(trans, root, path, data_size,
2870 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002871 if (wret < 0)
2872 return wret;
2873 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002874 wret = push_leaf_left(trans, root, path, data_size,
2875 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002876 if (wret < 0)
2877 return wret;
2878 }
2879 l = path->nodes[0];
2880
2881 /* did the pushes work? */
2882 if (btrfs_leaf_free_space(root, l) >= data_size)
2883 return 0;
2884 }
2885
2886 if (!path->nodes[1]) {
2887 ret = insert_new_root(trans, root, path, 1);
2888 if (ret)
2889 return ret;
2890 }
2891again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002892 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002893 l = path->nodes[0];
2894 slot = path->slots[0];
2895 nritems = btrfs_header_nritems(l);
2896 mid = (nritems + 1) / 2;
2897
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002898 if (mid <= slot) {
2899 if (nritems == 1 ||
2900 leaf_space_used(l, mid, nritems - mid) + data_size >
2901 BTRFS_LEAF_DATA_SIZE(root)) {
2902 if (slot >= nritems) {
2903 split = 0;
2904 } else {
2905 mid = slot;
2906 if (mid != nritems &&
2907 leaf_space_used(l, mid, nritems - mid) +
2908 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002909 if (data_size && !tried_avoid_double)
2910 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002911 split = 2;
2912 }
2913 }
2914 }
2915 } else {
2916 if (leaf_space_used(l, 0, mid) + data_size >
2917 BTRFS_LEAF_DATA_SIZE(root)) {
2918 if (!extend && data_size && slot == 0) {
2919 split = 0;
2920 } else if ((extend || !data_size) && slot == 0) {
2921 mid = 1;
2922 } else {
2923 mid = slot;
2924 if (mid != nritems &&
2925 leaf_space_used(l, mid, nritems - mid) +
2926 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002927 if (data_size && !tried_avoid_double)
2928 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002929 split = 2 ;
2930 }
2931 }
2932 }
2933 }
2934
2935 if (split == 0)
2936 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2937 else
2938 btrfs_item_key(l, &disk_key, mid);
2939
2940 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002941 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002942 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002943 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002944 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002945
2946 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002947
2948 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2949 btrfs_set_header_bytenr(right, right->start);
2950 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002951 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002952 btrfs_set_header_owner(right, root->root_key.objectid);
2953 btrfs_set_header_level(right, 0);
2954 write_extent_buffer(right, root->fs_info->fsid,
2955 (unsigned long)btrfs_header_fsid(right),
2956 BTRFS_FSID_SIZE);
2957
2958 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2959 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2960 BTRFS_UUID_SIZE);
2961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002962 if (split == 0) {
2963 if (mid <= slot) {
2964 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002965 insert_ptr(trans, root, path, &disk_key, right->start,
2966 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002967 btrfs_tree_unlock(path->nodes[0]);
2968 free_extent_buffer(path->nodes[0]);
2969 path->nodes[0] = right;
2970 path->slots[0] = 0;
2971 path->slots[1] += 1;
2972 } else {
2973 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002974 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002976 btrfs_tree_unlock(path->nodes[0]);
2977 free_extent_buffer(path->nodes[0]);
2978 path->nodes[0] = right;
2979 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01002980 if (path->slots[1] == 0)
2981 fixup_low_keys(trans, root, path,
2982 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002983 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002984 btrfs_mark_buffer_dirty(right);
2985 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002986 }
2987
Jeff Mahoney143bede2012-03-01 14:56:26 +01002988 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04002989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002990 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002991 BUG_ON(num_doubles != 0);
2992 num_doubles++;
2993 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002994 }
Chris Mason44871b12009-03-13 10:04:31 -04002995
Jeff Mahoney143bede2012-03-01 14:56:26 +01002996 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002997
2998push_for_double:
2999 push_for_double_split(trans, root, path, data_size);
3000 tried_avoid_double = 1;
3001 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3002 return 0;
3003 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003004}
3005
Yan, Zhengad48fd752009-11-12 09:33:58 +00003006static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3007 struct btrfs_root *root,
3008 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003009{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003010 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003011 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003012 struct btrfs_file_extent_item *fi;
3013 u64 extent_len = 0;
3014 u32 item_size;
3015 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003016
3017 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003018 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3019
3020 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3021 key.type != BTRFS_EXTENT_CSUM_KEY);
3022
3023 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3024 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003025
3026 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003027 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3028 fi = btrfs_item_ptr(leaf, path->slots[0],
3029 struct btrfs_file_extent_item);
3030 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3031 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003032 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003033
Chris Mason459931e2008-12-10 09:10:46 -05003034 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003035 path->search_for_split = 1;
3036 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003037 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003038 if (ret < 0)
3039 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003040
Yan, Zhengad48fd752009-11-12 09:33:58 +00003041 ret = -EAGAIN;
3042 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003043 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003044 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3045 goto err;
3046
Chris Mason109f6ae2010-04-02 09:20:18 -04003047 /* the leaf has changed, it now has room. return now */
3048 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3049 goto err;
3050
Yan, Zhengad48fd752009-11-12 09:33:58 +00003051 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3052 fi = btrfs_item_ptr(leaf, path->slots[0],
3053 struct btrfs_file_extent_item);
3054 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3055 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003056 }
3057
Chris Masonb9473432009-03-13 11:00:37 -04003058 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003059 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003060 if (ret)
3061 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003062
Yan, Zhengad48fd752009-11-12 09:33:58 +00003063 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003064 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003065 return 0;
3066err:
3067 path->keep_locks = 0;
3068 return ret;
3069}
3070
3071static noinline int split_item(struct btrfs_trans_handle *trans,
3072 struct btrfs_root *root,
3073 struct btrfs_path *path,
3074 struct btrfs_key *new_key,
3075 unsigned long split_offset)
3076{
3077 struct extent_buffer *leaf;
3078 struct btrfs_item *item;
3079 struct btrfs_item *new_item;
3080 int slot;
3081 char *buf;
3082 u32 nritems;
3083 u32 item_size;
3084 u32 orig_offset;
3085 struct btrfs_disk_key disk_key;
3086
Chris Masonb9473432009-03-13 11:00:37 -04003087 leaf = path->nodes[0];
3088 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3089
Chris Masonb4ce94d2009-02-04 09:25:08 -05003090 btrfs_set_path_blocking(path);
3091
Chris Mason459931e2008-12-10 09:10:46 -05003092 item = btrfs_item_nr(leaf, path->slots[0]);
3093 orig_offset = btrfs_item_offset(leaf, item);
3094 item_size = btrfs_item_size(leaf, item);
3095
Chris Mason459931e2008-12-10 09:10:46 -05003096 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003097 if (!buf)
3098 return -ENOMEM;
3099
Chris Mason459931e2008-12-10 09:10:46 -05003100 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3101 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003102
Chris Mason459931e2008-12-10 09:10:46 -05003103 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003104 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003105 if (slot != nritems) {
3106 /* shift the items */
3107 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003108 btrfs_item_nr_offset(slot),
3109 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003110 }
3111
3112 btrfs_cpu_key_to_disk(&disk_key, new_key);
3113 btrfs_set_item_key(leaf, &disk_key, slot);
3114
3115 new_item = btrfs_item_nr(leaf, slot);
3116
3117 btrfs_set_item_offset(leaf, new_item, orig_offset);
3118 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3119
3120 btrfs_set_item_offset(leaf, item,
3121 orig_offset + item_size - split_offset);
3122 btrfs_set_item_size(leaf, item, split_offset);
3123
3124 btrfs_set_header_nritems(leaf, nritems + 1);
3125
3126 /* write the data for the start of the original item */
3127 write_extent_buffer(leaf, buf,
3128 btrfs_item_ptr_offset(leaf, path->slots[0]),
3129 split_offset);
3130
3131 /* write the data for the new item */
3132 write_extent_buffer(leaf, buf + split_offset,
3133 btrfs_item_ptr_offset(leaf, slot),
3134 item_size - split_offset);
3135 btrfs_mark_buffer_dirty(leaf);
3136
Yan, Zhengad48fd752009-11-12 09:33:58 +00003137 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003138 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003139 return 0;
3140}
3141
3142/*
3143 * This function splits a single item into two items,
3144 * giving 'new_key' to the new item and splitting the
3145 * old one at split_offset (from the start of the item).
3146 *
3147 * The path may be released by this operation. After
3148 * the split, the path is pointing to the old item. The
3149 * new item is going to be in the same node as the old one.
3150 *
3151 * Note, the item being split must be smaller enough to live alone on
3152 * a tree block with room for one extra struct btrfs_item
3153 *
3154 * This allows us to split the item in place, keeping a lock on the
3155 * leaf the entire time.
3156 */
3157int btrfs_split_item(struct btrfs_trans_handle *trans,
3158 struct btrfs_root *root,
3159 struct btrfs_path *path,
3160 struct btrfs_key *new_key,
3161 unsigned long split_offset)
3162{
3163 int ret;
3164 ret = setup_leaf_for_split(trans, root, path,
3165 sizeof(struct btrfs_item));
3166 if (ret)
3167 return ret;
3168
3169 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003170 return ret;
3171}
3172
3173/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003174 * This function duplicate a item, giving 'new_key' to the new item.
3175 * It guarantees both items live in the same tree leaf and the new item
3176 * is contiguous with the original item.
3177 *
3178 * This allows us to split file extent in place, keeping a lock on the
3179 * leaf the entire time.
3180 */
3181int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3182 struct btrfs_root *root,
3183 struct btrfs_path *path,
3184 struct btrfs_key *new_key)
3185{
3186 struct extent_buffer *leaf;
3187 int ret;
3188 u32 item_size;
3189
3190 leaf = path->nodes[0];
3191 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3192 ret = setup_leaf_for_split(trans, root, path,
3193 item_size + sizeof(struct btrfs_item));
3194 if (ret)
3195 return ret;
3196
3197 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003198 setup_items_for_insert(trans, root, path, new_key, &item_size,
3199 item_size, item_size +
3200 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003201 leaf = path->nodes[0];
3202 memcpy_extent_buffer(leaf,
3203 btrfs_item_ptr_offset(leaf, path->slots[0]),
3204 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3205 item_size);
3206 return 0;
3207}
3208
3209/*
Chris Masond352ac62008-09-29 15:18:18 -04003210 * make the item pointed to by the path smaller. new_size indicates
3211 * how small to make it, and from_end tells us if we just chop bytes
3212 * off the end of the item or if we shift the item to chop bytes off
3213 * the front.
3214 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003215void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3216 struct btrfs_root *root,
3217 struct btrfs_path *path,
3218 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003219{
Chris Masonb18c6682007-04-17 13:26:50 -04003220 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003221 struct extent_buffer *leaf;
3222 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003223 u32 nritems;
3224 unsigned int data_end;
3225 unsigned int old_data_start;
3226 unsigned int old_size;
3227 unsigned int size_diff;
3228 int i;
3229
Chris Mason5f39d392007-10-15 16:14:19 -04003230 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003231 slot = path->slots[0];
3232
3233 old_size = btrfs_item_size_nr(leaf, slot);
3234 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003235 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003238 data_end = leaf_data_end(root, leaf);
3239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003241
Chris Masonb18c6682007-04-17 13:26:50 -04003242 size_diff = old_size - new_size;
3243
3244 BUG_ON(slot < 0);
3245 BUG_ON(slot >= nritems);
3246
3247 /*
3248 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3249 */
3250 /* first correct the data pointers */
3251 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003252 u32 ioff;
3253 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 ioff = btrfs_item_offset(leaf, item);
3256 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003257 }
Chris Masondb945352007-10-15 16:15:53 -04003258
Chris Masonb18c6682007-04-17 13:26:50 -04003259 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003260 if (from_end) {
3261 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3262 data_end + size_diff, btrfs_leaf_data(leaf) +
3263 data_end, old_data_start + new_size - data_end);
3264 } else {
3265 struct btrfs_disk_key disk_key;
3266 u64 offset;
3267
3268 btrfs_item_key(leaf, &disk_key, slot);
3269
3270 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3271 unsigned long ptr;
3272 struct btrfs_file_extent_item *fi;
3273
3274 fi = btrfs_item_ptr(leaf, slot,
3275 struct btrfs_file_extent_item);
3276 fi = (struct btrfs_file_extent_item *)(
3277 (unsigned long)fi - size_diff);
3278
3279 if (btrfs_file_extent_type(leaf, fi) ==
3280 BTRFS_FILE_EXTENT_INLINE) {
3281 ptr = btrfs_item_ptr_offset(leaf, slot);
3282 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003283 (unsigned long)fi,
3284 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003285 disk_bytenr));
3286 }
3287 }
3288
3289 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3290 data_end + size_diff, btrfs_leaf_data(leaf) +
3291 data_end, old_data_start - data_end);
3292
3293 offset = btrfs_disk_key_offset(&disk_key);
3294 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3295 btrfs_set_item_key(leaf, &disk_key, slot);
3296 if (slot == 0)
3297 fixup_low_keys(trans, root, path, &disk_key, 1);
3298 }
Chris Mason5f39d392007-10-15 16:14:19 -04003299
3300 item = btrfs_item_nr(leaf, slot);
3301 btrfs_set_item_size(leaf, item, new_size);
3302 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003303
Chris Mason5f39d392007-10-15 16:14:19 -04003304 if (btrfs_leaf_free_space(root, leaf) < 0) {
3305 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003306 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003307 }
Chris Masonb18c6682007-04-17 13:26:50 -04003308}
3309
Chris Masond352ac62008-09-29 15:18:18 -04003310/*
3311 * make the item pointed to by the path bigger, data_size is the new size.
3312 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003313void btrfs_extend_item(struct btrfs_trans_handle *trans,
3314 struct btrfs_root *root, struct btrfs_path *path,
3315 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003316{
Chris Mason6567e832007-04-16 09:22:45 -04003317 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003318 struct extent_buffer *leaf;
3319 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003320 u32 nritems;
3321 unsigned int data_end;
3322 unsigned int old_data;
3323 unsigned int old_size;
3324 int i;
3325
Chris Mason5f39d392007-10-15 16:14:19 -04003326 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003327
Chris Mason5f39d392007-10-15 16:14:19 -04003328 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003329 data_end = leaf_data_end(root, leaf);
3330
Chris Mason5f39d392007-10-15 16:14:19 -04003331 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3332 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003333 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003334 }
Chris Mason6567e832007-04-16 09:22:45 -04003335 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003336 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003337
3338 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003339 if (slot >= nritems) {
3340 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003341 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3342 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003343 BUG_ON(1);
3344 }
Chris Mason6567e832007-04-16 09:22:45 -04003345
3346 /*
3347 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3348 */
3349 /* first correct the data pointers */
3350 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003351 u32 ioff;
3352 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003353
Chris Mason5f39d392007-10-15 16:14:19 -04003354 ioff = btrfs_item_offset(leaf, item);
3355 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003356 }
Chris Mason5f39d392007-10-15 16:14:19 -04003357
Chris Mason6567e832007-04-16 09:22:45 -04003358 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003359 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003360 data_end - data_size, btrfs_leaf_data(leaf) +
3361 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003362
Chris Mason6567e832007-04-16 09:22:45 -04003363 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003364 old_size = btrfs_item_size_nr(leaf, slot);
3365 item = btrfs_item_nr(leaf, slot);
3366 btrfs_set_item_size(leaf, item, old_size + data_size);
3367 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003368
Chris Mason5f39d392007-10-15 16:14:19 -04003369 if (btrfs_leaf_free_space(root, leaf) < 0) {
3370 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003371 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003372 }
Chris Mason6567e832007-04-16 09:22:45 -04003373}
3374
Chris Mason74123bd2007-02-02 11:05:29 -05003375/*
Chris Masond352ac62008-09-29 15:18:18 -04003376 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003377 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003378 * Returns the number of keys that were inserted.
3379 */
3380int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3381 struct btrfs_root *root,
3382 struct btrfs_path *path,
3383 struct btrfs_key *cpu_key, u32 *data_size,
3384 int nr)
3385{
3386 struct extent_buffer *leaf;
3387 struct btrfs_item *item;
3388 int ret = 0;
3389 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003390 int i;
3391 u32 nritems;
3392 u32 total_data = 0;
3393 u32 total_size = 0;
3394 unsigned int data_end;
3395 struct btrfs_disk_key disk_key;
3396 struct btrfs_key found_key;
3397
Yan Zheng87b29b22008-12-17 10:21:48 -05003398 for (i = 0; i < nr; i++) {
3399 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3400 BTRFS_LEAF_DATA_SIZE(root)) {
3401 break;
3402 nr = i;
3403 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003404 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003405 total_size += data_size[i] + sizeof(struct btrfs_item);
3406 }
3407 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003408
Josef Bacikf3465ca2008-11-12 14:19:50 -05003409 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3410 if (ret == 0)
3411 return -EEXIST;
3412 if (ret < 0)
3413 goto out;
3414
Josef Bacikf3465ca2008-11-12 14:19:50 -05003415 leaf = path->nodes[0];
3416
3417 nritems = btrfs_header_nritems(leaf);
3418 data_end = leaf_data_end(root, leaf);
3419
3420 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3421 for (i = nr; i >= 0; i--) {
3422 total_data -= data_size[i];
3423 total_size -= data_size[i] + sizeof(struct btrfs_item);
3424 if (total_size < btrfs_leaf_free_space(root, leaf))
3425 break;
3426 }
3427 nr = i;
3428 }
3429
3430 slot = path->slots[0];
3431 BUG_ON(slot < 0);
3432
3433 if (slot != nritems) {
3434 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3435
3436 item = btrfs_item_nr(leaf, slot);
3437 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3438
3439 /* figure out how many keys we can insert in here */
3440 total_data = data_size[0];
3441 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003442 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003443 break;
3444 total_data += data_size[i];
3445 }
3446 nr = i;
3447
3448 if (old_data < data_end) {
3449 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003450 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003451 slot, old_data, data_end);
3452 BUG_ON(1);
3453 }
3454 /*
3455 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3456 */
3457 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003458 for (i = slot; i < nritems; i++) {
3459 u32 ioff;
3460
3461 item = btrfs_item_nr(leaf, i);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003462 ioff = btrfs_item_offset(leaf, item);
3463 btrfs_set_item_offset(leaf, item, ioff - total_data);
3464 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003465 /* shift the items */
3466 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3467 btrfs_item_nr_offset(slot),
3468 (nritems - slot) * sizeof(struct btrfs_item));
3469
3470 /* shift the data */
3471 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3472 data_end - total_data, btrfs_leaf_data(leaf) +
3473 data_end, old_data - data_end);
3474 data_end = old_data;
3475 } else {
3476 /*
3477 * this sucks but it has to be done, if we are inserting at
3478 * the end of the leaf only insert 1 of the items, since we
3479 * have no way of knowing whats on the next leaf and we'd have
3480 * to drop our current locks to figure it out
3481 */
3482 nr = 1;
3483 }
3484
3485 /* setup the item for the new data */
3486 for (i = 0; i < nr; i++) {
3487 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3488 btrfs_set_item_key(leaf, &disk_key, slot + i);
3489 item = btrfs_item_nr(leaf, slot + i);
3490 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3491 data_end -= data_size[i];
3492 btrfs_set_item_size(leaf, item, data_size[i]);
3493 }
3494 btrfs_set_header_nritems(leaf, nritems + nr);
3495 btrfs_mark_buffer_dirty(leaf);
3496
3497 ret = 0;
3498 if (slot == 0) {
3499 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003500 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003501 }
3502
3503 if (btrfs_leaf_free_space(root, leaf) < 0) {
3504 btrfs_print_leaf(root, leaf);
3505 BUG();
3506 }
3507out:
3508 if (!ret)
3509 ret = nr;
3510 return ret;
3511}
3512
3513/*
Chris Mason44871b12009-03-13 10:04:31 -04003514 * this is a helper for btrfs_insert_empty_items, the main goal here is
3515 * to save stack depth by doing the bulk of the work in a function
3516 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003517 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003518void setup_items_for_insert(struct btrfs_trans_handle *trans,
3519 struct btrfs_root *root, struct btrfs_path *path,
3520 struct btrfs_key *cpu_key, u32 *data_size,
3521 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003522{
Chris Mason5f39d392007-10-15 16:14:19 -04003523 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003524 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003525 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003526 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003527 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003528 struct extent_buffer *leaf;
3529 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003530
Chris Mason5f39d392007-10-15 16:14:19 -04003531 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003532 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003533
Chris Mason5f39d392007-10-15 16:14:19 -04003534 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003535 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003536
Chris Masonf25956c2008-09-12 15:32:53 -04003537 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003538 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003539 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003540 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003541 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003542 }
Chris Mason5f39d392007-10-15 16:14:19 -04003543
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003545 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003546
Chris Mason5f39d392007-10-15 16:14:19 -04003547 if (old_data < data_end) {
3548 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003549 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003550 slot, old_data, data_end);
3551 BUG_ON(1);
3552 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003553 /*
3554 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3555 */
3556 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003557 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003558 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003559
Chris Mason5f39d392007-10-15 16:14:19 -04003560 item = btrfs_item_nr(leaf, i);
3561 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003562 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003563 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003564 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003565 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003566 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003567 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003568
3569 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003570 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003571 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003572 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003573 data_end = old_data;
3574 }
Chris Mason5f39d392007-10-15 16:14:19 -04003575
Chris Mason62e27492007-03-15 12:56:47 -04003576 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003577 for (i = 0; i < nr; i++) {
3578 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3579 btrfs_set_item_key(leaf, &disk_key, slot + i);
3580 item = btrfs_item_nr(leaf, slot + i);
3581 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3582 data_end -= data_size[i];
3583 btrfs_set_item_size(leaf, item, data_size[i]);
3584 }
Chris Mason44871b12009-03-13 10:04:31 -04003585
Chris Mason9c583092008-01-29 15:15:18 -05003586 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003587
Chris Mason5a01a2e2008-01-30 11:43:54 -05003588 if (slot == 0) {
3589 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003590 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003591 }
Chris Masonb9473432009-03-13 11:00:37 -04003592 btrfs_unlock_up_safe(path, 1);
3593 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003594
Chris Mason5f39d392007-10-15 16:14:19 -04003595 if (btrfs_leaf_free_space(root, leaf) < 0) {
3596 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003598 }
Chris Mason44871b12009-03-13 10:04:31 -04003599}
3600
3601/*
3602 * Given a key and some data, insert items into the tree.
3603 * This does all the path init required, making room in the tree if needed.
3604 */
3605int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *root,
3607 struct btrfs_path *path,
3608 struct btrfs_key *cpu_key, u32 *data_size,
3609 int nr)
3610{
Chris Mason44871b12009-03-13 10:04:31 -04003611 int ret = 0;
3612 int slot;
3613 int i;
3614 u32 total_size = 0;
3615 u32 total_data = 0;
3616
3617 for (i = 0; i < nr; i++)
3618 total_data += data_size[i];
3619
3620 total_size = total_data + (nr * sizeof(struct btrfs_item));
3621 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3622 if (ret == 0)
3623 return -EEXIST;
3624 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003625 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003626
Chris Mason44871b12009-03-13 10:04:31 -04003627 slot = path->slots[0];
3628 BUG_ON(slot < 0);
3629
Jeff Mahoney143bede2012-03-01 14:56:26 +01003630 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003631 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003632 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003633}
3634
3635/*
3636 * Given a key and some data, insert an item into the tree.
3637 * This does all the path init required, making room in the tree if needed.
3638 */
Chris Masone089f052007-03-16 16:20:31 -04003639int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3640 *root, struct btrfs_key *cpu_key, void *data, u32
3641 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003642{
3643 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003644 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003645 struct extent_buffer *leaf;
3646 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003647
Chris Mason2c90e5d2007-04-02 10:50:19 -04003648 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003649 if (!path)
3650 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003651 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003652 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003653 leaf = path->nodes[0];
3654 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3655 write_extent_buffer(leaf, data, ptr, data_size);
3656 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003657 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003658 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003659 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003660}
3661
Chris Mason74123bd2007-02-02 11:05:29 -05003662/*
Chris Mason5de08d72007-02-24 06:24:44 -05003663 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003664 *
Chris Masond352ac62008-09-29 15:18:18 -04003665 * the tree should have been previously balanced so the deletion does not
3666 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003667 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003668static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3669 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670{
Chris Mason5f39d392007-10-15 16:14:19 -04003671 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003672 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003673
Chris Mason5f39d392007-10-15 16:14:19 -04003674 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003675 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003676 memmove_extent_buffer(parent,
3677 btrfs_node_key_ptr_offset(slot),
3678 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003679 sizeof(struct btrfs_key_ptr) *
3680 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003681 }
Chris Mason7518a232007-03-12 12:01:18 -04003682 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003683 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003684 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003685 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003686 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003687 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003688 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003689 struct btrfs_disk_key disk_key;
3690
3691 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003692 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003693 }
Chris Masond6025572007-03-30 14:27:56 -04003694 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003695}
3696
Chris Mason74123bd2007-02-02 11:05:29 -05003697/*
Chris Mason323ac952008-10-01 19:05:46 -04003698 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003699 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003700 *
3701 * This deletes the pointer in path->nodes[1] and frees the leaf
3702 * block extent. zero is returned if it all worked out, < 0 otherwise.
3703 *
3704 * The path must have already been setup for deleting the leaf, including
3705 * all the proper balancing. path->nodes[1] must be locked.
3706 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003707static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3708 struct btrfs_root *root,
3709 struct btrfs_path *path,
3710 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003711{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003712 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003713 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003714
Chris Mason4d081c42009-02-04 09:31:28 -05003715 /*
3716 * btrfs_free_extent is expensive, we want to make sure we
3717 * aren't holding any locks when we call it
3718 */
3719 btrfs_unlock_up_safe(path, 0);
3720
Yan, Zhengf0486c62010-05-16 10:46:25 -04003721 root_sub_used(root, leaf->len);
3722
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003723 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003724}
3725/*
Chris Mason74123bd2007-02-02 11:05:29 -05003726 * delete the item at the leaf level in path. If that empties
3727 * the leaf, remove it from the tree
3728 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003729int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3730 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003731{
Chris Mason5f39d392007-10-15 16:14:19 -04003732 struct extent_buffer *leaf;
3733 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003734 int last_off;
3735 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003736 int ret = 0;
3737 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003738 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003739 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003740
Chris Mason5f39d392007-10-15 16:14:19 -04003741 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003742 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3743
3744 for (i = 0; i < nr; i++)
3745 dsize += btrfs_item_size_nr(leaf, slot + i);
3746
Chris Mason5f39d392007-10-15 16:14:19 -04003747 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003748
Chris Mason85e21ba2008-01-29 15:11:36 -05003749 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003750 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003751
3752 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003753 data_end + dsize,
3754 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003755 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
Chris Mason85e21ba2008-01-29 15:11:36 -05003757 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003758 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003759
Chris Mason5f39d392007-10-15 16:14:19 -04003760 item = btrfs_item_nr(leaf, i);
3761 ioff = btrfs_item_offset(leaf, item);
3762 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003763 }
Chris Masondb945352007-10-15 16:15:53 -04003764
Chris Mason5f39d392007-10-15 16:14:19 -04003765 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003766 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003767 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003768 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003769 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003770 btrfs_set_header_nritems(leaf, nritems - nr);
3771 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003772
Chris Mason74123bd2007-02-02 11:05:29 -05003773 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003774 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003775 if (leaf == root->node) {
3776 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003777 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003778 btrfs_set_path_blocking(path);
3779 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003780 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003781 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003782 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003783 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003784 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003785 struct btrfs_disk_key disk_key;
3786
3787 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003788 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003789 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003790
Chris Mason74123bd2007-02-02 11:05:29 -05003791 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003792 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003793 /* push_leaf_left fixes the path.
3794 * make sure the path still points to our leaf
3795 * for possible call to del_ptr below
3796 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003797 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003798 extent_buffer_get(leaf);
3799
Chris Masonb9473432009-03-13 11:00:37 -04003800 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003801 wret = push_leaf_left(trans, root, path, 1, 1,
3802 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003803 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003804 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003805
3806 if (path->nodes[0] == leaf &&
3807 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003808 wret = push_leaf_right(trans, root, path, 1,
3809 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003810 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003811 ret = wret;
3812 }
Chris Mason5f39d392007-10-15 16:14:19 -04003813
3814 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003815 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003816 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003817 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003818 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003819 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003820 /* if we're still in the path, make sure
3821 * we're dirty. Otherwise, one of the
3822 * push_leaf functions must have already
3823 * dirtied this buffer
3824 */
3825 if (path->nodes[0] == leaf)
3826 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003827 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003828 }
Chris Masond5719762007-03-23 10:01:08 -04003829 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003830 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003831 }
3832 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003833 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834}
3835
Chris Mason97571fd2007-02-24 13:39:08 -05003836/*
Chris Mason925baed2008-06-25 16:01:30 -04003837 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003838 * returns 0 if it found something or 1 if there are no lesser leaves.
3839 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003840 *
3841 * This may release the path, and so you may lose any locks held at the
3842 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003843 */
3844int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3845{
Chris Mason925baed2008-06-25 16:01:30 -04003846 struct btrfs_key key;
3847 struct btrfs_disk_key found_key;
3848 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003849
Chris Mason925baed2008-06-25 16:01:30 -04003850 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003851
Chris Mason925baed2008-06-25 16:01:30 -04003852 if (key.offset > 0)
3853 key.offset--;
3854 else if (key.type > 0)
3855 key.type--;
3856 else if (key.objectid > 0)
3857 key.objectid--;
3858 else
3859 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003860
David Sterbab3b4aa72011-04-21 01:20:15 +02003861 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003862 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3863 if (ret < 0)
3864 return ret;
3865 btrfs_item_key(path->nodes[0], &found_key, 0);
3866 ret = comp_keys(&found_key, &key);
3867 if (ret < 0)
3868 return 0;
3869 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003870}
3871
Chris Mason3f157a22008-06-25 16:01:31 -04003872/*
3873 * A helper function to walk down the tree starting at min_key, and looking
3874 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003875 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003876 *
3877 * This does not cow, but it does stuff the starting key it finds back
3878 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3879 * key and get a writable path.
3880 *
3881 * This does lock as it descends, and path->keep_locks should be set
3882 * to 1 by the caller.
3883 *
3884 * This honors path->lowest_level to prevent descent past a given level
3885 * of the tree.
3886 *
Chris Masond352ac62008-09-29 15:18:18 -04003887 * min_trans indicates the oldest transaction that you are interested
3888 * in walking through. Any nodes or leaves older than min_trans are
3889 * skipped over (without reading them).
3890 *
Chris Mason3f157a22008-06-25 16:01:31 -04003891 * returns zero if something useful was found, < 0 on error and 1 if there
3892 * was nothing in the tree that matched the search criteria.
3893 */
3894int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003895 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003896 struct btrfs_path *path, int cache_only,
3897 u64 min_trans)
3898{
3899 struct extent_buffer *cur;
3900 struct btrfs_key found_key;
3901 int slot;
Yan96524802008-07-24 12:19:49 -04003902 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003903 u32 nritems;
3904 int level;
3905 int ret = 1;
3906
Chris Mason934d3752008-12-08 16:43:10 -05003907 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003908again:
Chris Masonbd681512011-07-16 15:23:14 -04003909 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003910 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003911 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003912 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003913 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003914
3915 if (btrfs_header_generation(cur) < min_trans) {
3916 ret = 1;
3917 goto out;
3918 }
Chris Masond3977122009-01-05 21:25:51 -05003919 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003920 nritems = btrfs_header_nritems(cur);
3921 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003922 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003923
Chris Mason323ac952008-10-01 19:05:46 -04003924 /* at the lowest level, we're done, setup the path and exit */
3925 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003926 if (slot >= nritems)
3927 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003928 ret = 0;
3929 path->slots[level] = slot;
3930 btrfs_item_key_to_cpu(cur, &found_key, slot);
3931 goto out;
3932 }
Yan96524802008-07-24 12:19:49 -04003933 if (sret && slot > 0)
3934 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003935 /*
3936 * check this node pointer against the cache_only and
3937 * min_trans parameters. If it isn't in cache or is too
3938 * old, skip to the next one.
3939 */
Chris Masond3977122009-01-05 21:25:51 -05003940 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003941 u64 blockptr;
3942 u64 gen;
3943 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003944 struct btrfs_disk_key disk_key;
3945
Chris Mason3f157a22008-06-25 16:01:31 -04003946 blockptr = btrfs_node_blockptr(cur, slot);
3947 gen = btrfs_node_ptr_generation(cur, slot);
3948 if (gen < min_trans) {
3949 slot++;
3950 continue;
3951 }
3952 if (!cache_only)
3953 break;
3954
Chris Masone02119d2008-09-05 16:13:11 -04003955 if (max_key) {
3956 btrfs_node_key(cur, &disk_key, slot);
3957 if (comp_keys(&disk_key, max_key) >= 0) {
3958 ret = 1;
3959 goto out;
3960 }
3961 }
3962
Chris Mason3f157a22008-06-25 16:01:31 -04003963 tmp = btrfs_find_tree_block(root, blockptr,
3964 btrfs_level_size(root, level - 1));
3965
3966 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3967 free_extent_buffer(tmp);
3968 break;
3969 }
3970 if (tmp)
3971 free_extent_buffer(tmp);
3972 slot++;
3973 }
Chris Masone02119d2008-09-05 16:13:11 -04003974find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003975 /*
3976 * we didn't find a candidate key in this node, walk forward
3977 * and find another one
3978 */
3979 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003980 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003981 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003982 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003983 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003984 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003985 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003986 goto again;
3987 } else {
3988 goto out;
3989 }
3990 }
3991 /* save our key for returning back */
3992 btrfs_node_key_to_cpu(cur, &found_key, slot);
3993 path->slots[level] = slot;
3994 if (level == path->lowest_level) {
3995 ret = 0;
3996 unlock_up(path, level, 1);
3997 goto out;
3998 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003999 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004000 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004001 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004002
Chris Masonbd681512011-07-16 15:23:14 -04004003 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004004
Chris Masonbd681512011-07-16 15:23:14 -04004005 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004006 path->nodes[level - 1] = cur;
4007 unlock_up(path, level, 1);
Chris Masonbd681512011-07-16 15:23:14 -04004008 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004009 }
4010out:
4011 if (ret == 0)
4012 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004013 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004014 return ret;
4015}
4016
4017/*
4018 * this is similar to btrfs_next_leaf, but does not try to preserve
4019 * and fixup the path. It looks for and returns the next key in the
4020 * tree based on the current path and the cache_only and min_trans
4021 * parameters.
4022 *
4023 * 0 is returned if another key is found, < 0 if there are any errors
4024 * and 1 is returned if there are no higher keys in the tree
4025 *
4026 * path->keep_locks should be set to 1 on the search made before
4027 * calling this function.
4028 */
Chris Masone7a84562008-06-25 16:01:31 -04004029int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004030 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004031 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004032{
Chris Masone7a84562008-06-25 16:01:31 -04004033 int slot;
4034 struct extent_buffer *c;
4035
Chris Mason934d3752008-12-08 16:43:10 -05004036 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004037 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004038 if (!path->nodes[level])
4039 return 1;
4040
4041 slot = path->slots[level] + 1;
4042 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004043next:
Chris Masone7a84562008-06-25 16:01:31 -04004044 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004045 int ret;
4046 int orig_lowest;
4047 struct btrfs_key cur_key;
4048 if (level + 1 >= BTRFS_MAX_LEVEL ||
4049 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004050 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004051
4052 if (path->locks[level + 1]) {
4053 level++;
4054 continue;
4055 }
4056
4057 slot = btrfs_header_nritems(c) - 1;
4058 if (level == 0)
4059 btrfs_item_key_to_cpu(c, &cur_key, slot);
4060 else
4061 btrfs_node_key_to_cpu(c, &cur_key, slot);
4062
4063 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004064 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004065 path->lowest_level = level;
4066 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4067 0, 0);
4068 path->lowest_level = orig_lowest;
4069 if (ret < 0)
4070 return ret;
4071
4072 c = path->nodes[level];
4073 slot = path->slots[level];
4074 if (ret == 0)
4075 slot++;
4076 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004077 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004078
Chris Masone7a84562008-06-25 16:01:31 -04004079 if (level == 0)
4080 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004081 else {
4082 u64 blockptr = btrfs_node_blockptr(c, slot);
4083 u64 gen = btrfs_node_ptr_generation(c, slot);
4084
4085 if (cache_only) {
4086 struct extent_buffer *cur;
4087 cur = btrfs_find_tree_block(root, blockptr,
4088 btrfs_level_size(root, level - 1));
4089 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4090 slot++;
4091 if (cur)
4092 free_extent_buffer(cur);
4093 goto next;
4094 }
4095 free_extent_buffer(cur);
4096 }
4097 if (gen < min_trans) {
4098 slot++;
4099 goto next;
4100 }
Chris Masone7a84562008-06-25 16:01:31 -04004101 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004102 }
Chris Masone7a84562008-06-25 16:01:31 -04004103 return 0;
4104 }
4105 return 1;
4106}
4107
Chris Mason7bb86312007-12-11 09:25:06 -05004108/*
Chris Mason925baed2008-06-25 16:01:30 -04004109 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004110 * returns 0 if it found something or 1 if there are no greater leaves.
4111 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004112 */
Chris Mason234b63a2007-03-13 10:46:10 -04004113int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004114{
4115 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004116 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004117 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004118 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004119 struct btrfs_key key;
4120 u32 nritems;
4121 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004122 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004123 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004124
4125 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004126 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004127 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004128
Chris Mason8e73f272009-04-03 10:14:18 -04004129 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4130again:
4131 level = 1;
4132 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004133 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004134 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004135
Chris Masona2135012008-06-25 16:01:30 -04004136 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004137 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004138
Chris Mason925baed2008-06-25 16:01:30 -04004139 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4140 path->keep_locks = 0;
4141
4142 if (ret < 0)
4143 return ret;
4144
Chris Masona2135012008-06-25 16:01:30 -04004145 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004146 /*
4147 * by releasing the path above we dropped all our locks. A balance
4148 * could have added more items next to the key that used to be
4149 * at the very end of the block. So, check again here and
4150 * advance the path if there are now more items available.
4151 */
Chris Masona2135012008-06-25 16:01:30 -04004152 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004153 if (ret == 0)
4154 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004155 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004156 goto done;
4157 }
Chris Masond97e63b2007-02-20 16:40:44 -05004158
Chris Masond3977122009-01-05 21:25:51 -05004159 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004160 if (!path->nodes[level]) {
4161 ret = 1;
4162 goto done;
4163 }
Chris Mason5f39d392007-10-15 16:14:19 -04004164
Chris Masond97e63b2007-02-20 16:40:44 -05004165 slot = path->slots[level] + 1;
4166 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004167 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004168 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004169 if (level == BTRFS_MAX_LEVEL) {
4170 ret = 1;
4171 goto done;
4172 }
Chris Masond97e63b2007-02-20 16:40:44 -05004173 continue;
4174 }
Chris Mason5f39d392007-10-15 16:14:19 -04004175
Chris Mason925baed2008-06-25 16:01:30 -04004176 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004177 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004178 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004179 }
Chris Mason5f39d392007-10-15 16:14:19 -04004180
Chris Mason8e73f272009-04-03 10:14:18 -04004181 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004182 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004183 ret = read_block_for_search(NULL, root, path, &next, level,
4184 slot, &key);
4185 if (ret == -EAGAIN)
4186 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004187
Chris Mason76a05b32009-05-14 13:24:30 -04004188 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004189 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004190 goto done;
4191 }
4192
Chris Mason5cd57b22008-06-25 16:01:30 -04004193 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004194 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004195 if (!ret) {
4196 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004197 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004198 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004199 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004200 }
Chris Mason31533fb2011-07-26 16:01:59 -04004201 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004202 }
Chris Masond97e63b2007-02-20 16:40:44 -05004203 break;
4204 }
4205 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004206 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004207 level--;
4208 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004209 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004210 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004211
Chris Mason5f39d392007-10-15 16:14:19 -04004212 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004213 path->nodes[level] = next;
4214 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004215 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004216 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004217 if (!level)
4218 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004219
Chris Mason8e73f272009-04-03 10:14:18 -04004220 ret = read_block_for_search(NULL, root, path, &next, level,
4221 0, &key);
4222 if (ret == -EAGAIN)
4223 goto again;
4224
Chris Mason76a05b32009-05-14 13:24:30 -04004225 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004226 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004227 goto done;
4228 }
4229
Chris Mason5cd57b22008-06-25 16:01:30 -04004230 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004231 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004232 if (!ret) {
4233 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004234 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004235 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004236 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004237 }
Chris Mason31533fb2011-07-26 16:01:59 -04004238 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004239 }
Chris Masond97e63b2007-02-20 16:40:44 -05004240 }
Chris Mason8e73f272009-04-03 10:14:18 -04004241 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004242done:
4243 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004244 path->leave_spinning = old_spinning;
4245 if (!old_spinning)
4246 btrfs_set_path_blocking(path);
4247
4248 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004249}
Chris Mason0b86a832008-03-24 15:01:56 -04004250
Chris Mason3f157a22008-06-25 16:01:31 -04004251/*
4252 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4253 * searching until it gets past min_objectid or finds an item of 'type'
4254 *
4255 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4256 */
Chris Mason0b86a832008-03-24 15:01:56 -04004257int btrfs_previous_item(struct btrfs_root *root,
4258 struct btrfs_path *path, u64 min_objectid,
4259 int type)
4260{
4261 struct btrfs_key found_key;
4262 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004263 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004264 int ret;
4265
Chris Masond3977122009-01-05 21:25:51 -05004266 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004267 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004268 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004269 ret = btrfs_prev_leaf(root, path);
4270 if (ret != 0)
4271 return ret;
4272 } else {
4273 path->slots[0]--;
4274 }
4275 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004276 nritems = btrfs_header_nritems(leaf);
4277 if (nritems == 0)
4278 return 1;
4279 if (path->slots[0] == nritems)
4280 path->slots[0]--;
4281
Chris Mason0b86a832008-03-24 15:01:56 -04004282 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004283 if (found_key.objectid < min_objectid)
4284 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004285 if (found_key.type == type)
4286 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004287 if (found_key.objectid == min_objectid &&
4288 found_key.type < type)
4289 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004290 }
4291 return 1;
4292}