blob: e2e43c07f6b16e3c43cf9c0ffb3857200c2302e2 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010039static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone089f052007-03-16 16:20:31 -040040 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040057 if (!p->nodes[i] || !p->locks[i])
58 continue;
59 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60 if (p->locks[i] == BTRFS_READ_LOCK)
61 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62 else if (p->locks[i] == BTRFS_WRITE_LOCK)
63 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050064 }
65}
66
67/*
68 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050069 *
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
73 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 */
Chris Mason4008c042009-02-12 14:09:45 -050075noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040076 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050077{
78 int i;
Chris Mason4008c042009-02-12 14:09:45 -050079
80#ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
85 * the path blocking.
86 */
Chris Masonbd681512011-07-16 15:23:14 -040087 if (held) {
88 btrfs_set_lock_blocking_rw(held, held_rw);
89 if (held_rw == BTRFS_WRITE_LOCK)
90 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91 else if (held_rw == BTRFS_READ_LOCK)
92 held_rw = BTRFS_READ_LOCK_BLOCKING;
93 }
Chris Mason4008c042009-02-12 14:09:45 -050094 btrfs_set_path_blocking(p);
95#endif
96
97 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040098 if (p->nodes[i] && p->locks[i]) {
99 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101 p->locks[i] = BTRFS_WRITE_LOCK;
102 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103 p->locks[i] = BTRFS_READ_LOCK;
104 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500105 }
Chris Mason4008c042009-02-12 14:09:45 -0500106
107#ifdef CONFIG_DEBUG_LOCK_ALLOC
108 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400109 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500110#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400114void btrfs_free_path(struct btrfs_path *p)
115{
Jesper Juhlff175d52010-12-25 21:22:30 +0000116 if (!p)
117 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200118 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400119 kmem_cache_free(btrfs_path_cachep, p);
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
125 *
126 * It is safe to call this on paths that no locks or extent buffers held.
127 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200128noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500129{
130 int i;
Chris Masona2135012008-06-25 16:01:30 -0400131
Chris Mason234b63a2007-03-13 10:46:10 -0400132 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400135 continue;
136 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400137 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400138 p->locks[i] = 0;
139 }
Chris Mason5f39d392007-10-15 16:14:19 -0400140 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400141 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 }
143}
144
Chris Masond352ac62008-09-29 15:18:18 -0400145/*
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
149 * looping required.
150 *
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400158
159 rcu_read_lock();
160 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400161 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400162 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400163 return eb;
164}
165
Chris Masond352ac62008-09-29 15:18:18 -0400166/* loop around taking references on and locking the root node of the
167 * tree until you end up with a lock on the root. A locked buffer
168 * is returned, with a reference held.
169 */
Chris Mason925baed2008-06-25 16:01:30 -0400170struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
171{
172 struct extent_buffer *eb;
173
Chris Masond3977122009-01-05 21:25:51 -0500174 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400175 eb = btrfs_root_node(root);
176 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400177 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400178 break;
Chris Mason925baed2008-06-25 16:01:30 -0400179 btrfs_tree_unlock(eb);
180 free_extent_buffer(eb);
181 }
182 return eb;
183}
184
Chris Masonbd681512011-07-16 15:23:14 -0400185/* loop around taking references on and locking the root node of the
186 * tree until you end up with a lock on the root. A locked buffer
187 * is returned, with a reference held.
188 */
189struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190{
191 struct extent_buffer *eb;
192
193 while (1) {
194 eb = btrfs_root_node(root);
195 btrfs_tree_read_lock(eb);
196 if (eb == root->node)
197 break;
198 btrfs_tree_read_unlock(eb);
199 free_extent_buffer(eb);
200 }
201 return eb;
202}
203
Chris Masond352ac62008-09-29 15:18:18 -0400204/* cowonly root (everything not a reference counted cow subvolume), just get
205 * put onto a simple dirty list. transaction.c walks this to make sure they
206 * get properly updated on disk.
207 */
Chris Mason0b86a832008-03-24 15:01:56 -0400208static void add_root_to_dirty_list(struct btrfs_root *root)
209{
210 if (root->track_dirty && list_empty(&root->dirty_list)) {
211 list_add(&root->dirty_list,
212 &root->fs_info->dirty_cowonly_roots);
213 }
214}
215
Chris Masond352ac62008-09-29 15:18:18 -0400216/*
217 * used by snapshot creation to make a copy of a root for a tree with
218 * a given objectid. The buffer with the new root node is returned in
219 * cow_ret, and this func returns zero on success or a negative error code.
220 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500221int btrfs_copy_root(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root,
223 struct extent_buffer *buf,
224 struct extent_buffer **cow_ret, u64 new_root_objectid)
225{
226 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 int ret = 0;
228 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400229 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500230
231 WARN_ON(root->ref_cows && trans->transid !=
232 root->fs_info->running_transaction->transid);
233 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
234
235 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400236 if (level == 0)
237 btrfs_item_key(buf, &disk_key, 0);
238 else
239 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400240
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400241 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
242 new_root_objectid, &disk_key, level,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200243 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400244 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 return PTR_ERR(cow);
246
247 copy_extent_buffer(cow, buf, 0, 0, cow->len);
248 btrfs_set_header_bytenr(cow, cow->start);
249 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400250 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
251 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
252 BTRFS_HEADER_FLAG_RELOC);
253 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
254 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
255 else
256 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500257
Yan Zheng2b820322008-11-17 21:11:30 -0500258 write_extent_buffer(cow, root->fs_info->fsid,
259 (unsigned long)btrfs_header_fsid(cow),
260 BTRFS_FSID_SIZE);
261
Chris Masonbe20aa92007-12-17 20:14:01 -0500262 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200264 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200266 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500267
Chris Masonbe20aa92007-12-17 20:14:01 -0500268 if (ret)
269 return ret;
270
271 btrfs_mark_buffer_dirty(cow);
272 *cow_ret = cow;
273 return 0;
274}
275
Chris Masond352ac62008-09-29 15:18:18 -0400276/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400277 * check if the tree block can be shared by multiple trees
278 */
279int btrfs_block_can_be_shared(struct btrfs_root *root,
280 struct extent_buffer *buf)
281{
282 /*
283 * Tree blocks not in refernece counted trees and tree roots
284 * are never shared. If a block was allocated after the last
285 * snapshot and the block was not allocated by tree relocation,
286 * we know the block is not shared.
287 */
288 if (root->ref_cows &&
289 buf != root->node && buf != root->commit_root &&
290 (btrfs_header_generation(buf) <=
291 btrfs_root_last_snapshot(&root->root_item) ||
292 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
293 return 1;
294#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
295 if (root->ref_cows &&
296 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
297 return 1;
298#endif
299 return 0;
300}
301
302static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
303 struct btrfs_root *root,
304 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400305 struct extent_buffer *cow,
306 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400307{
308 u64 refs;
309 u64 owner;
310 u64 flags;
311 u64 new_flags = 0;
312 int ret;
313
314 /*
315 * Backrefs update rules:
316 *
317 * Always use full backrefs for extent pointers in tree block
318 * allocated by tree relocation.
319 *
320 * If a shared tree block is no longer referenced by its owner
321 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
322 * use full backrefs for extent pointers in tree block.
323 *
324 * If a tree block is been relocating
325 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
326 * use full backrefs for extent pointers in tree block.
327 * The reason for this is some operations (such as drop tree)
328 * are only allowed for blocks use full backrefs.
329 */
330
331 if (btrfs_block_can_be_shared(root, buf)) {
332 ret = btrfs_lookup_extent_info(trans, root, buf->start,
333 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700334 if (ret)
335 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400336 BUG_ON(refs == 0);
337 } else {
338 refs = 1;
339 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
340 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
341 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
342 else
343 flags = 0;
344 }
345
346 owner = btrfs_header_owner(buf);
347 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
348 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
349
350 if (refs > 1) {
351 if ((owner == root->root_key.objectid ||
352 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
353 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200354 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400355 BUG_ON(ret);
356
357 if (root->root_key.objectid ==
358 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200359 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200361 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400362 BUG_ON(ret);
363 }
364 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
365 } else {
366
367 if (root->root_key.objectid ==
368 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200369 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200371 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400372 BUG_ON(ret);
373 }
374 if (new_flags != 0) {
375 ret = btrfs_set_disk_extent_flags(trans, root,
376 buf->start,
377 buf->len,
378 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700379 if (ret)
380 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400381 }
382 } else {
383 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
384 if (root->root_key.objectid ==
385 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200386 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200388 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200390 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400391 BUG_ON(ret);
392 }
393 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400394 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400395 }
396 return 0;
397}
398
399/*
Chris Masond3977122009-01-05 21:25:51 -0500400 * does the dirty work in cow of a single block. The parent block (if
401 * supplied) is updated to point to the new cow copy. The new buffer is marked
402 * dirty and returned locked. If you modify the block it needs to be marked
403 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400404 *
405 * search_start -- an allocation hint for the new block
406 *
Chris Masond3977122009-01-05 21:25:51 -0500407 * empty_size -- a hint that you plan on doing more cow. This is the size in
408 * bytes the allocator should try to find free next to the block it returns.
409 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400410 */
Chris Masond3977122009-01-05 21:25:51 -0500411static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400412 struct btrfs_root *root,
413 struct extent_buffer *buf,
414 struct extent_buffer *parent, int parent_slot,
415 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400416 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400417{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400418 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400419 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700420 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400421 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400422 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400423 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400424
Chris Mason925baed2008-06-25 16:01:30 -0400425 if (*cow_ret == buf)
426 unlock_orig = 1;
427
Chris Masonb9447ef82009-03-09 11:45:38 -0400428 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400429
Chris Mason7bb86312007-12-11 09:25:06 -0500430 WARN_ON(root->ref_cows && trans->transid !=
431 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400432 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400433
Chris Mason7bb86312007-12-11 09:25:06 -0500434 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400435
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 if (level == 0)
437 btrfs_item_key(buf, &disk_key, 0);
438 else
439 btrfs_node_key(buf, &disk_key, 0);
440
441 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
442 if (parent)
443 parent_start = parent->start;
444 else
445 parent_start = 0;
446 } else
447 parent_start = 0;
448
449 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
450 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200451 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400452 if (IS_ERR(cow))
453 return PTR_ERR(cow);
454
Chris Masonb4ce94d2009-02-04 09:25:08 -0500455 /* cow is set to blocking by btrfs_init_new_buffer */
456
Chris Mason5f39d392007-10-15 16:14:19 -0400457 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400458 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400459 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400460 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
461 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
462 BTRFS_HEADER_FLAG_RELOC);
463 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
464 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
465 else
466 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400467
Yan Zheng2b820322008-11-17 21:11:30 -0500468 write_extent_buffer(cow, root->fs_info->fsid,
469 (unsigned long)btrfs_header_fsid(cow),
470 BTRFS_FSID_SIZE);
471
Mark Fashehbe1a5562011-08-08 13:20:18 -0700472 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
473 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400474
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400475 if (root->ref_cows)
476 btrfs_reloc_cow_block(trans, root, buf, cow);
477
Chris Mason6702ed42007-08-07 16:15:09 -0400478 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400479 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400480 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
481 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
482 parent_start = buf->start;
483 else
484 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400485
Chris Mason5f39d392007-10-15 16:14:19 -0400486 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400487 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400488
Yan, Zhengf0486c62010-05-16 10:46:25 -0400489 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200490 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400491 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400492 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400493 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400494 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
495 parent_start = parent->start;
496 else
497 parent_start = 0;
498
499 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400500 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400501 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500502 btrfs_set_node_ptr_generation(parent, parent_slot,
503 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400504 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400505 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200506 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400507 }
Chris Mason925baed2008-06-25 16:01:30 -0400508 if (unlock_orig)
509 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400510 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400511 btrfs_mark_buffer_dirty(cow);
512 *cow_ret = cow;
513 return 0;
514}
515
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516static inline int should_cow_block(struct btrfs_trans_handle *trans,
517 struct btrfs_root *root,
518 struct extent_buffer *buf)
519{
Liu Bof1ebcc72011-11-14 20:48:06 -0500520 /* ensure we can see the force_cow */
521 smp_rmb();
522
523 /*
524 * We do not need to cow a block if
525 * 1) this block is not created or changed in this transaction;
526 * 2) this block does not belong to TREE_RELOC tree;
527 * 3) the root is not forced COW.
528 *
529 * What is forced COW:
530 * when we create snapshot during commiting the transaction,
531 * after we've finished coping src root, we must COW the shared
532 * block to ensure the metadata consistency.
533 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400534 if (btrfs_header_generation(buf) == trans->transid &&
535 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
536 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500537 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
538 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400539 return 0;
540 return 1;
541}
542
Chris Masond352ac62008-09-29 15:18:18 -0400543/*
544 * cows a single block, see __btrfs_cow_block for the real work.
545 * This version of it has extra checks so that a block isn't cow'd more than
546 * once per transaction, as long as it hasn't been written yet
547 */
Chris Masond3977122009-01-05 21:25:51 -0500548noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400549 struct btrfs_root *root, struct extent_buffer *buf,
550 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400551 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500552{
Chris Mason6702ed42007-08-07 16:15:09 -0400553 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400554 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500555
Chris Masonccd467d2007-06-28 15:57:36 -0400556 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500557 printk(KERN_CRIT "trans %llu running %llu\n",
558 (unsigned long long)trans->transid,
559 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400560 root->fs_info->running_transaction->transid);
561 WARN_ON(1);
562 }
563 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500564 printk(KERN_CRIT "trans %llu running %llu\n",
565 (unsigned long long)trans->transid,
566 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400567 WARN_ON(1);
568 }
Chris Masondc17ff82008-01-08 15:46:30 -0500569
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400570 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500571 *cow_ret = buf;
572 return 0;
573 }
Chris Masonc4876852009-02-04 09:24:25 -0500574
Chris Mason0b86a832008-03-24 15:01:56 -0400575 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500576
577 if (parent)
578 btrfs_set_lock_blocking(parent);
579 btrfs_set_lock_blocking(buf);
580
Chris Masonf510cfe2007-10-15 16:14:48 -0400581 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400582 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000583
584 trace_btrfs_cow_block(root, buf, *cow_ret);
585
Chris Masonf510cfe2007-10-15 16:14:48 -0400586 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400587}
588
Chris Masond352ac62008-09-29 15:18:18 -0400589/*
590 * helper function for defrag to decide if two blocks pointed to by a
591 * node are actually close by
592 */
Chris Mason6b800532007-10-15 16:17:34 -0400593static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400594{
Chris Mason6b800532007-10-15 16:17:34 -0400595 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400596 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400597 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400598 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500599 return 0;
600}
601
Chris Mason081e9572007-11-06 10:26:24 -0500602/*
603 * compare two keys in a memcmp fashion
604 */
605static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
606{
607 struct btrfs_key k1;
608
609 btrfs_disk_key_to_cpu(&k1, disk);
610
Diego Calleja20736ab2009-07-24 11:06:52 -0400611 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500612}
613
Josef Bacikf3465ca2008-11-12 14:19:50 -0500614/*
615 * same as comp_keys only with two btrfs_key's
616 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400617int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500618{
619 if (k1->objectid > k2->objectid)
620 return 1;
621 if (k1->objectid < k2->objectid)
622 return -1;
623 if (k1->type > k2->type)
624 return 1;
625 if (k1->type < k2->type)
626 return -1;
627 if (k1->offset > k2->offset)
628 return 1;
629 if (k1->offset < k2->offset)
630 return -1;
631 return 0;
632}
Chris Mason081e9572007-11-06 10:26:24 -0500633
Chris Masond352ac62008-09-29 15:18:18 -0400634/*
635 * this is used by the defrag code to go through all the
636 * leaves pointed to by a node and reallocate them so that
637 * disk order is close to key order
638 */
Chris Mason6702ed42007-08-07 16:15:09 -0400639int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400640 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400641 int start_slot, int cache_only, u64 *last_ret,
642 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400643{
Chris Mason6b800532007-10-15 16:17:34 -0400644 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400645 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400646 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400647 u64 search_start = *last_ret;
648 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400649 u64 other;
650 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400651 int end_slot;
652 int i;
653 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400654 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400655 int uptodate;
656 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500657 int progress_passed = 0;
658 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400659
Chris Mason5708b952007-10-25 15:43:18 -0400660 parent_level = btrfs_header_level(parent);
661 if (cache_only && parent_level != 1)
662 return 0;
663
Chris Masond3977122009-01-05 21:25:51 -0500664 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400665 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500666 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400667 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400668
Chris Mason6b800532007-10-15 16:17:34 -0400669 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400670 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400671 end_slot = parent_nritems;
672
673 if (parent_nritems == 1)
674 return 0;
675
Chris Masonb4ce94d2009-02-04 09:25:08 -0500676 btrfs_set_lock_blocking(parent);
677
Chris Mason6702ed42007-08-07 16:15:09 -0400678 for (i = start_slot; i < end_slot; i++) {
679 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400680
Chris Mason081e9572007-11-06 10:26:24 -0500681 btrfs_node_key(parent, &disk_key, i);
682 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
683 continue;
684
685 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400686 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400687 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400688 if (last_block == 0)
689 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400690
Chris Mason6702ed42007-08-07 16:15:09 -0400691 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400692 other = btrfs_node_blockptr(parent, i - 1);
693 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400694 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400695 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400696 other = btrfs_node_blockptr(parent, i + 1);
697 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400698 }
Chris Masone9d0b132007-08-10 14:06:19 -0400699 if (close) {
700 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400701 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400702 }
Chris Mason6702ed42007-08-07 16:15:09 -0400703
Chris Mason6b800532007-10-15 16:17:34 -0400704 cur = btrfs_find_tree_block(root, blocknr, blocksize);
705 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400706 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400707 else
708 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400709 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400710 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400711 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400712 continue;
713 }
Chris Mason6b800532007-10-15 16:17:34 -0400714 if (!cur) {
715 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400716 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000717 if (!cur)
718 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400719 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400720 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400721 }
Chris Mason6702ed42007-08-07 16:15:09 -0400722 }
Chris Masone9d0b132007-08-10 14:06:19 -0400723 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400724 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400725
Chris Masone7a84562008-06-25 16:01:31 -0400726 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500727 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400728 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400729 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400730 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400731 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400732 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400733 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400734 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400735 break;
Yan252c38f2007-08-29 09:11:44 -0400736 }
Chris Masone7a84562008-06-25 16:01:31 -0400737 search_start = cur->start;
738 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400739 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400740 btrfs_tree_unlock(cur);
741 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400742 }
743 return err;
744}
745
Chris Mason74123bd2007-02-02 11:05:29 -0500746/*
747 * The leaf data grows from end-to-front in the node.
748 * this returns the address of the start of the last item,
749 * which is the stop of the leaf data stack
750 */
Chris Mason123abc82007-03-14 14:14:43 -0400751static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400752 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500753{
Chris Mason5f39d392007-10-15 16:14:19 -0400754 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500755 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400756 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400757 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500758}
759
Chris Masonaa5d6be2007-02-28 16:35:06 -0500760
Chris Mason74123bd2007-02-02 11:05:29 -0500761/*
Chris Mason5f39d392007-10-15 16:14:19 -0400762 * search for key in the extent_buffer. The items start at offset p,
763 * and they are item_size apart. There are 'max' items in p.
764 *
Chris Mason74123bd2007-02-02 11:05:29 -0500765 * the slot in the array is returned via slot, and it points to
766 * the place where you would insert key if it is not found in
767 * the array.
768 *
769 * slot may point to max if the key is bigger than all of the keys
770 */
Chris Masone02119d2008-09-05 16:13:11 -0400771static noinline int generic_bin_search(struct extent_buffer *eb,
772 unsigned long p,
773 int item_size, struct btrfs_key *key,
774 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775{
776 int low = 0;
777 int high = max;
778 int mid;
779 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400780 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400781 struct btrfs_disk_key unaligned;
782 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400783 char *kaddr = NULL;
784 unsigned long map_start = 0;
785 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400786 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500787
Chris Masond3977122009-01-05 21:25:51 -0500788 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500789 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400790 offset = p + mid * item_size;
791
Chris Masona6591712011-07-19 12:04:14 -0400792 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400793 (offset + sizeof(struct btrfs_disk_key)) >
794 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500795
796 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400797 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400798 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400799
Chris Mason479965d2007-10-15 16:14:27 -0400800 if (!err) {
801 tmp = (struct btrfs_disk_key *)(kaddr + offset -
802 map_start);
803 } else {
804 read_extent_buffer(eb, &unaligned,
805 offset, sizeof(unaligned));
806 tmp = &unaligned;
807 }
808
Chris Mason5f39d392007-10-15 16:14:19 -0400809 } else {
810 tmp = (struct btrfs_disk_key *)(kaddr + offset -
811 map_start);
812 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500813 ret = comp_keys(tmp, key);
814
815 if (ret < 0)
816 low = mid + 1;
817 else if (ret > 0)
818 high = mid;
819 else {
820 *slot = mid;
821 return 0;
822 }
823 }
824 *slot = low;
825 return 1;
826}
827
Chris Mason97571fd2007-02-24 13:39:08 -0500828/*
829 * simple bin_search frontend that does the right thing for
830 * leaves vs nodes
831 */
Chris Mason5f39d392007-10-15 16:14:19 -0400832static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
833 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500834{
Chris Mason5f39d392007-10-15 16:14:19 -0400835 if (level == 0) {
836 return generic_bin_search(eb,
837 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400838 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400839 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400840 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500841 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400842 return generic_bin_search(eb,
843 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400844 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400845 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400846 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500847 }
848 return -1;
849}
850
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
852 int level, int *slot)
853{
854 return bin_search(eb, key, level, slot);
855}
856
Yan, Zhengf0486c62010-05-16 10:46:25 -0400857static void root_add_used(struct btrfs_root *root, u32 size)
858{
859 spin_lock(&root->accounting_lock);
860 btrfs_set_root_used(&root->root_item,
861 btrfs_root_used(&root->root_item) + size);
862 spin_unlock(&root->accounting_lock);
863}
864
865static void root_sub_used(struct btrfs_root *root, u32 size)
866{
867 spin_lock(&root->accounting_lock);
868 btrfs_set_root_used(&root->root_item,
869 btrfs_root_used(&root->root_item) - size);
870 spin_unlock(&root->accounting_lock);
871}
872
Chris Masond352ac62008-09-29 15:18:18 -0400873/* given a node and slot number, this reads the blocks it points to. The
874 * extent buffer is returned with a reference taken (but unlocked).
875 * NULL is returned on error.
876 */
Chris Masone02119d2008-09-05 16:13:11 -0400877static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400878 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500879{
Chris Masonca7a79a2008-05-12 12:59:19 -0400880 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500881 if (slot < 0)
882 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400883 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500884 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400885
886 BUG_ON(level == 0);
887
Chris Masondb945352007-10-15 16:15:53 -0400888 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400889 btrfs_level_size(root, level - 1),
890 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500891}
892
Chris Masond352ac62008-09-29 15:18:18 -0400893/*
894 * node level balancing, used to make sure nodes are in proper order for
895 * item deletion. We balance from the top down, so we have to make sure
896 * that a deletion won't leave an node completely empty later on.
897 */
Chris Masone02119d2008-09-05 16:13:11 -0400898static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500899 struct btrfs_root *root,
900 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500901{
Chris Mason5f39d392007-10-15 16:14:19 -0400902 struct extent_buffer *right = NULL;
903 struct extent_buffer *mid;
904 struct extent_buffer *left = NULL;
905 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500906 int ret = 0;
907 int wret;
908 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500909 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500910 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500911
912 if (level == 0)
913 return 0;
914
Chris Mason5f39d392007-10-15 16:14:19 -0400915 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500916
Chris Masonbd681512011-07-16 15:23:14 -0400917 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
918 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500919 WARN_ON(btrfs_header_generation(mid) != trans->transid);
920
Chris Mason1d4f8a02007-03-13 09:28:32 -0400921 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500922
Li Zefana05a9bb2011-09-06 16:55:34 +0800923 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400924 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800925 pslot = path->slots[level + 1];
926 }
Chris Masonbb803952007-03-01 12:04:21 -0500927
Chris Mason40689472007-03-17 14:29:23 -0400928 /*
929 * deal with the case where there is only one pointer in the root
930 * by promoting the node below to a root
931 */
Chris Mason5f39d392007-10-15 16:14:19 -0400932 if (!parent) {
933 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500934
Chris Mason5f39d392007-10-15 16:14:19 -0400935 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500936 return 0;
937
938 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500940 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400941 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500942 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400943 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400944 if (ret) {
945 btrfs_tree_unlock(child);
946 free_extent_buffer(child);
947 goto enospc;
948 }
Yan2f375ab2008-02-01 14:58:07 -0500949
Chris Mason240f62c2011-03-23 14:54:42 -0400950 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400951
Chris Mason0b86a832008-03-24 15:01:56 -0400952 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400953 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500954
Chris Mason925baed2008-06-25 16:01:30 -0400955 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500956 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400957 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400958 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500959 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400960 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400961
962 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200963 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500964 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400965 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400966 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500967 }
Chris Mason5f39d392007-10-15 16:14:19 -0400968 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400969 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500970 return 0;
971
Andi Kleen559af822010-10-29 15:14:37 -0400972 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400973
Chris Mason5f39d392007-10-15 16:14:19 -0400974 left = read_node_slot(root, parent, pslot - 1);
975 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400976 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500977 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400978 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400979 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400980 if (wret) {
981 ret = wret;
982 goto enospc;
983 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400984 }
Chris Mason5f39d392007-10-15 16:14:19 -0400985 right = read_node_slot(root, parent, pslot + 1);
986 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400987 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500988 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400989 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400990 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400991 if (wret) {
992 ret = wret;
993 goto enospc;
994 }
995 }
996
997 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400998 if (left) {
999 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001000 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001001 if (wret < 0)
1002 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001003 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001004 }
Chris Mason79f95c82007-03-01 15:16:26 -05001005
1006 /*
1007 * then try to empty the right most buffer into the middle
1008 */
Chris Mason5f39d392007-10-15 16:14:19 -04001009 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001010 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001011 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001012 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001014 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001015 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001016 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001017 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001018 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001019 free_extent_buffer(right);
1020 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001021 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001022 struct btrfs_disk_key right_key;
1023 btrfs_node_key(right, &right_key, 0);
1024 btrfs_set_node_key(parent, &right_key, pslot + 1);
1025 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001026 }
1027 }
Chris Mason5f39d392007-10-15 16:14:19 -04001028 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001029 /*
1030 * we're not allowed to leave a node with one item in the
1031 * tree during a delete. A deletion from lower in the tree
1032 * could try to delete the only pointer in this node.
1033 * So, pull some keys from the left.
1034 * There has to be a left pointer at this point because
1035 * otherwise we would have pulled some pointers from the
1036 * right
1037 */
Chris Mason5f39d392007-10-15 16:14:19 -04001038 BUG_ON(!left);
1039 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001040 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001041 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001042 goto enospc;
1043 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001044 if (wret == 1) {
1045 wret = push_node_left(trans, root, left, mid, 1);
1046 if (wret < 0)
1047 ret = wret;
1048 }
Chris Mason79f95c82007-03-01 15:16:26 -05001049 BUG_ON(wret == 1);
1050 }
Chris Mason5f39d392007-10-15 16:14:19 -04001051 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001052 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001053 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001054 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001055 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001056 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001057 free_extent_buffer(mid);
1058 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001059 } else {
1060 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001061 struct btrfs_disk_key mid_key;
1062 btrfs_node_key(mid, &mid_key, 0);
1063 btrfs_set_node_key(parent, &mid_key, pslot);
1064 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001065 }
Chris Masonbb803952007-03-01 12:04:21 -05001066
Chris Mason79f95c82007-03-01 15:16:26 -05001067 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001068 if (left) {
1069 if (btrfs_header_nritems(left) > orig_slot) {
1070 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001071 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001072 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001073 path->slots[level + 1] -= 1;
1074 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001075 if (mid) {
1076 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001077 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001078 }
Chris Masonbb803952007-03-01 12:04:21 -05001079 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001080 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001081 path->slots[level] = orig_slot;
1082 }
1083 }
Chris Mason79f95c82007-03-01 15:16:26 -05001084 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001085 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001086 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001087 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001088enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001089 if (right) {
1090 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001091 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001092 }
1093 if (left) {
1094 if (path->nodes[level] != left)
1095 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001096 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001097 }
Chris Masonbb803952007-03-01 12:04:21 -05001098 return ret;
1099}
1100
Chris Masond352ac62008-09-29 15:18:18 -04001101/* Node balancing for insertion. Here we only split or push nodes around
1102 * when they are completely full. This is also done top down, so we
1103 * have to be pessimistic.
1104 */
Chris Masond3977122009-01-05 21:25:51 -05001105static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001106 struct btrfs_root *root,
1107 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001108{
Chris Mason5f39d392007-10-15 16:14:19 -04001109 struct extent_buffer *right = NULL;
1110 struct extent_buffer *mid;
1111 struct extent_buffer *left = NULL;
1112 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001113 int ret = 0;
1114 int wret;
1115 int pslot;
1116 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001117
1118 if (level == 0)
1119 return 1;
1120
Chris Mason5f39d392007-10-15 16:14:19 -04001121 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001122 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001123
Li Zefana05a9bb2011-09-06 16:55:34 +08001124 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001125 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001126 pslot = path->slots[level + 1];
1127 }
Chris Masone66f7092007-04-20 13:16:02 -04001128
Chris Mason5f39d392007-10-15 16:14:19 -04001129 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001130 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001131
Chris Mason5f39d392007-10-15 16:14:19 -04001132 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001133
1134 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001135 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001136 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001137
1138 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001139 btrfs_set_lock_blocking(left);
1140
Chris Mason5f39d392007-10-15 16:14:19 -04001141 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001142 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1143 wret = 1;
1144 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001145 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001146 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001147 if (ret)
1148 wret = 1;
1149 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001150 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001151 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001152 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001153 }
Chris Masone66f7092007-04-20 13:16:02 -04001154 if (wret < 0)
1155 ret = wret;
1156 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001157 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001158 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001159 btrfs_node_key(mid, &disk_key, 0);
1160 btrfs_set_node_key(parent, &disk_key, pslot);
1161 btrfs_mark_buffer_dirty(parent);
1162 if (btrfs_header_nritems(left) > orig_slot) {
1163 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001164 path->slots[level + 1] -= 1;
1165 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001166 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001167 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001168 } else {
1169 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001170 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001171 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001172 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001173 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001174 }
Chris Masone66f7092007-04-20 13:16:02 -04001175 return 0;
1176 }
Chris Mason925baed2008-06-25 16:01:30 -04001177 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001178 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001179 }
Chris Mason925baed2008-06-25 16:01:30 -04001180 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001181
1182 /*
1183 * then try to empty the right most buffer into the middle
1184 */
Chris Mason5f39d392007-10-15 16:14:19 -04001185 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001186 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001187
Chris Mason925baed2008-06-25 16:01:30 -04001188 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001189 btrfs_set_lock_blocking(right);
1190
Chris Mason5f39d392007-10-15 16:14:19 -04001191 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001192 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1193 wret = 1;
1194 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001195 ret = btrfs_cow_block(trans, root, right,
1196 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001197 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001198 if (ret)
1199 wret = 1;
1200 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001201 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001202 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001203 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001204 }
Chris Masone66f7092007-04-20 13:16:02 -04001205 if (wret < 0)
1206 ret = wret;
1207 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001208 struct btrfs_disk_key disk_key;
1209
1210 btrfs_node_key(right, &disk_key, 0);
1211 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1212 btrfs_mark_buffer_dirty(parent);
1213
1214 if (btrfs_header_nritems(mid) <= orig_slot) {
1215 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001216 path->slots[level + 1] += 1;
1217 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001218 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001219 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001220 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001221 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001222 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001223 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001224 }
Chris Masone66f7092007-04-20 13:16:02 -04001225 return 0;
1226 }
Chris Mason925baed2008-06-25 16:01:30 -04001227 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001228 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001229 }
Chris Masone66f7092007-04-20 13:16:02 -04001230 return 1;
1231}
1232
Chris Mason74123bd2007-02-02 11:05:29 -05001233/*
Chris Masond352ac62008-09-29 15:18:18 -04001234 * readahead one full node of leaves, finding things that are close
1235 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001236 */
Chris Masonc8c42862009-04-03 10:14:18 -04001237static void reada_for_search(struct btrfs_root *root,
1238 struct btrfs_path *path,
1239 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001240{
Chris Mason5f39d392007-10-15 16:14:19 -04001241 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001242 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001243 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001244 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001245 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001246 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001247 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001248 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001249 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001250 u32 nr;
1251 u32 blocksize;
1252 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001253
Chris Masona6b6e752007-10-15 16:22:39 -04001254 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001255 return;
1256
Chris Mason6702ed42007-08-07 16:15:09 -04001257 if (!path->nodes[level])
1258 return;
1259
Chris Mason5f39d392007-10-15 16:14:19 -04001260 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001261
Chris Mason3c69fae2007-08-07 15:52:22 -04001262 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001263 blocksize = btrfs_level_size(root, level - 1);
1264 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001265 if (eb) {
1266 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 return;
1268 }
1269
Chris Masona7175312009-01-22 09:23:10 -05001270 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001271
Chris Mason5f39d392007-10-15 16:14:19 -04001272 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001273 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001274
Chris Masond3977122009-01-05 21:25:51 -05001275 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001276 if (direction < 0) {
1277 if (nr == 0)
1278 break;
1279 nr--;
1280 } else if (direction > 0) {
1281 nr++;
1282 if (nr >= nritems)
1283 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001284 }
Chris Mason01f46652007-12-21 16:24:26 -05001285 if (path->reada < 0 && objectid) {
1286 btrfs_node_key(node, &disk_key, nr);
1287 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1288 break;
1289 }
Chris Mason6b800532007-10-15 16:17:34 -04001290 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001291 if ((search <= target && target - search <= 65536) ||
1292 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001293 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001294 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001295 nread += blocksize;
1296 }
1297 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001298 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001299 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001300 }
1301}
Chris Mason925baed2008-06-25 16:01:30 -04001302
Chris Masond352ac62008-09-29 15:18:18 -04001303/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001304 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1305 * cache
1306 */
1307static noinline int reada_for_balance(struct btrfs_root *root,
1308 struct btrfs_path *path, int level)
1309{
1310 int slot;
1311 int nritems;
1312 struct extent_buffer *parent;
1313 struct extent_buffer *eb;
1314 u64 gen;
1315 u64 block1 = 0;
1316 u64 block2 = 0;
1317 int ret = 0;
1318 int blocksize;
1319
Chris Mason8c594ea2009-04-20 15:50:10 -04001320 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001321 if (!parent)
1322 return 0;
1323
1324 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001325 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001326 blocksize = btrfs_level_size(root, level);
1327
1328 if (slot > 0) {
1329 block1 = btrfs_node_blockptr(parent, slot - 1);
1330 gen = btrfs_node_ptr_generation(parent, slot - 1);
1331 eb = btrfs_find_tree_block(root, block1, blocksize);
1332 if (eb && btrfs_buffer_uptodate(eb, gen))
1333 block1 = 0;
1334 free_extent_buffer(eb);
1335 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001336 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001337 block2 = btrfs_node_blockptr(parent, slot + 1);
1338 gen = btrfs_node_ptr_generation(parent, slot + 1);
1339 eb = btrfs_find_tree_block(root, block2, blocksize);
1340 if (eb && btrfs_buffer_uptodate(eb, gen))
1341 block2 = 0;
1342 free_extent_buffer(eb);
1343 }
1344 if (block1 || block2) {
1345 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001346
1347 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001348 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001349
1350 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001351 if (block1)
1352 readahead_tree_block(root, block1, blocksize, 0);
1353 if (block2)
1354 readahead_tree_block(root, block2, blocksize, 0);
1355
1356 if (block1) {
1357 eb = read_tree_block(root, block1, blocksize, 0);
1358 free_extent_buffer(eb);
1359 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001360 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001361 eb = read_tree_block(root, block2, blocksize, 0);
1362 free_extent_buffer(eb);
1363 }
1364 }
1365 return ret;
1366}
1367
1368
1369/*
Chris Masond3977122009-01-05 21:25:51 -05001370 * when we walk down the tree, it is usually safe to unlock the higher layers
1371 * in the tree. The exceptions are when our path goes through slot 0, because
1372 * operations on the tree might require changing key pointers higher up in the
1373 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001374 *
Chris Masond3977122009-01-05 21:25:51 -05001375 * callers might also have set path->keep_locks, which tells this code to keep
1376 * the lock if the path points to the last slot in the block. This is part of
1377 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001378 *
Chris Masond3977122009-01-05 21:25:51 -05001379 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1380 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001381 */
Chris Masone02119d2008-09-05 16:13:11 -04001382static noinline void unlock_up(struct btrfs_path *path, int level,
1383 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001384{
1385 int i;
1386 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001387 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001388 struct extent_buffer *t;
1389
1390 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1391 if (!path->nodes[i])
1392 break;
1393 if (!path->locks[i])
1394 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001395 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001396 skip_level = i + 1;
1397 continue;
1398 }
Chris Mason051e1b92008-06-25 16:01:30 -04001399 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001400 u32 nritems;
1401 t = path->nodes[i];
1402 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001404 skip_level = i + 1;
1405 continue;
1406 }
1407 }
Chris Mason051e1b92008-06-25 16:01:30 -04001408 if (skip_level < i && i >= lowest_unlock)
1409 no_skips = 1;
1410
Chris Mason925baed2008-06-25 16:01:30 -04001411 t = path->nodes[i];
1412 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001413 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001414 path->locks[i] = 0;
1415 }
1416 }
1417}
1418
Chris Mason3c69fae2007-08-07 15:52:22 -04001419/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001420 * This releases any locks held in the path starting at level and
1421 * going all the way up to the root.
1422 *
1423 * btrfs_search_slot will keep the lock held on higher nodes in a few
1424 * corner cases, such as COW of the block at slot zero in the node. This
1425 * ignores those rules, and it should only be called when there are no
1426 * more updates to be done higher up in the tree.
1427 */
1428noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1429{
1430 int i;
1431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001432 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001433 return;
1434
1435 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1436 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001437 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001438 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001439 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001440 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001441 path->locks[i] = 0;
1442 }
1443}
1444
1445/*
Chris Masonc8c42862009-04-03 10:14:18 -04001446 * helper function for btrfs_search_slot. The goal is to find a block
1447 * in cache without setting the path to blocking. If we find the block
1448 * we return zero and the path is unchanged.
1449 *
1450 * If we can't find the block, we set the path blocking and do some
1451 * reada. -EAGAIN is returned and the search must be repeated.
1452 */
1453static int
1454read_block_for_search(struct btrfs_trans_handle *trans,
1455 struct btrfs_root *root, struct btrfs_path *p,
1456 struct extent_buffer **eb_ret, int level, int slot,
1457 struct btrfs_key *key)
1458{
1459 u64 blocknr;
1460 u64 gen;
1461 u32 blocksize;
1462 struct extent_buffer *b = *eb_ret;
1463 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001464 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001465
1466 blocknr = btrfs_node_blockptr(b, slot);
1467 gen = btrfs_node_ptr_generation(b, slot);
1468 blocksize = btrfs_level_size(root, level - 1);
1469
1470 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001471 if (tmp) {
1472 if (btrfs_buffer_uptodate(tmp, 0)) {
1473 if (btrfs_buffer_uptodate(tmp, gen)) {
1474 /*
1475 * we found an up to date block without
1476 * sleeping, return
1477 * right away
1478 */
1479 *eb_ret = tmp;
1480 return 0;
1481 }
1482 /* the pages were up to date, but we failed
1483 * the generation number check. Do a full
1484 * read for the generation number that is correct.
1485 * We must do this without dropping locks so
1486 * we can trust our generation number
1487 */
1488 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001489 btrfs_set_path_blocking(p);
1490
Chris Masoncb449212010-10-24 11:01:27 -04001491 tmp = read_tree_block(root, blocknr, blocksize, gen);
1492 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1493 *eb_ret = tmp;
1494 return 0;
1495 }
1496 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001497 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001498 return -EIO;
1499 }
Chris Masonc8c42862009-04-03 10:14:18 -04001500 }
1501
1502 /*
1503 * reduce lock contention at high levels
1504 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001505 * we read. Don't release the lock on the current
1506 * level because we need to walk this node to figure
1507 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001508 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001509 btrfs_unlock_up_safe(p, level + 1);
1510 btrfs_set_path_blocking(p);
1511
Chris Masoncb449212010-10-24 11:01:27 -04001512 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001513 if (p->reada)
1514 reada_for_search(root, p, level, slot, key->objectid);
1515
David Sterbab3b4aa72011-04-21 01:20:15 +02001516 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001517
1518 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001519 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001520 if (tmp) {
1521 /*
1522 * If the read above didn't mark this buffer up to date,
1523 * it will never end up being up to date. Set ret to EIO now
1524 * and give up so that our caller doesn't loop forever
1525 * on our EAGAINs.
1526 */
1527 if (!btrfs_buffer_uptodate(tmp, 0))
1528 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001529 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001530 }
1531 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001532}
1533
1534/*
1535 * helper function for btrfs_search_slot. This does all of the checks
1536 * for node-level blocks and does any balancing required based on
1537 * the ins_len.
1538 *
1539 * If no extra work was required, zero is returned. If we had to
1540 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1541 * start over
1542 */
1543static int
1544setup_nodes_for_search(struct btrfs_trans_handle *trans,
1545 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001546 struct extent_buffer *b, int level, int ins_len,
1547 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001548{
1549 int ret;
1550 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1551 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1552 int sret;
1553
Chris Masonbd681512011-07-16 15:23:14 -04001554 if (*write_lock_level < level + 1) {
1555 *write_lock_level = level + 1;
1556 btrfs_release_path(p);
1557 goto again;
1558 }
1559
Chris Masonc8c42862009-04-03 10:14:18 -04001560 sret = reada_for_balance(root, p, level);
1561 if (sret)
1562 goto again;
1563
1564 btrfs_set_path_blocking(p);
1565 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001566 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001567
1568 BUG_ON(sret > 0);
1569 if (sret) {
1570 ret = sret;
1571 goto done;
1572 }
1573 b = p->nodes[level];
1574 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001575 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001576 int sret;
1577
Chris Masonbd681512011-07-16 15:23:14 -04001578 if (*write_lock_level < level + 1) {
1579 *write_lock_level = level + 1;
1580 btrfs_release_path(p);
1581 goto again;
1582 }
1583
Chris Masonc8c42862009-04-03 10:14:18 -04001584 sret = reada_for_balance(root, p, level);
1585 if (sret)
1586 goto again;
1587
1588 btrfs_set_path_blocking(p);
1589 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001590 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001591
1592 if (sret) {
1593 ret = sret;
1594 goto done;
1595 }
1596 b = p->nodes[level];
1597 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001598 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001599 goto again;
1600 }
1601 BUG_ON(btrfs_header_nritems(b) == 1);
1602 }
1603 return 0;
1604
1605again:
1606 ret = -EAGAIN;
1607done:
1608 return ret;
1609}
1610
1611/*
Chris Mason74123bd2007-02-02 11:05:29 -05001612 * look for key in the tree. path is filled in with nodes along the way
1613 * if key is found, we return zero and you can find the item in the leaf
1614 * level of the path (level 0)
1615 *
1616 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001617 * be inserted, and 1 is returned. If there are other errors during the
1618 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001619 *
1620 * if ins_len > 0, nodes and leaves will be split as we walk down the
1621 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1622 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001623 */
Chris Masone089f052007-03-16 16:20:31 -04001624int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1625 *root, struct btrfs_key *key, struct btrfs_path *p, int
1626 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001627{
Chris Mason5f39d392007-10-15 16:14:19 -04001628 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001629 int slot;
1630 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001631 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001632 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001633 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001634 int root_lock;
1635 /* everything at write_lock_level or lower must be write locked */
1636 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001637 u8 lowest_level = 0;
1638
Chris Mason6702ed42007-08-07 16:15:09 -04001639 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001640 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001641 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001642
Chris Masonbd681512011-07-16 15:23:14 -04001643 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001644 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001645
Chris Masonbd681512011-07-16 15:23:14 -04001646 /* when we are removing items, we might have to go up to level
1647 * two as we update tree pointers Make sure we keep write
1648 * for those levels as well
1649 */
1650 write_lock_level = 2;
1651 } else if (ins_len > 0) {
1652 /*
1653 * for inserting items, make sure we have a write lock on
1654 * level 1 so we can update keys
1655 */
1656 write_lock_level = 1;
1657 }
1658
1659 if (!cow)
1660 write_lock_level = -1;
1661
1662 if (cow && (p->keep_locks || p->lowest_level))
1663 write_lock_level = BTRFS_MAX_LEVEL;
1664
Chris Masonbb803952007-03-01 12:04:21 -05001665again:
Chris Masonbd681512011-07-16 15:23:14 -04001666 /*
1667 * we try very hard to do read locks on the root
1668 */
1669 root_lock = BTRFS_READ_LOCK;
1670 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001671 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001672 /*
1673 * the commit roots are read only
1674 * so we always do read locks
1675 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001676 b = root->commit_root;
1677 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001678 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001679 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001680 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001681 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001682 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001684 level = btrfs_header_level(b);
1685 } else {
1686 /* we don't know the level of the root node
1687 * until we actually have it read locked
1688 */
1689 b = btrfs_read_lock_root_node(root);
1690 level = btrfs_header_level(b);
1691 if (level <= write_lock_level) {
1692 /* whoops, must trade for write lock */
1693 btrfs_tree_read_unlock(b);
1694 free_extent_buffer(b);
1695 b = btrfs_lock_root_node(root);
1696 root_lock = BTRFS_WRITE_LOCK;
1697
1698 /* the level might have changed, check again */
1699 level = btrfs_header_level(b);
1700 }
1701 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702 }
Chris Masonbd681512011-07-16 15:23:14 -04001703 p->nodes[level] = b;
1704 if (!p->skip_locking)
1705 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001706
Chris Masoneb60cea2007-02-02 09:18:22 -05001707 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001708 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001709
1710 /*
1711 * setup the path here so we can release it under lock
1712 * contention with the cow code
1713 */
Chris Mason02217ed2007-03-02 16:08:05 -05001714 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001715 /*
1716 * if we don't really need to cow this block
1717 * then we don't want to set the path blocking,
1718 * so we test it here
1719 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001720 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001721 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001722
Chris Masonb4ce94d2009-02-04 09:25:08 -05001723 btrfs_set_path_blocking(p);
1724
Chris Masonbd681512011-07-16 15:23:14 -04001725 /*
1726 * must have write locks on this node and the
1727 * parent
1728 */
1729 if (level + 1 > write_lock_level) {
1730 write_lock_level = level + 1;
1731 btrfs_release_path(p);
1732 goto again;
1733 }
1734
Yan Zheng33c66f42009-07-22 09:59:00 -04001735 err = btrfs_cow_block(trans, root, b,
1736 p->nodes[level + 1],
1737 p->slots[level + 1], &b);
1738 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001739 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001740 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001741 }
Chris Mason02217ed2007-03-02 16:08:05 -05001742 }
Chris Mason65b51a02008-08-01 15:11:20 -04001743cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001744 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001745
Chris Masoneb60cea2007-02-02 09:18:22 -05001746 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001747 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001748
1749 /*
1750 * we have a lock on b and as long as we aren't changing
1751 * the tree, there is no way to for the items in b to change.
1752 * It is safe to drop the lock on our parent before we
1753 * go through the expensive btree search on b.
1754 *
1755 * If cow is true, then we might be changing slot zero,
1756 * which may require changing the parent. So, we can't
1757 * drop the lock until after we know which slot we're
1758 * operating on.
1759 */
1760 if (!cow)
1761 btrfs_unlock_up_safe(p, level + 1);
1762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001764
Chris Mason5f39d392007-10-15 16:14:19 -04001765 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001766 int dec = 0;
1767 if (ret && slot > 0) {
1768 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001770 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001771 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001772 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001773 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001774 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001775 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001776 if (err) {
1777 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001778 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001779 }
Chris Masonc8c42862009-04-03 10:14:18 -04001780 b = p->nodes[level];
1781 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001782
Chris Masonbd681512011-07-16 15:23:14 -04001783 /*
1784 * slot 0 is special, if we change the key
1785 * we have to update the parent pointer
1786 * which means we must have a write lock
1787 * on the parent
1788 */
1789 if (slot == 0 && cow &&
1790 write_lock_level < level + 1) {
1791 write_lock_level = level + 1;
1792 btrfs_release_path(p);
1793 goto again;
1794 }
1795
Chris Masonf9efa9c2008-06-25 16:14:04 -04001796 unlock_up(p, level, lowest_unlock);
1797
Chris Mason925baed2008-06-25 16:01:30 -04001798 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001799 if (dec)
1800 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001801 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001802 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001803
Yan Zheng33c66f42009-07-22 09:59:00 -04001804 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001805 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001806 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001807 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 if (err) {
1809 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001810 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001811 }
Chris Mason76a05b32009-05-14 13:24:30 -04001812
Chris Masonb4ce94d2009-02-04 09:25:08 -05001813 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001814 level = btrfs_header_level(b);
1815 if (level <= write_lock_level) {
1816 err = btrfs_try_tree_write_lock(b);
1817 if (!err) {
1818 btrfs_set_path_blocking(p);
1819 btrfs_tree_lock(b);
1820 btrfs_clear_path_blocking(p, b,
1821 BTRFS_WRITE_LOCK);
1822 }
1823 p->locks[level] = BTRFS_WRITE_LOCK;
1824 } else {
1825 err = btrfs_try_tree_read_lock(b);
1826 if (!err) {
1827 btrfs_set_path_blocking(p);
1828 btrfs_tree_read_lock(b);
1829 btrfs_clear_path_blocking(p, b,
1830 BTRFS_READ_LOCK);
1831 }
1832 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001833 }
Chris Masonbd681512011-07-16 15:23:14 -04001834 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001835 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836 } else {
1837 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001838 if (ins_len > 0 &&
1839 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001840 if (write_lock_level < 1) {
1841 write_lock_level = 1;
1842 btrfs_release_path(p);
1843 goto again;
1844 }
1845
Chris Masonb4ce94d2009-02-04 09:25:08 -05001846 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001847 err = split_leaf(trans, root, key,
1848 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001849 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001850
Yan Zheng33c66f42009-07-22 09:59:00 -04001851 BUG_ON(err > 0);
1852 if (err) {
1853 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001854 goto done;
1855 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001856 }
Chris Mason459931e2008-12-10 09:10:46 -05001857 if (!p->search_for_split)
1858 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001859 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001860 }
1861 }
Chris Mason65b51a02008-08-01 15:11:20 -04001862 ret = 1;
1863done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001864 /*
1865 * we don't really know what they plan on doing with the path
1866 * from here on, so for now just mark it as blocking
1867 */
Chris Masonb9473432009-03-13 11:00:37 -04001868 if (!p->leave_spinning)
1869 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001870 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001871 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001872 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001873}
1874
Chris Mason74123bd2007-02-02 11:05:29 -05001875/*
1876 * adjust the pointers going up the tree, starting at level
1877 * making sure the right key of each node is points to 'key'.
1878 * This is used after shifting pointers to the left, so it stops
1879 * fixing up pointers when a given leaf/node is not in slot 0 of the
1880 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001881 *
Chris Mason74123bd2007-02-02 11:05:29 -05001882 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001883static void fixup_low_keys(struct btrfs_trans_handle *trans,
1884 struct btrfs_root *root, struct btrfs_path *path,
1885 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001886{
1887 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001888 struct extent_buffer *t;
1889
Chris Mason234b63a2007-03-13 10:46:10 -04001890 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001892 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001893 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001894 t = path->nodes[i];
1895 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001896 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 if (tslot != 0)
1898 break;
1899 }
1900}
1901
Chris Mason74123bd2007-02-02 11:05:29 -05001902/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001903 * update item key.
1904 *
1905 * This function isn't completely safe. It's the caller's responsibility
1906 * that the new key won't break the order
1907 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001908void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1909 struct btrfs_root *root, struct btrfs_path *path,
1910 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001911{
1912 struct btrfs_disk_key disk_key;
1913 struct extent_buffer *eb;
1914 int slot;
1915
1916 eb = path->nodes[0];
1917 slot = path->slots[0];
1918 if (slot > 0) {
1919 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001920 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001921 }
1922 if (slot < btrfs_header_nritems(eb) - 1) {
1923 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001924 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001925 }
1926
1927 btrfs_cpu_key_to_disk(&disk_key, new_key);
1928 btrfs_set_item_key(eb, &disk_key, slot);
1929 btrfs_mark_buffer_dirty(eb);
1930 if (slot == 0)
1931 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001932}
1933
1934/*
Chris Mason74123bd2007-02-02 11:05:29 -05001935 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001936 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001937 *
1938 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1939 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001940 */
Chris Mason98ed5172008-01-03 10:01:48 -05001941static int push_node_left(struct btrfs_trans_handle *trans,
1942 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001943 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001944{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001945 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001946 int src_nritems;
1947 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001948 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001949
Chris Mason5f39d392007-10-15 16:14:19 -04001950 src_nritems = btrfs_header_nritems(src);
1951 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001952 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001953 WARN_ON(btrfs_header_generation(src) != trans->transid);
1954 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001955
Chris Masonbce4eae2008-04-24 14:42:46 -04001956 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001957 return 1;
1958
Chris Masond3977122009-01-05 21:25:51 -05001959 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001960 return 1;
1961
Chris Masonbce4eae2008-04-24 14:42:46 -04001962 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001963 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001964 if (push_items < src_nritems) {
1965 /* leave at least 8 pointers in the node if
1966 * we aren't going to empty it
1967 */
1968 if (src_nritems - push_items < 8) {
1969 if (push_items <= 8)
1970 return 1;
1971 push_items -= 8;
1972 }
1973 }
1974 } else
1975 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001976
Chris Mason5f39d392007-10-15 16:14:19 -04001977 copy_extent_buffer(dst, src,
1978 btrfs_node_key_ptr_offset(dst_nritems),
1979 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001980 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001981
Chris Masonbb803952007-03-01 12:04:21 -05001982 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001983 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1984 btrfs_node_key_ptr_offset(push_items),
1985 (src_nritems - push_items) *
1986 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001987 }
Chris Mason5f39d392007-10-15 16:14:19 -04001988 btrfs_set_header_nritems(src, src_nritems - push_items);
1989 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1990 btrfs_mark_buffer_dirty(src);
1991 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001992
Chris Masonbb803952007-03-01 12:04:21 -05001993 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001994}
1995
Chris Mason97571fd2007-02-24 13:39:08 -05001996/*
Chris Mason79f95c82007-03-01 15:16:26 -05001997 * try to push data from one node into the next node right in the
1998 * tree.
1999 *
2000 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2001 * error, and > 0 if there was no room in the right hand block.
2002 *
2003 * this will only push up to 1/2 the contents of the left node over
2004 */
Chris Mason5f39d392007-10-15 16:14:19 -04002005static int balance_node_right(struct btrfs_trans_handle *trans,
2006 struct btrfs_root *root,
2007 struct extent_buffer *dst,
2008 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002009{
Chris Mason79f95c82007-03-01 15:16:26 -05002010 int push_items = 0;
2011 int max_push;
2012 int src_nritems;
2013 int dst_nritems;
2014 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002015
Chris Mason7bb86312007-12-11 09:25:06 -05002016 WARN_ON(btrfs_header_generation(src) != trans->transid);
2017 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2018
Chris Mason5f39d392007-10-15 16:14:19 -04002019 src_nritems = btrfs_header_nritems(src);
2020 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002021 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002022 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002023 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002024
Chris Masond3977122009-01-05 21:25:51 -05002025 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002026 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002027
2028 max_push = src_nritems / 2 + 1;
2029 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002030 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002031 return 1;
Yan252c38f2007-08-29 09:11:44 -04002032
Chris Mason79f95c82007-03-01 15:16:26 -05002033 if (max_push < push_items)
2034 push_items = max_push;
2035
Chris Mason5f39d392007-10-15 16:14:19 -04002036 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2037 btrfs_node_key_ptr_offset(0),
2038 (dst_nritems) *
2039 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002040
Chris Mason5f39d392007-10-15 16:14:19 -04002041 copy_extent_buffer(dst, src,
2042 btrfs_node_key_ptr_offset(0),
2043 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002044 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_set_header_nritems(src, src_nritems - push_items);
2047 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002048
Chris Mason5f39d392007-10-15 16:14:19 -04002049 btrfs_mark_buffer_dirty(src);
2050 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002051
Chris Mason79f95c82007-03-01 15:16:26 -05002052 return ret;
2053}
2054
2055/*
Chris Mason97571fd2007-02-24 13:39:08 -05002056 * helper function to insert a new root level in the tree.
2057 * A new node is allocated, and a single item is inserted to
2058 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002059 *
2060 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002061 */
Chris Masond3977122009-01-05 21:25:51 -05002062static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002063 struct btrfs_root *root,
2064 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002065{
Chris Mason7bb86312007-12-11 09:25:06 -05002066 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002067 struct extent_buffer *lower;
2068 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002069 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002070 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002071
2072 BUG_ON(path->nodes[level]);
2073 BUG_ON(path->nodes[level-1] != root->node);
2074
Chris Mason7bb86312007-12-11 09:25:06 -05002075 lower = path->nodes[level-1];
2076 if (level == 1)
2077 btrfs_item_key(lower, &lower_key, 0);
2078 else
2079 btrfs_node_key(lower, &lower_key, 0);
2080
Zheng Yan31840ae2008-09-23 13:14:14 -04002081 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002083 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 if (IS_ERR(c))
2085 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002086
Yan, Zhengf0486c62010-05-16 10:46:25 -04002087 root_add_used(root, root->nodesize);
2088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002089 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002090 btrfs_set_header_nritems(c, 1);
2091 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002092 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002093 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002094 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002095 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002096
Chris Mason5f39d392007-10-15 16:14:19 -04002097 write_extent_buffer(c, root->fs_info->fsid,
2098 (unsigned long)btrfs_header_fsid(c),
2099 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002100
2101 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2102 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2103 BTRFS_UUID_SIZE);
2104
Chris Mason5f39d392007-10-15 16:14:19 -04002105 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002106 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002107 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002108 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002109
2110 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002111
2112 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002113
Chris Mason925baed2008-06-25 16:01:30 -04002114 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002115 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002116
2117 /* the super has an extra ref to root->node */
2118 free_extent_buffer(old);
2119
Chris Mason0b86a832008-03-24 15:01:56 -04002120 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002121 extent_buffer_get(c);
2122 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002123 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002124 path->slots[level] = 0;
2125 return 0;
2126}
2127
Chris Mason74123bd2007-02-02 11:05:29 -05002128/*
2129 * worker function to insert a single pointer in a node.
2130 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002131 *
Chris Mason74123bd2007-02-02 11:05:29 -05002132 * slot and level indicate where you want the key to go, and
2133 * blocknr is the block the key points to.
2134 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002135static void insert_ptr(struct btrfs_trans_handle *trans,
2136 struct btrfs_root *root, struct btrfs_path *path,
2137 struct btrfs_disk_key *key, u64 bytenr,
2138 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002139{
Chris Mason5f39d392007-10-15 16:14:19 -04002140 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002141 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002142
2143 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002144 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002145 lower = path->nodes[level];
2146 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002147 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002148 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002149 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002150 memmove_extent_buffer(lower,
2151 btrfs_node_key_ptr_offset(slot + 1),
2152 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002153 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002154 }
Chris Mason5f39d392007-10-15 16:14:19 -04002155 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002156 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002157 WARN_ON(trans->transid == 0);
2158 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002159 btrfs_set_header_nritems(lower, nritems + 1);
2160 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002161}
2162
Chris Mason97571fd2007-02-24 13:39:08 -05002163/*
2164 * split the node at the specified level in path in two.
2165 * The path is corrected to point to the appropriate node after the split
2166 *
2167 * Before splitting this tries to make some room in the node by pushing
2168 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002169 *
2170 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002171 */
Chris Masone02119d2008-09-05 16:13:11 -04002172static noinline int split_node(struct btrfs_trans_handle *trans,
2173 struct btrfs_root *root,
2174 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002175{
Chris Mason5f39d392007-10-15 16:14:19 -04002176 struct extent_buffer *c;
2177 struct extent_buffer *split;
2178 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002179 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002180 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002181 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002182
Chris Mason5f39d392007-10-15 16:14:19 -04002183 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002184 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002186 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002187 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002188 if (ret)
2189 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002190 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002191 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002192 c = path->nodes[level];
2193 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002194 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002195 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002196 if (ret < 0)
2197 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002198 }
Chris Masone66f7092007-04-20 13:16:02 -04002199
Chris Mason5f39d392007-10-15 16:14:19 -04002200 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 mid = (c_nritems + 1) / 2;
2202 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002205 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002206 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002207 if (IS_ERR(split))
2208 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002209
Yan, Zhengf0486c62010-05-16 10:46:25 -04002210 root_add_used(root, root->nodesize);
2211
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002212 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002213 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002214 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002216 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002217 btrfs_set_header_owner(split, root->root_key.objectid);
2218 write_extent_buffer(split, root->fs_info->fsid,
2219 (unsigned long)btrfs_header_fsid(split),
2220 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002221 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2222 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2223 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002224
Chris Mason5f39d392007-10-15 16:14:19 -04002225
2226 copy_extent_buffer(split, c,
2227 btrfs_node_key_ptr_offset(0),
2228 btrfs_node_key_ptr_offset(mid),
2229 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2230 btrfs_set_header_nritems(split, c_nritems - mid);
2231 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002232 ret = 0;
2233
Chris Mason5f39d392007-10-15 16:14:19 -04002234 btrfs_mark_buffer_dirty(c);
2235 btrfs_mark_buffer_dirty(split);
2236
Jeff Mahoney143bede2012-03-01 14:56:26 +01002237 insert_ptr(trans, root, path, &disk_key, split->start,
2238 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002239
Chris Mason5de08d72007-02-24 06:24:44 -05002240 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002241 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002242 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002243 free_extent_buffer(c);
2244 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002245 path->slots[level + 1] += 1;
2246 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002247 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002248 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002249 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002250 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002251}
2252
Chris Mason74123bd2007-02-02 11:05:29 -05002253/*
2254 * how many bytes are required to store the items in a leaf. start
2255 * and nr indicate which items in the leaf to check. This totals up the
2256 * space used both by the item structs and the item data
2257 */
Chris Mason5f39d392007-10-15 16:14:19 -04002258static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002259{
2260 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002261 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002262 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002263
2264 if (!nr)
2265 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002266 data_len = btrfs_item_end_nr(l, start);
2267 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002268 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002269 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002270 return data_len;
2271}
2272
Chris Mason74123bd2007-02-02 11:05:29 -05002273/*
Chris Masond4dbff92007-04-04 14:08:15 -04002274 * The space between the end of the leaf items and
2275 * the start of the leaf data. IOW, how much room
2276 * the leaf has left for both items and data
2277 */
Chris Masond3977122009-01-05 21:25:51 -05002278noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002279 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002280{
Chris Mason5f39d392007-10-15 16:14:19 -04002281 int nritems = btrfs_header_nritems(leaf);
2282 int ret;
2283 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2284 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002285 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2286 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002287 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002288 leaf_space_used(leaf, 0, nritems), nritems);
2289 }
2290 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002291}
2292
Chris Mason99d8f832010-07-07 10:51:48 -04002293/*
2294 * min slot controls the lowest index we're willing to push to the
2295 * right. We'll push up to and including min_slot, but no lower
2296 */
Chris Mason44871b12009-03-13 10:04:31 -04002297static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2298 struct btrfs_root *root,
2299 struct btrfs_path *path,
2300 int data_size, int empty,
2301 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002302 int free_space, u32 left_nritems,
2303 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002304{
Chris Mason5f39d392007-10-15 16:14:19 -04002305 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002306 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002307 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002308 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002309 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002310 int push_space = 0;
2311 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002312 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002313 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002314 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002315 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002316 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002317
Chris Mason34a38212007-11-07 13:31:03 -05002318 if (empty)
2319 nr = 0;
2320 else
Chris Mason99d8f832010-07-07 10:51:48 -04002321 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002322
Zheng Yan31840ae2008-09-23 13:14:14 -04002323 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002324 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002325
Chris Mason44871b12009-03-13 10:04:31 -04002326 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002327 i = left_nritems - 1;
2328 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002329 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002330
Zheng Yan31840ae2008-09-23 13:14:14 -04002331 if (!empty && push_items > 0) {
2332 if (path->slots[0] > i)
2333 break;
2334 if (path->slots[0] == i) {
2335 int space = btrfs_leaf_free_space(root, left);
2336 if (space + push_space * 2 > free_space)
2337 break;
2338 }
2339 }
2340
Chris Mason00ec4c52007-02-24 12:47:20 -05002341 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002342 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002343
Chris Masondb945352007-10-15 16:15:53 -04002344 this_item_size = btrfs_item_size(left, item);
2345 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002346 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002347
Chris Mason00ec4c52007-02-24 12:47:20 -05002348 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002349 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002350 if (i == 0)
2351 break;
2352 i--;
Chris Masondb945352007-10-15 16:15:53 -04002353 }
Chris Mason5f39d392007-10-15 16:14:19 -04002354
Chris Mason925baed2008-06-25 16:01:30 -04002355 if (push_items == 0)
2356 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002357
Chris Mason34a38212007-11-07 13:31:03 -05002358 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002359 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002360
Chris Mason00ec4c52007-02-24 12:47:20 -05002361 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002362 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002363
Chris Mason5f39d392007-10-15 16:14:19 -04002364 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002365 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002366
Chris Mason00ec4c52007-02-24 12:47:20 -05002367 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002368 data_end = leaf_data_end(root, right);
2369 memmove_extent_buffer(right,
2370 btrfs_leaf_data(right) + data_end - push_space,
2371 btrfs_leaf_data(right) + data_end,
2372 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2373
Chris Mason00ec4c52007-02-24 12:47:20 -05002374 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002375 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002376 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2377 btrfs_leaf_data(left) + leaf_data_end(root, left),
2378 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002379
2380 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2381 btrfs_item_nr_offset(0),
2382 right_nritems * sizeof(struct btrfs_item));
2383
Chris Mason00ec4c52007-02-24 12:47:20 -05002384 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002385 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2386 btrfs_item_nr_offset(left_nritems - push_items),
2387 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002388
2389 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002390 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002391 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002392 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002393 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002394 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002395 push_space -= btrfs_item_size(right, item);
2396 btrfs_set_item_offset(right, item, push_space);
2397 }
2398
Chris Mason7518a232007-03-12 12:01:18 -04002399 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002400 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002401
Chris Mason34a38212007-11-07 13:31:03 -05002402 if (left_nritems)
2403 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002404 else
2405 clean_tree_block(trans, root, left);
2406
Chris Mason5f39d392007-10-15 16:14:19 -04002407 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002408
Chris Mason5f39d392007-10-15 16:14:19 -04002409 btrfs_item_key(right, &disk_key, 0);
2410 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002411 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002412
Chris Mason00ec4c52007-02-24 12:47:20 -05002413 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002414 if (path->slots[0] >= left_nritems) {
2415 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002416 if (btrfs_header_nritems(path->nodes[0]) == 0)
2417 clean_tree_block(trans, root, path->nodes[0]);
2418 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002419 free_extent_buffer(path->nodes[0]);
2420 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002421 path->slots[1] += 1;
2422 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002423 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002424 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002425 }
2426 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002427
2428out_unlock:
2429 btrfs_tree_unlock(right);
2430 free_extent_buffer(right);
2431 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002432}
Chris Mason925baed2008-06-25 16:01:30 -04002433
Chris Mason00ec4c52007-02-24 12:47:20 -05002434/*
Chris Mason44871b12009-03-13 10:04:31 -04002435 * push some data in the path leaf to the right, trying to free up at
2436 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2437 *
2438 * returns 1 if the push failed because the other node didn't have enough
2439 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002440 *
2441 * this will push starting from min_slot to the end of the leaf. It won't
2442 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002443 */
2444static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002445 *root, struct btrfs_path *path,
2446 int min_data_size, int data_size,
2447 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002448{
2449 struct extent_buffer *left = path->nodes[0];
2450 struct extent_buffer *right;
2451 struct extent_buffer *upper;
2452 int slot;
2453 int free_space;
2454 u32 left_nritems;
2455 int ret;
2456
2457 if (!path->nodes[1])
2458 return 1;
2459
2460 slot = path->slots[1];
2461 upper = path->nodes[1];
2462 if (slot >= btrfs_header_nritems(upper) - 1)
2463 return 1;
2464
2465 btrfs_assert_tree_locked(path->nodes[1]);
2466
2467 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002468 if (right == NULL)
2469 return 1;
2470
Chris Mason44871b12009-03-13 10:04:31 -04002471 btrfs_tree_lock(right);
2472 btrfs_set_lock_blocking(right);
2473
2474 free_space = btrfs_leaf_free_space(root, right);
2475 if (free_space < data_size)
2476 goto out_unlock;
2477
2478 /* cow and double check */
2479 ret = btrfs_cow_block(trans, root, right, upper,
2480 slot + 1, &right);
2481 if (ret)
2482 goto out_unlock;
2483
2484 free_space = btrfs_leaf_free_space(root, right);
2485 if (free_space < data_size)
2486 goto out_unlock;
2487
2488 left_nritems = btrfs_header_nritems(left);
2489 if (left_nritems == 0)
2490 goto out_unlock;
2491
Chris Mason99d8f832010-07-07 10:51:48 -04002492 return __push_leaf_right(trans, root, path, min_data_size, empty,
2493 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002494out_unlock:
2495 btrfs_tree_unlock(right);
2496 free_extent_buffer(right);
2497 return 1;
2498}
2499
2500/*
Chris Mason74123bd2007-02-02 11:05:29 -05002501 * push some data in the path leaf to the left, trying to free up at
2502 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002503 *
2504 * max_slot can put a limit on how far into the leaf we'll push items. The
2505 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2506 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002507 */
Chris Mason44871b12009-03-13 10:04:31 -04002508static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2509 struct btrfs_root *root,
2510 struct btrfs_path *path, int data_size,
2511 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002512 int free_space, u32 right_nritems,
2513 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002514{
Chris Mason5f39d392007-10-15 16:14:19 -04002515 struct btrfs_disk_key disk_key;
2516 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002517 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518 int push_space = 0;
2519 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002520 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002521 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002522 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002523 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002524 u32 this_item_size;
2525 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002526
Chris Mason34a38212007-11-07 13:31:03 -05002527 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002528 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002529 else
Chris Mason99d8f832010-07-07 10:51:48 -04002530 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002531
2532 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002533 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002534
Zheng Yan31840ae2008-09-23 13:14:14 -04002535 if (!empty && push_items > 0) {
2536 if (path->slots[0] < i)
2537 break;
2538 if (path->slots[0] == i) {
2539 int space = btrfs_leaf_free_space(root, right);
2540 if (space + push_space * 2 > free_space)
2541 break;
2542 }
2543 }
2544
Chris Masonbe0e5c02007-01-26 15:51:26 -05002545 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002546 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002547
2548 this_item_size = btrfs_item_size(right, item);
2549 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002550 break;
Chris Masondb945352007-10-15 16:15:53 -04002551
Chris Masonbe0e5c02007-01-26 15:51:26 -05002552 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002553 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 }
Chris Masondb945352007-10-15 16:15:53 -04002555
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002557 ret = 1;
2558 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 }
Chris Mason34a38212007-11-07 13:31:03 -05002560 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002561 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002562
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002564 copy_extent_buffer(left, right,
2565 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2566 btrfs_item_nr_offset(0),
2567 push_items * sizeof(struct btrfs_item));
2568
Chris Mason123abc82007-03-14 14:14:43 -04002569 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002570 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002571
2572 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002573 leaf_data_end(root, left) - push_space,
2574 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002575 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002576 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002577 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002578 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002579
Chris Masondb945352007-10-15 16:15:53 -04002580 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002581 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002582 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002583
Chris Mason5f39d392007-10-15 16:14:19 -04002584 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002585
Chris Mason5f39d392007-10-15 16:14:19 -04002586 ioff = btrfs_item_offset(left, item);
2587 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002588 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589 }
Chris Mason5f39d392007-10-15 16:14:19 -04002590 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002591
2592 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002593 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002594 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2595 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002596 WARN_ON(1);
2597 }
Chris Mason5f39d392007-10-15 16:14:19 -04002598
Chris Mason34a38212007-11-07 13:31:03 -05002599 if (push_items < right_nritems) {
2600 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2601 leaf_data_end(root, right);
2602 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2603 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2604 btrfs_leaf_data(right) +
2605 leaf_data_end(root, right), push_space);
2606
2607 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002608 btrfs_item_nr_offset(push_items),
2609 (btrfs_header_nritems(right) - push_items) *
2610 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002611 }
Yaneef1c492007-11-26 10:58:13 -05002612 right_nritems -= push_items;
2613 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002614 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002615 for (i = 0; i < right_nritems; i++) {
2616 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002617
Chris Masondb945352007-10-15 16:15:53 -04002618 push_space = push_space - btrfs_item_size(right, item);
2619 btrfs_set_item_offset(right, item, push_space);
2620 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002621
Chris Mason5f39d392007-10-15 16:14:19 -04002622 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002623 if (right_nritems)
2624 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002625 else
2626 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002627
Chris Mason5f39d392007-10-15 16:14:19 -04002628 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002629 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002630
2631 /* then fixup the leaf pointer in the path */
2632 if (path->slots[0] < push_items) {
2633 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002634 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002635 free_extent_buffer(path->nodes[0]);
2636 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002637 path->slots[1] -= 1;
2638 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002639 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002640 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002641 path->slots[0] -= push_items;
2642 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002643 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002644 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002645out:
2646 btrfs_tree_unlock(left);
2647 free_extent_buffer(left);
2648 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002649}
2650
Chris Mason74123bd2007-02-02 11:05:29 -05002651/*
Chris Mason44871b12009-03-13 10:04:31 -04002652 * push some data in the path leaf to the left, trying to free up at
2653 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002654 *
2655 * max_slot can put a limit on how far into the leaf we'll push items. The
2656 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2657 * items
Chris Mason44871b12009-03-13 10:04:31 -04002658 */
2659static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002660 *root, struct btrfs_path *path, int min_data_size,
2661 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002662{
2663 struct extent_buffer *right = path->nodes[0];
2664 struct extent_buffer *left;
2665 int slot;
2666 int free_space;
2667 u32 right_nritems;
2668 int ret = 0;
2669
2670 slot = path->slots[1];
2671 if (slot == 0)
2672 return 1;
2673 if (!path->nodes[1])
2674 return 1;
2675
2676 right_nritems = btrfs_header_nritems(right);
2677 if (right_nritems == 0)
2678 return 1;
2679
2680 btrfs_assert_tree_locked(path->nodes[1]);
2681
2682 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002683 if (left == NULL)
2684 return 1;
2685
Chris Mason44871b12009-03-13 10:04:31 -04002686 btrfs_tree_lock(left);
2687 btrfs_set_lock_blocking(left);
2688
2689 free_space = btrfs_leaf_free_space(root, left);
2690 if (free_space < data_size) {
2691 ret = 1;
2692 goto out;
2693 }
2694
2695 /* cow and double check */
2696 ret = btrfs_cow_block(trans, root, left,
2697 path->nodes[1], slot - 1, &left);
2698 if (ret) {
2699 /* we hit -ENOSPC, but it isn't fatal here */
2700 ret = 1;
2701 goto out;
2702 }
2703
2704 free_space = btrfs_leaf_free_space(root, left);
2705 if (free_space < data_size) {
2706 ret = 1;
2707 goto out;
2708 }
2709
Chris Mason99d8f832010-07-07 10:51:48 -04002710 return __push_leaf_left(trans, root, path, min_data_size,
2711 empty, left, free_space, right_nritems,
2712 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002713out:
2714 btrfs_tree_unlock(left);
2715 free_extent_buffer(left);
2716 return ret;
2717}
2718
2719/*
Chris Mason74123bd2007-02-02 11:05:29 -05002720 * split the path's leaf in two, making sure there is at least data_size
2721 * available for the resulting leaf level of the path.
2722 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002723static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2724 struct btrfs_root *root,
2725 struct btrfs_path *path,
2726 struct extent_buffer *l,
2727 struct extent_buffer *right,
2728 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002729{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002730 int data_copy_size;
2731 int rt_data_off;
2732 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002733 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002734
Chris Mason5f39d392007-10-15 16:14:19 -04002735 nritems = nritems - mid;
2736 btrfs_set_header_nritems(right, nritems);
2737 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2738
2739 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2740 btrfs_item_nr_offset(mid),
2741 nritems * sizeof(struct btrfs_item));
2742
2743 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002744 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2745 data_copy_size, btrfs_leaf_data(l) +
2746 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002747
Chris Mason5f39d392007-10-15 16:14:19 -04002748 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2749 btrfs_item_end_nr(l, mid);
2750
2751 for (i = 0; i < nritems; i++) {
2752 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002753 u32 ioff;
2754
Chris Masondb945352007-10-15 16:15:53 -04002755 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002756 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002757 }
Chris Mason74123bd2007-02-02 11:05:29 -05002758
Chris Mason5f39d392007-10-15 16:14:19 -04002759 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002760 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002761 insert_ptr(trans, root, path, &disk_key, right->start,
2762 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002763
2764 btrfs_mark_buffer_dirty(right);
2765 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002766 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002767
Chris Masonbe0e5c02007-01-26 15:51:26 -05002768 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002769 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002770 free_extent_buffer(path->nodes[0]);
2771 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002772 path->slots[0] -= mid;
2773 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002774 } else {
2775 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002776 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002777 }
Chris Mason5f39d392007-10-15 16:14:19 -04002778
Chris Masoneb60cea2007-02-02 09:18:22 -05002779 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002780}
2781
2782/*
Chris Mason99d8f832010-07-07 10:51:48 -04002783 * double splits happen when we need to insert a big item in the middle
2784 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2785 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2786 * A B C
2787 *
2788 * We avoid this by trying to push the items on either side of our target
2789 * into the adjacent leaves. If all goes well we can avoid the double split
2790 * completely.
2791 */
2792static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2793 struct btrfs_root *root,
2794 struct btrfs_path *path,
2795 int data_size)
2796{
2797 int ret;
2798 int progress = 0;
2799 int slot;
2800 u32 nritems;
2801
2802 slot = path->slots[0];
2803
2804 /*
2805 * try to push all the items after our slot into the
2806 * right leaf
2807 */
2808 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2809 if (ret < 0)
2810 return ret;
2811
2812 if (ret == 0)
2813 progress++;
2814
2815 nritems = btrfs_header_nritems(path->nodes[0]);
2816 /*
2817 * our goal is to get our slot at the start or end of a leaf. If
2818 * we've done so we're done
2819 */
2820 if (path->slots[0] == 0 || path->slots[0] == nritems)
2821 return 0;
2822
2823 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2824 return 0;
2825
2826 /* try to push all the items before our slot into the next leaf */
2827 slot = path->slots[0];
2828 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2829 if (ret < 0)
2830 return ret;
2831
2832 if (ret == 0)
2833 progress++;
2834
2835 if (progress)
2836 return 0;
2837 return 1;
2838}
2839
2840/*
Chris Mason44871b12009-03-13 10:04:31 -04002841 * split the path's leaf in two, making sure there is at least data_size
2842 * available for the resulting leaf level of the path.
2843 *
2844 * returns 0 if all went well and < 0 on failure.
2845 */
2846static noinline int split_leaf(struct btrfs_trans_handle *trans,
2847 struct btrfs_root *root,
2848 struct btrfs_key *ins_key,
2849 struct btrfs_path *path, int data_size,
2850 int extend)
2851{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002852 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002853 struct extent_buffer *l;
2854 u32 nritems;
2855 int mid;
2856 int slot;
2857 struct extent_buffer *right;
2858 int ret = 0;
2859 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002860 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002861 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002862 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002863
Yan, Zhenga5719522009-09-24 09:17:31 -04002864 l = path->nodes[0];
2865 slot = path->slots[0];
2866 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2867 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2868 return -EOVERFLOW;
2869
Chris Mason44871b12009-03-13 10:04:31 -04002870 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002871 if (data_size) {
2872 wret = push_leaf_right(trans, root, path, data_size,
2873 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002874 if (wret < 0)
2875 return wret;
2876 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002877 wret = push_leaf_left(trans, root, path, data_size,
2878 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002879 if (wret < 0)
2880 return wret;
2881 }
2882 l = path->nodes[0];
2883
2884 /* did the pushes work? */
2885 if (btrfs_leaf_free_space(root, l) >= data_size)
2886 return 0;
2887 }
2888
2889 if (!path->nodes[1]) {
2890 ret = insert_new_root(trans, root, path, 1);
2891 if (ret)
2892 return ret;
2893 }
2894again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002895 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002896 l = path->nodes[0];
2897 slot = path->slots[0];
2898 nritems = btrfs_header_nritems(l);
2899 mid = (nritems + 1) / 2;
2900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002901 if (mid <= slot) {
2902 if (nritems == 1 ||
2903 leaf_space_used(l, mid, nritems - mid) + data_size >
2904 BTRFS_LEAF_DATA_SIZE(root)) {
2905 if (slot >= nritems) {
2906 split = 0;
2907 } else {
2908 mid = slot;
2909 if (mid != nritems &&
2910 leaf_space_used(l, mid, nritems - mid) +
2911 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002912 if (data_size && !tried_avoid_double)
2913 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002914 split = 2;
2915 }
2916 }
2917 }
2918 } else {
2919 if (leaf_space_used(l, 0, mid) + data_size >
2920 BTRFS_LEAF_DATA_SIZE(root)) {
2921 if (!extend && data_size && slot == 0) {
2922 split = 0;
2923 } else if ((extend || !data_size) && slot == 0) {
2924 mid = 1;
2925 } else {
2926 mid = slot;
2927 if (mid != nritems &&
2928 leaf_space_used(l, mid, nritems - mid) +
2929 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002930 if (data_size && !tried_avoid_double)
2931 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002932 split = 2 ;
2933 }
2934 }
2935 }
2936 }
2937
2938 if (split == 0)
2939 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2940 else
2941 btrfs_item_key(l, &disk_key, mid);
2942
2943 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002944 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002945 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002946 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002947 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002948
2949 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002950
2951 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2952 btrfs_set_header_bytenr(right, right->start);
2953 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002955 btrfs_set_header_owner(right, root->root_key.objectid);
2956 btrfs_set_header_level(right, 0);
2957 write_extent_buffer(right, root->fs_info->fsid,
2958 (unsigned long)btrfs_header_fsid(right),
2959 BTRFS_FSID_SIZE);
2960
2961 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2962 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2963 BTRFS_UUID_SIZE);
2964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 if (split == 0) {
2966 if (mid <= slot) {
2967 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002968 insert_ptr(trans, root, path, &disk_key, right->start,
2969 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970 btrfs_tree_unlock(path->nodes[0]);
2971 free_extent_buffer(path->nodes[0]);
2972 path->nodes[0] = right;
2973 path->slots[0] = 0;
2974 path->slots[1] += 1;
2975 } else {
2976 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002977 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002978 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002979 btrfs_tree_unlock(path->nodes[0]);
2980 free_extent_buffer(path->nodes[0]);
2981 path->nodes[0] = right;
2982 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01002983 if (path->slots[1] == 0)
2984 fixup_low_keys(trans, root, path,
2985 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002986 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 btrfs_mark_buffer_dirty(right);
2988 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002989 }
2990
Jeff Mahoney143bede2012-03-01 14:56:26 +01002991 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04002992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002994 BUG_ON(num_doubles != 0);
2995 num_doubles++;
2996 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002997 }
Chris Mason44871b12009-03-13 10:04:31 -04002998
Jeff Mahoney143bede2012-03-01 14:56:26 +01002999 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003000
3001push_for_double:
3002 push_for_double_split(trans, root, path, data_size);
3003 tried_avoid_double = 1;
3004 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3005 return 0;
3006 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003007}
3008
Yan, Zhengad48fd752009-11-12 09:33:58 +00003009static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3010 struct btrfs_root *root,
3011 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003012{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003013 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003014 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003015 struct btrfs_file_extent_item *fi;
3016 u64 extent_len = 0;
3017 u32 item_size;
3018 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003019
3020 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003021 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3022
3023 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3024 key.type != BTRFS_EXTENT_CSUM_KEY);
3025
3026 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3027 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003028
3029 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003030 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3031 fi = btrfs_item_ptr(leaf, path->slots[0],
3032 struct btrfs_file_extent_item);
3033 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3034 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003035 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003036
Chris Mason459931e2008-12-10 09:10:46 -05003037 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003038 path->search_for_split = 1;
3039 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003040 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003041 if (ret < 0)
3042 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003043
Yan, Zhengad48fd752009-11-12 09:33:58 +00003044 ret = -EAGAIN;
3045 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003046 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003047 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3048 goto err;
3049
Chris Mason109f6ae2010-04-02 09:20:18 -04003050 /* the leaf has changed, it now has room. return now */
3051 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3052 goto err;
3053
Yan, Zhengad48fd752009-11-12 09:33:58 +00003054 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3055 fi = btrfs_item_ptr(leaf, path->slots[0],
3056 struct btrfs_file_extent_item);
3057 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3058 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003059 }
3060
Chris Masonb9473432009-03-13 11:00:37 -04003061 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003062 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003063 if (ret)
3064 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003065
Yan, Zhengad48fd752009-11-12 09:33:58 +00003066 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003067 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003068 return 0;
3069err:
3070 path->keep_locks = 0;
3071 return ret;
3072}
3073
3074static noinline int split_item(struct btrfs_trans_handle *trans,
3075 struct btrfs_root *root,
3076 struct btrfs_path *path,
3077 struct btrfs_key *new_key,
3078 unsigned long split_offset)
3079{
3080 struct extent_buffer *leaf;
3081 struct btrfs_item *item;
3082 struct btrfs_item *new_item;
3083 int slot;
3084 char *buf;
3085 u32 nritems;
3086 u32 item_size;
3087 u32 orig_offset;
3088 struct btrfs_disk_key disk_key;
3089
Chris Masonb9473432009-03-13 11:00:37 -04003090 leaf = path->nodes[0];
3091 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3092
Chris Masonb4ce94d2009-02-04 09:25:08 -05003093 btrfs_set_path_blocking(path);
3094
Chris Mason459931e2008-12-10 09:10:46 -05003095 item = btrfs_item_nr(leaf, path->slots[0]);
3096 orig_offset = btrfs_item_offset(leaf, item);
3097 item_size = btrfs_item_size(leaf, item);
3098
Chris Mason459931e2008-12-10 09:10:46 -05003099 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003100 if (!buf)
3101 return -ENOMEM;
3102
Chris Mason459931e2008-12-10 09:10:46 -05003103 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3104 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003105
Chris Mason459931e2008-12-10 09:10:46 -05003106 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003107 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003108 if (slot != nritems) {
3109 /* shift the items */
3110 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003111 btrfs_item_nr_offset(slot),
3112 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003113 }
3114
3115 btrfs_cpu_key_to_disk(&disk_key, new_key);
3116 btrfs_set_item_key(leaf, &disk_key, slot);
3117
3118 new_item = btrfs_item_nr(leaf, slot);
3119
3120 btrfs_set_item_offset(leaf, new_item, orig_offset);
3121 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3122
3123 btrfs_set_item_offset(leaf, item,
3124 orig_offset + item_size - split_offset);
3125 btrfs_set_item_size(leaf, item, split_offset);
3126
3127 btrfs_set_header_nritems(leaf, nritems + 1);
3128
3129 /* write the data for the start of the original item */
3130 write_extent_buffer(leaf, buf,
3131 btrfs_item_ptr_offset(leaf, path->slots[0]),
3132 split_offset);
3133
3134 /* write the data for the new item */
3135 write_extent_buffer(leaf, buf + split_offset,
3136 btrfs_item_ptr_offset(leaf, slot),
3137 item_size - split_offset);
3138 btrfs_mark_buffer_dirty(leaf);
3139
Yan, Zhengad48fd752009-11-12 09:33:58 +00003140 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003141 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003142 return 0;
3143}
3144
3145/*
3146 * This function splits a single item into two items,
3147 * giving 'new_key' to the new item and splitting the
3148 * old one at split_offset (from the start of the item).
3149 *
3150 * The path may be released by this operation. After
3151 * the split, the path is pointing to the old item. The
3152 * new item is going to be in the same node as the old one.
3153 *
3154 * Note, the item being split must be smaller enough to live alone on
3155 * a tree block with room for one extra struct btrfs_item
3156 *
3157 * This allows us to split the item in place, keeping a lock on the
3158 * leaf the entire time.
3159 */
3160int btrfs_split_item(struct btrfs_trans_handle *trans,
3161 struct btrfs_root *root,
3162 struct btrfs_path *path,
3163 struct btrfs_key *new_key,
3164 unsigned long split_offset)
3165{
3166 int ret;
3167 ret = setup_leaf_for_split(trans, root, path,
3168 sizeof(struct btrfs_item));
3169 if (ret)
3170 return ret;
3171
3172 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003173 return ret;
3174}
3175
3176/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003177 * This function duplicate a item, giving 'new_key' to the new item.
3178 * It guarantees both items live in the same tree leaf and the new item
3179 * is contiguous with the original item.
3180 *
3181 * This allows us to split file extent in place, keeping a lock on the
3182 * leaf the entire time.
3183 */
3184int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3185 struct btrfs_root *root,
3186 struct btrfs_path *path,
3187 struct btrfs_key *new_key)
3188{
3189 struct extent_buffer *leaf;
3190 int ret;
3191 u32 item_size;
3192
3193 leaf = path->nodes[0];
3194 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3195 ret = setup_leaf_for_split(trans, root, path,
3196 item_size + sizeof(struct btrfs_item));
3197 if (ret)
3198 return ret;
3199
3200 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003201 setup_items_for_insert(trans, root, path, new_key, &item_size,
3202 item_size, item_size +
3203 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003204 leaf = path->nodes[0];
3205 memcpy_extent_buffer(leaf,
3206 btrfs_item_ptr_offset(leaf, path->slots[0]),
3207 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3208 item_size);
3209 return 0;
3210}
3211
3212/*
Chris Masond352ac62008-09-29 15:18:18 -04003213 * make the item pointed to by the path smaller. new_size indicates
3214 * how small to make it, and from_end tells us if we just chop bytes
3215 * off the end of the item or if we shift the item to chop bytes off
3216 * the front.
3217 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003218void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3219 struct btrfs_root *root,
3220 struct btrfs_path *path,
3221 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003222{
Chris Masonb18c6682007-04-17 13:26:50 -04003223 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003224 struct extent_buffer *leaf;
3225 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003226 u32 nritems;
3227 unsigned int data_end;
3228 unsigned int old_data_start;
3229 unsigned int old_size;
3230 unsigned int size_diff;
3231 int i;
3232
Chris Mason5f39d392007-10-15 16:14:19 -04003233 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003234 slot = path->slots[0];
3235
3236 old_size = btrfs_item_size_nr(leaf, slot);
3237 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003238 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003241 data_end = leaf_data_end(root, leaf);
3242
Chris Mason5f39d392007-10-15 16:14:19 -04003243 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003244
Chris Masonb18c6682007-04-17 13:26:50 -04003245 size_diff = old_size - new_size;
3246
3247 BUG_ON(slot < 0);
3248 BUG_ON(slot >= nritems);
3249
3250 /*
3251 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3252 */
3253 /* first correct the data pointers */
3254 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003255 u32 ioff;
3256 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003257
Chris Mason5f39d392007-10-15 16:14:19 -04003258 ioff = btrfs_item_offset(leaf, item);
3259 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003260 }
Chris Masondb945352007-10-15 16:15:53 -04003261
Chris Masonb18c6682007-04-17 13:26:50 -04003262 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003263 if (from_end) {
3264 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3265 data_end + size_diff, btrfs_leaf_data(leaf) +
3266 data_end, old_data_start + new_size - data_end);
3267 } else {
3268 struct btrfs_disk_key disk_key;
3269 u64 offset;
3270
3271 btrfs_item_key(leaf, &disk_key, slot);
3272
3273 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3274 unsigned long ptr;
3275 struct btrfs_file_extent_item *fi;
3276
3277 fi = btrfs_item_ptr(leaf, slot,
3278 struct btrfs_file_extent_item);
3279 fi = (struct btrfs_file_extent_item *)(
3280 (unsigned long)fi - size_diff);
3281
3282 if (btrfs_file_extent_type(leaf, fi) ==
3283 BTRFS_FILE_EXTENT_INLINE) {
3284 ptr = btrfs_item_ptr_offset(leaf, slot);
3285 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003286 (unsigned long)fi,
3287 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003288 disk_bytenr));
3289 }
3290 }
3291
3292 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3293 data_end + size_diff, btrfs_leaf_data(leaf) +
3294 data_end, old_data_start - data_end);
3295
3296 offset = btrfs_disk_key_offset(&disk_key);
3297 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3298 btrfs_set_item_key(leaf, &disk_key, slot);
3299 if (slot == 0)
3300 fixup_low_keys(trans, root, path, &disk_key, 1);
3301 }
Chris Mason5f39d392007-10-15 16:14:19 -04003302
3303 item = btrfs_item_nr(leaf, slot);
3304 btrfs_set_item_size(leaf, item, new_size);
3305 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003306
Chris Mason5f39d392007-10-15 16:14:19 -04003307 if (btrfs_leaf_free_space(root, leaf) < 0) {
3308 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003309 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003310 }
Chris Masonb18c6682007-04-17 13:26:50 -04003311}
3312
Chris Masond352ac62008-09-29 15:18:18 -04003313/*
3314 * make the item pointed to by the path bigger, data_size is the new size.
3315 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003316void btrfs_extend_item(struct btrfs_trans_handle *trans,
3317 struct btrfs_root *root, struct btrfs_path *path,
3318 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003319{
Chris Mason6567e832007-04-16 09:22:45 -04003320 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003321 struct extent_buffer *leaf;
3322 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003323 u32 nritems;
3324 unsigned int data_end;
3325 unsigned int old_data;
3326 unsigned int old_size;
3327 int i;
3328
Chris Mason5f39d392007-10-15 16:14:19 -04003329 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003330
Chris Mason5f39d392007-10-15 16:14:19 -04003331 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003332 data_end = leaf_data_end(root, leaf);
3333
Chris Mason5f39d392007-10-15 16:14:19 -04003334 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3335 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003336 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003337 }
Chris Mason6567e832007-04-16 09:22:45 -04003338 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003339 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003340
3341 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003342 if (slot >= nritems) {
3343 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003344 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3345 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003346 BUG_ON(1);
3347 }
Chris Mason6567e832007-04-16 09:22:45 -04003348
3349 /*
3350 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3351 */
3352 /* first correct the data pointers */
3353 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003354 u32 ioff;
3355 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003356
Chris Mason5f39d392007-10-15 16:14:19 -04003357 ioff = btrfs_item_offset(leaf, item);
3358 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003359 }
Chris Mason5f39d392007-10-15 16:14:19 -04003360
Chris Mason6567e832007-04-16 09:22:45 -04003361 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003362 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003363 data_end - data_size, btrfs_leaf_data(leaf) +
3364 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003365
Chris Mason6567e832007-04-16 09:22:45 -04003366 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003367 old_size = btrfs_item_size_nr(leaf, slot);
3368 item = btrfs_item_nr(leaf, slot);
3369 btrfs_set_item_size(leaf, item, old_size + data_size);
3370 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003371
Chris Mason5f39d392007-10-15 16:14:19 -04003372 if (btrfs_leaf_free_space(root, leaf) < 0) {
3373 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003374 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003375 }
Chris Mason6567e832007-04-16 09:22:45 -04003376}
3377
Chris Mason74123bd2007-02-02 11:05:29 -05003378/*
Chris Masond352ac62008-09-29 15:18:18 -04003379 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003380 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003381 * Returns the number of keys that were inserted.
3382 */
3383int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3384 struct btrfs_root *root,
3385 struct btrfs_path *path,
3386 struct btrfs_key *cpu_key, u32 *data_size,
3387 int nr)
3388{
3389 struct extent_buffer *leaf;
3390 struct btrfs_item *item;
3391 int ret = 0;
3392 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003393 int i;
3394 u32 nritems;
3395 u32 total_data = 0;
3396 u32 total_size = 0;
3397 unsigned int data_end;
3398 struct btrfs_disk_key disk_key;
3399 struct btrfs_key found_key;
3400
Yan Zheng87b29b22008-12-17 10:21:48 -05003401 for (i = 0; i < nr; i++) {
3402 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3403 BTRFS_LEAF_DATA_SIZE(root)) {
3404 break;
3405 nr = i;
3406 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003407 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003408 total_size += data_size[i] + sizeof(struct btrfs_item);
3409 }
3410 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003411
Josef Bacikf3465ca2008-11-12 14:19:50 -05003412 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3413 if (ret == 0)
3414 return -EEXIST;
3415 if (ret < 0)
3416 goto out;
3417
Josef Bacikf3465ca2008-11-12 14:19:50 -05003418 leaf = path->nodes[0];
3419
3420 nritems = btrfs_header_nritems(leaf);
3421 data_end = leaf_data_end(root, leaf);
3422
3423 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3424 for (i = nr; i >= 0; i--) {
3425 total_data -= data_size[i];
3426 total_size -= data_size[i] + sizeof(struct btrfs_item);
3427 if (total_size < btrfs_leaf_free_space(root, leaf))
3428 break;
3429 }
3430 nr = i;
3431 }
3432
3433 slot = path->slots[0];
3434 BUG_ON(slot < 0);
3435
3436 if (slot != nritems) {
3437 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3438
3439 item = btrfs_item_nr(leaf, slot);
3440 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3441
3442 /* figure out how many keys we can insert in here */
3443 total_data = data_size[0];
3444 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003445 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003446 break;
3447 total_data += data_size[i];
3448 }
3449 nr = i;
3450
3451 if (old_data < data_end) {
3452 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003453 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003454 slot, old_data, data_end);
3455 BUG_ON(1);
3456 }
3457 /*
3458 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3459 */
3460 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003461 for (i = slot; i < nritems; i++) {
3462 u32 ioff;
3463
3464 item = btrfs_item_nr(leaf, i);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003465 ioff = btrfs_item_offset(leaf, item);
3466 btrfs_set_item_offset(leaf, item, ioff - total_data);
3467 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003468 /* shift the items */
3469 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3470 btrfs_item_nr_offset(slot),
3471 (nritems - slot) * sizeof(struct btrfs_item));
3472
3473 /* shift the data */
3474 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3475 data_end - total_data, btrfs_leaf_data(leaf) +
3476 data_end, old_data - data_end);
3477 data_end = old_data;
3478 } else {
3479 /*
3480 * this sucks but it has to be done, if we are inserting at
3481 * the end of the leaf only insert 1 of the items, since we
3482 * have no way of knowing whats on the next leaf and we'd have
3483 * to drop our current locks to figure it out
3484 */
3485 nr = 1;
3486 }
3487
3488 /* setup the item for the new data */
3489 for (i = 0; i < nr; i++) {
3490 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3491 btrfs_set_item_key(leaf, &disk_key, slot + i);
3492 item = btrfs_item_nr(leaf, slot + i);
3493 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3494 data_end -= data_size[i];
3495 btrfs_set_item_size(leaf, item, data_size[i]);
3496 }
3497 btrfs_set_header_nritems(leaf, nritems + nr);
3498 btrfs_mark_buffer_dirty(leaf);
3499
3500 ret = 0;
3501 if (slot == 0) {
3502 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003503 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003504 }
3505
3506 if (btrfs_leaf_free_space(root, leaf) < 0) {
3507 btrfs_print_leaf(root, leaf);
3508 BUG();
3509 }
3510out:
3511 if (!ret)
3512 ret = nr;
3513 return ret;
3514}
3515
3516/*
Chris Mason44871b12009-03-13 10:04:31 -04003517 * this is a helper for btrfs_insert_empty_items, the main goal here is
3518 * to save stack depth by doing the bulk of the work in a function
3519 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003520 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003521void setup_items_for_insert(struct btrfs_trans_handle *trans,
3522 struct btrfs_root *root, struct btrfs_path *path,
3523 struct btrfs_key *cpu_key, u32 *data_size,
3524 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003525{
Chris Mason5f39d392007-10-15 16:14:19 -04003526 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003527 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003528 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003529 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003530 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003531 struct extent_buffer *leaf;
3532 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003533
Chris Mason5f39d392007-10-15 16:14:19 -04003534 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003535 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003536
Chris Mason5f39d392007-10-15 16:14:19 -04003537 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003538 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003539
Chris Masonf25956c2008-09-12 15:32:53 -04003540 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003541 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003542 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003543 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003545 }
Chris Mason5f39d392007-10-15 16:14:19 -04003546
Chris Masonbe0e5c02007-01-26 15:51:26 -05003547 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003548 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003549
Chris Mason5f39d392007-10-15 16:14:19 -04003550 if (old_data < data_end) {
3551 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003552 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003553 slot, old_data, data_end);
3554 BUG_ON(1);
3555 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003556 /*
3557 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3558 */
3559 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003560 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003561 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003562
Chris Mason5f39d392007-10-15 16:14:19 -04003563 item = btrfs_item_nr(leaf, i);
3564 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003565 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003566 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003568 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003569 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003570 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571
3572 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003573 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003574 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003575 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003576 data_end = old_data;
3577 }
Chris Mason5f39d392007-10-15 16:14:19 -04003578
Chris Mason62e27492007-03-15 12:56:47 -04003579 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003580 for (i = 0; i < nr; i++) {
3581 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3582 btrfs_set_item_key(leaf, &disk_key, slot + i);
3583 item = btrfs_item_nr(leaf, slot + i);
3584 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3585 data_end -= data_size[i];
3586 btrfs_set_item_size(leaf, item, data_size[i]);
3587 }
Chris Mason44871b12009-03-13 10:04:31 -04003588
Chris Mason9c583092008-01-29 15:15:18 -05003589 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003590
Chris Mason5a01a2e2008-01-30 11:43:54 -05003591 if (slot == 0) {
3592 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003593 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003594 }
Chris Masonb9473432009-03-13 11:00:37 -04003595 btrfs_unlock_up_safe(path, 1);
3596 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003597
Chris Mason5f39d392007-10-15 16:14:19 -04003598 if (btrfs_leaf_free_space(root, leaf) < 0) {
3599 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003601 }
Chris Mason44871b12009-03-13 10:04:31 -04003602}
3603
3604/*
3605 * Given a key and some data, insert items into the tree.
3606 * This does all the path init required, making room in the tree if needed.
3607 */
3608int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3609 struct btrfs_root *root,
3610 struct btrfs_path *path,
3611 struct btrfs_key *cpu_key, u32 *data_size,
3612 int nr)
3613{
Chris Mason44871b12009-03-13 10:04:31 -04003614 int ret = 0;
3615 int slot;
3616 int i;
3617 u32 total_size = 0;
3618 u32 total_data = 0;
3619
3620 for (i = 0; i < nr; i++)
3621 total_data += data_size[i];
3622
3623 total_size = total_data + (nr * sizeof(struct btrfs_item));
3624 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3625 if (ret == 0)
3626 return -EEXIST;
3627 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003628 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003629
Chris Mason44871b12009-03-13 10:04:31 -04003630 slot = path->slots[0];
3631 BUG_ON(slot < 0);
3632
Jeff Mahoney143bede2012-03-01 14:56:26 +01003633 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003634 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003635 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003636}
3637
3638/*
3639 * Given a key and some data, insert an item into the tree.
3640 * This does all the path init required, making room in the tree if needed.
3641 */
Chris Masone089f052007-03-16 16:20:31 -04003642int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3643 *root, struct btrfs_key *cpu_key, void *data, u32
3644 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003645{
3646 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003647 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003648 struct extent_buffer *leaf;
3649 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003650
Chris Mason2c90e5d2007-04-02 10:50:19 -04003651 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003652 if (!path)
3653 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003654 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003655 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003656 leaf = path->nodes[0];
3657 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3658 write_extent_buffer(leaf, data, ptr, data_size);
3659 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003660 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003661 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003662 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003663}
3664
Chris Mason74123bd2007-02-02 11:05:29 -05003665/*
Chris Mason5de08d72007-02-24 06:24:44 -05003666 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003667 *
Chris Masond352ac62008-09-29 15:18:18 -04003668 * the tree should have been previously balanced so the deletion does not
3669 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003670 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003671static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3672 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003673{
Chris Mason5f39d392007-10-15 16:14:19 -04003674 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003675 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003676
Chris Mason5f39d392007-10-15 16:14:19 -04003677 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003678 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003679 memmove_extent_buffer(parent,
3680 btrfs_node_key_ptr_offset(slot),
3681 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003682 sizeof(struct btrfs_key_ptr) *
3683 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003684 }
Chris Mason7518a232007-03-12 12:01:18 -04003685 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003686 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003687 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003688 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003689 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003690 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003691 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003692 struct btrfs_disk_key disk_key;
3693
3694 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003695 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003696 }
Chris Masond6025572007-03-30 14:27:56 -04003697 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003698}
3699
Chris Mason74123bd2007-02-02 11:05:29 -05003700/*
Chris Mason323ac952008-10-01 19:05:46 -04003701 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003702 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003703 *
3704 * This deletes the pointer in path->nodes[1] and frees the leaf
3705 * block extent. zero is returned if it all worked out, < 0 otherwise.
3706 *
3707 * The path must have already been setup for deleting the leaf, including
3708 * all the proper balancing. path->nodes[1] must be locked.
3709 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003710static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3711 struct btrfs_root *root,
3712 struct btrfs_path *path,
3713 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003714{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003715 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003716 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003717
Chris Mason4d081c42009-02-04 09:31:28 -05003718 /*
3719 * btrfs_free_extent is expensive, we want to make sure we
3720 * aren't holding any locks when we call it
3721 */
3722 btrfs_unlock_up_safe(path, 0);
3723
Yan, Zhengf0486c62010-05-16 10:46:25 -04003724 root_sub_used(root, leaf->len);
3725
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003726 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003727}
3728/*
Chris Mason74123bd2007-02-02 11:05:29 -05003729 * delete the item at the leaf level in path. If that empties
3730 * the leaf, remove it from the tree
3731 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003732int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3733 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003734{
Chris Mason5f39d392007-10-15 16:14:19 -04003735 struct extent_buffer *leaf;
3736 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003737 int last_off;
3738 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003739 int ret = 0;
3740 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003741 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003742 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003743
Chris Mason5f39d392007-10-15 16:14:19 -04003744 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003745 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3746
3747 for (i = 0; i < nr; i++)
3748 dsize += btrfs_item_size_nr(leaf, slot + i);
3749
Chris Mason5f39d392007-10-15 16:14:19 -04003750 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003751
Chris Mason85e21ba2008-01-29 15:11:36 -05003752 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003753 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
3755 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003756 data_end + dsize,
3757 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003758 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
Chris Mason85e21ba2008-01-29 15:11:36 -05003760 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003761 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003762
Chris Mason5f39d392007-10-15 16:14:19 -04003763 item = btrfs_item_nr(leaf, i);
3764 ioff = btrfs_item_offset(leaf, item);
3765 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003766 }
Chris Masondb945352007-10-15 16:15:53 -04003767
Chris Mason5f39d392007-10-15 16:14:19 -04003768 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003769 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003770 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003771 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003772 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003773 btrfs_set_header_nritems(leaf, nritems - nr);
3774 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003775
Chris Mason74123bd2007-02-02 11:05:29 -05003776 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003777 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003778 if (leaf == root->node) {
3779 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003780 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003781 btrfs_set_path_blocking(path);
3782 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003783 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003784 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003785 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003786 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003787 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003788 struct btrfs_disk_key disk_key;
3789
3790 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003791 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003792 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003793
Chris Mason74123bd2007-02-02 11:05:29 -05003794 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003795 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003796 /* push_leaf_left fixes the path.
3797 * make sure the path still points to our leaf
3798 * for possible call to del_ptr below
3799 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003800 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003801 extent_buffer_get(leaf);
3802
Chris Masonb9473432009-03-13 11:00:37 -04003803 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003804 wret = push_leaf_left(trans, root, path, 1, 1,
3805 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003806 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003807 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003808
3809 if (path->nodes[0] == leaf &&
3810 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003811 wret = push_leaf_right(trans, root, path, 1,
3812 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003813 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003814 ret = wret;
3815 }
Chris Mason5f39d392007-10-15 16:14:19 -04003816
3817 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003818 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003819 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003820 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003821 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003822 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003823 /* if we're still in the path, make sure
3824 * we're dirty. Otherwise, one of the
3825 * push_leaf functions must have already
3826 * dirtied this buffer
3827 */
3828 if (path->nodes[0] == leaf)
3829 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003830 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003831 }
Chris Masond5719762007-03-23 10:01:08 -04003832 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003833 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834 }
3835 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003836 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003837}
3838
Chris Mason97571fd2007-02-24 13:39:08 -05003839/*
Chris Mason925baed2008-06-25 16:01:30 -04003840 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003841 * returns 0 if it found something or 1 if there are no lesser leaves.
3842 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003843 *
3844 * This may release the path, and so you may lose any locks held at the
3845 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003846 */
3847int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3848{
Chris Mason925baed2008-06-25 16:01:30 -04003849 struct btrfs_key key;
3850 struct btrfs_disk_key found_key;
3851 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003852
Chris Mason925baed2008-06-25 16:01:30 -04003853 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003854
Chris Mason925baed2008-06-25 16:01:30 -04003855 if (key.offset > 0)
3856 key.offset--;
3857 else if (key.type > 0)
3858 key.type--;
3859 else if (key.objectid > 0)
3860 key.objectid--;
3861 else
3862 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003863
David Sterbab3b4aa72011-04-21 01:20:15 +02003864 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003865 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3866 if (ret < 0)
3867 return ret;
3868 btrfs_item_key(path->nodes[0], &found_key, 0);
3869 ret = comp_keys(&found_key, &key);
3870 if (ret < 0)
3871 return 0;
3872 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003873}
3874
Chris Mason3f157a22008-06-25 16:01:31 -04003875/*
3876 * A helper function to walk down the tree starting at min_key, and looking
3877 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003878 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003879 *
3880 * This does not cow, but it does stuff the starting key it finds back
3881 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3882 * key and get a writable path.
3883 *
3884 * This does lock as it descends, and path->keep_locks should be set
3885 * to 1 by the caller.
3886 *
3887 * This honors path->lowest_level to prevent descent past a given level
3888 * of the tree.
3889 *
Chris Masond352ac62008-09-29 15:18:18 -04003890 * min_trans indicates the oldest transaction that you are interested
3891 * in walking through. Any nodes or leaves older than min_trans are
3892 * skipped over (without reading them).
3893 *
Chris Mason3f157a22008-06-25 16:01:31 -04003894 * returns zero if something useful was found, < 0 on error and 1 if there
3895 * was nothing in the tree that matched the search criteria.
3896 */
3897int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003898 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003899 struct btrfs_path *path, int cache_only,
3900 u64 min_trans)
3901{
3902 struct extent_buffer *cur;
3903 struct btrfs_key found_key;
3904 int slot;
Yan96524802008-07-24 12:19:49 -04003905 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003906 u32 nritems;
3907 int level;
3908 int ret = 1;
3909
Chris Mason934d3752008-12-08 16:43:10 -05003910 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003911again:
Chris Masonbd681512011-07-16 15:23:14 -04003912 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003913 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003914 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003915 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003916 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003917
3918 if (btrfs_header_generation(cur) < min_trans) {
3919 ret = 1;
3920 goto out;
3921 }
Chris Masond3977122009-01-05 21:25:51 -05003922 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003923 nritems = btrfs_header_nritems(cur);
3924 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003925 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003926
Chris Mason323ac952008-10-01 19:05:46 -04003927 /* at the lowest level, we're done, setup the path and exit */
3928 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003929 if (slot >= nritems)
3930 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003931 ret = 0;
3932 path->slots[level] = slot;
3933 btrfs_item_key_to_cpu(cur, &found_key, slot);
3934 goto out;
3935 }
Yan96524802008-07-24 12:19:49 -04003936 if (sret && slot > 0)
3937 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003938 /*
3939 * check this node pointer against the cache_only and
3940 * min_trans parameters. If it isn't in cache or is too
3941 * old, skip to the next one.
3942 */
Chris Masond3977122009-01-05 21:25:51 -05003943 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003944 u64 blockptr;
3945 u64 gen;
3946 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003947 struct btrfs_disk_key disk_key;
3948
Chris Mason3f157a22008-06-25 16:01:31 -04003949 blockptr = btrfs_node_blockptr(cur, slot);
3950 gen = btrfs_node_ptr_generation(cur, slot);
3951 if (gen < min_trans) {
3952 slot++;
3953 continue;
3954 }
3955 if (!cache_only)
3956 break;
3957
Chris Masone02119d2008-09-05 16:13:11 -04003958 if (max_key) {
3959 btrfs_node_key(cur, &disk_key, slot);
3960 if (comp_keys(&disk_key, max_key) >= 0) {
3961 ret = 1;
3962 goto out;
3963 }
3964 }
3965
Chris Mason3f157a22008-06-25 16:01:31 -04003966 tmp = btrfs_find_tree_block(root, blockptr,
3967 btrfs_level_size(root, level - 1));
3968
3969 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3970 free_extent_buffer(tmp);
3971 break;
3972 }
3973 if (tmp)
3974 free_extent_buffer(tmp);
3975 slot++;
3976 }
Chris Masone02119d2008-09-05 16:13:11 -04003977find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003978 /*
3979 * we didn't find a candidate key in this node, walk forward
3980 * and find another one
3981 */
3982 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003983 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003984 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003985 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003986 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003987 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003988 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003989 goto again;
3990 } else {
3991 goto out;
3992 }
3993 }
3994 /* save our key for returning back */
3995 btrfs_node_key_to_cpu(cur, &found_key, slot);
3996 path->slots[level] = slot;
3997 if (level == path->lowest_level) {
3998 ret = 0;
3999 unlock_up(path, level, 1);
4000 goto out;
4001 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004002 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004003 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004004 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004005
Chris Masonbd681512011-07-16 15:23:14 -04004006 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004007
Chris Masonbd681512011-07-16 15:23:14 -04004008 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004009 path->nodes[level - 1] = cur;
4010 unlock_up(path, level, 1);
Chris Masonbd681512011-07-16 15:23:14 -04004011 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004012 }
4013out:
4014 if (ret == 0)
4015 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004016 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004017 return ret;
4018}
4019
4020/*
4021 * this is similar to btrfs_next_leaf, but does not try to preserve
4022 * and fixup the path. It looks for and returns the next key in the
4023 * tree based on the current path and the cache_only and min_trans
4024 * parameters.
4025 *
4026 * 0 is returned if another key is found, < 0 if there are any errors
4027 * and 1 is returned if there are no higher keys in the tree
4028 *
4029 * path->keep_locks should be set to 1 on the search made before
4030 * calling this function.
4031 */
Chris Masone7a84562008-06-25 16:01:31 -04004032int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004033 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004034 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004035{
Chris Masone7a84562008-06-25 16:01:31 -04004036 int slot;
4037 struct extent_buffer *c;
4038
Chris Mason934d3752008-12-08 16:43:10 -05004039 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004040 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004041 if (!path->nodes[level])
4042 return 1;
4043
4044 slot = path->slots[level] + 1;
4045 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004046next:
Chris Masone7a84562008-06-25 16:01:31 -04004047 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004048 int ret;
4049 int orig_lowest;
4050 struct btrfs_key cur_key;
4051 if (level + 1 >= BTRFS_MAX_LEVEL ||
4052 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004053 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004054
4055 if (path->locks[level + 1]) {
4056 level++;
4057 continue;
4058 }
4059
4060 slot = btrfs_header_nritems(c) - 1;
4061 if (level == 0)
4062 btrfs_item_key_to_cpu(c, &cur_key, slot);
4063 else
4064 btrfs_node_key_to_cpu(c, &cur_key, slot);
4065
4066 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004067 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004068 path->lowest_level = level;
4069 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4070 0, 0);
4071 path->lowest_level = orig_lowest;
4072 if (ret < 0)
4073 return ret;
4074
4075 c = path->nodes[level];
4076 slot = path->slots[level];
4077 if (ret == 0)
4078 slot++;
4079 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004080 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004081
Chris Masone7a84562008-06-25 16:01:31 -04004082 if (level == 0)
4083 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004084 else {
4085 u64 blockptr = btrfs_node_blockptr(c, slot);
4086 u64 gen = btrfs_node_ptr_generation(c, slot);
4087
4088 if (cache_only) {
4089 struct extent_buffer *cur;
4090 cur = btrfs_find_tree_block(root, blockptr,
4091 btrfs_level_size(root, level - 1));
4092 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4093 slot++;
4094 if (cur)
4095 free_extent_buffer(cur);
4096 goto next;
4097 }
4098 free_extent_buffer(cur);
4099 }
4100 if (gen < min_trans) {
4101 slot++;
4102 goto next;
4103 }
Chris Masone7a84562008-06-25 16:01:31 -04004104 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004105 }
Chris Masone7a84562008-06-25 16:01:31 -04004106 return 0;
4107 }
4108 return 1;
4109}
4110
Chris Mason7bb86312007-12-11 09:25:06 -05004111/*
Chris Mason925baed2008-06-25 16:01:30 -04004112 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004113 * returns 0 if it found something or 1 if there are no greater leaves.
4114 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004115 */
Chris Mason234b63a2007-03-13 10:46:10 -04004116int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004117{
4118 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004119 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004120 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004121 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004122 struct btrfs_key key;
4123 u32 nritems;
4124 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004125 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004126 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004127
4128 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004129 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004130 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004131
Chris Mason8e73f272009-04-03 10:14:18 -04004132 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4133again:
4134 level = 1;
4135 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004136 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004137 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004138
Chris Masona2135012008-06-25 16:01:30 -04004139 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004140 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004141
Chris Mason925baed2008-06-25 16:01:30 -04004142 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4143 path->keep_locks = 0;
4144
4145 if (ret < 0)
4146 return ret;
4147
Chris Masona2135012008-06-25 16:01:30 -04004148 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004149 /*
4150 * by releasing the path above we dropped all our locks. A balance
4151 * could have added more items next to the key that used to be
4152 * at the very end of the block. So, check again here and
4153 * advance the path if there are now more items available.
4154 */
Chris Masona2135012008-06-25 16:01:30 -04004155 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004156 if (ret == 0)
4157 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004158 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004159 goto done;
4160 }
Chris Masond97e63b2007-02-20 16:40:44 -05004161
Chris Masond3977122009-01-05 21:25:51 -05004162 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004163 if (!path->nodes[level]) {
4164 ret = 1;
4165 goto done;
4166 }
Chris Mason5f39d392007-10-15 16:14:19 -04004167
Chris Masond97e63b2007-02-20 16:40:44 -05004168 slot = path->slots[level] + 1;
4169 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004170 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004171 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004172 if (level == BTRFS_MAX_LEVEL) {
4173 ret = 1;
4174 goto done;
4175 }
Chris Masond97e63b2007-02-20 16:40:44 -05004176 continue;
4177 }
Chris Mason5f39d392007-10-15 16:14:19 -04004178
Chris Mason925baed2008-06-25 16:01:30 -04004179 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004180 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004181 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004182 }
Chris Mason5f39d392007-10-15 16:14:19 -04004183
Chris Mason8e73f272009-04-03 10:14:18 -04004184 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004185 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004186 ret = read_block_for_search(NULL, root, path, &next, level,
4187 slot, &key);
4188 if (ret == -EAGAIN)
4189 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004190
Chris Mason76a05b32009-05-14 13:24:30 -04004191 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004192 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004193 goto done;
4194 }
4195
Chris Mason5cd57b22008-06-25 16:01:30 -04004196 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004197 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004198 if (!ret) {
4199 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004200 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004201 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004202 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004203 }
Chris Mason31533fb2011-07-26 16:01:59 -04004204 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004205 }
Chris Masond97e63b2007-02-20 16:40:44 -05004206 break;
4207 }
4208 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004209 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004210 level--;
4211 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004212 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004213 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004214
Chris Mason5f39d392007-10-15 16:14:19 -04004215 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004216 path->nodes[level] = next;
4217 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004218 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004219 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004220 if (!level)
4221 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004222
Chris Mason8e73f272009-04-03 10:14:18 -04004223 ret = read_block_for_search(NULL, root, path, &next, level,
4224 0, &key);
4225 if (ret == -EAGAIN)
4226 goto again;
4227
Chris Mason76a05b32009-05-14 13:24:30 -04004228 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004229 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004230 goto done;
4231 }
4232
Chris Mason5cd57b22008-06-25 16:01:30 -04004233 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004234 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004235 if (!ret) {
4236 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004237 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004238 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004239 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004240 }
Chris Mason31533fb2011-07-26 16:01:59 -04004241 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004242 }
Chris Masond97e63b2007-02-20 16:40:44 -05004243 }
Chris Mason8e73f272009-04-03 10:14:18 -04004244 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004245done:
4246 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004247 path->leave_spinning = old_spinning;
4248 if (!old_spinning)
4249 btrfs_set_path_blocking(path);
4250
4251 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004252}
Chris Mason0b86a832008-03-24 15:01:56 -04004253
Chris Mason3f157a22008-06-25 16:01:31 -04004254/*
4255 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4256 * searching until it gets past min_objectid or finds an item of 'type'
4257 *
4258 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4259 */
Chris Mason0b86a832008-03-24 15:01:56 -04004260int btrfs_previous_item(struct btrfs_root *root,
4261 struct btrfs_path *path, u64 min_objectid,
4262 int type)
4263{
4264 struct btrfs_key found_key;
4265 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004266 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004267 int ret;
4268
Chris Masond3977122009-01-05 21:25:51 -05004269 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004270 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004271 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004272 ret = btrfs_prev_leaf(root, path);
4273 if (ret != 0)
4274 return ret;
4275 } else {
4276 path->slots[0]--;
4277 }
4278 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004279 nritems = btrfs_header_nritems(leaf);
4280 if (nritems == 0)
4281 return 1;
4282 if (path->slots[0] == nritems)
4283 path->slots[0]--;
4284
Chris Mason0b86a832008-03-24 15:01:56 -04004285 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004286 if (found_key.objectid < min_objectid)
4287 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004288 if (found_key.type == type)
4289 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004290 if (found_key.objectid == min_objectid &&
4291 found_key.type < type)
4292 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004293 }
4294 return 1;
4295}