blob: 36e16bd507984df09b335d97a6b898ddd458bf91 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010039static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone089f052007-03-16 16:20:31 -040040 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040057 if (!p->nodes[i] || !p->locks[i])
58 continue;
59 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60 if (p->locks[i] == BTRFS_READ_LOCK)
61 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62 else if (p->locks[i] == BTRFS_WRITE_LOCK)
63 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050064 }
65}
66
67/*
68 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050069 *
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
73 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 */
Chris Mason4008c042009-02-12 14:09:45 -050075noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040076 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050077{
78 int i;
Chris Mason4008c042009-02-12 14:09:45 -050079
80#ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
85 * the path blocking.
86 */
Chris Masonbd681512011-07-16 15:23:14 -040087 if (held) {
88 btrfs_set_lock_blocking_rw(held, held_rw);
89 if (held_rw == BTRFS_WRITE_LOCK)
90 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91 else if (held_rw == BTRFS_READ_LOCK)
92 held_rw = BTRFS_READ_LOCK_BLOCKING;
93 }
Chris Mason4008c042009-02-12 14:09:45 -050094 btrfs_set_path_blocking(p);
95#endif
96
97 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040098 if (p->nodes[i] && p->locks[i]) {
99 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101 p->locks[i] = BTRFS_WRITE_LOCK;
102 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103 p->locks[i] = BTRFS_READ_LOCK;
104 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500105 }
Chris Mason4008c042009-02-12 14:09:45 -0500106
107#ifdef CONFIG_DEBUG_LOCK_ALLOC
108 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400109 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500110#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400114void btrfs_free_path(struct btrfs_path *p)
115{
Jesper Juhlff175d52010-12-25 21:22:30 +0000116 if (!p)
117 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200118 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400119 kmem_cache_free(btrfs_path_cachep, p);
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
125 *
126 * It is safe to call this on paths that no locks or extent buffers held.
127 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200128noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500129{
130 int i;
Chris Masona2135012008-06-25 16:01:30 -0400131
Chris Mason234b63a2007-03-13 10:46:10 -0400132 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400135 continue;
136 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400137 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400138 p->locks[i] = 0;
139 }
Chris Mason5f39d392007-10-15 16:14:19 -0400140 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400141 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 }
143}
144
Chris Masond352ac62008-09-29 15:18:18 -0400145/*
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
149 * looping required.
150 *
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400158
159 rcu_read_lock();
160 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400161 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400162 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400163 return eb;
164}
165
Chris Masond352ac62008-09-29 15:18:18 -0400166/* loop around taking references on and locking the root node of the
167 * tree until you end up with a lock on the root. A locked buffer
168 * is returned, with a reference held.
169 */
Chris Mason925baed2008-06-25 16:01:30 -0400170struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
171{
172 struct extent_buffer *eb;
173
Chris Masond3977122009-01-05 21:25:51 -0500174 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400175 eb = btrfs_root_node(root);
176 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400177 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400178 break;
Chris Mason925baed2008-06-25 16:01:30 -0400179 btrfs_tree_unlock(eb);
180 free_extent_buffer(eb);
181 }
182 return eb;
183}
184
Chris Masonbd681512011-07-16 15:23:14 -0400185/* loop around taking references on and locking the root node of the
186 * tree until you end up with a lock on the root. A locked buffer
187 * is returned, with a reference held.
188 */
189struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190{
191 struct extent_buffer *eb;
192
193 while (1) {
194 eb = btrfs_root_node(root);
195 btrfs_tree_read_lock(eb);
196 if (eb == root->node)
197 break;
198 btrfs_tree_read_unlock(eb);
199 free_extent_buffer(eb);
200 }
201 return eb;
202}
203
Chris Masond352ac62008-09-29 15:18:18 -0400204/* cowonly root (everything not a reference counted cow subvolume), just get
205 * put onto a simple dirty list. transaction.c walks this to make sure they
206 * get properly updated on disk.
207 */
Chris Mason0b86a832008-03-24 15:01:56 -0400208static void add_root_to_dirty_list(struct btrfs_root *root)
209{
210 if (root->track_dirty && list_empty(&root->dirty_list)) {
211 list_add(&root->dirty_list,
212 &root->fs_info->dirty_cowonly_roots);
213 }
214}
215
Chris Masond352ac62008-09-29 15:18:18 -0400216/*
217 * used by snapshot creation to make a copy of a root for a tree with
218 * a given objectid. The buffer with the new root node is returned in
219 * cow_ret, and this func returns zero on success or a negative error code.
220 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500221int btrfs_copy_root(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root,
223 struct extent_buffer *buf,
224 struct extent_buffer **cow_ret, u64 new_root_objectid)
225{
226 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 int ret = 0;
228 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400229 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500230
231 WARN_ON(root->ref_cows && trans->transid !=
232 root->fs_info->running_transaction->transid);
233 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
234
235 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400236 if (level == 0)
237 btrfs_item_key(buf, &disk_key, 0);
238 else
239 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400240
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400241 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
242 new_root_objectid, &disk_key, level,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200243 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400244 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 return PTR_ERR(cow);
246
247 copy_extent_buffer(cow, buf, 0, 0, cow->len);
248 btrfs_set_header_bytenr(cow, cow->start);
249 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400250 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
251 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
252 BTRFS_HEADER_FLAG_RELOC);
253 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
254 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
255 else
256 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500257
Yan Zheng2b820322008-11-17 21:11:30 -0500258 write_extent_buffer(cow, root->fs_info->fsid,
259 (unsigned long)btrfs_header_fsid(cow),
260 BTRFS_FSID_SIZE);
261
Chris Masonbe20aa92007-12-17 20:14:01 -0500262 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200264 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200266 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500267
Chris Masonbe20aa92007-12-17 20:14:01 -0500268 if (ret)
269 return ret;
270
271 btrfs_mark_buffer_dirty(cow);
272 *cow_ret = cow;
273 return 0;
274}
275
Chris Masond352ac62008-09-29 15:18:18 -0400276/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400277 * check if the tree block can be shared by multiple trees
278 */
279int btrfs_block_can_be_shared(struct btrfs_root *root,
280 struct extent_buffer *buf)
281{
282 /*
283 * Tree blocks not in refernece counted trees and tree roots
284 * are never shared. If a block was allocated after the last
285 * snapshot and the block was not allocated by tree relocation,
286 * we know the block is not shared.
287 */
288 if (root->ref_cows &&
289 buf != root->node && buf != root->commit_root &&
290 (btrfs_header_generation(buf) <=
291 btrfs_root_last_snapshot(&root->root_item) ||
292 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
293 return 1;
294#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
295 if (root->ref_cows &&
296 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
297 return 1;
298#endif
299 return 0;
300}
301
302static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
303 struct btrfs_root *root,
304 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400305 struct extent_buffer *cow,
306 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400307{
308 u64 refs;
309 u64 owner;
310 u64 flags;
311 u64 new_flags = 0;
312 int ret;
313
314 /*
315 * Backrefs update rules:
316 *
317 * Always use full backrefs for extent pointers in tree block
318 * allocated by tree relocation.
319 *
320 * If a shared tree block is no longer referenced by its owner
321 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
322 * use full backrefs for extent pointers in tree block.
323 *
324 * If a tree block is been relocating
325 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
326 * use full backrefs for extent pointers in tree block.
327 * The reason for this is some operations (such as drop tree)
328 * are only allowed for blocks use full backrefs.
329 */
330
331 if (btrfs_block_can_be_shared(root, buf)) {
332 ret = btrfs_lookup_extent_info(trans, root, buf->start,
333 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700334 if (ret)
335 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700336 if (refs == 0) {
337 ret = -EROFS;
338 btrfs_std_error(root->fs_info, ret);
339 return ret;
340 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400341 } else {
342 refs = 1;
343 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
344 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
345 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
346 else
347 flags = 0;
348 }
349
350 owner = btrfs_header_owner(buf);
351 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
352 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
353
354 if (refs > 1) {
355 if ((owner == root->root_key.objectid ||
356 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
357 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200358 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 BUG_ON(ret);
360
361 if (root->root_key.objectid ==
362 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200363 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400364 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200365 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400366 BUG_ON(ret);
367 }
368 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
369 } else {
370
371 if (root->root_key.objectid ==
372 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200373 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400374 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200375 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400376 BUG_ON(ret);
377 }
378 if (new_flags != 0) {
379 ret = btrfs_set_disk_extent_flags(trans, root,
380 buf->start,
381 buf->len,
382 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700383 if (ret)
384 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400385 }
386 } else {
387 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
388 if (root->root_key.objectid ==
389 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200390 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400391 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200392 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400393 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200394 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400395 BUG_ON(ret);
396 }
397 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400398 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400399 }
400 return 0;
401}
402
403/*
Chris Masond3977122009-01-05 21:25:51 -0500404 * does the dirty work in cow of a single block. The parent block (if
405 * supplied) is updated to point to the new cow copy. The new buffer is marked
406 * dirty and returned locked. If you modify the block it needs to be marked
407 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400408 *
409 * search_start -- an allocation hint for the new block
410 *
Chris Masond3977122009-01-05 21:25:51 -0500411 * empty_size -- a hint that you plan on doing more cow. This is the size in
412 * bytes the allocator should try to find free next to the block it returns.
413 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400414 */
Chris Masond3977122009-01-05 21:25:51 -0500415static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400416 struct btrfs_root *root,
417 struct extent_buffer *buf,
418 struct extent_buffer *parent, int parent_slot,
419 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400420 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400421{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400422 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400423 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700424 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400425 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400426 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400427 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400428
Chris Mason925baed2008-06-25 16:01:30 -0400429 if (*cow_ret == buf)
430 unlock_orig = 1;
431
Chris Masonb9447ef82009-03-09 11:45:38 -0400432 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400433
Chris Mason7bb86312007-12-11 09:25:06 -0500434 WARN_ON(root->ref_cows && trans->transid !=
435 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400436 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400437
Chris Mason7bb86312007-12-11 09:25:06 -0500438 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400439
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400440 if (level == 0)
441 btrfs_item_key(buf, &disk_key, 0);
442 else
443 btrfs_node_key(buf, &disk_key, 0);
444
445 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
446 if (parent)
447 parent_start = parent->start;
448 else
449 parent_start = 0;
450 } else
451 parent_start = 0;
452
453 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
454 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200455 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400456 if (IS_ERR(cow))
457 return PTR_ERR(cow);
458
Chris Masonb4ce94d2009-02-04 09:25:08 -0500459 /* cow is set to blocking by btrfs_init_new_buffer */
460
Chris Mason5f39d392007-10-15 16:14:19 -0400461 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400462 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400464 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
465 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
466 BTRFS_HEADER_FLAG_RELOC);
467 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
468 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
469 else
470 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400471
Yan Zheng2b820322008-11-17 21:11:30 -0500472 write_extent_buffer(cow, root->fs_info->fsid,
473 (unsigned long)btrfs_header_fsid(cow),
474 BTRFS_FSID_SIZE);
475
Mark Fashehbe1a5562011-08-08 13:20:18 -0700476 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700477 if (ret) {
478 btrfs_std_error(root->fs_info, ret);
479 return ret;
480 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400481
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400482 if (root->ref_cows)
483 btrfs_reloc_cow_block(trans, root, buf, cow);
484
Chris Mason6702ed42007-08-07 16:15:09 -0400485 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400486 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400487 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
488 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
489 parent_start = buf->start;
490 else
491 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400492
Chris Mason5f39d392007-10-15 16:14:19 -0400493 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400494 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400495
Yan, Zhengf0486c62010-05-16 10:46:25 -0400496 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200497 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400498 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400499 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400500 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400501 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
502 parent_start = parent->start;
503 else
504 parent_start = 0;
505
506 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400507 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400508 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500509 btrfs_set_node_ptr_generation(parent, parent_slot,
510 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400511 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400512 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200513 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400514 }
Chris Mason925baed2008-06-25 16:01:30 -0400515 if (unlock_orig)
516 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400517 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400518 btrfs_mark_buffer_dirty(cow);
519 *cow_ret = cow;
520 return 0;
521}
522
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400523static inline int should_cow_block(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root,
525 struct extent_buffer *buf)
526{
Liu Bof1ebcc72011-11-14 20:48:06 -0500527 /* ensure we can see the force_cow */
528 smp_rmb();
529
530 /*
531 * We do not need to cow a block if
532 * 1) this block is not created or changed in this transaction;
533 * 2) this block does not belong to TREE_RELOC tree;
534 * 3) the root is not forced COW.
535 *
536 * What is forced COW:
537 * when we create snapshot during commiting the transaction,
538 * after we've finished coping src root, we must COW the shared
539 * block to ensure the metadata consistency.
540 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400541 if (btrfs_header_generation(buf) == trans->transid &&
542 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
543 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500544 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
545 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400546 return 0;
547 return 1;
548}
549
Chris Masond352ac62008-09-29 15:18:18 -0400550/*
551 * cows a single block, see __btrfs_cow_block for the real work.
552 * This version of it has extra checks so that a block isn't cow'd more than
553 * once per transaction, as long as it hasn't been written yet
554 */
Chris Masond3977122009-01-05 21:25:51 -0500555noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400556 struct btrfs_root *root, struct extent_buffer *buf,
557 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400558 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500559{
Chris Mason6702ed42007-08-07 16:15:09 -0400560 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400561 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500562
Chris Masonccd467d2007-06-28 15:57:36 -0400563 if (trans->transaction != root->fs_info->running_transaction) {
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)
Chris Masonccd467d2007-06-28 15:57:36 -0400567 root->fs_info->running_transaction->transid);
568 WARN_ON(1);
569 }
570 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500571 printk(KERN_CRIT "trans %llu running %llu\n",
572 (unsigned long long)trans->transid,
573 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400574 WARN_ON(1);
575 }
Chris Masondc17ff82008-01-08 15:46:30 -0500576
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400577 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500578 *cow_ret = buf;
579 return 0;
580 }
Chris Masonc4876852009-02-04 09:24:25 -0500581
Chris Mason0b86a832008-03-24 15:01:56 -0400582 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500583
584 if (parent)
585 btrfs_set_lock_blocking(parent);
586 btrfs_set_lock_blocking(buf);
587
Chris Masonf510cfe2007-10-15 16:14:48 -0400588 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400589 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000590
591 trace_btrfs_cow_block(root, buf, *cow_ret);
592
Chris Masonf510cfe2007-10-15 16:14:48 -0400593 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400594}
595
Chris Masond352ac62008-09-29 15:18:18 -0400596/*
597 * helper function for defrag to decide if two blocks pointed to by a
598 * node are actually close by
599 */
Chris Mason6b800532007-10-15 16:17:34 -0400600static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400601{
Chris Mason6b800532007-10-15 16:17:34 -0400602 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400603 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400604 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400605 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500606 return 0;
607}
608
Chris Mason081e9572007-11-06 10:26:24 -0500609/*
610 * compare two keys in a memcmp fashion
611 */
612static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
613{
614 struct btrfs_key k1;
615
616 btrfs_disk_key_to_cpu(&k1, disk);
617
Diego Calleja20736ab2009-07-24 11:06:52 -0400618 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500619}
620
Josef Bacikf3465ca2008-11-12 14:19:50 -0500621/*
622 * same as comp_keys only with two btrfs_key's
623 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400624int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500625{
626 if (k1->objectid > k2->objectid)
627 return 1;
628 if (k1->objectid < k2->objectid)
629 return -1;
630 if (k1->type > k2->type)
631 return 1;
632 if (k1->type < k2->type)
633 return -1;
634 if (k1->offset > k2->offset)
635 return 1;
636 if (k1->offset < k2->offset)
637 return -1;
638 return 0;
639}
Chris Mason081e9572007-11-06 10:26:24 -0500640
Chris Masond352ac62008-09-29 15:18:18 -0400641/*
642 * this is used by the defrag code to go through all the
643 * leaves pointed to by a node and reallocate them so that
644 * disk order is close to key order
645 */
Chris Mason6702ed42007-08-07 16:15:09 -0400646int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400647 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400648 int start_slot, int cache_only, u64 *last_ret,
649 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400650{
Chris Mason6b800532007-10-15 16:17:34 -0400651 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400652 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400653 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400654 u64 search_start = *last_ret;
655 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400656 u64 other;
657 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400658 int end_slot;
659 int i;
660 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400661 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400662 int uptodate;
663 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500664 int progress_passed = 0;
665 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400666
Chris Mason5708b952007-10-25 15:43:18 -0400667 parent_level = btrfs_header_level(parent);
668 if (cache_only && parent_level != 1)
669 return 0;
670
Chris Masond3977122009-01-05 21:25:51 -0500671 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400672 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500673 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400674 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400675
Chris Mason6b800532007-10-15 16:17:34 -0400676 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400677 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400678 end_slot = parent_nritems;
679
680 if (parent_nritems == 1)
681 return 0;
682
Chris Masonb4ce94d2009-02-04 09:25:08 -0500683 btrfs_set_lock_blocking(parent);
684
Chris Mason6702ed42007-08-07 16:15:09 -0400685 for (i = start_slot; i < end_slot; i++) {
686 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400687
Chris Mason081e9572007-11-06 10:26:24 -0500688 btrfs_node_key(parent, &disk_key, i);
689 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
690 continue;
691
692 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400693 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400694 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400695 if (last_block == 0)
696 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400697
Chris Mason6702ed42007-08-07 16:15:09 -0400698 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400699 other = btrfs_node_blockptr(parent, i - 1);
700 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400701 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400702 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400703 other = btrfs_node_blockptr(parent, i + 1);
704 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400705 }
Chris Masone9d0b132007-08-10 14:06:19 -0400706 if (close) {
707 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400708 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400709 }
Chris Mason6702ed42007-08-07 16:15:09 -0400710
Chris Mason6b800532007-10-15 16:17:34 -0400711 cur = btrfs_find_tree_block(root, blocknr, blocksize);
712 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400713 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400714 else
715 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400716 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400717 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400718 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400719 continue;
720 }
Chris Mason6b800532007-10-15 16:17:34 -0400721 if (!cur) {
722 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400723 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000724 if (!cur)
725 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400726 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400727 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400728 }
Chris Mason6702ed42007-08-07 16:15:09 -0400729 }
Chris Masone9d0b132007-08-10 14:06:19 -0400730 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400731 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400732
Chris Masone7a84562008-06-25 16:01:31 -0400733 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500734 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400735 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400736 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400737 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400738 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400739 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400740 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400741 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400742 break;
Yan252c38f2007-08-29 09:11:44 -0400743 }
Chris Masone7a84562008-06-25 16:01:31 -0400744 search_start = cur->start;
745 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400746 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400747 btrfs_tree_unlock(cur);
748 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400749 }
750 return err;
751}
752
Chris Mason74123bd2007-02-02 11:05:29 -0500753/*
754 * The leaf data grows from end-to-front in the node.
755 * this returns the address of the start of the last item,
756 * which is the stop of the leaf data stack
757 */
Chris Mason123abc82007-03-14 14:14:43 -0400758static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400759 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500760{
Chris Mason5f39d392007-10-15 16:14:19 -0400761 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500762 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400763 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400764 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500765}
766
Chris Masonaa5d6be2007-02-28 16:35:06 -0500767
Chris Mason74123bd2007-02-02 11:05:29 -0500768/*
Chris Mason5f39d392007-10-15 16:14:19 -0400769 * search for key in the extent_buffer. The items start at offset p,
770 * and they are item_size apart. There are 'max' items in p.
771 *
Chris Mason74123bd2007-02-02 11:05:29 -0500772 * the slot in the array is returned via slot, and it points to
773 * the place where you would insert key if it is not found in
774 * the array.
775 *
776 * slot may point to max if the key is bigger than all of the keys
777 */
Chris Masone02119d2008-09-05 16:13:11 -0400778static noinline int generic_bin_search(struct extent_buffer *eb,
779 unsigned long p,
780 int item_size, struct btrfs_key *key,
781 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500782{
783 int low = 0;
784 int high = max;
785 int mid;
786 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400787 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400788 struct btrfs_disk_key unaligned;
789 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400790 char *kaddr = NULL;
791 unsigned long map_start = 0;
792 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400793 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500794
Chris Masond3977122009-01-05 21:25:51 -0500795 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500796 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400797 offset = p + mid * item_size;
798
Chris Masona6591712011-07-19 12:04:14 -0400799 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400800 (offset + sizeof(struct btrfs_disk_key)) >
801 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500802
803 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400804 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400805 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400806
Chris Mason479965d2007-10-15 16:14:27 -0400807 if (!err) {
808 tmp = (struct btrfs_disk_key *)(kaddr + offset -
809 map_start);
810 } else {
811 read_extent_buffer(eb, &unaligned,
812 offset, sizeof(unaligned));
813 tmp = &unaligned;
814 }
815
Chris Mason5f39d392007-10-15 16:14:19 -0400816 } else {
817 tmp = (struct btrfs_disk_key *)(kaddr + offset -
818 map_start);
819 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500820 ret = comp_keys(tmp, key);
821
822 if (ret < 0)
823 low = mid + 1;
824 else if (ret > 0)
825 high = mid;
826 else {
827 *slot = mid;
828 return 0;
829 }
830 }
831 *slot = low;
832 return 1;
833}
834
Chris Mason97571fd2007-02-24 13:39:08 -0500835/*
836 * simple bin_search frontend that does the right thing for
837 * leaves vs nodes
838 */
Chris Mason5f39d392007-10-15 16:14:19 -0400839static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
840 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500841{
Chris Mason5f39d392007-10-15 16:14:19 -0400842 if (level == 0) {
843 return generic_bin_search(eb,
844 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400845 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400846 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400847 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500848 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400849 return generic_bin_search(eb,
850 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400851 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400852 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400853 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500854 }
855 return -1;
856}
857
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
859 int level, int *slot)
860{
861 return bin_search(eb, key, level, slot);
862}
863
Yan, Zhengf0486c62010-05-16 10:46:25 -0400864static void root_add_used(struct btrfs_root *root, u32 size)
865{
866 spin_lock(&root->accounting_lock);
867 btrfs_set_root_used(&root->root_item,
868 btrfs_root_used(&root->root_item) + size);
869 spin_unlock(&root->accounting_lock);
870}
871
872static void root_sub_used(struct btrfs_root *root, u32 size)
873{
874 spin_lock(&root->accounting_lock);
875 btrfs_set_root_used(&root->root_item,
876 btrfs_root_used(&root->root_item) - size);
877 spin_unlock(&root->accounting_lock);
878}
879
Chris Masond352ac62008-09-29 15:18:18 -0400880/* given a node and slot number, this reads the blocks it points to. The
881 * extent buffer is returned with a reference taken (but unlocked).
882 * NULL is returned on error.
883 */
Chris Masone02119d2008-09-05 16:13:11 -0400884static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400885 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500886{
Chris Masonca7a79a2008-05-12 12:59:19 -0400887 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500888 if (slot < 0)
889 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400890 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500891 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400892
893 BUG_ON(level == 0);
894
Chris Masondb945352007-10-15 16:15:53 -0400895 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400896 btrfs_level_size(root, level - 1),
897 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500898}
899
Chris Masond352ac62008-09-29 15:18:18 -0400900/*
901 * node level balancing, used to make sure nodes are in proper order for
902 * item deletion. We balance from the top down, so we have to make sure
903 * that a deletion won't leave an node completely empty later on.
904 */
Chris Masone02119d2008-09-05 16:13:11 -0400905static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500906 struct btrfs_root *root,
907 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500908{
Chris Mason5f39d392007-10-15 16:14:19 -0400909 struct extent_buffer *right = NULL;
910 struct extent_buffer *mid;
911 struct extent_buffer *left = NULL;
912 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500913 int ret = 0;
914 int wret;
915 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500916 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500917 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500918
919 if (level == 0)
920 return 0;
921
Chris Mason5f39d392007-10-15 16:14:19 -0400922 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500923
Chris Masonbd681512011-07-16 15:23:14 -0400924 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
925 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500926 WARN_ON(btrfs_header_generation(mid) != trans->transid);
927
Chris Mason1d4f8a02007-03-13 09:28:32 -0400928 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500929
Li Zefana05a9bb2011-09-06 16:55:34 +0800930 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400931 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800932 pslot = path->slots[level + 1];
933 }
Chris Masonbb803952007-03-01 12:04:21 -0500934
Chris Mason40689472007-03-17 14:29:23 -0400935 /*
936 * deal with the case where there is only one pointer in the root
937 * by promoting the node below to a root
938 */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 if (!parent) {
940 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500941
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500943 return 0;
944
945 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400946 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500947 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400948 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500949 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400950 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400951 if (ret) {
952 btrfs_tree_unlock(child);
953 free_extent_buffer(child);
954 goto enospc;
955 }
Yan2f375ab2008-02-01 14:58:07 -0500956
Chris Mason240f62c2011-03-23 14:54:42 -0400957 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400958
Chris Mason0b86a832008-03-24 15:01:56 -0400959 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400960 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500961
Chris Mason925baed2008-06-25 16:01:30 -0400962 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500963 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400964 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400965 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500966 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400967 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400968
969 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200970 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500971 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400972 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400973 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500974 }
Chris Mason5f39d392007-10-15 16:14:19 -0400975 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400976 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500977 return 0;
978
Andi Kleen559af822010-10-29 15:14:37 -0400979 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400980
Chris Mason5f39d392007-10-15 16:14:19 -0400981 left = read_node_slot(root, parent, pslot - 1);
982 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400983 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500984 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400985 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400986 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 if (wret) {
988 ret = wret;
989 goto enospc;
990 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400991 }
Chris Mason5f39d392007-10-15 16:14:19 -0400992 right = read_node_slot(root, parent, pslot + 1);
993 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400994 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500995 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400996 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400997 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400998 if (wret) {
999 ret = wret;
1000 goto enospc;
1001 }
1002 }
1003
1004 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001005 if (left) {
1006 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001007 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001008 if (wret < 0)
1009 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001010 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001011 }
Chris Mason79f95c82007-03-01 15:16:26 -05001012
1013 /*
1014 * then try to empty the right most buffer into the middle
1015 */
Chris Mason5f39d392007-10-15 16:14:19 -04001016 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001017 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001018 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001019 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001020 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001021 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001022 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001023 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001024 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001025 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001026 free_extent_buffer(right);
1027 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001028 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001029 struct btrfs_disk_key right_key;
1030 btrfs_node_key(right, &right_key, 0);
1031 btrfs_set_node_key(parent, &right_key, pslot + 1);
1032 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001033 }
1034 }
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001036 /*
1037 * we're not allowed to leave a node with one item in the
1038 * tree during a delete. A deletion from lower in the tree
1039 * could try to delete the only pointer in this node.
1040 * So, pull some keys from the left.
1041 * There has to be a left pointer at this point because
1042 * otherwise we would have pulled some pointers from the
1043 * right
1044 */
Chris Mason5f39d392007-10-15 16:14:19 -04001045 BUG_ON(!left);
1046 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001047 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001048 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001049 goto enospc;
1050 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001051 if (wret == 1) {
1052 wret = push_node_left(trans, root, left, mid, 1);
1053 if (wret < 0)
1054 ret = wret;
1055 }
Chris Mason79f95c82007-03-01 15:16:26 -05001056 BUG_ON(wret == 1);
1057 }
Chris Mason5f39d392007-10-15 16:14:19 -04001058 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001059 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001060 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001061 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001062 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001063 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001064 free_extent_buffer(mid);
1065 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001066 } else {
1067 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001068 struct btrfs_disk_key mid_key;
1069 btrfs_node_key(mid, &mid_key, 0);
1070 btrfs_set_node_key(parent, &mid_key, pslot);
1071 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001072 }
Chris Masonbb803952007-03-01 12:04:21 -05001073
Chris Mason79f95c82007-03-01 15:16:26 -05001074 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001075 if (left) {
1076 if (btrfs_header_nritems(left) > orig_slot) {
1077 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001078 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001079 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001080 path->slots[level + 1] -= 1;
1081 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001082 if (mid) {
1083 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001084 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001085 }
Chris Masonbb803952007-03-01 12:04:21 -05001086 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001087 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001088 path->slots[level] = orig_slot;
1089 }
1090 }
Chris Mason79f95c82007-03-01 15:16:26 -05001091 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001092 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001093 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001094 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001095enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001096 if (right) {
1097 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001098 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001099 }
1100 if (left) {
1101 if (path->nodes[level] != left)
1102 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001103 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001104 }
Chris Masonbb803952007-03-01 12:04:21 -05001105 return ret;
1106}
1107
Chris Masond352ac62008-09-29 15:18:18 -04001108/* Node balancing for insertion. Here we only split or push nodes around
1109 * when they are completely full. This is also done top down, so we
1110 * have to be pessimistic.
1111 */
Chris Masond3977122009-01-05 21:25:51 -05001112static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001113 struct btrfs_root *root,
1114 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001115{
Chris Mason5f39d392007-10-15 16:14:19 -04001116 struct extent_buffer *right = NULL;
1117 struct extent_buffer *mid;
1118 struct extent_buffer *left = NULL;
1119 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001120 int ret = 0;
1121 int wret;
1122 int pslot;
1123 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001124
1125 if (level == 0)
1126 return 1;
1127
Chris Mason5f39d392007-10-15 16:14:19 -04001128 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001129 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001130
Li Zefana05a9bb2011-09-06 16:55:34 +08001131 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001132 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001133 pslot = path->slots[level + 1];
1134 }
Chris Masone66f7092007-04-20 13:16:02 -04001135
Chris Mason5f39d392007-10-15 16:14:19 -04001136 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001137 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001138
Chris Mason5f39d392007-10-15 16:14:19 -04001139 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001140
1141 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001142 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001143 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001144
1145 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001146 btrfs_set_lock_blocking(left);
1147
Chris Mason5f39d392007-10-15 16:14:19 -04001148 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001149 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1150 wret = 1;
1151 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001152 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001153 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001154 if (ret)
1155 wret = 1;
1156 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001157 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001158 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001159 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001160 }
Chris Masone66f7092007-04-20 13:16:02 -04001161 if (wret < 0)
1162 ret = wret;
1163 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001164 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001165 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001166 btrfs_node_key(mid, &disk_key, 0);
1167 btrfs_set_node_key(parent, &disk_key, pslot);
1168 btrfs_mark_buffer_dirty(parent);
1169 if (btrfs_header_nritems(left) > orig_slot) {
1170 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001171 path->slots[level + 1] -= 1;
1172 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001173 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001174 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001175 } else {
1176 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001177 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001178 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001179 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001180 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001181 }
Chris Masone66f7092007-04-20 13:16:02 -04001182 return 0;
1183 }
Chris Mason925baed2008-06-25 16:01:30 -04001184 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001185 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001186 }
Chris Mason925baed2008-06-25 16:01:30 -04001187 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001188
1189 /*
1190 * then try to empty the right most buffer into the middle
1191 */
Chris Mason5f39d392007-10-15 16:14:19 -04001192 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001193 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001194
Chris Mason925baed2008-06-25 16:01:30 -04001195 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001196 btrfs_set_lock_blocking(right);
1197
Chris Mason5f39d392007-10-15 16:14:19 -04001198 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001199 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1200 wret = 1;
1201 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001202 ret = btrfs_cow_block(trans, root, right,
1203 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001204 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001205 if (ret)
1206 wret = 1;
1207 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001208 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001209 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001210 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001211 }
Chris Masone66f7092007-04-20 13:16:02 -04001212 if (wret < 0)
1213 ret = wret;
1214 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001215 struct btrfs_disk_key disk_key;
1216
1217 btrfs_node_key(right, &disk_key, 0);
1218 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1219 btrfs_mark_buffer_dirty(parent);
1220
1221 if (btrfs_header_nritems(mid) <= orig_slot) {
1222 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001223 path->slots[level + 1] += 1;
1224 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001225 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001226 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001227 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001228 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001229 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001230 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001231 }
Chris Masone66f7092007-04-20 13:16:02 -04001232 return 0;
1233 }
Chris Mason925baed2008-06-25 16:01:30 -04001234 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001235 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001236 }
Chris Masone66f7092007-04-20 13:16:02 -04001237 return 1;
1238}
1239
Chris Mason74123bd2007-02-02 11:05:29 -05001240/*
Chris Masond352ac62008-09-29 15:18:18 -04001241 * readahead one full node of leaves, finding things that are close
1242 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001243 */
Chris Masonc8c42862009-04-03 10:14:18 -04001244static void reada_for_search(struct btrfs_root *root,
1245 struct btrfs_path *path,
1246 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001247{
Chris Mason5f39d392007-10-15 16:14:19 -04001248 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001249 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001250 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001251 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001252 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001253 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001254 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001255 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001256 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001257 u32 nr;
1258 u32 blocksize;
1259 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001260
Chris Masona6b6e752007-10-15 16:22:39 -04001261 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001262 return;
1263
Chris Mason6702ed42007-08-07 16:15:09 -04001264 if (!path->nodes[level])
1265 return;
1266
Chris Mason5f39d392007-10-15 16:14:19 -04001267 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001268
Chris Mason3c69fae2007-08-07 15:52:22 -04001269 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001270 blocksize = btrfs_level_size(root, level - 1);
1271 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001272 if (eb) {
1273 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001274 return;
1275 }
1276
Chris Masona7175312009-01-22 09:23:10 -05001277 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001278
Chris Mason5f39d392007-10-15 16:14:19 -04001279 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001280 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001281
Chris Masond3977122009-01-05 21:25:51 -05001282 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001283 if (direction < 0) {
1284 if (nr == 0)
1285 break;
1286 nr--;
1287 } else if (direction > 0) {
1288 nr++;
1289 if (nr >= nritems)
1290 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001291 }
Chris Mason01f46652007-12-21 16:24:26 -05001292 if (path->reada < 0 && objectid) {
1293 btrfs_node_key(node, &disk_key, nr);
1294 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1295 break;
1296 }
Chris Mason6b800532007-10-15 16:17:34 -04001297 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001298 if ((search <= target && target - search <= 65536) ||
1299 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001300 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001301 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001302 nread += blocksize;
1303 }
1304 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001305 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001306 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001307 }
1308}
Chris Mason925baed2008-06-25 16:01:30 -04001309
Chris Masond352ac62008-09-29 15:18:18 -04001310/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001311 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1312 * cache
1313 */
1314static noinline int reada_for_balance(struct btrfs_root *root,
1315 struct btrfs_path *path, int level)
1316{
1317 int slot;
1318 int nritems;
1319 struct extent_buffer *parent;
1320 struct extent_buffer *eb;
1321 u64 gen;
1322 u64 block1 = 0;
1323 u64 block2 = 0;
1324 int ret = 0;
1325 int blocksize;
1326
Chris Mason8c594ea2009-04-20 15:50:10 -04001327 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001328 if (!parent)
1329 return 0;
1330
1331 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001332 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001333 blocksize = btrfs_level_size(root, level);
1334
1335 if (slot > 0) {
1336 block1 = btrfs_node_blockptr(parent, slot - 1);
1337 gen = btrfs_node_ptr_generation(parent, slot - 1);
1338 eb = btrfs_find_tree_block(root, block1, blocksize);
1339 if (eb && btrfs_buffer_uptodate(eb, gen))
1340 block1 = 0;
1341 free_extent_buffer(eb);
1342 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001343 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001344 block2 = btrfs_node_blockptr(parent, slot + 1);
1345 gen = btrfs_node_ptr_generation(parent, slot + 1);
1346 eb = btrfs_find_tree_block(root, block2, blocksize);
1347 if (eb && btrfs_buffer_uptodate(eb, gen))
1348 block2 = 0;
1349 free_extent_buffer(eb);
1350 }
1351 if (block1 || block2) {
1352 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001353
1354 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001355 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001356
1357 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001358 if (block1)
1359 readahead_tree_block(root, block1, blocksize, 0);
1360 if (block2)
1361 readahead_tree_block(root, block2, blocksize, 0);
1362
1363 if (block1) {
1364 eb = read_tree_block(root, block1, blocksize, 0);
1365 free_extent_buffer(eb);
1366 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001367 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001368 eb = read_tree_block(root, block2, blocksize, 0);
1369 free_extent_buffer(eb);
1370 }
1371 }
1372 return ret;
1373}
1374
1375
1376/*
Chris Masond3977122009-01-05 21:25:51 -05001377 * when we walk down the tree, it is usually safe to unlock the higher layers
1378 * in the tree. The exceptions are when our path goes through slot 0, because
1379 * operations on the tree might require changing key pointers higher up in the
1380 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001381 *
Chris Masond3977122009-01-05 21:25:51 -05001382 * callers might also have set path->keep_locks, which tells this code to keep
1383 * the lock if the path points to the last slot in the block. This is part of
1384 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001385 *
Chris Masond3977122009-01-05 21:25:51 -05001386 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1387 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001388 */
Chris Masone02119d2008-09-05 16:13:11 -04001389static noinline void unlock_up(struct btrfs_path *path, int level,
1390 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001391{
1392 int i;
1393 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001394 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001395 struct extent_buffer *t;
1396
1397 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1398 if (!path->nodes[i])
1399 break;
1400 if (!path->locks[i])
1401 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001402 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001403 skip_level = i + 1;
1404 continue;
1405 }
Chris Mason051e1b92008-06-25 16:01:30 -04001406 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001407 u32 nritems;
1408 t = path->nodes[i];
1409 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001410 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001411 skip_level = i + 1;
1412 continue;
1413 }
1414 }
Chris Mason051e1b92008-06-25 16:01:30 -04001415 if (skip_level < i && i >= lowest_unlock)
1416 no_skips = 1;
1417
Chris Mason925baed2008-06-25 16:01:30 -04001418 t = path->nodes[i];
1419 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001420 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001421 path->locks[i] = 0;
1422 }
1423 }
1424}
1425
Chris Mason3c69fae2007-08-07 15:52:22 -04001426/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001427 * This releases any locks held in the path starting at level and
1428 * going all the way up to the root.
1429 *
1430 * btrfs_search_slot will keep the lock held on higher nodes in a few
1431 * corner cases, such as COW of the block at slot zero in the node. This
1432 * ignores those rules, and it should only be called when there are no
1433 * more updates to be done higher up in the tree.
1434 */
1435noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1436{
1437 int i;
1438
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001439 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001440 return;
1441
1442 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1443 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001444 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001445 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001446 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001447 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001448 path->locks[i] = 0;
1449 }
1450}
1451
1452/*
Chris Masonc8c42862009-04-03 10:14:18 -04001453 * helper function for btrfs_search_slot. The goal is to find a block
1454 * in cache without setting the path to blocking. If we find the block
1455 * we return zero and the path is unchanged.
1456 *
1457 * If we can't find the block, we set the path blocking and do some
1458 * reada. -EAGAIN is returned and the search must be repeated.
1459 */
1460static int
1461read_block_for_search(struct btrfs_trans_handle *trans,
1462 struct btrfs_root *root, struct btrfs_path *p,
1463 struct extent_buffer **eb_ret, int level, int slot,
1464 struct btrfs_key *key)
1465{
1466 u64 blocknr;
1467 u64 gen;
1468 u32 blocksize;
1469 struct extent_buffer *b = *eb_ret;
1470 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001471 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001472
1473 blocknr = btrfs_node_blockptr(b, slot);
1474 gen = btrfs_node_ptr_generation(b, slot);
1475 blocksize = btrfs_level_size(root, level - 1);
1476
1477 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001478 if (tmp) {
1479 if (btrfs_buffer_uptodate(tmp, 0)) {
1480 if (btrfs_buffer_uptodate(tmp, gen)) {
1481 /*
1482 * we found an up to date block without
1483 * sleeping, return
1484 * right away
1485 */
1486 *eb_ret = tmp;
1487 return 0;
1488 }
1489 /* the pages were up to date, but we failed
1490 * the generation number check. Do a full
1491 * read for the generation number that is correct.
1492 * We must do this without dropping locks so
1493 * we can trust our generation number
1494 */
1495 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001496 btrfs_set_path_blocking(p);
1497
Chris Masoncb449212010-10-24 11:01:27 -04001498 tmp = read_tree_block(root, blocknr, blocksize, gen);
1499 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1500 *eb_ret = tmp;
1501 return 0;
1502 }
1503 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001504 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001505 return -EIO;
1506 }
Chris Masonc8c42862009-04-03 10:14:18 -04001507 }
1508
1509 /*
1510 * reduce lock contention at high levels
1511 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001512 * we read. Don't release the lock on the current
1513 * level because we need to walk this node to figure
1514 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001515 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001516 btrfs_unlock_up_safe(p, level + 1);
1517 btrfs_set_path_blocking(p);
1518
Chris Masoncb449212010-10-24 11:01:27 -04001519 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001520 if (p->reada)
1521 reada_for_search(root, p, level, slot, key->objectid);
1522
David Sterbab3b4aa72011-04-21 01:20:15 +02001523 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001524
1525 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001526 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001527 if (tmp) {
1528 /*
1529 * If the read above didn't mark this buffer up to date,
1530 * it will never end up being up to date. Set ret to EIO now
1531 * and give up so that our caller doesn't loop forever
1532 * on our EAGAINs.
1533 */
1534 if (!btrfs_buffer_uptodate(tmp, 0))
1535 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001536 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001537 }
1538 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001539}
1540
1541/*
1542 * helper function for btrfs_search_slot. This does all of the checks
1543 * for node-level blocks and does any balancing required based on
1544 * the ins_len.
1545 *
1546 * If no extra work was required, zero is returned. If we had to
1547 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1548 * start over
1549 */
1550static int
1551setup_nodes_for_search(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001553 struct extent_buffer *b, int level, int ins_len,
1554 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001555{
1556 int ret;
1557 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1558 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1559 int sret;
1560
Chris Masonbd681512011-07-16 15:23:14 -04001561 if (*write_lock_level < level + 1) {
1562 *write_lock_level = level + 1;
1563 btrfs_release_path(p);
1564 goto again;
1565 }
1566
Chris Masonc8c42862009-04-03 10:14:18 -04001567 sret = reada_for_balance(root, p, level);
1568 if (sret)
1569 goto again;
1570
1571 btrfs_set_path_blocking(p);
1572 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001573 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001574
1575 BUG_ON(sret > 0);
1576 if (sret) {
1577 ret = sret;
1578 goto done;
1579 }
1580 b = p->nodes[level];
1581 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001582 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001583 int sret;
1584
Chris Masonbd681512011-07-16 15:23:14 -04001585 if (*write_lock_level < level + 1) {
1586 *write_lock_level = level + 1;
1587 btrfs_release_path(p);
1588 goto again;
1589 }
1590
Chris Masonc8c42862009-04-03 10:14:18 -04001591 sret = reada_for_balance(root, p, level);
1592 if (sret)
1593 goto again;
1594
1595 btrfs_set_path_blocking(p);
1596 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001597 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001598
1599 if (sret) {
1600 ret = sret;
1601 goto done;
1602 }
1603 b = p->nodes[level];
1604 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001605 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001606 goto again;
1607 }
1608 BUG_ON(btrfs_header_nritems(b) == 1);
1609 }
1610 return 0;
1611
1612again:
1613 ret = -EAGAIN;
1614done:
1615 return ret;
1616}
1617
1618/*
Chris Mason74123bd2007-02-02 11:05:29 -05001619 * look for key in the tree. path is filled in with nodes along the way
1620 * if key is found, we return zero and you can find the item in the leaf
1621 * level of the path (level 0)
1622 *
1623 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001624 * be inserted, and 1 is returned. If there are other errors during the
1625 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001626 *
1627 * if ins_len > 0, nodes and leaves will be split as we walk down the
1628 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1629 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001630 */
Chris Masone089f052007-03-16 16:20:31 -04001631int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1632 *root, struct btrfs_key *key, struct btrfs_path *p, int
1633 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001634{
Chris Mason5f39d392007-10-15 16:14:19 -04001635 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001636 int slot;
1637 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001638 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001639 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001640 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001641 int root_lock;
1642 /* everything at write_lock_level or lower must be write locked */
1643 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001644 u8 lowest_level = 0;
1645
Chris Mason6702ed42007-08-07 16:15:09 -04001646 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001647 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001648 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001649
Chris Masonbd681512011-07-16 15:23:14 -04001650 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001651 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001652
Chris Masonbd681512011-07-16 15:23:14 -04001653 /* when we are removing items, we might have to go up to level
1654 * two as we update tree pointers Make sure we keep write
1655 * for those levels as well
1656 */
1657 write_lock_level = 2;
1658 } else if (ins_len > 0) {
1659 /*
1660 * for inserting items, make sure we have a write lock on
1661 * level 1 so we can update keys
1662 */
1663 write_lock_level = 1;
1664 }
1665
1666 if (!cow)
1667 write_lock_level = -1;
1668
1669 if (cow && (p->keep_locks || p->lowest_level))
1670 write_lock_level = BTRFS_MAX_LEVEL;
1671
Chris Masonbb803952007-03-01 12:04:21 -05001672again:
Chris Masonbd681512011-07-16 15:23:14 -04001673 /*
1674 * we try very hard to do read locks on the root
1675 */
1676 root_lock = BTRFS_READ_LOCK;
1677 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001679 /*
1680 * the commit roots are read only
1681 * so we always do read locks
1682 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001683 b = root->commit_root;
1684 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001685 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001687 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001688 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001689 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001690 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001691 level = btrfs_header_level(b);
1692 } else {
1693 /* we don't know the level of the root node
1694 * until we actually have it read locked
1695 */
1696 b = btrfs_read_lock_root_node(root);
1697 level = btrfs_header_level(b);
1698 if (level <= write_lock_level) {
1699 /* whoops, must trade for write lock */
1700 btrfs_tree_read_unlock(b);
1701 free_extent_buffer(b);
1702 b = btrfs_lock_root_node(root);
1703 root_lock = BTRFS_WRITE_LOCK;
1704
1705 /* the level might have changed, check again */
1706 level = btrfs_header_level(b);
1707 }
1708 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 }
Chris Masonbd681512011-07-16 15:23:14 -04001710 p->nodes[level] = b;
1711 if (!p->skip_locking)
1712 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001713
Chris Masoneb60cea2007-02-02 09:18:22 -05001714 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001715 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001716
1717 /*
1718 * setup the path here so we can release it under lock
1719 * contention with the cow code
1720 */
Chris Mason02217ed2007-03-02 16:08:05 -05001721 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001722 /*
1723 * if we don't really need to cow this block
1724 * then we don't want to set the path blocking,
1725 * so we test it here
1726 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001727 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001728 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001729
Chris Masonb4ce94d2009-02-04 09:25:08 -05001730 btrfs_set_path_blocking(p);
1731
Chris Masonbd681512011-07-16 15:23:14 -04001732 /*
1733 * must have write locks on this node and the
1734 * parent
1735 */
1736 if (level + 1 > write_lock_level) {
1737 write_lock_level = level + 1;
1738 btrfs_release_path(p);
1739 goto again;
1740 }
1741
Yan Zheng33c66f42009-07-22 09:59:00 -04001742 err = btrfs_cow_block(trans, root, b,
1743 p->nodes[level + 1],
1744 p->slots[level + 1], &b);
1745 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001746 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001747 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001748 }
Chris Mason02217ed2007-03-02 16:08:05 -05001749 }
Chris Mason65b51a02008-08-01 15:11:20 -04001750cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001751 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001752
Chris Masoneb60cea2007-02-02 09:18:22 -05001753 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001754 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001755
1756 /*
1757 * we have a lock on b and as long as we aren't changing
1758 * the tree, there is no way to for the items in b to change.
1759 * It is safe to drop the lock on our parent before we
1760 * go through the expensive btree search on b.
1761 *
1762 * If cow is true, then we might be changing slot zero,
1763 * which may require changing the parent. So, we can't
1764 * drop the lock until after we know which slot we're
1765 * operating on.
1766 */
1767 if (!cow)
1768 btrfs_unlock_up_safe(p, level + 1);
1769
Chris Mason5f39d392007-10-15 16:14:19 -04001770 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001771
Chris Mason5f39d392007-10-15 16:14:19 -04001772 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001773 int dec = 0;
1774 if (ret && slot > 0) {
1775 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001777 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001778 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001779 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001780 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001781 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001782 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001783 if (err) {
1784 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001785 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001786 }
Chris Masonc8c42862009-04-03 10:14:18 -04001787 b = p->nodes[level];
1788 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001789
Chris Masonbd681512011-07-16 15:23:14 -04001790 /*
1791 * slot 0 is special, if we change the key
1792 * we have to update the parent pointer
1793 * which means we must have a write lock
1794 * on the parent
1795 */
1796 if (slot == 0 && cow &&
1797 write_lock_level < level + 1) {
1798 write_lock_level = level + 1;
1799 btrfs_release_path(p);
1800 goto again;
1801 }
1802
Chris Masonf9efa9c2008-06-25 16:14:04 -04001803 unlock_up(p, level, lowest_unlock);
1804
Chris Mason925baed2008-06-25 16:01:30 -04001805 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001806 if (dec)
1807 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001808 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001809 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001810
Yan Zheng33c66f42009-07-22 09:59:00 -04001811 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001812 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001813 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001814 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001815 if (err) {
1816 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001817 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 }
Chris Mason76a05b32009-05-14 13:24:30 -04001819
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001821 level = btrfs_header_level(b);
1822 if (level <= write_lock_level) {
1823 err = btrfs_try_tree_write_lock(b);
1824 if (!err) {
1825 btrfs_set_path_blocking(p);
1826 btrfs_tree_lock(b);
1827 btrfs_clear_path_blocking(p, b,
1828 BTRFS_WRITE_LOCK);
1829 }
1830 p->locks[level] = BTRFS_WRITE_LOCK;
1831 } else {
1832 err = btrfs_try_tree_read_lock(b);
1833 if (!err) {
1834 btrfs_set_path_blocking(p);
1835 btrfs_tree_read_lock(b);
1836 btrfs_clear_path_blocking(p, b,
1837 BTRFS_READ_LOCK);
1838 }
1839 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001840 }
Chris Masonbd681512011-07-16 15:23:14 -04001841 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001842 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001843 } else {
1844 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001845 if (ins_len > 0 &&
1846 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001847 if (write_lock_level < 1) {
1848 write_lock_level = 1;
1849 btrfs_release_path(p);
1850 goto again;
1851 }
1852
Chris Masonb4ce94d2009-02-04 09:25:08 -05001853 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001854 err = split_leaf(trans, root, key,
1855 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001856 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001857
Yan Zheng33c66f42009-07-22 09:59:00 -04001858 BUG_ON(err > 0);
1859 if (err) {
1860 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001861 goto done;
1862 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001863 }
Chris Mason459931e2008-12-10 09:10:46 -05001864 if (!p->search_for_split)
1865 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001866 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001867 }
1868 }
Chris Mason65b51a02008-08-01 15:11:20 -04001869 ret = 1;
1870done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001871 /*
1872 * we don't really know what they plan on doing with the path
1873 * from here on, so for now just mark it as blocking
1874 */
Chris Masonb9473432009-03-13 11:00:37 -04001875 if (!p->leave_spinning)
1876 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001877 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001878 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001879 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001880}
1881
Chris Mason74123bd2007-02-02 11:05:29 -05001882/*
1883 * adjust the pointers going up the tree, starting at level
1884 * making sure the right key of each node is points to 'key'.
1885 * This is used after shifting pointers to the left, so it stops
1886 * fixing up pointers when a given leaf/node is not in slot 0 of the
1887 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001888 *
Chris Mason74123bd2007-02-02 11:05:29 -05001889 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001890static void fixup_low_keys(struct btrfs_trans_handle *trans,
1891 struct btrfs_root *root, struct btrfs_path *path,
1892 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001893{
1894 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001895 struct extent_buffer *t;
1896
Chris Mason234b63a2007-03-13 10:46:10 -04001897 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001898 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001899 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001900 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001901 t = path->nodes[i];
1902 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001903 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001904 if (tslot != 0)
1905 break;
1906 }
1907}
1908
Chris Mason74123bd2007-02-02 11:05:29 -05001909/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001910 * update item key.
1911 *
1912 * This function isn't completely safe. It's the caller's responsibility
1913 * that the new key won't break the order
1914 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001915void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1916 struct btrfs_root *root, struct btrfs_path *path,
1917 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001918{
1919 struct btrfs_disk_key disk_key;
1920 struct extent_buffer *eb;
1921 int slot;
1922
1923 eb = path->nodes[0];
1924 slot = path->slots[0];
1925 if (slot > 0) {
1926 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001927 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001928 }
1929 if (slot < btrfs_header_nritems(eb) - 1) {
1930 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001931 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001932 }
1933
1934 btrfs_cpu_key_to_disk(&disk_key, new_key);
1935 btrfs_set_item_key(eb, &disk_key, slot);
1936 btrfs_mark_buffer_dirty(eb);
1937 if (slot == 0)
1938 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001939}
1940
1941/*
Chris Mason74123bd2007-02-02 11:05:29 -05001942 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001943 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001944 *
1945 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1946 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001947 */
Chris Mason98ed5172008-01-03 10:01:48 -05001948static int push_node_left(struct btrfs_trans_handle *trans,
1949 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001950 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001951{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001952 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001953 int src_nritems;
1954 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001955 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001956
Chris Mason5f39d392007-10-15 16:14:19 -04001957 src_nritems = btrfs_header_nritems(src);
1958 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001959 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001960 WARN_ON(btrfs_header_generation(src) != trans->transid);
1961 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001962
Chris Masonbce4eae2008-04-24 14:42:46 -04001963 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001964 return 1;
1965
Chris Masond3977122009-01-05 21:25:51 -05001966 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001967 return 1;
1968
Chris Masonbce4eae2008-04-24 14:42:46 -04001969 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001970 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001971 if (push_items < src_nritems) {
1972 /* leave at least 8 pointers in the node if
1973 * we aren't going to empty it
1974 */
1975 if (src_nritems - push_items < 8) {
1976 if (push_items <= 8)
1977 return 1;
1978 push_items -= 8;
1979 }
1980 }
1981 } else
1982 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001983
Chris Mason5f39d392007-10-15 16:14:19 -04001984 copy_extent_buffer(dst, src,
1985 btrfs_node_key_ptr_offset(dst_nritems),
1986 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001987 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001988
Chris Masonbb803952007-03-01 12:04:21 -05001989 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001990 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1991 btrfs_node_key_ptr_offset(push_items),
1992 (src_nritems - push_items) *
1993 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001994 }
Chris Mason5f39d392007-10-15 16:14:19 -04001995 btrfs_set_header_nritems(src, src_nritems - push_items);
1996 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1997 btrfs_mark_buffer_dirty(src);
1998 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001999
Chris Masonbb803952007-03-01 12:04:21 -05002000 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002001}
2002
Chris Mason97571fd2007-02-24 13:39:08 -05002003/*
Chris Mason79f95c82007-03-01 15:16:26 -05002004 * try to push data from one node into the next node right in the
2005 * tree.
2006 *
2007 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2008 * error, and > 0 if there was no room in the right hand block.
2009 *
2010 * this will only push up to 1/2 the contents of the left node over
2011 */
Chris Mason5f39d392007-10-15 16:14:19 -04002012static int balance_node_right(struct btrfs_trans_handle *trans,
2013 struct btrfs_root *root,
2014 struct extent_buffer *dst,
2015 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002016{
Chris Mason79f95c82007-03-01 15:16:26 -05002017 int push_items = 0;
2018 int max_push;
2019 int src_nritems;
2020 int dst_nritems;
2021 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002022
Chris Mason7bb86312007-12-11 09:25:06 -05002023 WARN_ON(btrfs_header_generation(src) != trans->transid);
2024 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2025
Chris Mason5f39d392007-10-15 16:14:19 -04002026 src_nritems = btrfs_header_nritems(src);
2027 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002028 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002029 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002030 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002031
Chris Masond3977122009-01-05 21:25:51 -05002032 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002033 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002034
2035 max_push = src_nritems / 2 + 1;
2036 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002037 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002038 return 1;
Yan252c38f2007-08-29 09:11:44 -04002039
Chris Mason79f95c82007-03-01 15:16:26 -05002040 if (max_push < push_items)
2041 push_items = max_push;
2042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2044 btrfs_node_key_ptr_offset(0),
2045 (dst_nritems) *
2046 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002047
Chris Mason5f39d392007-10-15 16:14:19 -04002048 copy_extent_buffer(dst, src,
2049 btrfs_node_key_ptr_offset(0),
2050 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002051 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002052
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_set_header_nritems(src, src_nritems - push_items);
2054 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002055
Chris Mason5f39d392007-10-15 16:14:19 -04002056 btrfs_mark_buffer_dirty(src);
2057 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002058
Chris Mason79f95c82007-03-01 15:16:26 -05002059 return ret;
2060}
2061
2062/*
Chris Mason97571fd2007-02-24 13:39:08 -05002063 * helper function to insert a new root level in the tree.
2064 * A new node is allocated, and a single item is inserted to
2065 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002066 *
2067 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002068 */
Chris Masond3977122009-01-05 21:25:51 -05002069static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002070 struct btrfs_root *root,
2071 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002072{
Chris Mason7bb86312007-12-11 09:25:06 -05002073 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002074 struct extent_buffer *lower;
2075 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002076 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002077 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002078
2079 BUG_ON(path->nodes[level]);
2080 BUG_ON(path->nodes[level-1] != root->node);
2081
Chris Mason7bb86312007-12-11 09:25:06 -05002082 lower = path->nodes[level-1];
2083 if (level == 1)
2084 btrfs_item_key(lower, &lower_key, 0);
2085 else
2086 btrfs_node_key(lower, &lower_key, 0);
2087
Zheng Yan31840ae2008-09-23 13:14:14 -04002088 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002089 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002090 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002091 if (IS_ERR(c))
2092 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002093
Yan, Zhengf0486c62010-05-16 10:46:25 -04002094 root_add_used(root, root->nodesize);
2095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_header_nritems(c, 1);
2098 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002099 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002102 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002103
Chris Mason5f39d392007-10-15 16:14:19 -04002104 write_extent_buffer(c, root->fs_info->fsid,
2105 (unsigned long)btrfs_header_fsid(c),
2106 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002107
2108 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2109 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2110 BTRFS_UUID_SIZE);
2111
Chris Mason5f39d392007-10-15 16:14:19 -04002112 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002113 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002114 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002115 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002116
2117 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002118
2119 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002120
Chris Mason925baed2008-06-25 16:01:30 -04002121 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002122 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002123
2124 /* the super has an extra ref to root->node */
2125 free_extent_buffer(old);
2126
Chris Mason0b86a832008-03-24 15:01:56 -04002127 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002128 extent_buffer_get(c);
2129 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002130 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002131 path->slots[level] = 0;
2132 return 0;
2133}
2134
Chris Mason74123bd2007-02-02 11:05:29 -05002135/*
2136 * worker function to insert a single pointer in a node.
2137 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002138 *
Chris Mason74123bd2007-02-02 11:05:29 -05002139 * slot and level indicate where you want the key to go, and
2140 * blocknr is the block the key points to.
2141 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002142static void insert_ptr(struct btrfs_trans_handle *trans,
2143 struct btrfs_root *root, struct btrfs_path *path,
2144 struct btrfs_disk_key *key, u64 bytenr,
2145 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002146{
Chris Mason5f39d392007-10-15 16:14:19 -04002147 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002148 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002149
2150 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002151 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002152 lower = path->nodes[level];
2153 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002154 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002155 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002156 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002157 memmove_extent_buffer(lower,
2158 btrfs_node_key_ptr_offset(slot + 1),
2159 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002160 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002161 }
Chris Mason5f39d392007-10-15 16:14:19 -04002162 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002163 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002164 WARN_ON(trans->transid == 0);
2165 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002166 btrfs_set_header_nritems(lower, nritems + 1);
2167 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002168}
2169
Chris Mason97571fd2007-02-24 13:39:08 -05002170/*
2171 * split the node at the specified level in path in two.
2172 * The path is corrected to point to the appropriate node after the split
2173 *
2174 * Before splitting this tries to make some room in the node by pushing
2175 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002176 *
2177 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002178 */
Chris Masone02119d2008-09-05 16:13:11 -04002179static noinline int split_node(struct btrfs_trans_handle *trans,
2180 struct btrfs_root *root,
2181 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002182{
Chris Mason5f39d392007-10-15 16:14:19 -04002183 struct extent_buffer *c;
2184 struct extent_buffer *split;
2185 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002186 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002187 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002188 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002189
Chris Mason5f39d392007-10-15 16:14:19 -04002190 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002191 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002192 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002193 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002194 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002195 if (ret)
2196 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002197 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002198 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002199 c = path->nodes[level];
2200 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002201 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002202 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002203 if (ret < 0)
2204 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002205 }
Chris Masone66f7092007-04-20 13:16:02 -04002206
Chris Mason5f39d392007-10-15 16:14:19 -04002207 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002208 mid = (c_nritems + 1) / 2;
2209 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002210
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002211 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002212 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002213 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 if (IS_ERR(split))
2215 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002216
Yan, Zhengf0486c62010-05-16 10:46:25 -04002217 root_add_used(root, root->nodesize);
2218
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002219 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002220 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002221 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002222 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002223 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002224 btrfs_set_header_owner(split, root->root_key.objectid);
2225 write_extent_buffer(split, root->fs_info->fsid,
2226 (unsigned long)btrfs_header_fsid(split),
2227 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002228 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2229 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2230 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002231
Chris Mason5f39d392007-10-15 16:14:19 -04002232
2233 copy_extent_buffer(split, c,
2234 btrfs_node_key_ptr_offset(0),
2235 btrfs_node_key_ptr_offset(mid),
2236 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2237 btrfs_set_header_nritems(split, c_nritems - mid);
2238 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002239 ret = 0;
2240
Chris Mason5f39d392007-10-15 16:14:19 -04002241 btrfs_mark_buffer_dirty(c);
2242 btrfs_mark_buffer_dirty(split);
2243
Jeff Mahoney143bede2012-03-01 14:56:26 +01002244 insert_ptr(trans, root, path, &disk_key, split->start,
2245 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002246
Chris Mason5de08d72007-02-24 06:24:44 -05002247 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002248 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002249 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002250 free_extent_buffer(c);
2251 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002252 path->slots[level + 1] += 1;
2253 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002254 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002255 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002256 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002257 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002258}
2259
Chris Mason74123bd2007-02-02 11:05:29 -05002260/*
2261 * how many bytes are required to store the items in a leaf. start
2262 * and nr indicate which items in the leaf to check. This totals up the
2263 * space used both by the item structs and the item data
2264 */
Chris Mason5f39d392007-10-15 16:14:19 -04002265static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002266{
2267 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002268 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002269 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002270
2271 if (!nr)
2272 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002273 data_len = btrfs_item_end_nr(l, start);
2274 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002275 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002276 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002277 return data_len;
2278}
2279
Chris Mason74123bd2007-02-02 11:05:29 -05002280/*
Chris Masond4dbff92007-04-04 14:08:15 -04002281 * The space between the end of the leaf items and
2282 * the start of the leaf data. IOW, how much room
2283 * the leaf has left for both items and data
2284 */
Chris Masond3977122009-01-05 21:25:51 -05002285noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002286 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002287{
Chris Mason5f39d392007-10-15 16:14:19 -04002288 int nritems = btrfs_header_nritems(leaf);
2289 int ret;
2290 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2291 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002292 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2293 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002294 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002295 leaf_space_used(leaf, 0, nritems), nritems);
2296 }
2297 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002298}
2299
Chris Mason99d8f832010-07-07 10:51:48 -04002300/*
2301 * min slot controls the lowest index we're willing to push to the
2302 * right. We'll push up to and including min_slot, but no lower
2303 */
Chris Mason44871b12009-03-13 10:04:31 -04002304static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2305 struct btrfs_root *root,
2306 struct btrfs_path *path,
2307 int data_size, int empty,
2308 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002309 int free_space, u32 left_nritems,
2310 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002311{
Chris Mason5f39d392007-10-15 16:14:19 -04002312 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002313 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002314 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002315 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002316 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002317 int push_space = 0;
2318 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002319 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002320 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002321 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002322 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002323 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002324
Chris Mason34a38212007-11-07 13:31:03 -05002325 if (empty)
2326 nr = 0;
2327 else
Chris Mason99d8f832010-07-07 10:51:48 -04002328 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002329
Zheng Yan31840ae2008-09-23 13:14:14 -04002330 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002331 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002332
Chris Mason44871b12009-03-13 10:04:31 -04002333 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002334 i = left_nritems - 1;
2335 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002336 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002337
Zheng Yan31840ae2008-09-23 13:14:14 -04002338 if (!empty && push_items > 0) {
2339 if (path->slots[0] > i)
2340 break;
2341 if (path->slots[0] == i) {
2342 int space = btrfs_leaf_free_space(root, left);
2343 if (space + push_space * 2 > free_space)
2344 break;
2345 }
2346 }
2347
Chris Mason00ec4c52007-02-24 12:47:20 -05002348 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002349 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002350
Chris Masondb945352007-10-15 16:15:53 -04002351 this_item_size = btrfs_item_size(left, item);
2352 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002353 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002354
Chris Mason00ec4c52007-02-24 12:47:20 -05002355 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002356 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002357 if (i == 0)
2358 break;
2359 i--;
Chris Masondb945352007-10-15 16:15:53 -04002360 }
Chris Mason5f39d392007-10-15 16:14:19 -04002361
Chris Mason925baed2008-06-25 16:01:30 -04002362 if (push_items == 0)
2363 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002364
Chris Mason34a38212007-11-07 13:31:03 -05002365 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002366 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Chris Mason00ec4c52007-02-24 12:47:20 -05002368 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002369 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002370
Chris Mason5f39d392007-10-15 16:14:19 -04002371 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002372 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002373
Chris Mason00ec4c52007-02-24 12:47:20 -05002374 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002375 data_end = leaf_data_end(root, right);
2376 memmove_extent_buffer(right,
2377 btrfs_leaf_data(right) + data_end - push_space,
2378 btrfs_leaf_data(right) + data_end,
2379 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2380
Chris Mason00ec4c52007-02-24 12:47:20 -05002381 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002382 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002383 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2384 btrfs_leaf_data(left) + leaf_data_end(root, left),
2385 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002386
2387 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2388 btrfs_item_nr_offset(0),
2389 right_nritems * sizeof(struct btrfs_item));
2390
Chris Mason00ec4c52007-02-24 12:47:20 -05002391 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002392 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2393 btrfs_item_nr_offset(left_nritems - push_items),
2394 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002395
2396 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002397 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002398 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002399 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002400 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002401 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002402 push_space -= btrfs_item_size(right, item);
2403 btrfs_set_item_offset(right, item, push_space);
2404 }
2405
Chris Mason7518a232007-03-12 12:01:18 -04002406 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002407 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002408
Chris Mason34a38212007-11-07 13:31:03 -05002409 if (left_nritems)
2410 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002411 else
2412 clean_tree_block(trans, root, left);
2413
Chris Mason5f39d392007-10-15 16:14:19 -04002414 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002415
Chris Mason5f39d392007-10-15 16:14:19 -04002416 btrfs_item_key(right, &disk_key, 0);
2417 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002418 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002419
Chris Mason00ec4c52007-02-24 12:47:20 -05002420 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002421 if (path->slots[0] >= left_nritems) {
2422 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002423 if (btrfs_header_nritems(path->nodes[0]) == 0)
2424 clean_tree_block(trans, root, path->nodes[0]);
2425 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002426 free_extent_buffer(path->nodes[0]);
2427 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002428 path->slots[1] += 1;
2429 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002430 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002431 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002432 }
2433 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002434
2435out_unlock:
2436 btrfs_tree_unlock(right);
2437 free_extent_buffer(right);
2438 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002439}
Chris Mason925baed2008-06-25 16:01:30 -04002440
Chris Mason00ec4c52007-02-24 12:47:20 -05002441/*
Chris Mason44871b12009-03-13 10:04:31 -04002442 * push some data in the path leaf to the right, trying to free up at
2443 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2444 *
2445 * returns 1 if the push failed because the other node didn't have enough
2446 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002447 *
2448 * this will push starting from min_slot to the end of the leaf. It won't
2449 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002450 */
2451static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002452 *root, struct btrfs_path *path,
2453 int min_data_size, int data_size,
2454 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002455{
2456 struct extent_buffer *left = path->nodes[0];
2457 struct extent_buffer *right;
2458 struct extent_buffer *upper;
2459 int slot;
2460 int free_space;
2461 u32 left_nritems;
2462 int ret;
2463
2464 if (!path->nodes[1])
2465 return 1;
2466
2467 slot = path->slots[1];
2468 upper = path->nodes[1];
2469 if (slot >= btrfs_header_nritems(upper) - 1)
2470 return 1;
2471
2472 btrfs_assert_tree_locked(path->nodes[1]);
2473
2474 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002475 if (right == NULL)
2476 return 1;
2477
Chris Mason44871b12009-03-13 10:04:31 -04002478 btrfs_tree_lock(right);
2479 btrfs_set_lock_blocking(right);
2480
2481 free_space = btrfs_leaf_free_space(root, right);
2482 if (free_space < data_size)
2483 goto out_unlock;
2484
2485 /* cow and double check */
2486 ret = btrfs_cow_block(trans, root, right, upper,
2487 slot + 1, &right);
2488 if (ret)
2489 goto out_unlock;
2490
2491 free_space = btrfs_leaf_free_space(root, right);
2492 if (free_space < data_size)
2493 goto out_unlock;
2494
2495 left_nritems = btrfs_header_nritems(left);
2496 if (left_nritems == 0)
2497 goto out_unlock;
2498
Chris Mason99d8f832010-07-07 10:51:48 -04002499 return __push_leaf_right(trans, root, path, min_data_size, empty,
2500 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002501out_unlock:
2502 btrfs_tree_unlock(right);
2503 free_extent_buffer(right);
2504 return 1;
2505}
2506
2507/*
Chris Mason74123bd2007-02-02 11:05:29 -05002508 * push some data in the path leaf to the left, trying to free up at
2509 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002510 *
2511 * max_slot can put a limit on how far into the leaf we'll push items. The
2512 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2513 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002514 */
Chris Mason44871b12009-03-13 10:04:31 -04002515static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2516 struct btrfs_root *root,
2517 struct btrfs_path *path, int data_size,
2518 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002519 int free_space, u32 right_nritems,
2520 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002521{
Chris Mason5f39d392007-10-15 16:14:19 -04002522 struct btrfs_disk_key disk_key;
2523 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002524 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002525 int push_space = 0;
2526 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002527 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002528 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002529 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002530 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002531 u32 this_item_size;
2532 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002533
Chris Mason34a38212007-11-07 13:31:03 -05002534 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002535 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002536 else
Chris Mason99d8f832010-07-07 10:51:48 -04002537 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002538
2539 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002540 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002541
Zheng Yan31840ae2008-09-23 13:14:14 -04002542 if (!empty && push_items > 0) {
2543 if (path->slots[0] < i)
2544 break;
2545 if (path->slots[0] == i) {
2546 int space = btrfs_leaf_free_space(root, right);
2547 if (space + push_space * 2 > free_space)
2548 break;
2549 }
2550 }
2551
Chris Masonbe0e5c02007-01-26 15:51:26 -05002552 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002553 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002554
2555 this_item_size = btrfs_item_size(right, item);
2556 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 break;
Chris Masondb945352007-10-15 16:15:53 -04002558
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002560 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 }
Chris Masondb945352007-10-15 16:15:53 -04002562
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002564 ret = 1;
2565 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002566 }
Chris Mason34a38212007-11-07 13:31:03 -05002567 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002568 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002569
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002571 copy_extent_buffer(left, right,
2572 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2573 btrfs_item_nr_offset(0),
2574 push_items * sizeof(struct btrfs_item));
2575
Chris Mason123abc82007-03-14 14:14:43 -04002576 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002577 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002578
2579 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002580 leaf_data_end(root, left) - push_space,
2581 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002582 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002583 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002584 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002585 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002586
Chris Masondb945352007-10-15 16:15:53 -04002587 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002588 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002589 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002590
Chris Mason5f39d392007-10-15 16:14:19 -04002591 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002592
Chris Mason5f39d392007-10-15 16:14:19 -04002593 ioff = btrfs_item_offset(left, item);
2594 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002595 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596 }
Chris Mason5f39d392007-10-15 16:14:19 -04002597 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598
2599 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002600 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002601 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2602 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002603 WARN_ON(1);
2604 }
Chris Mason5f39d392007-10-15 16:14:19 -04002605
Chris Mason34a38212007-11-07 13:31:03 -05002606 if (push_items < right_nritems) {
2607 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2608 leaf_data_end(root, right);
2609 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2610 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2611 btrfs_leaf_data(right) +
2612 leaf_data_end(root, right), push_space);
2613
2614 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002615 btrfs_item_nr_offset(push_items),
2616 (btrfs_header_nritems(right) - push_items) *
2617 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002618 }
Yaneef1c492007-11-26 10:58:13 -05002619 right_nritems -= push_items;
2620 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002621 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002622 for (i = 0; i < right_nritems; i++) {
2623 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002624
Chris Masondb945352007-10-15 16:15:53 -04002625 push_space = push_space - btrfs_item_size(right, item);
2626 btrfs_set_item_offset(right, item, push_space);
2627 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002628
Chris Mason5f39d392007-10-15 16:14:19 -04002629 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002630 if (right_nritems)
2631 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002632 else
2633 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002634
Chris Mason5f39d392007-10-15 16:14:19 -04002635 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002636 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002637
2638 /* then fixup the leaf pointer in the path */
2639 if (path->slots[0] < push_items) {
2640 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002641 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002642 free_extent_buffer(path->nodes[0]);
2643 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002644 path->slots[1] -= 1;
2645 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002646 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002647 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648 path->slots[0] -= push_items;
2649 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002650 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002651 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002652out:
2653 btrfs_tree_unlock(left);
2654 free_extent_buffer(left);
2655 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002656}
2657
Chris Mason74123bd2007-02-02 11:05:29 -05002658/*
Chris Mason44871b12009-03-13 10:04:31 -04002659 * push some data in the path leaf to the left, trying to free up at
2660 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002661 *
2662 * max_slot can put a limit on how far into the leaf we'll push items. The
2663 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2664 * items
Chris Mason44871b12009-03-13 10:04:31 -04002665 */
2666static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002667 *root, struct btrfs_path *path, int min_data_size,
2668 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002669{
2670 struct extent_buffer *right = path->nodes[0];
2671 struct extent_buffer *left;
2672 int slot;
2673 int free_space;
2674 u32 right_nritems;
2675 int ret = 0;
2676
2677 slot = path->slots[1];
2678 if (slot == 0)
2679 return 1;
2680 if (!path->nodes[1])
2681 return 1;
2682
2683 right_nritems = btrfs_header_nritems(right);
2684 if (right_nritems == 0)
2685 return 1;
2686
2687 btrfs_assert_tree_locked(path->nodes[1]);
2688
2689 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002690 if (left == NULL)
2691 return 1;
2692
Chris Mason44871b12009-03-13 10:04:31 -04002693 btrfs_tree_lock(left);
2694 btrfs_set_lock_blocking(left);
2695
2696 free_space = btrfs_leaf_free_space(root, left);
2697 if (free_space < data_size) {
2698 ret = 1;
2699 goto out;
2700 }
2701
2702 /* cow and double check */
2703 ret = btrfs_cow_block(trans, root, left,
2704 path->nodes[1], slot - 1, &left);
2705 if (ret) {
2706 /* we hit -ENOSPC, but it isn't fatal here */
2707 ret = 1;
2708 goto out;
2709 }
2710
2711 free_space = btrfs_leaf_free_space(root, left);
2712 if (free_space < data_size) {
2713 ret = 1;
2714 goto out;
2715 }
2716
Chris Mason99d8f832010-07-07 10:51:48 -04002717 return __push_leaf_left(trans, root, path, min_data_size,
2718 empty, left, free_space, right_nritems,
2719 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002720out:
2721 btrfs_tree_unlock(left);
2722 free_extent_buffer(left);
2723 return ret;
2724}
2725
2726/*
Chris Mason74123bd2007-02-02 11:05:29 -05002727 * split the path's leaf in two, making sure there is at least data_size
2728 * available for the resulting leaf level of the path.
2729 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002730static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2731 struct btrfs_root *root,
2732 struct btrfs_path *path,
2733 struct extent_buffer *l,
2734 struct extent_buffer *right,
2735 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002736{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002737 int data_copy_size;
2738 int rt_data_off;
2739 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002740 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002741
Chris Mason5f39d392007-10-15 16:14:19 -04002742 nritems = nritems - mid;
2743 btrfs_set_header_nritems(right, nritems);
2744 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2745
2746 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2747 btrfs_item_nr_offset(mid),
2748 nritems * sizeof(struct btrfs_item));
2749
2750 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002751 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2752 data_copy_size, btrfs_leaf_data(l) +
2753 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002754
Chris Mason5f39d392007-10-15 16:14:19 -04002755 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2756 btrfs_item_end_nr(l, mid);
2757
2758 for (i = 0; i < nritems; i++) {
2759 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002760 u32 ioff;
2761
Chris Masondb945352007-10-15 16:15:53 -04002762 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002763 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002764 }
Chris Mason74123bd2007-02-02 11:05:29 -05002765
Chris Mason5f39d392007-10-15 16:14:19 -04002766 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002767 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002768 insert_ptr(trans, root, path, &disk_key, right->start,
2769 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002770
2771 btrfs_mark_buffer_dirty(right);
2772 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002773 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002774
Chris Masonbe0e5c02007-01-26 15:51:26 -05002775 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002776 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002777 free_extent_buffer(path->nodes[0]);
2778 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002779 path->slots[0] -= mid;
2780 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002781 } else {
2782 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002783 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002784 }
Chris Mason5f39d392007-10-15 16:14:19 -04002785
Chris Masoneb60cea2007-02-02 09:18:22 -05002786 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002787}
2788
2789/*
Chris Mason99d8f832010-07-07 10:51:48 -04002790 * double splits happen when we need to insert a big item in the middle
2791 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2792 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2793 * A B C
2794 *
2795 * We avoid this by trying to push the items on either side of our target
2796 * into the adjacent leaves. If all goes well we can avoid the double split
2797 * completely.
2798 */
2799static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2800 struct btrfs_root *root,
2801 struct btrfs_path *path,
2802 int data_size)
2803{
2804 int ret;
2805 int progress = 0;
2806 int slot;
2807 u32 nritems;
2808
2809 slot = path->slots[0];
2810
2811 /*
2812 * try to push all the items after our slot into the
2813 * right leaf
2814 */
2815 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2816 if (ret < 0)
2817 return ret;
2818
2819 if (ret == 0)
2820 progress++;
2821
2822 nritems = btrfs_header_nritems(path->nodes[0]);
2823 /*
2824 * our goal is to get our slot at the start or end of a leaf. If
2825 * we've done so we're done
2826 */
2827 if (path->slots[0] == 0 || path->slots[0] == nritems)
2828 return 0;
2829
2830 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2831 return 0;
2832
2833 /* try to push all the items before our slot into the next leaf */
2834 slot = path->slots[0];
2835 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2836 if (ret < 0)
2837 return ret;
2838
2839 if (ret == 0)
2840 progress++;
2841
2842 if (progress)
2843 return 0;
2844 return 1;
2845}
2846
2847/*
Chris Mason44871b12009-03-13 10:04:31 -04002848 * split the path's leaf in two, making sure there is at least data_size
2849 * available for the resulting leaf level of the path.
2850 *
2851 * returns 0 if all went well and < 0 on failure.
2852 */
2853static noinline int split_leaf(struct btrfs_trans_handle *trans,
2854 struct btrfs_root *root,
2855 struct btrfs_key *ins_key,
2856 struct btrfs_path *path, int data_size,
2857 int extend)
2858{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002859 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002860 struct extent_buffer *l;
2861 u32 nritems;
2862 int mid;
2863 int slot;
2864 struct extent_buffer *right;
2865 int ret = 0;
2866 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002867 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002868 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002869 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002870
Yan, Zhenga5719522009-09-24 09:17:31 -04002871 l = path->nodes[0];
2872 slot = path->slots[0];
2873 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2874 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2875 return -EOVERFLOW;
2876
Chris Mason44871b12009-03-13 10:04:31 -04002877 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002878 if (data_size) {
2879 wret = push_leaf_right(trans, root, path, data_size,
2880 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002881 if (wret < 0)
2882 return wret;
2883 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002884 wret = push_leaf_left(trans, root, path, data_size,
2885 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002886 if (wret < 0)
2887 return wret;
2888 }
2889 l = path->nodes[0];
2890
2891 /* did the pushes work? */
2892 if (btrfs_leaf_free_space(root, l) >= data_size)
2893 return 0;
2894 }
2895
2896 if (!path->nodes[1]) {
2897 ret = insert_new_root(trans, root, path, 1);
2898 if (ret)
2899 return ret;
2900 }
2901again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002902 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002903 l = path->nodes[0];
2904 slot = path->slots[0];
2905 nritems = btrfs_header_nritems(l);
2906 mid = (nritems + 1) / 2;
2907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002908 if (mid <= slot) {
2909 if (nritems == 1 ||
2910 leaf_space_used(l, mid, nritems - mid) + data_size >
2911 BTRFS_LEAF_DATA_SIZE(root)) {
2912 if (slot >= nritems) {
2913 split = 0;
2914 } else {
2915 mid = slot;
2916 if (mid != nritems &&
2917 leaf_space_used(l, mid, nritems - mid) +
2918 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002919 if (data_size && !tried_avoid_double)
2920 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002921 split = 2;
2922 }
2923 }
2924 }
2925 } else {
2926 if (leaf_space_used(l, 0, mid) + data_size >
2927 BTRFS_LEAF_DATA_SIZE(root)) {
2928 if (!extend && data_size && slot == 0) {
2929 split = 0;
2930 } else if ((extend || !data_size) && slot == 0) {
2931 mid = 1;
2932 } else {
2933 mid = slot;
2934 if (mid != nritems &&
2935 leaf_space_used(l, mid, nritems - mid) +
2936 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002937 if (data_size && !tried_avoid_double)
2938 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002939 split = 2 ;
2940 }
2941 }
2942 }
2943 }
2944
2945 if (split == 0)
2946 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2947 else
2948 btrfs_item_key(l, &disk_key, mid);
2949
2950 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002951 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002952 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002953 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002954 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002955
2956 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002957
2958 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2959 btrfs_set_header_bytenr(right, right->start);
2960 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002961 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002962 btrfs_set_header_owner(right, root->root_key.objectid);
2963 btrfs_set_header_level(right, 0);
2964 write_extent_buffer(right, root->fs_info->fsid,
2965 (unsigned long)btrfs_header_fsid(right),
2966 BTRFS_FSID_SIZE);
2967
2968 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2969 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2970 BTRFS_UUID_SIZE);
2971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002972 if (split == 0) {
2973 if (mid <= slot) {
2974 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002975 insert_ptr(trans, root, path, &disk_key, right->start,
2976 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002977 btrfs_tree_unlock(path->nodes[0]);
2978 free_extent_buffer(path->nodes[0]);
2979 path->nodes[0] = right;
2980 path->slots[0] = 0;
2981 path->slots[1] += 1;
2982 } else {
2983 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002984 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 btrfs_tree_unlock(path->nodes[0]);
2987 free_extent_buffer(path->nodes[0]);
2988 path->nodes[0] = right;
2989 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01002990 if (path->slots[1] == 0)
2991 fixup_low_keys(trans, root, path,
2992 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002993 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002994 btrfs_mark_buffer_dirty(right);
2995 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002996 }
2997
Jeff Mahoney143bede2012-03-01 14:56:26 +01002998 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04002999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003001 BUG_ON(num_doubles != 0);
3002 num_doubles++;
3003 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003004 }
Chris Mason44871b12009-03-13 10:04:31 -04003005
Jeff Mahoney143bede2012-03-01 14:56:26 +01003006 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003007
3008push_for_double:
3009 push_for_double_split(trans, root, path, data_size);
3010 tried_avoid_double = 1;
3011 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3012 return 0;
3013 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003014}
3015
Yan, Zhengad48fd752009-11-12 09:33:58 +00003016static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3017 struct btrfs_root *root,
3018 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003019{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003020 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003021 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003022 struct btrfs_file_extent_item *fi;
3023 u64 extent_len = 0;
3024 u32 item_size;
3025 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003026
3027 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3029
3030 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3031 key.type != BTRFS_EXTENT_CSUM_KEY);
3032
3033 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3034 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003035
3036 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003037 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3038 fi = btrfs_item_ptr(leaf, path->slots[0],
3039 struct btrfs_file_extent_item);
3040 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3041 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003042 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003043
Chris Mason459931e2008-12-10 09:10:46 -05003044 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003045 path->search_for_split = 1;
3046 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003047 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003048 if (ret < 0)
3049 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003050
Yan, Zhengad48fd752009-11-12 09:33:58 +00003051 ret = -EAGAIN;
3052 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003053 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003054 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3055 goto err;
3056
Chris Mason109f6ae2010-04-02 09:20:18 -04003057 /* the leaf has changed, it now has room. return now */
3058 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3059 goto err;
3060
Yan, Zhengad48fd752009-11-12 09:33:58 +00003061 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3062 fi = btrfs_item_ptr(leaf, path->slots[0],
3063 struct btrfs_file_extent_item);
3064 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3065 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003066 }
3067
Chris Masonb9473432009-03-13 11:00:37 -04003068 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003069 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003070 if (ret)
3071 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003072
Yan, Zhengad48fd752009-11-12 09:33:58 +00003073 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003074 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003075 return 0;
3076err:
3077 path->keep_locks = 0;
3078 return ret;
3079}
3080
3081static noinline int split_item(struct btrfs_trans_handle *trans,
3082 struct btrfs_root *root,
3083 struct btrfs_path *path,
3084 struct btrfs_key *new_key,
3085 unsigned long split_offset)
3086{
3087 struct extent_buffer *leaf;
3088 struct btrfs_item *item;
3089 struct btrfs_item *new_item;
3090 int slot;
3091 char *buf;
3092 u32 nritems;
3093 u32 item_size;
3094 u32 orig_offset;
3095 struct btrfs_disk_key disk_key;
3096
Chris Masonb9473432009-03-13 11:00:37 -04003097 leaf = path->nodes[0];
3098 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3099
Chris Masonb4ce94d2009-02-04 09:25:08 -05003100 btrfs_set_path_blocking(path);
3101
Chris Mason459931e2008-12-10 09:10:46 -05003102 item = btrfs_item_nr(leaf, path->slots[0]);
3103 orig_offset = btrfs_item_offset(leaf, item);
3104 item_size = btrfs_item_size(leaf, item);
3105
Chris Mason459931e2008-12-10 09:10:46 -05003106 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003107 if (!buf)
3108 return -ENOMEM;
3109
Chris Mason459931e2008-12-10 09:10:46 -05003110 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3111 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003112
Chris Mason459931e2008-12-10 09:10:46 -05003113 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003114 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003115 if (slot != nritems) {
3116 /* shift the items */
3117 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003118 btrfs_item_nr_offset(slot),
3119 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003120 }
3121
3122 btrfs_cpu_key_to_disk(&disk_key, new_key);
3123 btrfs_set_item_key(leaf, &disk_key, slot);
3124
3125 new_item = btrfs_item_nr(leaf, slot);
3126
3127 btrfs_set_item_offset(leaf, new_item, orig_offset);
3128 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3129
3130 btrfs_set_item_offset(leaf, item,
3131 orig_offset + item_size - split_offset);
3132 btrfs_set_item_size(leaf, item, split_offset);
3133
3134 btrfs_set_header_nritems(leaf, nritems + 1);
3135
3136 /* write the data for the start of the original item */
3137 write_extent_buffer(leaf, buf,
3138 btrfs_item_ptr_offset(leaf, path->slots[0]),
3139 split_offset);
3140
3141 /* write the data for the new item */
3142 write_extent_buffer(leaf, buf + split_offset,
3143 btrfs_item_ptr_offset(leaf, slot),
3144 item_size - split_offset);
3145 btrfs_mark_buffer_dirty(leaf);
3146
Yan, Zhengad48fd752009-11-12 09:33:58 +00003147 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003148 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003149 return 0;
3150}
3151
3152/*
3153 * This function splits a single item into two items,
3154 * giving 'new_key' to the new item and splitting the
3155 * old one at split_offset (from the start of the item).
3156 *
3157 * The path may be released by this operation. After
3158 * the split, the path is pointing to the old item. The
3159 * new item is going to be in the same node as the old one.
3160 *
3161 * Note, the item being split must be smaller enough to live alone on
3162 * a tree block with room for one extra struct btrfs_item
3163 *
3164 * This allows us to split the item in place, keeping a lock on the
3165 * leaf the entire time.
3166 */
3167int btrfs_split_item(struct btrfs_trans_handle *trans,
3168 struct btrfs_root *root,
3169 struct btrfs_path *path,
3170 struct btrfs_key *new_key,
3171 unsigned long split_offset)
3172{
3173 int ret;
3174 ret = setup_leaf_for_split(trans, root, path,
3175 sizeof(struct btrfs_item));
3176 if (ret)
3177 return ret;
3178
3179 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003180 return ret;
3181}
3182
3183/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003184 * This function duplicate a item, giving 'new_key' to the new item.
3185 * It guarantees both items live in the same tree leaf and the new item
3186 * is contiguous with the original item.
3187 *
3188 * This allows us to split file extent in place, keeping a lock on the
3189 * leaf the entire time.
3190 */
3191int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 struct btrfs_path *path,
3194 struct btrfs_key *new_key)
3195{
3196 struct extent_buffer *leaf;
3197 int ret;
3198 u32 item_size;
3199
3200 leaf = path->nodes[0];
3201 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3202 ret = setup_leaf_for_split(trans, root, path,
3203 item_size + sizeof(struct btrfs_item));
3204 if (ret)
3205 return ret;
3206
3207 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003208 setup_items_for_insert(trans, root, path, new_key, &item_size,
3209 item_size, item_size +
3210 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003211 leaf = path->nodes[0];
3212 memcpy_extent_buffer(leaf,
3213 btrfs_item_ptr_offset(leaf, path->slots[0]),
3214 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3215 item_size);
3216 return 0;
3217}
3218
3219/*
Chris Masond352ac62008-09-29 15:18:18 -04003220 * make the item pointed to by the path smaller. new_size indicates
3221 * how small to make it, and from_end tells us if we just chop bytes
3222 * off the end of the item or if we shift the item to chop bytes off
3223 * the front.
3224 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003225void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3226 struct btrfs_root *root,
3227 struct btrfs_path *path,
3228 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003229{
Chris Masonb18c6682007-04-17 13:26:50 -04003230 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003231 struct extent_buffer *leaf;
3232 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003233 u32 nritems;
3234 unsigned int data_end;
3235 unsigned int old_data_start;
3236 unsigned int old_size;
3237 unsigned int size_diff;
3238 int i;
3239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003241 slot = path->slots[0];
3242
3243 old_size = btrfs_item_size_nr(leaf, slot);
3244 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003245 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003246
Chris Mason5f39d392007-10-15 16:14:19 -04003247 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003248 data_end = leaf_data_end(root, leaf);
3249
Chris Mason5f39d392007-10-15 16:14:19 -04003250 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003251
Chris Masonb18c6682007-04-17 13:26:50 -04003252 size_diff = old_size - new_size;
3253
3254 BUG_ON(slot < 0);
3255 BUG_ON(slot >= nritems);
3256
3257 /*
3258 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3259 */
3260 /* first correct the data pointers */
3261 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003262 u32 ioff;
3263 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003264
Chris Mason5f39d392007-10-15 16:14:19 -04003265 ioff = btrfs_item_offset(leaf, item);
3266 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003267 }
Chris Masondb945352007-10-15 16:15:53 -04003268
Chris Masonb18c6682007-04-17 13:26:50 -04003269 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003270 if (from_end) {
3271 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3272 data_end + size_diff, btrfs_leaf_data(leaf) +
3273 data_end, old_data_start + new_size - data_end);
3274 } else {
3275 struct btrfs_disk_key disk_key;
3276 u64 offset;
3277
3278 btrfs_item_key(leaf, &disk_key, slot);
3279
3280 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3281 unsigned long ptr;
3282 struct btrfs_file_extent_item *fi;
3283
3284 fi = btrfs_item_ptr(leaf, slot,
3285 struct btrfs_file_extent_item);
3286 fi = (struct btrfs_file_extent_item *)(
3287 (unsigned long)fi - size_diff);
3288
3289 if (btrfs_file_extent_type(leaf, fi) ==
3290 BTRFS_FILE_EXTENT_INLINE) {
3291 ptr = btrfs_item_ptr_offset(leaf, slot);
3292 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003293 (unsigned long)fi,
3294 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003295 disk_bytenr));
3296 }
3297 }
3298
3299 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3300 data_end + size_diff, btrfs_leaf_data(leaf) +
3301 data_end, old_data_start - data_end);
3302
3303 offset = btrfs_disk_key_offset(&disk_key);
3304 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3305 btrfs_set_item_key(leaf, &disk_key, slot);
3306 if (slot == 0)
3307 fixup_low_keys(trans, root, path, &disk_key, 1);
3308 }
Chris Mason5f39d392007-10-15 16:14:19 -04003309
3310 item = btrfs_item_nr(leaf, slot);
3311 btrfs_set_item_size(leaf, item, new_size);
3312 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003313
Chris Mason5f39d392007-10-15 16:14:19 -04003314 if (btrfs_leaf_free_space(root, leaf) < 0) {
3315 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003316 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003317 }
Chris Masonb18c6682007-04-17 13:26:50 -04003318}
3319
Chris Masond352ac62008-09-29 15:18:18 -04003320/*
3321 * make the item pointed to by the path bigger, data_size is the new size.
3322 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003323void btrfs_extend_item(struct btrfs_trans_handle *trans,
3324 struct btrfs_root *root, struct btrfs_path *path,
3325 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003326{
Chris Mason6567e832007-04-16 09:22:45 -04003327 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003328 struct extent_buffer *leaf;
3329 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003330 u32 nritems;
3331 unsigned int data_end;
3332 unsigned int old_data;
3333 unsigned int old_size;
3334 int i;
3335
Chris Mason5f39d392007-10-15 16:14:19 -04003336 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003337
Chris Mason5f39d392007-10-15 16:14:19 -04003338 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003339 data_end = leaf_data_end(root, leaf);
3340
Chris Mason5f39d392007-10-15 16:14:19 -04003341 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3342 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003343 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003344 }
Chris Mason6567e832007-04-16 09:22:45 -04003345 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003346 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003347
3348 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003349 if (slot >= nritems) {
3350 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003351 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3352 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003353 BUG_ON(1);
3354 }
Chris Mason6567e832007-04-16 09:22:45 -04003355
3356 /*
3357 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3358 */
3359 /* first correct the data pointers */
3360 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003361 u32 ioff;
3362 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 ioff = btrfs_item_offset(leaf, item);
3365 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003366 }
Chris Mason5f39d392007-10-15 16:14:19 -04003367
Chris Mason6567e832007-04-16 09:22:45 -04003368 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003369 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003370 data_end - data_size, btrfs_leaf_data(leaf) +
3371 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003372
Chris Mason6567e832007-04-16 09:22:45 -04003373 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003374 old_size = btrfs_item_size_nr(leaf, slot);
3375 item = btrfs_item_nr(leaf, slot);
3376 btrfs_set_item_size(leaf, item, old_size + data_size);
3377 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003378
Chris Mason5f39d392007-10-15 16:14:19 -04003379 if (btrfs_leaf_free_space(root, leaf) < 0) {
3380 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003381 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003382 }
Chris Mason6567e832007-04-16 09:22:45 -04003383}
3384
Chris Mason74123bd2007-02-02 11:05:29 -05003385/*
Chris Masond352ac62008-09-29 15:18:18 -04003386 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003387 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003388 * Returns the number of keys that were inserted.
3389 */
3390int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3391 struct btrfs_root *root,
3392 struct btrfs_path *path,
3393 struct btrfs_key *cpu_key, u32 *data_size,
3394 int nr)
3395{
3396 struct extent_buffer *leaf;
3397 struct btrfs_item *item;
3398 int ret = 0;
3399 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003400 int i;
3401 u32 nritems;
3402 u32 total_data = 0;
3403 u32 total_size = 0;
3404 unsigned int data_end;
3405 struct btrfs_disk_key disk_key;
3406 struct btrfs_key found_key;
3407
Yan Zheng87b29b22008-12-17 10:21:48 -05003408 for (i = 0; i < nr; i++) {
3409 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3410 BTRFS_LEAF_DATA_SIZE(root)) {
3411 break;
3412 nr = i;
3413 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003414 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003415 total_size += data_size[i] + sizeof(struct btrfs_item);
3416 }
3417 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003418
Josef Bacikf3465ca2008-11-12 14:19:50 -05003419 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3420 if (ret == 0)
3421 return -EEXIST;
3422 if (ret < 0)
3423 goto out;
3424
Josef Bacikf3465ca2008-11-12 14:19:50 -05003425 leaf = path->nodes[0];
3426
3427 nritems = btrfs_header_nritems(leaf);
3428 data_end = leaf_data_end(root, leaf);
3429
3430 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3431 for (i = nr; i >= 0; i--) {
3432 total_data -= data_size[i];
3433 total_size -= data_size[i] + sizeof(struct btrfs_item);
3434 if (total_size < btrfs_leaf_free_space(root, leaf))
3435 break;
3436 }
3437 nr = i;
3438 }
3439
3440 slot = path->slots[0];
3441 BUG_ON(slot < 0);
3442
3443 if (slot != nritems) {
3444 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3445
3446 item = btrfs_item_nr(leaf, slot);
3447 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3448
3449 /* figure out how many keys we can insert in here */
3450 total_data = data_size[0];
3451 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003452 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003453 break;
3454 total_data += data_size[i];
3455 }
3456 nr = i;
3457
3458 if (old_data < data_end) {
3459 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003460 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003461 slot, old_data, data_end);
3462 BUG_ON(1);
3463 }
3464 /*
3465 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3466 */
3467 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003468 for (i = slot; i < nritems; i++) {
3469 u32 ioff;
3470
3471 item = btrfs_item_nr(leaf, i);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003472 ioff = btrfs_item_offset(leaf, item);
3473 btrfs_set_item_offset(leaf, item, ioff - total_data);
3474 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003475 /* shift the items */
3476 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3477 btrfs_item_nr_offset(slot),
3478 (nritems - slot) * sizeof(struct btrfs_item));
3479
3480 /* shift the data */
3481 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3482 data_end - total_data, btrfs_leaf_data(leaf) +
3483 data_end, old_data - data_end);
3484 data_end = old_data;
3485 } else {
3486 /*
3487 * this sucks but it has to be done, if we are inserting at
3488 * the end of the leaf only insert 1 of the items, since we
3489 * have no way of knowing whats on the next leaf and we'd have
3490 * to drop our current locks to figure it out
3491 */
3492 nr = 1;
3493 }
3494
3495 /* setup the item for the new data */
3496 for (i = 0; i < nr; i++) {
3497 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3498 btrfs_set_item_key(leaf, &disk_key, slot + i);
3499 item = btrfs_item_nr(leaf, slot + i);
3500 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3501 data_end -= data_size[i];
3502 btrfs_set_item_size(leaf, item, data_size[i]);
3503 }
3504 btrfs_set_header_nritems(leaf, nritems + nr);
3505 btrfs_mark_buffer_dirty(leaf);
3506
3507 ret = 0;
3508 if (slot == 0) {
3509 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003510 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003511 }
3512
3513 if (btrfs_leaf_free_space(root, leaf) < 0) {
3514 btrfs_print_leaf(root, leaf);
3515 BUG();
3516 }
3517out:
3518 if (!ret)
3519 ret = nr;
3520 return ret;
3521}
3522
3523/*
Chris Mason44871b12009-03-13 10:04:31 -04003524 * this is a helper for btrfs_insert_empty_items, the main goal here is
3525 * to save stack depth by doing the bulk of the work in a function
3526 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003527 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003528void setup_items_for_insert(struct btrfs_trans_handle *trans,
3529 struct btrfs_root *root, struct btrfs_path *path,
3530 struct btrfs_key *cpu_key, u32 *data_size,
3531 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003532{
Chris Mason5f39d392007-10-15 16:14:19 -04003533 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003534 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003535 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003536 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003537 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003538 struct extent_buffer *leaf;
3539 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003540
Chris Mason5f39d392007-10-15 16:14:19 -04003541 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003542 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003543
Chris Mason5f39d392007-10-15 16:14:19 -04003544 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003545 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003546
Chris Masonf25956c2008-09-12 15:32:53 -04003547 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003548 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003549 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003550 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003551 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003552 }
Chris Mason5f39d392007-10-15 16:14:19 -04003553
Chris Masonbe0e5c02007-01-26 15:51:26 -05003554 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003555 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003556
Chris Mason5f39d392007-10-15 16:14:19 -04003557 if (old_data < data_end) {
3558 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003559 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003560 slot, old_data, data_end);
3561 BUG_ON(1);
3562 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003563 /*
3564 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3565 */
3566 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003567 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003568 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003569
Chris Mason5f39d392007-10-15 16:14:19 -04003570 item = btrfs_item_nr(leaf, i);
3571 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003572 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003573 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003575 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003576 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003577 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003578
3579 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003580 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003581 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003582 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583 data_end = old_data;
3584 }
Chris Mason5f39d392007-10-15 16:14:19 -04003585
Chris Mason62e27492007-03-15 12:56:47 -04003586 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003587 for (i = 0; i < nr; i++) {
3588 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3589 btrfs_set_item_key(leaf, &disk_key, slot + i);
3590 item = btrfs_item_nr(leaf, slot + i);
3591 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3592 data_end -= data_size[i];
3593 btrfs_set_item_size(leaf, item, data_size[i]);
3594 }
Chris Mason44871b12009-03-13 10:04:31 -04003595
Chris Mason9c583092008-01-29 15:15:18 -05003596 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003597
Chris Mason5a01a2e2008-01-30 11:43:54 -05003598 if (slot == 0) {
3599 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003600 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003601 }
Chris Masonb9473432009-03-13 11:00:37 -04003602 btrfs_unlock_up_safe(path, 1);
3603 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 if (btrfs_leaf_free_space(root, leaf) < 0) {
3606 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003607 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003608 }
Chris Mason44871b12009-03-13 10:04:31 -04003609}
3610
3611/*
3612 * Given a key and some data, insert items into the tree.
3613 * This does all the path init required, making room in the tree if needed.
3614 */
3615int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3616 struct btrfs_root *root,
3617 struct btrfs_path *path,
3618 struct btrfs_key *cpu_key, u32 *data_size,
3619 int nr)
3620{
Chris Mason44871b12009-03-13 10:04:31 -04003621 int ret = 0;
3622 int slot;
3623 int i;
3624 u32 total_size = 0;
3625 u32 total_data = 0;
3626
3627 for (i = 0; i < nr; i++)
3628 total_data += data_size[i];
3629
3630 total_size = total_data + (nr * sizeof(struct btrfs_item));
3631 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3632 if (ret == 0)
3633 return -EEXIST;
3634 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003635 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003636
Chris Mason44871b12009-03-13 10:04:31 -04003637 slot = path->slots[0];
3638 BUG_ON(slot < 0);
3639
Jeff Mahoney143bede2012-03-01 14:56:26 +01003640 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003641 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003642 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003643}
3644
3645/*
3646 * Given a key and some data, insert an item into the tree.
3647 * This does all the path init required, making room in the tree if needed.
3648 */
Chris Masone089f052007-03-16 16:20:31 -04003649int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3650 *root, struct btrfs_key *cpu_key, void *data, u32
3651 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003652{
3653 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003654 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003655 struct extent_buffer *leaf;
3656 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003657
Chris Mason2c90e5d2007-04-02 10:50:19 -04003658 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003659 if (!path)
3660 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003661 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003662 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003663 leaf = path->nodes[0];
3664 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3665 write_extent_buffer(leaf, data, ptr, data_size);
3666 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003667 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003668 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003669 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670}
3671
Chris Mason74123bd2007-02-02 11:05:29 -05003672/*
Chris Mason5de08d72007-02-24 06:24:44 -05003673 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003674 *
Chris Masond352ac62008-09-29 15:18:18 -04003675 * the tree should have been previously balanced so the deletion does not
3676 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003677 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003678static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3679 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003680{
Chris Mason5f39d392007-10-15 16:14:19 -04003681 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003682 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003683
Chris Mason5f39d392007-10-15 16:14:19 -04003684 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003685 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003686 memmove_extent_buffer(parent,
3687 btrfs_node_key_ptr_offset(slot),
3688 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003689 sizeof(struct btrfs_key_ptr) *
3690 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003691 }
Chris Mason7518a232007-03-12 12:01:18 -04003692 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003693 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003694 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003695 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003696 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003697 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003698 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003699 struct btrfs_disk_key disk_key;
3700
3701 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003702 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003703 }
Chris Masond6025572007-03-30 14:27:56 -04003704 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003705}
3706
Chris Mason74123bd2007-02-02 11:05:29 -05003707/*
Chris Mason323ac952008-10-01 19:05:46 -04003708 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003709 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003710 *
3711 * This deletes the pointer in path->nodes[1] and frees the leaf
3712 * block extent. zero is returned if it all worked out, < 0 otherwise.
3713 *
3714 * The path must have already been setup for deleting the leaf, including
3715 * all the proper balancing. path->nodes[1] must be locked.
3716 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003717static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3718 struct btrfs_root *root,
3719 struct btrfs_path *path,
3720 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003721{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003722 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003723 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003724
Chris Mason4d081c42009-02-04 09:31:28 -05003725 /*
3726 * btrfs_free_extent is expensive, we want to make sure we
3727 * aren't holding any locks when we call it
3728 */
3729 btrfs_unlock_up_safe(path, 0);
3730
Yan, Zhengf0486c62010-05-16 10:46:25 -04003731 root_sub_used(root, leaf->len);
3732
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003733 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003734}
3735/*
Chris Mason74123bd2007-02-02 11:05:29 -05003736 * delete the item at the leaf level in path. If that empties
3737 * the leaf, remove it from the tree
3738 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003739int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3740 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003741{
Chris Mason5f39d392007-10-15 16:14:19 -04003742 struct extent_buffer *leaf;
3743 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003744 int last_off;
3745 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003746 int ret = 0;
3747 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003748 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003749 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003750
Chris Mason5f39d392007-10-15 16:14:19 -04003751 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003752 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3753
3754 for (i = 0; i < nr; i++)
3755 dsize += btrfs_item_size_nr(leaf, slot + i);
3756
Chris Mason5f39d392007-10-15 16:14:19 -04003757 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003758
Chris Mason85e21ba2008-01-29 15:11:36 -05003759 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003760 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
3762 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003763 data_end + dsize,
3764 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003765 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003766
Chris Mason85e21ba2008-01-29 15:11:36 -05003767 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003768 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003769
Chris Mason5f39d392007-10-15 16:14:19 -04003770 item = btrfs_item_nr(leaf, i);
3771 ioff = btrfs_item_offset(leaf, item);
3772 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003773 }
Chris Masondb945352007-10-15 16:15:53 -04003774
Chris Mason5f39d392007-10-15 16:14:19 -04003775 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003776 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003777 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003778 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003779 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003780 btrfs_set_header_nritems(leaf, nritems - nr);
3781 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003782
Chris Mason74123bd2007-02-02 11:05:29 -05003783 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003784 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003785 if (leaf == root->node) {
3786 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003787 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003788 btrfs_set_path_blocking(path);
3789 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003790 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003791 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003792 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003793 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003794 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003795 struct btrfs_disk_key disk_key;
3796
3797 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003798 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003799 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003800
Chris Mason74123bd2007-02-02 11:05:29 -05003801 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003802 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003803 /* push_leaf_left fixes the path.
3804 * make sure the path still points to our leaf
3805 * for possible call to del_ptr below
3806 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003807 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003808 extent_buffer_get(leaf);
3809
Chris Masonb9473432009-03-13 11:00:37 -04003810 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003811 wret = push_leaf_left(trans, root, path, 1, 1,
3812 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003813 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003814 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003815
3816 if (path->nodes[0] == leaf &&
3817 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003818 wret = push_leaf_right(trans, root, path, 1,
3819 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003820 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003821 ret = wret;
3822 }
Chris Mason5f39d392007-10-15 16:14:19 -04003823
3824 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003825 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003826 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003827 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003828 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003829 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003830 /* if we're still in the path, make sure
3831 * we're dirty. Otherwise, one of the
3832 * push_leaf functions must have already
3833 * dirtied this buffer
3834 */
3835 if (path->nodes[0] == leaf)
3836 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003837 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003838 }
Chris Masond5719762007-03-23 10:01:08 -04003839 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003840 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003841 }
3842 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003843 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003844}
3845
Chris Mason97571fd2007-02-24 13:39:08 -05003846/*
Chris Mason925baed2008-06-25 16:01:30 -04003847 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003848 * returns 0 if it found something or 1 if there are no lesser leaves.
3849 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003850 *
3851 * This may release the path, and so you may lose any locks held at the
3852 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003853 */
3854int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3855{
Chris Mason925baed2008-06-25 16:01:30 -04003856 struct btrfs_key key;
3857 struct btrfs_disk_key found_key;
3858 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003859
Chris Mason925baed2008-06-25 16:01:30 -04003860 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003861
Chris Mason925baed2008-06-25 16:01:30 -04003862 if (key.offset > 0)
3863 key.offset--;
3864 else if (key.type > 0)
3865 key.type--;
3866 else if (key.objectid > 0)
3867 key.objectid--;
3868 else
3869 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003870
David Sterbab3b4aa72011-04-21 01:20:15 +02003871 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3873 if (ret < 0)
3874 return ret;
3875 btrfs_item_key(path->nodes[0], &found_key, 0);
3876 ret = comp_keys(&found_key, &key);
3877 if (ret < 0)
3878 return 0;
3879 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003880}
3881
Chris Mason3f157a22008-06-25 16:01:31 -04003882/*
3883 * A helper function to walk down the tree starting at min_key, and looking
3884 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003885 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003886 *
3887 * This does not cow, but it does stuff the starting key it finds back
3888 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3889 * key and get a writable path.
3890 *
3891 * This does lock as it descends, and path->keep_locks should be set
3892 * to 1 by the caller.
3893 *
3894 * This honors path->lowest_level to prevent descent past a given level
3895 * of the tree.
3896 *
Chris Masond352ac62008-09-29 15:18:18 -04003897 * min_trans indicates the oldest transaction that you are interested
3898 * in walking through. Any nodes or leaves older than min_trans are
3899 * skipped over (without reading them).
3900 *
Chris Mason3f157a22008-06-25 16:01:31 -04003901 * returns zero if something useful was found, < 0 on error and 1 if there
3902 * was nothing in the tree that matched the search criteria.
3903 */
3904int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003905 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003906 struct btrfs_path *path, int cache_only,
3907 u64 min_trans)
3908{
3909 struct extent_buffer *cur;
3910 struct btrfs_key found_key;
3911 int slot;
Yan96524802008-07-24 12:19:49 -04003912 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003913 u32 nritems;
3914 int level;
3915 int ret = 1;
3916
Chris Mason934d3752008-12-08 16:43:10 -05003917 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003918again:
Chris Masonbd681512011-07-16 15:23:14 -04003919 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003920 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003921 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003922 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003923 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003924
3925 if (btrfs_header_generation(cur) < min_trans) {
3926 ret = 1;
3927 goto out;
3928 }
Chris Masond3977122009-01-05 21:25:51 -05003929 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003930 nritems = btrfs_header_nritems(cur);
3931 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003932 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003933
Chris Mason323ac952008-10-01 19:05:46 -04003934 /* at the lowest level, we're done, setup the path and exit */
3935 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003936 if (slot >= nritems)
3937 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003938 ret = 0;
3939 path->slots[level] = slot;
3940 btrfs_item_key_to_cpu(cur, &found_key, slot);
3941 goto out;
3942 }
Yan96524802008-07-24 12:19:49 -04003943 if (sret && slot > 0)
3944 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003945 /*
3946 * check this node pointer against the cache_only and
3947 * min_trans parameters. If it isn't in cache or is too
3948 * old, skip to the next one.
3949 */
Chris Masond3977122009-01-05 21:25:51 -05003950 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003951 u64 blockptr;
3952 u64 gen;
3953 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003954 struct btrfs_disk_key disk_key;
3955
Chris Mason3f157a22008-06-25 16:01:31 -04003956 blockptr = btrfs_node_blockptr(cur, slot);
3957 gen = btrfs_node_ptr_generation(cur, slot);
3958 if (gen < min_trans) {
3959 slot++;
3960 continue;
3961 }
3962 if (!cache_only)
3963 break;
3964
Chris Masone02119d2008-09-05 16:13:11 -04003965 if (max_key) {
3966 btrfs_node_key(cur, &disk_key, slot);
3967 if (comp_keys(&disk_key, max_key) >= 0) {
3968 ret = 1;
3969 goto out;
3970 }
3971 }
3972
Chris Mason3f157a22008-06-25 16:01:31 -04003973 tmp = btrfs_find_tree_block(root, blockptr,
3974 btrfs_level_size(root, level - 1));
3975
3976 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3977 free_extent_buffer(tmp);
3978 break;
3979 }
3980 if (tmp)
3981 free_extent_buffer(tmp);
3982 slot++;
3983 }
Chris Masone02119d2008-09-05 16:13:11 -04003984find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003985 /*
3986 * we didn't find a candidate key in this node, walk forward
3987 * and find another one
3988 */
3989 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003990 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003991 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003992 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003993 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003994 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003995 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003996 goto again;
3997 } else {
3998 goto out;
3999 }
4000 }
4001 /* save our key for returning back */
4002 btrfs_node_key_to_cpu(cur, &found_key, slot);
4003 path->slots[level] = slot;
4004 if (level == path->lowest_level) {
4005 ret = 0;
4006 unlock_up(path, level, 1);
4007 goto out;
4008 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004009 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004010 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004011 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004012
Chris Masonbd681512011-07-16 15:23:14 -04004013 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004014
Chris Masonbd681512011-07-16 15:23:14 -04004015 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004016 path->nodes[level - 1] = cur;
4017 unlock_up(path, level, 1);
Chris Masonbd681512011-07-16 15:23:14 -04004018 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004019 }
4020out:
4021 if (ret == 0)
4022 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004023 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004024 return ret;
4025}
4026
4027/*
4028 * this is similar to btrfs_next_leaf, but does not try to preserve
4029 * and fixup the path. It looks for and returns the next key in the
4030 * tree based on the current path and the cache_only and min_trans
4031 * parameters.
4032 *
4033 * 0 is returned if another key is found, < 0 if there are any errors
4034 * and 1 is returned if there are no higher keys in the tree
4035 *
4036 * path->keep_locks should be set to 1 on the search made before
4037 * calling this function.
4038 */
Chris Masone7a84562008-06-25 16:01:31 -04004039int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004040 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004041 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004042{
Chris Masone7a84562008-06-25 16:01:31 -04004043 int slot;
4044 struct extent_buffer *c;
4045
Chris Mason934d3752008-12-08 16:43:10 -05004046 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004047 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004048 if (!path->nodes[level])
4049 return 1;
4050
4051 slot = path->slots[level] + 1;
4052 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004053next:
Chris Masone7a84562008-06-25 16:01:31 -04004054 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004055 int ret;
4056 int orig_lowest;
4057 struct btrfs_key cur_key;
4058 if (level + 1 >= BTRFS_MAX_LEVEL ||
4059 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004060 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004061
4062 if (path->locks[level + 1]) {
4063 level++;
4064 continue;
4065 }
4066
4067 slot = btrfs_header_nritems(c) - 1;
4068 if (level == 0)
4069 btrfs_item_key_to_cpu(c, &cur_key, slot);
4070 else
4071 btrfs_node_key_to_cpu(c, &cur_key, slot);
4072
4073 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004074 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004075 path->lowest_level = level;
4076 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4077 0, 0);
4078 path->lowest_level = orig_lowest;
4079 if (ret < 0)
4080 return ret;
4081
4082 c = path->nodes[level];
4083 slot = path->slots[level];
4084 if (ret == 0)
4085 slot++;
4086 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004087 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004088
Chris Masone7a84562008-06-25 16:01:31 -04004089 if (level == 0)
4090 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004091 else {
4092 u64 blockptr = btrfs_node_blockptr(c, slot);
4093 u64 gen = btrfs_node_ptr_generation(c, slot);
4094
4095 if (cache_only) {
4096 struct extent_buffer *cur;
4097 cur = btrfs_find_tree_block(root, blockptr,
4098 btrfs_level_size(root, level - 1));
4099 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4100 slot++;
4101 if (cur)
4102 free_extent_buffer(cur);
4103 goto next;
4104 }
4105 free_extent_buffer(cur);
4106 }
4107 if (gen < min_trans) {
4108 slot++;
4109 goto next;
4110 }
Chris Masone7a84562008-06-25 16:01:31 -04004111 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004112 }
Chris Masone7a84562008-06-25 16:01:31 -04004113 return 0;
4114 }
4115 return 1;
4116}
4117
Chris Mason7bb86312007-12-11 09:25:06 -05004118/*
Chris Mason925baed2008-06-25 16:01:30 -04004119 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004120 * returns 0 if it found something or 1 if there are no greater leaves.
4121 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004122 */
Chris Mason234b63a2007-03-13 10:46:10 -04004123int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004124{
4125 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004126 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004127 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004128 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004129 struct btrfs_key key;
4130 u32 nritems;
4131 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004132 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004133 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004134
4135 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004136 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004137 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004138
Chris Mason8e73f272009-04-03 10:14:18 -04004139 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4140again:
4141 level = 1;
4142 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004143 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004144 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004145
Chris Masona2135012008-06-25 16:01:30 -04004146 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004147 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004148
Chris Mason925baed2008-06-25 16:01:30 -04004149 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4150 path->keep_locks = 0;
4151
4152 if (ret < 0)
4153 return ret;
4154
Chris Masona2135012008-06-25 16:01:30 -04004155 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004156 /*
4157 * by releasing the path above we dropped all our locks. A balance
4158 * could have added more items next to the key that used to be
4159 * at the very end of the block. So, check again here and
4160 * advance the path if there are now more items available.
4161 */
Chris Masona2135012008-06-25 16:01:30 -04004162 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004163 if (ret == 0)
4164 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004165 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004166 goto done;
4167 }
Chris Masond97e63b2007-02-20 16:40:44 -05004168
Chris Masond3977122009-01-05 21:25:51 -05004169 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004170 if (!path->nodes[level]) {
4171 ret = 1;
4172 goto done;
4173 }
Chris Mason5f39d392007-10-15 16:14:19 -04004174
Chris Masond97e63b2007-02-20 16:40:44 -05004175 slot = path->slots[level] + 1;
4176 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004177 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004178 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004179 if (level == BTRFS_MAX_LEVEL) {
4180 ret = 1;
4181 goto done;
4182 }
Chris Masond97e63b2007-02-20 16:40:44 -05004183 continue;
4184 }
Chris Mason5f39d392007-10-15 16:14:19 -04004185
Chris Mason925baed2008-06-25 16:01:30 -04004186 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004187 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004188 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004189 }
Chris Mason5f39d392007-10-15 16:14:19 -04004190
Chris Mason8e73f272009-04-03 10:14:18 -04004191 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004192 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004193 ret = read_block_for_search(NULL, root, path, &next, level,
4194 slot, &key);
4195 if (ret == -EAGAIN)
4196 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004197
Chris Mason76a05b32009-05-14 13:24:30 -04004198 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004199 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004200 goto done;
4201 }
4202
Chris Mason5cd57b22008-06-25 16:01:30 -04004203 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004204 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004205 if (!ret) {
4206 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004207 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004208 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004209 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004210 }
Chris Mason31533fb2011-07-26 16:01:59 -04004211 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004212 }
Chris Masond97e63b2007-02-20 16:40:44 -05004213 break;
4214 }
4215 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004216 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004217 level--;
4218 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004219 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004220 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004221
Chris Mason5f39d392007-10-15 16:14:19 -04004222 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004223 path->nodes[level] = next;
4224 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004225 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004226 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004227 if (!level)
4228 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004229
Chris Mason8e73f272009-04-03 10:14:18 -04004230 ret = read_block_for_search(NULL, root, path, &next, level,
4231 0, &key);
4232 if (ret == -EAGAIN)
4233 goto again;
4234
Chris Mason76a05b32009-05-14 13:24:30 -04004235 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004236 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004237 goto done;
4238 }
4239
Chris Mason5cd57b22008-06-25 16:01:30 -04004240 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004241 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004242 if (!ret) {
4243 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004244 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004245 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004246 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004247 }
Chris Mason31533fb2011-07-26 16:01:59 -04004248 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004249 }
Chris Masond97e63b2007-02-20 16:40:44 -05004250 }
Chris Mason8e73f272009-04-03 10:14:18 -04004251 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004252done:
4253 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004254 path->leave_spinning = old_spinning;
4255 if (!old_spinning)
4256 btrfs_set_path_blocking(path);
4257
4258 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004259}
Chris Mason0b86a832008-03-24 15:01:56 -04004260
Chris Mason3f157a22008-06-25 16:01:31 -04004261/*
4262 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4263 * searching until it gets past min_objectid or finds an item of 'type'
4264 *
4265 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4266 */
Chris Mason0b86a832008-03-24 15:01:56 -04004267int btrfs_previous_item(struct btrfs_root *root,
4268 struct btrfs_path *path, u64 min_objectid,
4269 int type)
4270{
4271 struct btrfs_key found_key;
4272 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004273 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004274 int ret;
4275
Chris Masond3977122009-01-05 21:25:51 -05004276 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004277 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004278 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004279 ret = btrfs_prev_leaf(root, path);
4280 if (ret != 0)
4281 return ret;
4282 } else {
4283 path->slots[0]--;
4284 }
4285 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004286 nritems = btrfs_header_nritems(leaf);
4287 if (nritems == 0)
4288 return 1;
4289 if (path->slots[0] == nritems)
4290 path->slots[0]--;
4291
Chris Mason0b86a832008-03-24 15:01:56 -04004292 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004293 if (found_key.objectid < min_objectid)
4294 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004295 if (found_key.type == type)
4296 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004297 if (found_key.objectid == min_objectid &&
4298 found_key.type < type)
4299 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004300 }
4301 return 1;
4302}