blob: e801f226d7e028b72ca08b5726ed409215c27b84 [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
Josef Bacik3083ee22012-03-09 16:01:49 -0500159 while (1) {
160 rcu_read_lock();
161 eb = rcu_dereference(root->node);
162
163 /*
164 * RCU really hurts here, we could free up the root node because
165 * it was cow'ed but we may not get the new root node yet so do
166 * the inc_not_zero dance and if it doesn't work then
167 * synchronize_rcu and try again.
168 */
169 if (atomic_inc_not_zero(&eb->refs)) {
170 rcu_read_unlock();
171 break;
172 }
173 rcu_read_unlock();
174 synchronize_rcu();
175 }
Chris Mason925baed2008-06-25 16:01:30 -0400176 return eb;
177}
178
Chris Masond352ac62008-09-29 15:18:18 -0400179/* loop around taking references on and locking the root node of the
180 * tree until you end up with a lock on the root. A locked buffer
181 * is returned, with a reference held.
182 */
Chris Mason925baed2008-06-25 16:01:30 -0400183struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
184{
185 struct extent_buffer *eb;
186
Chris Masond3977122009-01-05 21:25:51 -0500187 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400188 eb = btrfs_root_node(root);
189 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400190 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400191 break;
Chris Mason925baed2008-06-25 16:01:30 -0400192 btrfs_tree_unlock(eb);
193 free_extent_buffer(eb);
194 }
195 return eb;
196}
197
Chris Masonbd681512011-07-16 15:23:14 -0400198/* loop around taking references on and locking the root node of the
199 * tree until you end up with a lock on the root. A locked buffer
200 * is returned, with a reference held.
201 */
202struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
203{
204 struct extent_buffer *eb;
205
206 while (1) {
207 eb = btrfs_root_node(root);
208 btrfs_tree_read_lock(eb);
209 if (eb == root->node)
210 break;
211 btrfs_tree_read_unlock(eb);
212 free_extent_buffer(eb);
213 }
214 return eb;
215}
216
Chris Masond352ac62008-09-29 15:18:18 -0400217/* cowonly root (everything not a reference counted cow subvolume), just get
218 * put onto a simple dirty list. transaction.c walks this to make sure they
219 * get properly updated on disk.
220 */
Chris Mason0b86a832008-03-24 15:01:56 -0400221static void add_root_to_dirty_list(struct btrfs_root *root)
222{
223 if (root->track_dirty && list_empty(&root->dirty_list)) {
224 list_add(&root->dirty_list,
225 &root->fs_info->dirty_cowonly_roots);
226 }
227}
228
Chris Masond352ac62008-09-29 15:18:18 -0400229/*
230 * used by snapshot creation to make a copy of a root for a tree with
231 * a given objectid. The buffer with the new root node is returned in
232 * cow_ret, and this func returns zero on success or a negative error code.
233 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500234int btrfs_copy_root(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root,
236 struct extent_buffer *buf,
237 struct extent_buffer **cow_ret, u64 new_root_objectid)
238{
239 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 int ret = 0;
241 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400242 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243
244 WARN_ON(root->ref_cows && trans->transid !=
245 root->fs_info->running_transaction->transid);
246 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
247
248 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400249 if (level == 0)
250 btrfs_item_key(buf, &disk_key, 0);
251 else
252 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400253
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
255 new_root_objectid, &disk_key, level,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200256 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400257 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 return PTR_ERR(cow);
259
260 copy_extent_buffer(cow, buf, 0, 0, cow->len);
261 btrfs_set_header_bytenr(cow, cow->start);
262 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
264 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
265 BTRFS_HEADER_FLAG_RELOC);
266 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
267 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
268 else
269 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500270
Yan Zheng2b820322008-11-17 21:11:30 -0500271 write_extent_buffer(cow, root->fs_info->fsid,
272 (unsigned long)btrfs_header_fsid(cow),
273 BTRFS_FSID_SIZE);
274
Chris Masonbe20aa92007-12-17 20:14:01 -0500275 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400276 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200277 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400278 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200279 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 if (ret)
282 return ret;
283
284 btrfs_mark_buffer_dirty(cow);
285 *cow_ret = cow;
286 return 0;
287}
288
Chris Masond352ac62008-09-29 15:18:18 -0400289/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 * check if the tree block can be shared by multiple trees
291 */
292int btrfs_block_can_be_shared(struct btrfs_root *root,
293 struct extent_buffer *buf)
294{
295 /*
296 * Tree blocks not in refernece counted trees and tree roots
297 * are never shared. If a block was allocated after the last
298 * snapshot and the block was not allocated by tree relocation,
299 * we know the block is not shared.
300 */
301 if (root->ref_cows &&
302 buf != root->node && buf != root->commit_root &&
303 (btrfs_header_generation(buf) <=
304 btrfs_root_last_snapshot(&root->root_item) ||
305 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
306 return 1;
307#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
308 if (root->ref_cows &&
309 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
310 return 1;
311#endif
312 return 0;
313}
314
315static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
316 struct btrfs_root *root,
317 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400318 struct extent_buffer *cow,
319 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400320{
321 u64 refs;
322 u64 owner;
323 u64 flags;
324 u64 new_flags = 0;
325 int ret;
326
327 /*
328 * Backrefs update rules:
329 *
330 * Always use full backrefs for extent pointers in tree block
331 * allocated by tree relocation.
332 *
333 * If a shared tree block is no longer referenced by its owner
334 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
335 * use full backrefs for extent pointers in tree block.
336 *
337 * If a tree block is been relocating
338 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
339 * use full backrefs for extent pointers in tree block.
340 * The reason for this is some operations (such as drop tree)
341 * are only allowed for blocks use full backrefs.
342 */
343
344 if (btrfs_block_can_be_shared(root, buf)) {
345 ret = btrfs_lookup_extent_info(trans, root, buf->start,
346 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700347 if (ret)
348 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700349 if (refs == 0) {
350 ret = -EROFS;
351 btrfs_std_error(root->fs_info, ret);
352 return ret;
353 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 } else {
355 refs = 1;
356 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
357 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
358 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
359 else
360 flags = 0;
361 }
362
363 owner = btrfs_header_owner(buf);
364 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
365 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
366
367 if (refs > 1) {
368 if ((owner == root->root_key.objectid ||
369 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
370 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200371 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100372 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400373
374 if (root->root_key.objectid ==
375 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200376 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100377 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200378 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100379 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400380 }
381 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
382 } else {
383
384 if (root->root_key.objectid ==
385 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200386 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200388 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100389 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400390 }
391 if (new_flags != 0) {
392 ret = btrfs_set_disk_extent_flags(trans, root,
393 buf->start,
394 buf->len,
395 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700396 if (ret)
397 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400398 }
399 } else {
400 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
401 if (root->root_key.objectid ==
402 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200403 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400404 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200405 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100406 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200407 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100408 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400409 }
410 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400411 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 }
413 return 0;
414}
415
416/*
Chris Masond3977122009-01-05 21:25:51 -0500417 * does the dirty work in cow of a single block. The parent block (if
418 * supplied) is updated to point to the new cow copy. The new buffer is marked
419 * dirty and returned locked. If you modify the block it needs to be marked
420 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400421 *
422 * search_start -- an allocation hint for the new block
423 *
Chris Masond3977122009-01-05 21:25:51 -0500424 * empty_size -- a hint that you plan on doing more cow. This is the size in
425 * bytes the allocator should try to find free next to the block it returns.
426 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400427 */
Chris Masond3977122009-01-05 21:25:51 -0500428static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400429 struct btrfs_root *root,
430 struct extent_buffer *buf,
431 struct extent_buffer *parent, int parent_slot,
432 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400433 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400434{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400435 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400436 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700437 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400438 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400439 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400440 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400441
Chris Mason925baed2008-06-25 16:01:30 -0400442 if (*cow_ret == buf)
443 unlock_orig = 1;
444
Chris Masonb9447ef2009-03-09 11:45:38 -0400445 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400446
Chris Mason7bb86312007-12-11 09:25:06 -0500447 WARN_ON(root->ref_cows && trans->transid !=
448 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400449 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400450
Chris Mason7bb86312007-12-11 09:25:06 -0500451 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400452
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400453 if (level == 0)
454 btrfs_item_key(buf, &disk_key, 0);
455 else
456 btrfs_node_key(buf, &disk_key, 0);
457
458 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
459 if (parent)
460 parent_start = parent->start;
461 else
462 parent_start = 0;
463 } else
464 parent_start = 0;
465
466 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
467 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200468 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400469 if (IS_ERR(cow))
470 return PTR_ERR(cow);
471
Chris Masonb4ce94d2009-02-04 09:25:08 -0500472 /* cow is set to blocking by btrfs_init_new_buffer */
473
Chris Mason5f39d392007-10-15 16:14:19 -0400474 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400475 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400476 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
478 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
479 BTRFS_HEADER_FLAG_RELOC);
480 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
481 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
482 else
483 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400484
Yan Zheng2b820322008-11-17 21:11:30 -0500485 write_extent_buffer(cow, root->fs_info->fsid,
486 (unsigned long)btrfs_header_fsid(cow),
487 BTRFS_FSID_SIZE);
488
Mark Fashehbe1a5562011-08-08 13:20:18 -0700489 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700490 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100491 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700492 return ret;
493 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400494
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400495 if (root->ref_cows)
496 btrfs_reloc_cow_block(trans, root, buf, cow);
497
Chris Mason6702ed42007-08-07 16:15:09 -0400498 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400499 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400500 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
501 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
502 parent_start = buf->start;
503 else
504 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400505
Chris Mason5f39d392007-10-15 16:14:19 -0400506 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400507 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400508
Yan, Zhengf0486c62010-05-16 10:46:25 -0400509 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200510 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400511 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400512 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400513 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400514 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
515 parent_start = parent->start;
516 else
517 parent_start = 0;
518
519 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400520 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400521 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500522 btrfs_set_node_ptr_generation(parent, parent_slot,
523 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400524 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400525 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200526 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400527 }
Chris Mason925baed2008-06-25 16:01:30 -0400528 if (unlock_orig)
529 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500530 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400531 btrfs_mark_buffer_dirty(cow);
532 *cow_ret = cow;
533 return 0;
534}
535
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400536static inline int should_cow_block(struct btrfs_trans_handle *trans,
537 struct btrfs_root *root,
538 struct extent_buffer *buf)
539{
Liu Bof1ebcc72011-11-14 20:48:06 -0500540 /* ensure we can see the force_cow */
541 smp_rmb();
542
543 /*
544 * We do not need to cow a block if
545 * 1) this block is not created or changed in this transaction;
546 * 2) this block does not belong to TREE_RELOC tree;
547 * 3) the root is not forced COW.
548 *
549 * What is forced COW:
550 * when we create snapshot during commiting the transaction,
551 * after we've finished coping src root, we must COW the shared
552 * block to ensure the metadata consistency.
553 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400554 if (btrfs_header_generation(buf) == trans->transid &&
555 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
556 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500557 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
558 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400559 return 0;
560 return 1;
561}
562
Chris Masond352ac62008-09-29 15:18:18 -0400563/*
564 * cows a single block, see __btrfs_cow_block for the real work.
565 * This version of it has extra checks so that a block isn't cow'd more than
566 * once per transaction, as long as it hasn't been written yet
567 */
Chris Masond3977122009-01-05 21:25:51 -0500568noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400569 struct btrfs_root *root, struct extent_buffer *buf,
570 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400571 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500572{
Chris Mason6702ed42007-08-07 16:15:09 -0400573 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400574 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500575
Chris Masonccd467d2007-06-28 15:57:36 -0400576 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500577 printk(KERN_CRIT "trans %llu running %llu\n",
578 (unsigned long long)trans->transid,
579 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400580 root->fs_info->running_transaction->transid);
581 WARN_ON(1);
582 }
583 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500584 printk(KERN_CRIT "trans %llu running %llu\n",
585 (unsigned long long)trans->transid,
586 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400587 WARN_ON(1);
588 }
Chris Masondc17ff82008-01-08 15:46:30 -0500589
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500591 *cow_ret = buf;
592 return 0;
593 }
Chris Masonc4876852009-02-04 09:24:25 -0500594
Chris Mason0b86a832008-03-24 15:01:56 -0400595 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500596
597 if (parent)
598 btrfs_set_lock_blocking(parent);
599 btrfs_set_lock_blocking(buf);
600
Chris Masonf510cfe2007-10-15 16:14:48 -0400601 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400602 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000603
604 trace_btrfs_cow_block(root, buf, *cow_ret);
605
Chris Masonf510cfe2007-10-15 16:14:48 -0400606 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400607}
608
Chris Masond352ac62008-09-29 15:18:18 -0400609/*
610 * helper function for defrag to decide if two blocks pointed to by a
611 * node are actually close by
612 */
Chris Mason6b800532007-10-15 16:17:34 -0400613static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400614{
Chris Mason6b800532007-10-15 16:17:34 -0400615 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400616 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400617 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400618 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500619 return 0;
620}
621
Chris Mason081e9572007-11-06 10:26:24 -0500622/*
623 * compare two keys in a memcmp fashion
624 */
625static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
626{
627 struct btrfs_key k1;
628
629 btrfs_disk_key_to_cpu(&k1, disk);
630
Diego Calleja20736ab2009-07-24 11:06:52 -0400631 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500632}
633
Josef Bacikf3465ca2008-11-12 14:19:50 -0500634/*
635 * same as comp_keys only with two btrfs_key's
636 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400637int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500638{
639 if (k1->objectid > k2->objectid)
640 return 1;
641 if (k1->objectid < k2->objectid)
642 return -1;
643 if (k1->type > k2->type)
644 return 1;
645 if (k1->type < k2->type)
646 return -1;
647 if (k1->offset > k2->offset)
648 return 1;
649 if (k1->offset < k2->offset)
650 return -1;
651 return 0;
652}
Chris Mason081e9572007-11-06 10:26:24 -0500653
Chris Masond352ac62008-09-29 15:18:18 -0400654/*
655 * this is used by the defrag code to go through all the
656 * leaves pointed to by a node and reallocate them so that
657 * disk order is close to key order
658 */
Chris Mason6702ed42007-08-07 16:15:09 -0400659int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400660 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400661 int start_slot, int cache_only, u64 *last_ret,
662 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400663{
Chris Mason6b800532007-10-15 16:17:34 -0400664 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400665 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400666 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400667 u64 search_start = *last_ret;
668 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400669 u64 other;
670 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400671 int end_slot;
672 int i;
673 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400674 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400675 int uptodate;
676 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500677 int progress_passed = 0;
678 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400679
Chris Mason5708b952007-10-25 15:43:18 -0400680 parent_level = btrfs_header_level(parent);
681 if (cache_only && parent_level != 1)
682 return 0;
683
Chris Masond3977122009-01-05 21:25:51 -0500684 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400685 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500686 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400687 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400688
Chris Mason6b800532007-10-15 16:17:34 -0400689 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400690 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400691 end_slot = parent_nritems;
692
693 if (parent_nritems == 1)
694 return 0;
695
Chris Masonb4ce94d2009-02-04 09:25:08 -0500696 btrfs_set_lock_blocking(parent);
697
Chris Mason6702ed42007-08-07 16:15:09 -0400698 for (i = start_slot; i < end_slot; i++) {
699 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400700
Chris Mason081e9572007-11-06 10:26:24 -0500701 btrfs_node_key(parent, &disk_key, i);
702 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
703 continue;
704
705 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400706 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400707 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400708 if (last_block == 0)
709 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400710
Chris Mason6702ed42007-08-07 16:15:09 -0400711 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400712 other = btrfs_node_blockptr(parent, i - 1);
713 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400714 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400715 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400716 other = btrfs_node_blockptr(parent, i + 1);
717 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400718 }
Chris Masone9d0b132007-08-10 14:06:19 -0400719 if (close) {
720 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400721 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400722 }
Chris Mason6702ed42007-08-07 16:15:09 -0400723
Chris Mason6b800532007-10-15 16:17:34 -0400724 cur = btrfs_find_tree_block(root, blocknr, blocksize);
725 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400726 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400727 else
728 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400729 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400730 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400731 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400732 continue;
733 }
Chris Mason6b800532007-10-15 16:17:34 -0400734 if (!cur) {
735 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400736 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000737 if (!cur)
738 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400739 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400740 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400741 }
Chris Mason6702ed42007-08-07 16:15:09 -0400742 }
Chris Masone9d0b132007-08-10 14:06:19 -0400743 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400744 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400745
Chris Masone7a84562008-06-25 16:01:31 -0400746 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500747 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400748 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400749 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400750 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400751 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400752 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400753 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400754 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400755 break;
Yan252c38f2007-08-29 09:11:44 -0400756 }
Chris Masone7a84562008-06-25 16:01:31 -0400757 search_start = cur->start;
758 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400759 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400760 btrfs_tree_unlock(cur);
761 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400762 }
763 return err;
764}
765
Chris Mason74123bd2007-02-02 11:05:29 -0500766/*
767 * The leaf data grows from end-to-front in the node.
768 * this returns the address of the start of the last item,
769 * which is the stop of the leaf data stack
770 */
Chris Mason123abc82007-03-14 14:14:43 -0400771static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400772 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500773{
Chris Mason5f39d392007-10-15 16:14:19 -0400774 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400776 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400777 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500778}
779
Chris Masonaa5d6be2007-02-28 16:35:06 -0500780
Chris Mason74123bd2007-02-02 11:05:29 -0500781/*
Chris Mason5f39d392007-10-15 16:14:19 -0400782 * search for key in the extent_buffer. The items start at offset p,
783 * and they are item_size apart. There are 'max' items in p.
784 *
Chris Mason74123bd2007-02-02 11:05:29 -0500785 * the slot in the array is returned via slot, and it points to
786 * the place where you would insert key if it is not found in
787 * the array.
788 *
789 * slot may point to max if the key is bigger than all of the keys
790 */
Chris Masone02119d2008-09-05 16:13:11 -0400791static noinline int generic_bin_search(struct extent_buffer *eb,
792 unsigned long p,
793 int item_size, struct btrfs_key *key,
794 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500795{
796 int low = 0;
797 int high = max;
798 int mid;
799 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400800 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400801 struct btrfs_disk_key unaligned;
802 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400803 char *kaddr = NULL;
804 unsigned long map_start = 0;
805 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400806 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500807
Chris Masond3977122009-01-05 21:25:51 -0500808 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500809 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400810 offset = p + mid * item_size;
811
Chris Masona6591712011-07-19 12:04:14 -0400812 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400813 (offset + sizeof(struct btrfs_disk_key)) >
814 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500815
816 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400817 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400818 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400819
Chris Mason479965d2007-10-15 16:14:27 -0400820 if (!err) {
821 tmp = (struct btrfs_disk_key *)(kaddr + offset -
822 map_start);
823 } else {
824 read_extent_buffer(eb, &unaligned,
825 offset, sizeof(unaligned));
826 tmp = &unaligned;
827 }
828
Chris Mason5f39d392007-10-15 16:14:19 -0400829 } else {
830 tmp = (struct btrfs_disk_key *)(kaddr + offset -
831 map_start);
832 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500833 ret = comp_keys(tmp, key);
834
835 if (ret < 0)
836 low = mid + 1;
837 else if (ret > 0)
838 high = mid;
839 else {
840 *slot = mid;
841 return 0;
842 }
843 }
844 *slot = low;
845 return 1;
846}
847
Chris Mason97571fd2007-02-24 13:39:08 -0500848/*
849 * simple bin_search frontend that does the right thing for
850 * leaves vs nodes
851 */
Chris Mason5f39d392007-10-15 16:14:19 -0400852static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
853 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500854{
Chris Mason5f39d392007-10-15 16:14:19 -0400855 if (level == 0) {
856 return generic_bin_search(eb,
857 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400858 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400859 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400860 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500861 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400862 return generic_bin_search(eb,
863 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400864 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400865 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400866 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500867 }
868 return -1;
869}
870
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
872 int level, int *slot)
873{
874 return bin_search(eb, key, level, slot);
875}
876
Yan, Zhengf0486c62010-05-16 10:46:25 -0400877static void root_add_used(struct btrfs_root *root, u32 size)
878{
879 spin_lock(&root->accounting_lock);
880 btrfs_set_root_used(&root->root_item,
881 btrfs_root_used(&root->root_item) + size);
882 spin_unlock(&root->accounting_lock);
883}
884
885static void root_sub_used(struct btrfs_root *root, u32 size)
886{
887 spin_lock(&root->accounting_lock);
888 btrfs_set_root_used(&root->root_item,
889 btrfs_root_used(&root->root_item) - size);
890 spin_unlock(&root->accounting_lock);
891}
892
Chris Masond352ac62008-09-29 15:18:18 -0400893/* given a node and slot number, this reads the blocks it points to. The
894 * extent buffer is returned with a reference taken (but unlocked).
895 * NULL is returned on error.
896 */
Chris Masone02119d2008-09-05 16:13:11 -0400897static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400898 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500899{
Chris Masonca7a79a2008-05-12 12:59:19 -0400900 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500901 if (slot < 0)
902 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400903 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500904 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400905
906 BUG_ON(level == 0);
907
Chris Masondb945352007-10-15 16:15:53 -0400908 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400909 btrfs_level_size(root, level - 1),
910 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500911}
912
Chris Masond352ac62008-09-29 15:18:18 -0400913/*
914 * node level balancing, used to make sure nodes are in proper order for
915 * item deletion. We balance from the top down, so we have to make sure
916 * that a deletion won't leave an node completely empty later on.
917 */
Chris Masone02119d2008-09-05 16:13:11 -0400918static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500919 struct btrfs_root *root,
920 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500921{
Chris Mason5f39d392007-10-15 16:14:19 -0400922 struct extent_buffer *right = NULL;
923 struct extent_buffer *mid;
924 struct extent_buffer *left = NULL;
925 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500926 int ret = 0;
927 int wret;
928 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500929 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500930 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500931
932 if (level == 0)
933 return 0;
934
Chris Mason5f39d392007-10-15 16:14:19 -0400935 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500936
Chris Masonbd681512011-07-16 15:23:14 -0400937 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
938 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500939 WARN_ON(btrfs_header_generation(mid) != trans->transid);
940
Chris Mason1d4f8a02007-03-13 09:28:32 -0400941 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500942
Li Zefana05a9bb2011-09-06 16:55:34 +0800943 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400944 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800945 pslot = path->slots[level + 1];
946 }
Chris Masonbb803952007-03-01 12:04:21 -0500947
Chris Mason40689472007-03-17 14:29:23 -0400948 /*
949 * deal with the case where there is only one pointer in the root
950 * by promoting the node below to a root
951 */
Chris Mason5f39d392007-10-15 16:14:19 -0400952 if (!parent) {
953 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500956 return 0;
957
958 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400959 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -0700960 if (!child) {
961 ret = -EROFS;
962 btrfs_std_error(root->fs_info, ret);
963 goto enospc;
964 }
965
Chris Mason925baed2008-06-25 16:01:30 -0400966 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500967 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400968 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400969 if (ret) {
970 btrfs_tree_unlock(child);
971 free_extent_buffer(child);
972 goto enospc;
973 }
Yan2f375ab2008-02-01 14:58:07 -0500974
Chris Mason240f62c2011-03-23 14:54:42 -0400975 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400976
Chris Mason0b86a832008-03-24 15:01:56 -0400977 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400978 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500979
Chris Mason925baed2008-06-25 16:01:30 -0400980 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500981 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400982 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400983 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500984 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400985 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400986
987 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200988 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500989 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -0500990 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400991 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500992 }
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400994 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500995 return 0;
996
Andi Kleen559af822010-10-29 15:14:37 -0400997 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400998
Chris Mason5f39d392007-10-15 16:14:19 -0400999 left = read_node_slot(root, parent, pslot - 1);
1000 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001001 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001002 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001003 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001004 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001005 if (wret) {
1006 ret = wret;
1007 goto enospc;
1008 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001009 }
Chris Mason5f39d392007-10-15 16:14:19 -04001010 right = read_node_slot(root, parent, pslot + 1);
1011 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001012 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001013 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001014 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001015 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001016 if (wret) {
1017 ret = wret;
1018 goto enospc;
1019 }
1020 }
1021
1022 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001023 if (left) {
1024 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001025 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001026 if (wret < 0)
1027 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001028 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001029 }
Chris Mason79f95c82007-03-01 15:16:26 -05001030
1031 /*
1032 * then try to empty the right most buffer into the middle
1033 */
Chris Mason5f39d392007-10-15 16:14:19 -04001034 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001035 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001036 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001037 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001038 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001039 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001040 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001041 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001042 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001043 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001044 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001045 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001046 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001047 struct btrfs_disk_key right_key;
1048 btrfs_node_key(right, &right_key, 0);
1049 btrfs_set_node_key(parent, &right_key, pslot + 1);
1050 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001051 }
1052 }
Chris Mason5f39d392007-10-15 16:14:19 -04001053 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001054 /*
1055 * we're not allowed to leave a node with one item in the
1056 * tree during a delete. A deletion from lower in the tree
1057 * could try to delete the only pointer in this node.
1058 * So, pull some keys from the left.
1059 * There has to be a left pointer at this point because
1060 * otherwise we would have pulled some pointers from the
1061 * right
1062 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001063 if (!left) {
1064 ret = -EROFS;
1065 btrfs_std_error(root->fs_info, ret);
1066 goto enospc;
1067 }
Chris Mason5f39d392007-10-15 16:14:19 -04001068 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001069 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001070 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001071 goto enospc;
1072 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001073 if (wret == 1) {
1074 wret = push_node_left(trans, root, left, mid, 1);
1075 if (wret < 0)
1076 ret = wret;
1077 }
Chris Mason79f95c82007-03-01 15:16:26 -05001078 BUG_ON(wret == 1);
1079 }
Chris Mason5f39d392007-10-15 16:14:19 -04001080 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001081 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001082 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001083 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001084 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001085 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001086 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001087 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001088 } else {
1089 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001090 struct btrfs_disk_key mid_key;
1091 btrfs_node_key(mid, &mid_key, 0);
1092 btrfs_set_node_key(parent, &mid_key, pslot);
1093 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001094 }
Chris Masonbb803952007-03-01 12:04:21 -05001095
Chris Mason79f95c82007-03-01 15:16:26 -05001096 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001097 if (left) {
1098 if (btrfs_header_nritems(left) > orig_slot) {
1099 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001100 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001101 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001102 path->slots[level + 1] -= 1;
1103 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001104 if (mid) {
1105 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001106 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001107 }
Chris Masonbb803952007-03-01 12:04:21 -05001108 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001109 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001110 path->slots[level] = orig_slot;
1111 }
1112 }
Chris Mason79f95c82007-03-01 15:16:26 -05001113 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001114 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001115 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001116 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001117enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001118 if (right) {
1119 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001120 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001121 }
1122 if (left) {
1123 if (path->nodes[level] != left)
1124 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001125 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001126 }
Chris Masonbb803952007-03-01 12:04:21 -05001127 return ret;
1128}
1129
Chris Masond352ac62008-09-29 15:18:18 -04001130/* Node balancing for insertion. Here we only split or push nodes around
1131 * when they are completely full. This is also done top down, so we
1132 * have to be pessimistic.
1133 */
Chris Masond3977122009-01-05 21:25:51 -05001134static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001135 struct btrfs_root *root,
1136 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001137{
Chris Mason5f39d392007-10-15 16:14:19 -04001138 struct extent_buffer *right = NULL;
1139 struct extent_buffer *mid;
1140 struct extent_buffer *left = NULL;
1141 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001142 int ret = 0;
1143 int wret;
1144 int pslot;
1145 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001146
1147 if (level == 0)
1148 return 1;
1149
Chris Mason5f39d392007-10-15 16:14:19 -04001150 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001151 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001152
Li Zefana05a9bb2011-09-06 16:55:34 +08001153 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001154 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001155 pslot = path->slots[level + 1];
1156 }
Chris Masone66f7092007-04-20 13:16:02 -04001157
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001159 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001160
Chris Mason5f39d392007-10-15 16:14:19 -04001161 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001162
1163 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001164 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001165 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001166
1167 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001168 btrfs_set_lock_blocking(left);
1169
Chris Mason5f39d392007-10-15 16:14:19 -04001170 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001171 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1172 wret = 1;
1173 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001174 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001175 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 if (ret)
1177 wret = 1;
1178 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001179 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001180 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001182 }
Chris Masone66f7092007-04-20 13:16:02 -04001183 if (wret < 0)
1184 ret = wret;
1185 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001187 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001188 btrfs_node_key(mid, &disk_key, 0);
1189 btrfs_set_node_key(parent, &disk_key, pslot);
1190 btrfs_mark_buffer_dirty(parent);
1191 if (btrfs_header_nritems(left) > orig_slot) {
1192 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001193 path->slots[level + 1] -= 1;
1194 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001195 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001196 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001197 } else {
1198 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001199 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001200 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001201 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001202 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001203 }
Chris Masone66f7092007-04-20 13:16:02 -04001204 return 0;
1205 }
Chris Mason925baed2008-06-25 16:01:30 -04001206 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001207 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001208 }
Chris Mason925baed2008-06-25 16:01:30 -04001209 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001210
1211 /*
1212 * then try to empty the right most buffer into the middle
1213 */
Chris Mason5f39d392007-10-15 16:14:19 -04001214 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001215 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001216
Chris Mason925baed2008-06-25 16:01:30 -04001217 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001218 btrfs_set_lock_blocking(right);
1219
Chris Mason5f39d392007-10-15 16:14:19 -04001220 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001221 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1222 wret = 1;
1223 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001224 ret = btrfs_cow_block(trans, root, right,
1225 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001226 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001227 if (ret)
1228 wret = 1;
1229 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001230 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001231 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001232 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001233 }
Chris Masone66f7092007-04-20 13:16:02 -04001234 if (wret < 0)
1235 ret = wret;
1236 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001237 struct btrfs_disk_key disk_key;
1238
1239 btrfs_node_key(right, &disk_key, 0);
1240 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1241 btrfs_mark_buffer_dirty(parent);
1242
1243 if (btrfs_header_nritems(mid) <= orig_slot) {
1244 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001245 path->slots[level + 1] += 1;
1246 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001247 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001248 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001249 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001250 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001251 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001252 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001253 }
Chris Masone66f7092007-04-20 13:16:02 -04001254 return 0;
1255 }
Chris Mason925baed2008-06-25 16:01:30 -04001256 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001257 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001258 }
Chris Masone66f7092007-04-20 13:16:02 -04001259 return 1;
1260}
1261
Chris Mason74123bd2007-02-02 11:05:29 -05001262/*
Chris Masond352ac62008-09-29 15:18:18 -04001263 * readahead one full node of leaves, finding things that are close
1264 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001265 */
Chris Masonc8c42862009-04-03 10:14:18 -04001266static void reada_for_search(struct btrfs_root *root,
1267 struct btrfs_path *path,
1268 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001269{
Chris Mason5f39d392007-10-15 16:14:19 -04001270 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001271 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001272 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001273 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001274 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001275 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001276 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001278 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001279 u32 nr;
1280 u32 blocksize;
1281 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001282
Chris Masona6b6e752007-10-15 16:22:39 -04001283 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001284 return;
1285
Chris Mason6702ed42007-08-07 16:15:09 -04001286 if (!path->nodes[level])
1287 return;
1288
Chris Mason5f39d392007-10-15 16:14:19 -04001289 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001290
Chris Mason3c69fae2007-08-07 15:52:22 -04001291 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001292 blocksize = btrfs_level_size(root, level - 1);
1293 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001294 if (eb) {
1295 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001296 return;
1297 }
1298
Chris Masona7175312009-01-22 09:23:10 -05001299 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001300
Chris Mason5f39d392007-10-15 16:14:19 -04001301 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001302 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001303
Chris Masond3977122009-01-05 21:25:51 -05001304 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001305 if (direction < 0) {
1306 if (nr == 0)
1307 break;
1308 nr--;
1309 } else if (direction > 0) {
1310 nr++;
1311 if (nr >= nritems)
1312 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001313 }
Chris Mason01f46652007-12-21 16:24:26 -05001314 if (path->reada < 0 && objectid) {
1315 btrfs_node_key(node, &disk_key, nr);
1316 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1317 break;
1318 }
Chris Mason6b800532007-10-15 16:17:34 -04001319 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001320 if ((search <= target && target - search <= 65536) ||
1321 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001322 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001323 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001324 nread += blocksize;
1325 }
1326 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001327 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001328 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001329 }
1330}
Chris Mason925baed2008-06-25 16:01:30 -04001331
Chris Masond352ac62008-09-29 15:18:18 -04001332/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001333 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1334 * cache
1335 */
1336static noinline int reada_for_balance(struct btrfs_root *root,
1337 struct btrfs_path *path, int level)
1338{
1339 int slot;
1340 int nritems;
1341 struct extent_buffer *parent;
1342 struct extent_buffer *eb;
1343 u64 gen;
1344 u64 block1 = 0;
1345 u64 block2 = 0;
1346 int ret = 0;
1347 int blocksize;
1348
Chris Mason8c594ea2009-04-20 15:50:10 -04001349 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001350 if (!parent)
1351 return 0;
1352
1353 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001354 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001355 blocksize = btrfs_level_size(root, level);
1356
1357 if (slot > 0) {
1358 block1 = btrfs_node_blockptr(parent, slot - 1);
1359 gen = btrfs_node_ptr_generation(parent, slot - 1);
1360 eb = btrfs_find_tree_block(root, block1, blocksize);
1361 if (eb && btrfs_buffer_uptodate(eb, gen))
1362 block1 = 0;
1363 free_extent_buffer(eb);
1364 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001365 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001366 block2 = btrfs_node_blockptr(parent, slot + 1);
1367 gen = btrfs_node_ptr_generation(parent, slot + 1);
1368 eb = btrfs_find_tree_block(root, block2, blocksize);
1369 if (eb && btrfs_buffer_uptodate(eb, gen))
1370 block2 = 0;
1371 free_extent_buffer(eb);
1372 }
1373 if (block1 || block2) {
1374 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001375
1376 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001377 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001378
1379 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001380 if (block1)
1381 readahead_tree_block(root, block1, blocksize, 0);
1382 if (block2)
1383 readahead_tree_block(root, block2, blocksize, 0);
1384
1385 if (block1) {
1386 eb = read_tree_block(root, block1, blocksize, 0);
1387 free_extent_buffer(eb);
1388 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001389 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001390 eb = read_tree_block(root, block2, blocksize, 0);
1391 free_extent_buffer(eb);
1392 }
1393 }
1394 return ret;
1395}
1396
1397
1398/*
Chris Masond3977122009-01-05 21:25:51 -05001399 * when we walk down the tree, it is usually safe to unlock the higher layers
1400 * in the tree. The exceptions are when our path goes through slot 0, because
1401 * operations on the tree might require changing key pointers higher up in the
1402 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001403 *
Chris Masond3977122009-01-05 21:25:51 -05001404 * callers might also have set path->keep_locks, which tells this code to keep
1405 * the lock if the path points to the last slot in the block. This is part of
1406 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001407 *
Chris Masond3977122009-01-05 21:25:51 -05001408 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1409 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001410 */
Chris Masone02119d2008-09-05 16:13:11 -04001411static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001412 int lowest_unlock, int min_write_lock_level,
1413 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001414{
1415 int i;
1416 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001417 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001418 struct extent_buffer *t;
1419
1420 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1421 if (!path->nodes[i])
1422 break;
1423 if (!path->locks[i])
1424 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001425 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001426 skip_level = i + 1;
1427 continue;
1428 }
Chris Mason051e1b92008-06-25 16:01:30 -04001429 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001430 u32 nritems;
1431 t = path->nodes[i];
1432 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001433 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001434 skip_level = i + 1;
1435 continue;
1436 }
1437 }
Chris Mason051e1b92008-06-25 16:01:30 -04001438 if (skip_level < i && i >= lowest_unlock)
1439 no_skips = 1;
1440
Chris Mason925baed2008-06-25 16:01:30 -04001441 t = path->nodes[i];
1442 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001443 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001444 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001445 if (write_lock_level &&
1446 i > min_write_lock_level &&
1447 i <= *write_lock_level) {
1448 *write_lock_level = i - 1;
1449 }
Chris Mason925baed2008-06-25 16:01:30 -04001450 }
1451 }
1452}
1453
Chris Mason3c69fae2007-08-07 15:52:22 -04001454/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001455 * This releases any locks held in the path starting at level and
1456 * going all the way up to the root.
1457 *
1458 * btrfs_search_slot will keep the lock held on higher nodes in a few
1459 * corner cases, such as COW of the block at slot zero in the node. This
1460 * ignores those rules, and it should only be called when there are no
1461 * more updates to be done higher up in the tree.
1462 */
1463noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1464{
1465 int i;
1466
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001467 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001468 return;
1469
1470 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1471 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001472 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001473 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001474 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001475 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001476 path->locks[i] = 0;
1477 }
1478}
1479
1480/*
Chris Masonc8c42862009-04-03 10:14:18 -04001481 * helper function for btrfs_search_slot. The goal is to find a block
1482 * in cache without setting the path to blocking. If we find the block
1483 * we return zero and the path is unchanged.
1484 *
1485 * If we can't find the block, we set the path blocking and do some
1486 * reada. -EAGAIN is returned and the search must be repeated.
1487 */
1488static int
1489read_block_for_search(struct btrfs_trans_handle *trans,
1490 struct btrfs_root *root, struct btrfs_path *p,
1491 struct extent_buffer **eb_ret, int level, int slot,
1492 struct btrfs_key *key)
1493{
1494 u64 blocknr;
1495 u64 gen;
1496 u32 blocksize;
1497 struct extent_buffer *b = *eb_ret;
1498 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001499 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001500
1501 blocknr = btrfs_node_blockptr(b, slot);
1502 gen = btrfs_node_ptr_generation(b, slot);
1503 blocksize = btrfs_level_size(root, level - 1);
1504
1505 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001506 if (tmp) {
1507 if (btrfs_buffer_uptodate(tmp, 0)) {
1508 if (btrfs_buffer_uptodate(tmp, gen)) {
1509 /*
1510 * we found an up to date block without
1511 * sleeping, return
1512 * right away
1513 */
1514 *eb_ret = tmp;
1515 return 0;
1516 }
1517 /* the pages were up to date, but we failed
1518 * the generation number check. Do a full
1519 * read for the generation number that is correct.
1520 * We must do this without dropping locks so
1521 * we can trust our generation number
1522 */
1523 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001524 btrfs_set_path_blocking(p);
1525
Chris Masoncb449212010-10-24 11:01:27 -04001526 tmp = read_tree_block(root, blocknr, blocksize, gen);
1527 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1528 *eb_ret = tmp;
1529 return 0;
1530 }
1531 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001532 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001533 return -EIO;
1534 }
Chris Masonc8c42862009-04-03 10:14:18 -04001535 }
1536
1537 /*
1538 * reduce lock contention at high levels
1539 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001540 * we read. Don't release the lock on the current
1541 * level because we need to walk this node to figure
1542 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001543 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001544 btrfs_unlock_up_safe(p, level + 1);
1545 btrfs_set_path_blocking(p);
1546
Chris Masoncb449212010-10-24 11:01:27 -04001547 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001548 if (p->reada)
1549 reada_for_search(root, p, level, slot, key->objectid);
1550
David Sterbab3b4aa72011-04-21 01:20:15 +02001551 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001552
1553 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001554 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001555 if (tmp) {
1556 /*
1557 * If the read above didn't mark this buffer up to date,
1558 * it will never end up being up to date. Set ret to EIO now
1559 * and give up so that our caller doesn't loop forever
1560 * on our EAGAINs.
1561 */
1562 if (!btrfs_buffer_uptodate(tmp, 0))
1563 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001564 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001565 }
1566 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001567}
1568
1569/*
1570 * helper function for btrfs_search_slot. This does all of the checks
1571 * for node-level blocks and does any balancing required based on
1572 * the ins_len.
1573 *
1574 * If no extra work was required, zero is returned. If we had to
1575 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1576 * start over
1577 */
1578static int
1579setup_nodes_for_search(struct btrfs_trans_handle *trans,
1580 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001581 struct extent_buffer *b, int level, int ins_len,
1582 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001583{
1584 int ret;
1585 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1586 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1587 int sret;
1588
Chris Masonbd681512011-07-16 15:23:14 -04001589 if (*write_lock_level < level + 1) {
1590 *write_lock_level = level + 1;
1591 btrfs_release_path(p);
1592 goto again;
1593 }
1594
Chris Masonc8c42862009-04-03 10:14:18 -04001595 sret = reada_for_balance(root, p, level);
1596 if (sret)
1597 goto again;
1598
1599 btrfs_set_path_blocking(p);
1600 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001601 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001602
1603 BUG_ON(sret > 0);
1604 if (sret) {
1605 ret = sret;
1606 goto done;
1607 }
1608 b = p->nodes[level];
1609 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001610 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001611 int sret;
1612
Chris Masonbd681512011-07-16 15:23:14 -04001613 if (*write_lock_level < level + 1) {
1614 *write_lock_level = level + 1;
1615 btrfs_release_path(p);
1616 goto again;
1617 }
1618
Chris Masonc8c42862009-04-03 10:14:18 -04001619 sret = reada_for_balance(root, p, level);
1620 if (sret)
1621 goto again;
1622
1623 btrfs_set_path_blocking(p);
1624 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001625 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001626
1627 if (sret) {
1628 ret = sret;
1629 goto done;
1630 }
1631 b = p->nodes[level];
1632 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001633 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001634 goto again;
1635 }
1636 BUG_ON(btrfs_header_nritems(b) == 1);
1637 }
1638 return 0;
1639
1640again:
1641 ret = -EAGAIN;
1642done:
1643 return ret;
1644}
1645
1646/*
Chris Mason74123bd2007-02-02 11:05:29 -05001647 * look for key in the tree. path is filled in with nodes along the way
1648 * if key is found, we return zero and you can find the item in the leaf
1649 * level of the path (level 0)
1650 *
1651 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001652 * be inserted, and 1 is returned. If there are other errors during the
1653 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001654 *
1655 * if ins_len > 0, nodes and leaves will be split as we walk down the
1656 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1657 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001658 */
Chris Masone089f052007-03-16 16:20:31 -04001659int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1660 *root, struct btrfs_key *key, struct btrfs_path *p, int
1661 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001662{
Chris Mason5f39d392007-10-15 16:14:19 -04001663 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001664 int slot;
1665 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001666 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001667 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001668 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001669 int root_lock;
1670 /* everything at write_lock_level or lower must be write locked */
1671 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001672 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001673 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001674
Chris Mason6702ed42007-08-07 16:15:09 -04001675 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001676 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001677 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001678
Chris Masonbd681512011-07-16 15:23:14 -04001679 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001680 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001681
Chris Masonbd681512011-07-16 15:23:14 -04001682 /* when we are removing items, we might have to go up to level
1683 * two as we update tree pointers Make sure we keep write
1684 * for those levels as well
1685 */
1686 write_lock_level = 2;
1687 } else if (ins_len > 0) {
1688 /*
1689 * for inserting items, make sure we have a write lock on
1690 * level 1 so we can update keys
1691 */
1692 write_lock_level = 1;
1693 }
1694
1695 if (!cow)
1696 write_lock_level = -1;
1697
1698 if (cow && (p->keep_locks || p->lowest_level))
1699 write_lock_level = BTRFS_MAX_LEVEL;
1700
Chris Masonf7c79f32012-03-19 15:54:38 -04001701 min_write_lock_level = write_lock_level;
1702
Chris Masonbb803952007-03-01 12:04:21 -05001703again:
Chris Masonbd681512011-07-16 15:23:14 -04001704 /*
1705 * we try very hard to do read locks on the root
1706 */
1707 root_lock = BTRFS_READ_LOCK;
1708 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001709 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001710 /*
1711 * the commit roots are read only
1712 * so we always do read locks
1713 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001714 b = root->commit_root;
1715 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001716 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001718 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001720 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001722 level = btrfs_header_level(b);
1723 } else {
1724 /* we don't know the level of the root node
1725 * until we actually have it read locked
1726 */
1727 b = btrfs_read_lock_root_node(root);
1728 level = btrfs_header_level(b);
1729 if (level <= write_lock_level) {
1730 /* whoops, must trade for write lock */
1731 btrfs_tree_read_unlock(b);
1732 free_extent_buffer(b);
1733 b = btrfs_lock_root_node(root);
1734 root_lock = BTRFS_WRITE_LOCK;
1735
1736 /* the level might have changed, check again */
1737 level = btrfs_header_level(b);
1738 }
1739 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001740 }
Chris Masonbd681512011-07-16 15:23:14 -04001741 p->nodes[level] = b;
1742 if (!p->skip_locking)
1743 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001744
Chris Masoneb60cea2007-02-02 09:18:22 -05001745 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001746 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001747
1748 /*
1749 * setup the path here so we can release it under lock
1750 * contention with the cow code
1751 */
Chris Mason02217ed2007-03-02 16:08:05 -05001752 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001753 /*
1754 * if we don't really need to cow this block
1755 * then we don't want to set the path blocking,
1756 * so we test it here
1757 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001758 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001759 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001760
Chris Masonb4ce94d2009-02-04 09:25:08 -05001761 btrfs_set_path_blocking(p);
1762
Chris Masonbd681512011-07-16 15:23:14 -04001763 /*
1764 * must have write locks on this node and the
1765 * parent
1766 */
1767 if (level + 1 > write_lock_level) {
1768 write_lock_level = level + 1;
1769 btrfs_release_path(p);
1770 goto again;
1771 }
1772
Yan Zheng33c66f42009-07-22 09:59:00 -04001773 err = btrfs_cow_block(trans, root, b,
1774 p->nodes[level + 1],
1775 p->slots[level + 1], &b);
1776 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001777 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001778 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001779 }
Chris Mason02217ed2007-03-02 16:08:05 -05001780 }
Chris Mason65b51a02008-08-01 15:11:20 -04001781cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001782 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001783
Chris Masoneb60cea2007-02-02 09:18:22 -05001784 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001785 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001786
1787 /*
1788 * we have a lock on b and as long as we aren't changing
1789 * the tree, there is no way to for the items in b to change.
1790 * It is safe to drop the lock on our parent before we
1791 * go through the expensive btree search on b.
1792 *
1793 * If cow is true, then we might be changing slot zero,
1794 * which may require changing the parent. So, we can't
1795 * drop the lock until after we know which slot we're
1796 * operating on.
1797 */
1798 if (!cow)
1799 btrfs_unlock_up_safe(p, level + 1);
1800
Chris Mason5f39d392007-10-15 16:14:19 -04001801 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001802
Chris Mason5f39d392007-10-15 16:14:19 -04001803 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001804 int dec = 0;
1805 if (ret && slot > 0) {
1806 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001807 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001809 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001810 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001811 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001813 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 if (err) {
1815 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001816 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001817 }
Chris Masonc8c42862009-04-03 10:14:18 -04001818 b = p->nodes[level];
1819 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820
Chris Masonbd681512011-07-16 15:23:14 -04001821 /*
1822 * slot 0 is special, if we change the key
1823 * we have to update the parent pointer
1824 * which means we must have a write lock
1825 * on the parent
1826 */
1827 if (slot == 0 && cow &&
1828 write_lock_level < level + 1) {
1829 write_lock_level = level + 1;
1830 btrfs_release_path(p);
1831 goto again;
1832 }
1833
Chris Masonf7c79f32012-03-19 15:54:38 -04001834 unlock_up(p, level, lowest_unlock,
1835 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001836
Chris Mason925baed2008-06-25 16:01:30 -04001837 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001838 if (dec)
1839 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001840 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001841 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001842
Yan Zheng33c66f42009-07-22 09:59:00 -04001843 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001844 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001845 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001846 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001847 if (err) {
1848 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001849 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001850 }
Chris Mason76a05b32009-05-14 13:24:30 -04001851
Chris Masonb4ce94d2009-02-04 09:25:08 -05001852 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001853 level = btrfs_header_level(b);
1854 if (level <= write_lock_level) {
1855 err = btrfs_try_tree_write_lock(b);
1856 if (!err) {
1857 btrfs_set_path_blocking(p);
1858 btrfs_tree_lock(b);
1859 btrfs_clear_path_blocking(p, b,
1860 BTRFS_WRITE_LOCK);
1861 }
1862 p->locks[level] = BTRFS_WRITE_LOCK;
1863 } else {
1864 err = btrfs_try_tree_read_lock(b);
1865 if (!err) {
1866 btrfs_set_path_blocking(p);
1867 btrfs_tree_read_lock(b);
1868 btrfs_clear_path_blocking(p, b,
1869 BTRFS_READ_LOCK);
1870 }
1871 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001872 }
Chris Masonbd681512011-07-16 15:23:14 -04001873 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001874 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001875 } else {
1876 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001877 if (ins_len > 0 &&
1878 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001879 if (write_lock_level < 1) {
1880 write_lock_level = 1;
1881 btrfs_release_path(p);
1882 goto again;
1883 }
1884
Chris Masonb4ce94d2009-02-04 09:25:08 -05001885 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001886 err = split_leaf(trans, root, key,
1887 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001888 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001889
Yan Zheng33c66f42009-07-22 09:59:00 -04001890 BUG_ON(err > 0);
1891 if (err) {
1892 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001893 goto done;
1894 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001895 }
Chris Mason459931e2008-12-10 09:10:46 -05001896 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04001897 unlock_up(p, level, lowest_unlock,
1898 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04001899 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001900 }
1901 }
Chris Mason65b51a02008-08-01 15:11:20 -04001902 ret = 1;
1903done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001904 /*
1905 * we don't really know what they plan on doing with the path
1906 * from here on, so for now just mark it as blocking
1907 */
Chris Masonb9473432009-03-13 11:00:37 -04001908 if (!p->leave_spinning)
1909 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001910 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001911 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001912 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001913}
1914
Chris Mason74123bd2007-02-02 11:05:29 -05001915/*
1916 * adjust the pointers going up the tree, starting at level
1917 * making sure the right key of each node is points to 'key'.
1918 * This is used after shifting pointers to the left, so it stops
1919 * fixing up pointers when a given leaf/node is not in slot 0 of the
1920 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001921 *
Chris Mason74123bd2007-02-02 11:05:29 -05001922 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001923static void fixup_low_keys(struct btrfs_trans_handle *trans,
1924 struct btrfs_root *root, struct btrfs_path *path,
1925 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001926{
1927 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001928 struct extent_buffer *t;
1929
Chris Mason234b63a2007-03-13 10:46:10 -04001930 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001931 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001932 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001933 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001934 t = path->nodes[i];
1935 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001936 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001937 if (tslot != 0)
1938 break;
1939 }
1940}
1941
Chris Mason74123bd2007-02-02 11:05:29 -05001942/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001943 * update item key.
1944 *
1945 * This function isn't completely safe. It's the caller's responsibility
1946 * that the new key won't break the order
1947 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001948void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1949 struct btrfs_root *root, struct btrfs_path *path,
1950 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001951{
1952 struct btrfs_disk_key disk_key;
1953 struct extent_buffer *eb;
1954 int slot;
1955
1956 eb = path->nodes[0];
1957 slot = path->slots[0];
1958 if (slot > 0) {
1959 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001960 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001961 }
1962 if (slot < btrfs_header_nritems(eb) - 1) {
1963 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001964 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001965 }
1966
1967 btrfs_cpu_key_to_disk(&disk_key, new_key);
1968 btrfs_set_item_key(eb, &disk_key, slot);
1969 btrfs_mark_buffer_dirty(eb);
1970 if (slot == 0)
1971 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001972}
1973
1974/*
Chris Mason74123bd2007-02-02 11:05:29 -05001975 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001976 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001977 *
1978 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1979 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001980 */
Chris Mason98ed5172008-01-03 10:01:48 -05001981static int push_node_left(struct btrfs_trans_handle *trans,
1982 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001983 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001984{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001985 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001986 int src_nritems;
1987 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001988 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001989
Chris Mason5f39d392007-10-15 16:14:19 -04001990 src_nritems = btrfs_header_nritems(src);
1991 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001992 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001993 WARN_ON(btrfs_header_generation(src) != trans->transid);
1994 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001995
Chris Masonbce4eae2008-04-24 14:42:46 -04001996 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001997 return 1;
1998
Chris Masond3977122009-01-05 21:25:51 -05001999 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002000 return 1;
2001
Chris Masonbce4eae2008-04-24 14:42:46 -04002002 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002003 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002004 if (push_items < src_nritems) {
2005 /* leave at least 8 pointers in the node if
2006 * we aren't going to empty it
2007 */
2008 if (src_nritems - push_items < 8) {
2009 if (push_items <= 8)
2010 return 1;
2011 push_items -= 8;
2012 }
2013 }
2014 } else
2015 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002016
Chris Mason5f39d392007-10-15 16:14:19 -04002017 copy_extent_buffer(dst, src,
2018 btrfs_node_key_ptr_offset(dst_nritems),
2019 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002020 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002021
Chris Masonbb803952007-03-01 12:04:21 -05002022 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002023 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2024 btrfs_node_key_ptr_offset(push_items),
2025 (src_nritems - push_items) *
2026 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002027 }
Chris Mason5f39d392007-10-15 16:14:19 -04002028 btrfs_set_header_nritems(src, src_nritems - push_items);
2029 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2030 btrfs_mark_buffer_dirty(src);
2031 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002032
Chris Masonbb803952007-03-01 12:04:21 -05002033 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002034}
2035
Chris Mason97571fd2007-02-24 13:39:08 -05002036/*
Chris Mason79f95c82007-03-01 15:16:26 -05002037 * try to push data from one node into the next node right in the
2038 * tree.
2039 *
2040 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2041 * error, and > 0 if there was no room in the right hand block.
2042 *
2043 * this will only push up to 1/2 the contents of the left node over
2044 */
Chris Mason5f39d392007-10-15 16:14:19 -04002045static int balance_node_right(struct btrfs_trans_handle *trans,
2046 struct btrfs_root *root,
2047 struct extent_buffer *dst,
2048 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002049{
Chris Mason79f95c82007-03-01 15:16:26 -05002050 int push_items = 0;
2051 int max_push;
2052 int src_nritems;
2053 int dst_nritems;
2054 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002055
Chris Mason7bb86312007-12-11 09:25:06 -05002056 WARN_ON(btrfs_header_generation(src) != trans->transid);
2057 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2058
Chris Mason5f39d392007-10-15 16:14:19 -04002059 src_nritems = btrfs_header_nritems(src);
2060 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002061 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002062 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002063 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002064
Chris Masond3977122009-01-05 21:25:51 -05002065 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002066 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002067
2068 max_push = src_nritems / 2 + 1;
2069 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002070 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002071 return 1;
Yan252c38f2007-08-29 09:11:44 -04002072
Chris Mason79f95c82007-03-01 15:16:26 -05002073 if (max_push < push_items)
2074 push_items = max_push;
2075
Chris Mason5f39d392007-10-15 16:14:19 -04002076 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2077 btrfs_node_key_ptr_offset(0),
2078 (dst_nritems) *
2079 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002080
Chris Mason5f39d392007-10-15 16:14:19 -04002081 copy_extent_buffer(dst, src,
2082 btrfs_node_key_ptr_offset(0),
2083 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002084 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002085
Chris Mason5f39d392007-10-15 16:14:19 -04002086 btrfs_set_header_nritems(src, src_nritems - push_items);
2087 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002088
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_mark_buffer_dirty(src);
2090 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002091
Chris Mason79f95c82007-03-01 15:16:26 -05002092 return ret;
2093}
2094
2095/*
Chris Mason97571fd2007-02-24 13:39:08 -05002096 * helper function to insert a new root level in the tree.
2097 * A new node is allocated, and a single item is inserted to
2098 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002099 *
2100 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002101 */
Chris Masond3977122009-01-05 21:25:51 -05002102static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002103 struct btrfs_root *root,
2104 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002105{
Chris Mason7bb86312007-12-11 09:25:06 -05002106 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002107 struct extent_buffer *lower;
2108 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002109 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002110 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002111
2112 BUG_ON(path->nodes[level]);
2113 BUG_ON(path->nodes[level-1] != root->node);
2114
Chris Mason7bb86312007-12-11 09:25:06 -05002115 lower = path->nodes[level-1];
2116 if (level == 1)
2117 btrfs_item_key(lower, &lower_key, 0);
2118 else
2119 btrfs_node_key(lower, &lower_key, 0);
2120
Zheng Yan31840ae2008-09-23 13:14:14 -04002121 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002122 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002123 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002124 if (IS_ERR(c))
2125 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002126
Yan, Zhengf0486c62010-05-16 10:46:25 -04002127 root_add_used(root, root->nodesize);
2128
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002129 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_set_header_nritems(c, 1);
2131 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002132 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002133 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002135 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002136
Chris Mason5f39d392007-10-15 16:14:19 -04002137 write_extent_buffer(c, root->fs_info->fsid,
2138 (unsigned long)btrfs_header_fsid(c),
2139 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002140
2141 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2142 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2143 BTRFS_UUID_SIZE);
2144
Chris Mason5f39d392007-10-15 16:14:19 -04002145 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002146 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002147 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002148 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002149
2150 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002151
2152 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002153
Chris Mason925baed2008-06-25 16:01:30 -04002154 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002155 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002156
2157 /* the super has an extra ref to root->node */
2158 free_extent_buffer(old);
2159
Chris Mason0b86a832008-03-24 15:01:56 -04002160 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 extent_buffer_get(c);
2162 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002163 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002164 path->slots[level] = 0;
2165 return 0;
2166}
2167
Chris Mason74123bd2007-02-02 11:05:29 -05002168/*
2169 * worker function to insert a single pointer in a node.
2170 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002171 *
Chris Mason74123bd2007-02-02 11:05:29 -05002172 * slot and level indicate where you want the key to go, and
2173 * blocknr is the block the key points to.
2174 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002175static void insert_ptr(struct btrfs_trans_handle *trans,
2176 struct btrfs_root *root, struct btrfs_path *path,
2177 struct btrfs_disk_key *key, u64 bytenr,
2178 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002179{
Chris Mason5f39d392007-10-15 16:14:19 -04002180 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002181 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002182
2183 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002184 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 lower = path->nodes[level];
2186 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002187 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002188 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002189 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002190 memmove_extent_buffer(lower,
2191 btrfs_node_key_ptr_offset(slot + 1),
2192 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002193 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002194 }
Chris Mason5f39d392007-10-15 16:14:19 -04002195 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002196 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002197 WARN_ON(trans->transid == 0);
2198 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002199 btrfs_set_header_nritems(lower, nritems + 1);
2200 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002201}
2202
Chris Mason97571fd2007-02-24 13:39:08 -05002203/*
2204 * split the node at the specified level in path in two.
2205 * The path is corrected to point to the appropriate node after the split
2206 *
2207 * Before splitting this tries to make some room in the node by pushing
2208 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002209 *
2210 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002211 */
Chris Masone02119d2008-09-05 16:13:11 -04002212static noinline int split_node(struct btrfs_trans_handle *trans,
2213 struct btrfs_root *root,
2214 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002215{
Chris Mason5f39d392007-10-15 16:14:19 -04002216 struct extent_buffer *c;
2217 struct extent_buffer *split;
2218 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002219 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002220 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002221 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002222
Chris Mason5f39d392007-10-15 16:14:19 -04002223 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002224 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002225 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002226 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002227 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002228 if (ret)
2229 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002230 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002231 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002232 c = path->nodes[level];
2233 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002234 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002235 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002236 if (ret < 0)
2237 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002238 }
Chris Masone66f7092007-04-20 13:16:02 -04002239
Chris Mason5f39d392007-10-15 16:14:19 -04002240 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 mid = (c_nritems + 1) / 2;
2242 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002243
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002244 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002245 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002246 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002247 if (IS_ERR(split))
2248 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002249
Yan, Zhengf0486c62010-05-16 10:46:25 -04002250 root_add_used(root, root->nodesize);
2251
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002252 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002253 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002254 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002255 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002257 btrfs_set_header_owner(split, root->root_key.objectid);
2258 write_extent_buffer(split, root->fs_info->fsid,
2259 (unsigned long)btrfs_header_fsid(split),
2260 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002261 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2262 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2263 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002264
Chris Mason5f39d392007-10-15 16:14:19 -04002265
2266 copy_extent_buffer(split, c,
2267 btrfs_node_key_ptr_offset(0),
2268 btrfs_node_key_ptr_offset(mid),
2269 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2270 btrfs_set_header_nritems(split, c_nritems - mid);
2271 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002272 ret = 0;
2273
Chris Mason5f39d392007-10-15 16:14:19 -04002274 btrfs_mark_buffer_dirty(c);
2275 btrfs_mark_buffer_dirty(split);
2276
Jeff Mahoney143bede2012-03-01 14:56:26 +01002277 insert_ptr(trans, root, path, &disk_key, split->start,
2278 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002279
Chris Mason5de08d72007-02-24 06:24:44 -05002280 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002281 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002282 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002283 free_extent_buffer(c);
2284 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002285 path->slots[level + 1] += 1;
2286 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002287 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002288 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002289 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002290 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002291}
2292
Chris Mason74123bd2007-02-02 11:05:29 -05002293/*
2294 * how many bytes are required to store the items in a leaf. start
2295 * and nr indicate which items in the leaf to check. This totals up the
2296 * space used both by the item structs and the item data
2297 */
Chris Mason5f39d392007-10-15 16:14:19 -04002298static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002299{
2300 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002301 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002302 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002303
2304 if (!nr)
2305 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002306 data_len = btrfs_item_end_nr(l, start);
2307 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002308 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002309 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002310 return data_len;
2311}
2312
Chris Mason74123bd2007-02-02 11:05:29 -05002313/*
Chris Masond4dbff92007-04-04 14:08:15 -04002314 * The space between the end of the leaf items and
2315 * the start of the leaf data. IOW, how much room
2316 * the leaf has left for both items and data
2317 */
Chris Masond3977122009-01-05 21:25:51 -05002318noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002319 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002320{
Chris Mason5f39d392007-10-15 16:14:19 -04002321 int nritems = btrfs_header_nritems(leaf);
2322 int ret;
2323 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2324 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002325 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2326 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002327 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002328 leaf_space_used(leaf, 0, nritems), nritems);
2329 }
2330 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002331}
2332
Chris Mason99d8f832010-07-07 10:51:48 -04002333/*
2334 * min slot controls the lowest index we're willing to push to the
2335 * right. We'll push up to and including min_slot, but no lower
2336 */
Chris Mason44871b12009-03-13 10:04:31 -04002337static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2338 struct btrfs_root *root,
2339 struct btrfs_path *path,
2340 int data_size, int empty,
2341 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002342 int free_space, u32 left_nritems,
2343 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002344{
Chris Mason5f39d392007-10-15 16:14:19 -04002345 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002346 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002347 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002348 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002349 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002350 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002351 int push_space = 0;
2352 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002353 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002354 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002355 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002356 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002357 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002358
Chris Masoncfed81a2012-03-03 07:40:03 -05002359 btrfs_init_map_token(&token);
2360
Chris Mason34a38212007-11-07 13:31:03 -05002361 if (empty)
2362 nr = 0;
2363 else
Chris Mason99d8f832010-07-07 10:51:48 -04002364 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002365
Zheng Yan31840ae2008-09-23 13:14:14 -04002366 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002367 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002368
Chris Mason44871b12009-03-13 10:04:31 -04002369 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002370 i = left_nritems - 1;
2371 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002372 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002373
Zheng Yan31840ae2008-09-23 13:14:14 -04002374 if (!empty && push_items > 0) {
2375 if (path->slots[0] > i)
2376 break;
2377 if (path->slots[0] == i) {
2378 int space = btrfs_leaf_free_space(root, left);
2379 if (space + push_space * 2 > free_space)
2380 break;
2381 }
2382 }
2383
Chris Mason00ec4c52007-02-24 12:47:20 -05002384 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002385 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002386
Chris Masondb945352007-10-15 16:15:53 -04002387 this_item_size = btrfs_item_size(left, item);
2388 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002389 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002390
Chris Mason00ec4c52007-02-24 12:47:20 -05002391 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002392 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002393 if (i == 0)
2394 break;
2395 i--;
Chris Masondb945352007-10-15 16:15:53 -04002396 }
Chris Mason5f39d392007-10-15 16:14:19 -04002397
Chris Mason925baed2008-06-25 16:01:30 -04002398 if (push_items == 0)
2399 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002400
Chris Mason34a38212007-11-07 13:31:03 -05002401 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002402 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002403
Chris Mason00ec4c52007-02-24 12:47:20 -05002404 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002405 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002406
Chris Mason5f39d392007-10-15 16:14:19 -04002407 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002408 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002409
Chris Mason00ec4c52007-02-24 12:47:20 -05002410 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002411 data_end = leaf_data_end(root, right);
2412 memmove_extent_buffer(right,
2413 btrfs_leaf_data(right) + data_end - push_space,
2414 btrfs_leaf_data(right) + data_end,
2415 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2416
Chris Mason00ec4c52007-02-24 12:47:20 -05002417 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002418 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002419 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2420 btrfs_leaf_data(left) + leaf_data_end(root, left),
2421 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002422
2423 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2424 btrfs_item_nr_offset(0),
2425 right_nritems * sizeof(struct btrfs_item));
2426
Chris Mason00ec4c52007-02-24 12:47:20 -05002427 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002428 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2429 btrfs_item_nr_offset(left_nritems - push_items),
2430 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002431
2432 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002433 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002434 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002435 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002436 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002437 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002438 push_space -= btrfs_token_item_size(right, item, &token);
2439 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002440 }
2441
Chris Mason7518a232007-03-12 12:01:18 -04002442 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002443 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002444
Chris Mason34a38212007-11-07 13:31:03 -05002445 if (left_nritems)
2446 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002447 else
2448 clean_tree_block(trans, root, left);
2449
Chris Mason5f39d392007-10-15 16:14:19 -04002450 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002451
Chris Mason5f39d392007-10-15 16:14:19 -04002452 btrfs_item_key(right, &disk_key, 0);
2453 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002454 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002455
Chris Mason00ec4c52007-02-24 12:47:20 -05002456 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002457 if (path->slots[0] >= left_nritems) {
2458 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002459 if (btrfs_header_nritems(path->nodes[0]) == 0)
2460 clean_tree_block(trans, root, path->nodes[0]);
2461 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002462 free_extent_buffer(path->nodes[0]);
2463 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002464 path->slots[1] += 1;
2465 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002466 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002467 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002468 }
2469 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002470
2471out_unlock:
2472 btrfs_tree_unlock(right);
2473 free_extent_buffer(right);
2474 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002475}
Chris Mason925baed2008-06-25 16:01:30 -04002476
Chris Mason00ec4c52007-02-24 12:47:20 -05002477/*
Chris Mason44871b12009-03-13 10:04:31 -04002478 * push some data in the path leaf to the right, trying to free up at
2479 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2480 *
2481 * returns 1 if the push failed because the other node didn't have enough
2482 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002483 *
2484 * this will push starting from min_slot to the end of the leaf. It won't
2485 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002486 */
2487static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002488 *root, struct btrfs_path *path,
2489 int min_data_size, int data_size,
2490 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002491{
2492 struct extent_buffer *left = path->nodes[0];
2493 struct extent_buffer *right;
2494 struct extent_buffer *upper;
2495 int slot;
2496 int free_space;
2497 u32 left_nritems;
2498 int ret;
2499
2500 if (!path->nodes[1])
2501 return 1;
2502
2503 slot = path->slots[1];
2504 upper = path->nodes[1];
2505 if (slot >= btrfs_header_nritems(upper) - 1)
2506 return 1;
2507
2508 btrfs_assert_tree_locked(path->nodes[1]);
2509
2510 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002511 if (right == NULL)
2512 return 1;
2513
Chris Mason44871b12009-03-13 10:04:31 -04002514 btrfs_tree_lock(right);
2515 btrfs_set_lock_blocking(right);
2516
2517 free_space = btrfs_leaf_free_space(root, right);
2518 if (free_space < data_size)
2519 goto out_unlock;
2520
2521 /* cow and double check */
2522 ret = btrfs_cow_block(trans, root, right, upper,
2523 slot + 1, &right);
2524 if (ret)
2525 goto out_unlock;
2526
2527 free_space = btrfs_leaf_free_space(root, right);
2528 if (free_space < data_size)
2529 goto out_unlock;
2530
2531 left_nritems = btrfs_header_nritems(left);
2532 if (left_nritems == 0)
2533 goto out_unlock;
2534
Chris Mason99d8f832010-07-07 10:51:48 -04002535 return __push_leaf_right(trans, root, path, min_data_size, empty,
2536 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002537out_unlock:
2538 btrfs_tree_unlock(right);
2539 free_extent_buffer(right);
2540 return 1;
2541}
2542
2543/*
Chris Mason74123bd2007-02-02 11:05:29 -05002544 * push some data in the path leaf to the left, trying to free up at
2545 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002546 *
2547 * max_slot can put a limit on how far into the leaf we'll push items. The
2548 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2549 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002550 */
Chris Mason44871b12009-03-13 10:04:31 -04002551static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2552 struct btrfs_root *root,
2553 struct btrfs_path *path, int data_size,
2554 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002555 int free_space, u32 right_nritems,
2556 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557{
Chris Mason5f39d392007-10-15 16:14:19 -04002558 struct btrfs_disk_key disk_key;
2559 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 int push_space = 0;
2562 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002563 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002564 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002565 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002566 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002567 u32 this_item_size;
2568 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002569 struct btrfs_map_token token;
2570
2571 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002572
Chris Mason34a38212007-11-07 13:31:03 -05002573 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002574 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002575 else
Chris Mason99d8f832010-07-07 10:51:48 -04002576 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002577
2578 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002579 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002580
Zheng Yan31840ae2008-09-23 13:14:14 -04002581 if (!empty && push_items > 0) {
2582 if (path->slots[0] < i)
2583 break;
2584 if (path->slots[0] == i) {
2585 int space = btrfs_leaf_free_space(root, right);
2586 if (space + push_space * 2 > free_space)
2587 break;
2588 }
2589 }
2590
Chris Masonbe0e5c02007-01-26 15:51:26 -05002591 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002592 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002593
2594 this_item_size = btrfs_item_size(right, item);
2595 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596 break;
Chris Masondb945352007-10-15 16:15:53 -04002597
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002599 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 }
Chris Masondb945352007-10-15 16:15:53 -04002601
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002603 ret = 1;
2604 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002605 }
Chris Mason34a38212007-11-07 13:31:03 -05002606 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002607 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002608
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002610 copy_extent_buffer(left, right,
2611 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2612 btrfs_item_nr_offset(0),
2613 push_items * sizeof(struct btrfs_item));
2614
Chris Mason123abc82007-03-14 14:14:43 -04002615 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002616 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002617
2618 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002619 leaf_data_end(root, left) - push_space,
2620 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002621 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002622 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002623 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002624 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002625
Chris Masondb945352007-10-15 16:15:53 -04002626 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002627 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002628 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002629
Chris Mason5f39d392007-10-15 16:14:19 -04002630 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002631
Chris Masoncfed81a2012-03-03 07:40:03 -05002632 ioff = btrfs_token_item_offset(left, item, &token);
2633 btrfs_set_token_item_offset(left, item,
2634 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2635 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002636 }
Chris Mason5f39d392007-10-15 16:14:19 -04002637 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002638
2639 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002640 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002641 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2642 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002643 WARN_ON(1);
2644 }
Chris Mason5f39d392007-10-15 16:14:19 -04002645
Chris Mason34a38212007-11-07 13:31:03 -05002646 if (push_items < right_nritems) {
2647 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2648 leaf_data_end(root, right);
2649 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2650 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2651 btrfs_leaf_data(right) +
2652 leaf_data_end(root, right), push_space);
2653
2654 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002655 btrfs_item_nr_offset(push_items),
2656 (btrfs_header_nritems(right) - push_items) *
2657 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002658 }
Yaneef1c492007-11-26 10:58:13 -05002659 right_nritems -= push_items;
2660 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002661 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002662 for (i = 0; i < right_nritems; i++) {
2663 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002664
Chris Masoncfed81a2012-03-03 07:40:03 -05002665 push_space = push_space - btrfs_token_item_size(right,
2666 item, &token);
2667 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002668 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002669
Chris Mason5f39d392007-10-15 16:14:19 -04002670 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002671 if (right_nritems)
2672 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002673 else
2674 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002675
Chris Mason5f39d392007-10-15 16:14:19 -04002676 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002677 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002678
2679 /* then fixup the leaf pointer in the path */
2680 if (path->slots[0] < push_items) {
2681 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002682 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002683 free_extent_buffer(path->nodes[0]);
2684 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002685 path->slots[1] -= 1;
2686 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002687 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002688 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689 path->slots[0] -= push_items;
2690 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002691 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002692 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002693out:
2694 btrfs_tree_unlock(left);
2695 free_extent_buffer(left);
2696 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002697}
2698
Chris Mason74123bd2007-02-02 11:05:29 -05002699/*
Chris Mason44871b12009-03-13 10:04:31 -04002700 * push some data in the path leaf to the left, trying to free up at
2701 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002702 *
2703 * max_slot can put a limit on how far into the leaf we'll push items. The
2704 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2705 * items
Chris Mason44871b12009-03-13 10:04:31 -04002706 */
2707static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002708 *root, struct btrfs_path *path, int min_data_size,
2709 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002710{
2711 struct extent_buffer *right = path->nodes[0];
2712 struct extent_buffer *left;
2713 int slot;
2714 int free_space;
2715 u32 right_nritems;
2716 int ret = 0;
2717
2718 slot = path->slots[1];
2719 if (slot == 0)
2720 return 1;
2721 if (!path->nodes[1])
2722 return 1;
2723
2724 right_nritems = btrfs_header_nritems(right);
2725 if (right_nritems == 0)
2726 return 1;
2727
2728 btrfs_assert_tree_locked(path->nodes[1]);
2729
2730 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002731 if (left == NULL)
2732 return 1;
2733
Chris Mason44871b12009-03-13 10:04:31 -04002734 btrfs_tree_lock(left);
2735 btrfs_set_lock_blocking(left);
2736
2737 free_space = btrfs_leaf_free_space(root, left);
2738 if (free_space < data_size) {
2739 ret = 1;
2740 goto out;
2741 }
2742
2743 /* cow and double check */
2744 ret = btrfs_cow_block(trans, root, left,
2745 path->nodes[1], slot - 1, &left);
2746 if (ret) {
2747 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002748 if (ret == -ENOSPC)
2749 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002750 goto out;
2751 }
2752
2753 free_space = btrfs_leaf_free_space(root, left);
2754 if (free_space < data_size) {
2755 ret = 1;
2756 goto out;
2757 }
2758
Chris Mason99d8f832010-07-07 10:51:48 -04002759 return __push_leaf_left(trans, root, path, min_data_size,
2760 empty, left, free_space, right_nritems,
2761 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002762out:
2763 btrfs_tree_unlock(left);
2764 free_extent_buffer(left);
2765 return ret;
2766}
2767
2768/*
Chris Mason74123bd2007-02-02 11:05:29 -05002769 * split the path's leaf in two, making sure there is at least data_size
2770 * available for the resulting leaf level of the path.
2771 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002772static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2773 struct btrfs_root *root,
2774 struct btrfs_path *path,
2775 struct extent_buffer *l,
2776 struct extent_buffer *right,
2777 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002778{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002779 int data_copy_size;
2780 int rt_data_off;
2781 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002782 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05002783 struct btrfs_map_token token;
2784
2785 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002786
Chris Mason5f39d392007-10-15 16:14:19 -04002787 nritems = nritems - mid;
2788 btrfs_set_header_nritems(right, nritems);
2789 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2790
2791 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2792 btrfs_item_nr_offset(mid),
2793 nritems * sizeof(struct btrfs_item));
2794
2795 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002796 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2797 data_copy_size, btrfs_leaf_data(l) +
2798 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002799
Chris Mason5f39d392007-10-15 16:14:19 -04002800 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2801 btrfs_item_end_nr(l, mid);
2802
2803 for (i = 0; i < nritems; i++) {
2804 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002805 u32 ioff;
2806
Chris Masoncfed81a2012-03-03 07:40:03 -05002807 ioff = btrfs_token_item_offset(right, item, &token);
2808 btrfs_set_token_item_offset(right, item,
2809 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002810 }
Chris Mason74123bd2007-02-02 11:05:29 -05002811
Chris Mason5f39d392007-10-15 16:14:19 -04002812 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002813 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002814 insert_ptr(trans, root, path, &disk_key, right->start,
2815 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002816
2817 btrfs_mark_buffer_dirty(right);
2818 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002819 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002820
Chris Masonbe0e5c02007-01-26 15:51:26 -05002821 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002822 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 free_extent_buffer(path->nodes[0]);
2824 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002825 path->slots[0] -= mid;
2826 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002827 } else {
2828 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002829 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002830 }
Chris Mason5f39d392007-10-15 16:14:19 -04002831
Chris Masoneb60cea2007-02-02 09:18:22 -05002832 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002833}
2834
2835/*
Chris Mason99d8f832010-07-07 10:51:48 -04002836 * double splits happen when we need to insert a big item in the middle
2837 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2838 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2839 * A B C
2840 *
2841 * We avoid this by trying to push the items on either side of our target
2842 * into the adjacent leaves. If all goes well we can avoid the double split
2843 * completely.
2844 */
2845static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2846 struct btrfs_root *root,
2847 struct btrfs_path *path,
2848 int data_size)
2849{
2850 int ret;
2851 int progress = 0;
2852 int slot;
2853 u32 nritems;
2854
2855 slot = path->slots[0];
2856
2857 /*
2858 * try to push all the items after our slot into the
2859 * right leaf
2860 */
2861 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2862 if (ret < 0)
2863 return ret;
2864
2865 if (ret == 0)
2866 progress++;
2867
2868 nritems = btrfs_header_nritems(path->nodes[0]);
2869 /*
2870 * our goal is to get our slot at the start or end of a leaf. If
2871 * we've done so we're done
2872 */
2873 if (path->slots[0] == 0 || path->slots[0] == nritems)
2874 return 0;
2875
2876 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2877 return 0;
2878
2879 /* try to push all the items before our slot into the next leaf */
2880 slot = path->slots[0];
2881 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2882 if (ret < 0)
2883 return ret;
2884
2885 if (ret == 0)
2886 progress++;
2887
2888 if (progress)
2889 return 0;
2890 return 1;
2891}
2892
2893/*
Chris Mason44871b12009-03-13 10:04:31 -04002894 * split the path's leaf in two, making sure there is at least data_size
2895 * available for the resulting leaf level of the path.
2896 *
2897 * returns 0 if all went well and < 0 on failure.
2898 */
2899static noinline int split_leaf(struct btrfs_trans_handle *trans,
2900 struct btrfs_root *root,
2901 struct btrfs_key *ins_key,
2902 struct btrfs_path *path, int data_size,
2903 int extend)
2904{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002906 struct extent_buffer *l;
2907 u32 nritems;
2908 int mid;
2909 int slot;
2910 struct extent_buffer *right;
2911 int ret = 0;
2912 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002913 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002914 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002915 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002916
Yan, Zhenga5719522009-09-24 09:17:31 -04002917 l = path->nodes[0];
2918 slot = path->slots[0];
2919 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2920 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2921 return -EOVERFLOW;
2922
Chris Mason44871b12009-03-13 10:04:31 -04002923 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002924 if (data_size) {
2925 wret = push_leaf_right(trans, root, path, data_size,
2926 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002927 if (wret < 0)
2928 return wret;
2929 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002930 wret = push_leaf_left(trans, root, path, data_size,
2931 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002932 if (wret < 0)
2933 return wret;
2934 }
2935 l = path->nodes[0];
2936
2937 /* did the pushes work? */
2938 if (btrfs_leaf_free_space(root, l) >= data_size)
2939 return 0;
2940 }
2941
2942 if (!path->nodes[1]) {
2943 ret = insert_new_root(trans, root, path, 1);
2944 if (ret)
2945 return ret;
2946 }
2947again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002948 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002949 l = path->nodes[0];
2950 slot = path->slots[0];
2951 nritems = btrfs_header_nritems(l);
2952 mid = (nritems + 1) / 2;
2953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 if (mid <= slot) {
2955 if (nritems == 1 ||
2956 leaf_space_used(l, mid, nritems - mid) + data_size >
2957 BTRFS_LEAF_DATA_SIZE(root)) {
2958 if (slot >= nritems) {
2959 split = 0;
2960 } else {
2961 mid = slot;
2962 if (mid != nritems &&
2963 leaf_space_used(l, mid, nritems - mid) +
2964 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002965 if (data_size && !tried_avoid_double)
2966 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002967 split = 2;
2968 }
2969 }
2970 }
2971 } else {
2972 if (leaf_space_used(l, 0, mid) + data_size >
2973 BTRFS_LEAF_DATA_SIZE(root)) {
2974 if (!extend && data_size && slot == 0) {
2975 split = 0;
2976 } else if ((extend || !data_size) && slot == 0) {
2977 mid = 1;
2978 } else {
2979 mid = slot;
2980 if (mid != nritems &&
2981 leaf_space_used(l, mid, nritems - mid) +
2982 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002983 if (data_size && !tried_avoid_double)
2984 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 split = 2 ;
2986 }
2987 }
2988 }
2989 }
2990
2991 if (split == 0)
2992 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2993 else
2994 btrfs_item_key(l, &disk_key, mid);
2995
2996 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002997 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002998 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002999 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003000 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003001
3002 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003003
3004 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3005 btrfs_set_header_bytenr(right, right->start);
3006 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003007 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003008 btrfs_set_header_owner(right, root->root_key.objectid);
3009 btrfs_set_header_level(right, 0);
3010 write_extent_buffer(right, root->fs_info->fsid,
3011 (unsigned long)btrfs_header_fsid(right),
3012 BTRFS_FSID_SIZE);
3013
3014 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3015 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3016 BTRFS_UUID_SIZE);
3017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 if (split == 0) {
3019 if (mid <= slot) {
3020 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003021 insert_ptr(trans, root, path, &disk_key, right->start,
3022 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003023 btrfs_tree_unlock(path->nodes[0]);
3024 free_extent_buffer(path->nodes[0]);
3025 path->nodes[0] = right;
3026 path->slots[0] = 0;
3027 path->slots[1] += 1;
3028 } else {
3029 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003030 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003031 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003032 btrfs_tree_unlock(path->nodes[0]);
3033 free_extent_buffer(path->nodes[0]);
3034 path->nodes[0] = right;
3035 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003036 if (path->slots[1] == 0)
3037 fixup_low_keys(trans, root, path,
3038 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003039 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003040 btrfs_mark_buffer_dirty(right);
3041 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003042 }
3043
Jeff Mahoney143bede2012-03-01 14:56:26 +01003044 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003045
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003046 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003047 BUG_ON(num_doubles != 0);
3048 num_doubles++;
3049 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003050 }
Chris Mason44871b12009-03-13 10:04:31 -04003051
Jeff Mahoney143bede2012-03-01 14:56:26 +01003052 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003053
3054push_for_double:
3055 push_for_double_split(trans, root, path, data_size);
3056 tried_avoid_double = 1;
3057 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3058 return 0;
3059 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003060}
3061
Yan, Zhengad48fd752009-11-12 09:33:58 +00003062static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3063 struct btrfs_root *root,
3064 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003065{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003066 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003067 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003068 struct btrfs_file_extent_item *fi;
3069 u64 extent_len = 0;
3070 u32 item_size;
3071 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003072
3073 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003074 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3075
3076 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3077 key.type != BTRFS_EXTENT_CSUM_KEY);
3078
3079 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3080 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003081
3082 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003083 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3084 fi = btrfs_item_ptr(leaf, path->slots[0],
3085 struct btrfs_file_extent_item);
3086 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3087 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003088 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003089
Chris Mason459931e2008-12-10 09:10:46 -05003090 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003091 path->search_for_split = 1;
3092 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003093 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003094 if (ret < 0)
3095 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003096
Yan, Zhengad48fd752009-11-12 09:33:58 +00003097 ret = -EAGAIN;
3098 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003099 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003100 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3101 goto err;
3102
Chris Mason109f6ae2010-04-02 09:20:18 -04003103 /* the leaf has changed, it now has room. return now */
3104 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3105 goto err;
3106
Yan, Zhengad48fd752009-11-12 09:33:58 +00003107 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3108 fi = btrfs_item_ptr(leaf, path->slots[0],
3109 struct btrfs_file_extent_item);
3110 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3111 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003112 }
3113
Chris Masonb9473432009-03-13 11:00:37 -04003114 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003115 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003116 if (ret)
3117 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003118
Yan, Zhengad48fd752009-11-12 09:33:58 +00003119 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003120 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003121 return 0;
3122err:
3123 path->keep_locks = 0;
3124 return ret;
3125}
3126
3127static noinline int split_item(struct btrfs_trans_handle *trans,
3128 struct btrfs_root *root,
3129 struct btrfs_path *path,
3130 struct btrfs_key *new_key,
3131 unsigned long split_offset)
3132{
3133 struct extent_buffer *leaf;
3134 struct btrfs_item *item;
3135 struct btrfs_item *new_item;
3136 int slot;
3137 char *buf;
3138 u32 nritems;
3139 u32 item_size;
3140 u32 orig_offset;
3141 struct btrfs_disk_key disk_key;
3142
Chris Masonb9473432009-03-13 11:00:37 -04003143 leaf = path->nodes[0];
3144 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3145
Chris Masonb4ce94d2009-02-04 09:25:08 -05003146 btrfs_set_path_blocking(path);
3147
Chris Mason459931e2008-12-10 09:10:46 -05003148 item = btrfs_item_nr(leaf, path->slots[0]);
3149 orig_offset = btrfs_item_offset(leaf, item);
3150 item_size = btrfs_item_size(leaf, item);
3151
Chris Mason459931e2008-12-10 09:10:46 -05003152 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003153 if (!buf)
3154 return -ENOMEM;
3155
Chris Mason459931e2008-12-10 09:10:46 -05003156 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3157 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003158
Chris Mason459931e2008-12-10 09:10:46 -05003159 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003160 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003161 if (slot != nritems) {
3162 /* shift the items */
3163 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003164 btrfs_item_nr_offset(slot),
3165 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003166 }
3167
3168 btrfs_cpu_key_to_disk(&disk_key, new_key);
3169 btrfs_set_item_key(leaf, &disk_key, slot);
3170
3171 new_item = btrfs_item_nr(leaf, slot);
3172
3173 btrfs_set_item_offset(leaf, new_item, orig_offset);
3174 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3175
3176 btrfs_set_item_offset(leaf, item,
3177 orig_offset + item_size - split_offset);
3178 btrfs_set_item_size(leaf, item, split_offset);
3179
3180 btrfs_set_header_nritems(leaf, nritems + 1);
3181
3182 /* write the data for the start of the original item */
3183 write_extent_buffer(leaf, buf,
3184 btrfs_item_ptr_offset(leaf, path->slots[0]),
3185 split_offset);
3186
3187 /* write the data for the new item */
3188 write_extent_buffer(leaf, buf + split_offset,
3189 btrfs_item_ptr_offset(leaf, slot),
3190 item_size - split_offset);
3191 btrfs_mark_buffer_dirty(leaf);
3192
Yan, Zhengad48fd752009-11-12 09:33:58 +00003193 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003194 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003195 return 0;
3196}
3197
3198/*
3199 * This function splits a single item into two items,
3200 * giving 'new_key' to the new item and splitting the
3201 * old one at split_offset (from the start of the item).
3202 *
3203 * The path may be released by this operation. After
3204 * the split, the path is pointing to the old item. The
3205 * new item is going to be in the same node as the old one.
3206 *
3207 * Note, the item being split must be smaller enough to live alone on
3208 * a tree block with room for one extra struct btrfs_item
3209 *
3210 * This allows us to split the item in place, keeping a lock on the
3211 * leaf the entire time.
3212 */
3213int btrfs_split_item(struct btrfs_trans_handle *trans,
3214 struct btrfs_root *root,
3215 struct btrfs_path *path,
3216 struct btrfs_key *new_key,
3217 unsigned long split_offset)
3218{
3219 int ret;
3220 ret = setup_leaf_for_split(trans, root, path,
3221 sizeof(struct btrfs_item));
3222 if (ret)
3223 return ret;
3224
3225 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003226 return ret;
3227}
3228
3229/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003230 * This function duplicate a item, giving 'new_key' to the new item.
3231 * It guarantees both items live in the same tree leaf and the new item
3232 * is contiguous with the original item.
3233 *
3234 * This allows us to split file extent in place, keeping a lock on the
3235 * leaf the entire time.
3236 */
3237int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3238 struct btrfs_root *root,
3239 struct btrfs_path *path,
3240 struct btrfs_key *new_key)
3241{
3242 struct extent_buffer *leaf;
3243 int ret;
3244 u32 item_size;
3245
3246 leaf = path->nodes[0];
3247 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3248 ret = setup_leaf_for_split(trans, root, path,
3249 item_size + sizeof(struct btrfs_item));
3250 if (ret)
3251 return ret;
3252
3253 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003254 setup_items_for_insert(trans, root, path, new_key, &item_size,
3255 item_size, item_size +
3256 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003257 leaf = path->nodes[0];
3258 memcpy_extent_buffer(leaf,
3259 btrfs_item_ptr_offset(leaf, path->slots[0]),
3260 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3261 item_size);
3262 return 0;
3263}
3264
3265/*
Chris Masond352ac62008-09-29 15:18:18 -04003266 * make the item pointed to by the path smaller. new_size indicates
3267 * how small to make it, and from_end tells us if we just chop bytes
3268 * off the end of the item or if we shift the item to chop bytes off
3269 * the front.
3270 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003271void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3272 struct btrfs_root *root,
3273 struct btrfs_path *path,
3274 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003275{
Chris Masonb18c6682007-04-17 13:26:50 -04003276 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003277 struct extent_buffer *leaf;
3278 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003279 u32 nritems;
3280 unsigned int data_end;
3281 unsigned int old_data_start;
3282 unsigned int old_size;
3283 unsigned int size_diff;
3284 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003285 struct btrfs_map_token token;
3286
3287 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003288
Chris Mason5f39d392007-10-15 16:14:19 -04003289 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003290 slot = path->slots[0];
3291
3292 old_size = btrfs_item_size_nr(leaf, slot);
3293 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003294 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003295
Chris Mason5f39d392007-10-15 16:14:19 -04003296 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003297 data_end = leaf_data_end(root, leaf);
3298
Chris Mason5f39d392007-10-15 16:14:19 -04003299 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003300
Chris Masonb18c6682007-04-17 13:26:50 -04003301 size_diff = old_size - new_size;
3302
3303 BUG_ON(slot < 0);
3304 BUG_ON(slot >= nritems);
3305
3306 /*
3307 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3308 */
3309 /* first correct the data pointers */
3310 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003311 u32 ioff;
3312 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003313
Chris Masoncfed81a2012-03-03 07:40:03 -05003314 ioff = btrfs_token_item_offset(leaf, item, &token);
3315 btrfs_set_token_item_offset(leaf, item,
3316 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003317 }
Chris Masondb945352007-10-15 16:15:53 -04003318
Chris Masonb18c6682007-04-17 13:26:50 -04003319 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003320 if (from_end) {
3321 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3322 data_end + size_diff, btrfs_leaf_data(leaf) +
3323 data_end, old_data_start + new_size - data_end);
3324 } else {
3325 struct btrfs_disk_key disk_key;
3326 u64 offset;
3327
3328 btrfs_item_key(leaf, &disk_key, slot);
3329
3330 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3331 unsigned long ptr;
3332 struct btrfs_file_extent_item *fi;
3333
3334 fi = btrfs_item_ptr(leaf, slot,
3335 struct btrfs_file_extent_item);
3336 fi = (struct btrfs_file_extent_item *)(
3337 (unsigned long)fi - size_diff);
3338
3339 if (btrfs_file_extent_type(leaf, fi) ==
3340 BTRFS_FILE_EXTENT_INLINE) {
3341 ptr = btrfs_item_ptr_offset(leaf, slot);
3342 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003343 (unsigned long)fi,
3344 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003345 disk_bytenr));
3346 }
3347 }
3348
3349 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3350 data_end + size_diff, btrfs_leaf_data(leaf) +
3351 data_end, old_data_start - data_end);
3352
3353 offset = btrfs_disk_key_offset(&disk_key);
3354 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3355 btrfs_set_item_key(leaf, &disk_key, slot);
3356 if (slot == 0)
3357 fixup_low_keys(trans, root, path, &disk_key, 1);
3358 }
Chris Mason5f39d392007-10-15 16:14:19 -04003359
3360 item = btrfs_item_nr(leaf, slot);
3361 btrfs_set_item_size(leaf, item, new_size);
3362 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 if (btrfs_leaf_free_space(root, leaf) < 0) {
3365 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003366 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003367 }
Chris Masonb18c6682007-04-17 13:26:50 -04003368}
3369
Chris Masond352ac62008-09-29 15:18:18 -04003370/*
3371 * make the item pointed to by the path bigger, data_size is the new size.
3372 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003373void btrfs_extend_item(struct btrfs_trans_handle *trans,
3374 struct btrfs_root *root, struct btrfs_path *path,
3375 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003376{
Chris Mason6567e832007-04-16 09:22:45 -04003377 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003378 struct extent_buffer *leaf;
3379 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003380 u32 nritems;
3381 unsigned int data_end;
3382 unsigned int old_data;
3383 unsigned int old_size;
3384 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003385 struct btrfs_map_token token;
3386
3387 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003388
Chris Mason5f39d392007-10-15 16:14:19 -04003389 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003390
Chris Mason5f39d392007-10-15 16:14:19 -04003391 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003392 data_end = leaf_data_end(root, leaf);
3393
Chris Mason5f39d392007-10-15 16:14:19 -04003394 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3395 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003396 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003397 }
Chris Mason6567e832007-04-16 09:22:45 -04003398 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003399 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003400
3401 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003402 if (slot >= nritems) {
3403 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003404 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3405 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003406 BUG_ON(1);
3407 }
Chris Mason6567e832007-04-16 09:22:45 -04003408
3409 /*
3410 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3411 */
3412 /* first correct the data pointers */
3413 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003414 u32 ioff;
3415 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003416
Chris Masoncfed81a2012-03-03 07:40:03 -05003417 ioff = btrfs_token_item_offset(leaf, item, &token);
3418 btrfs_set_token_item_offset(leaf, item,
3419 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003420 }
Chris Mason5f39d392007-10-15 16:14:19 -04003421
Chris Mason6567e832007-04-16 09:22:45 -04003422 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003423 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003424 data_end - data_size, btrfs_leaf_data(leaf) +
3425 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003426
Chris Mason6567e832007-04-16 09:22:45 -04003427 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003428 old_size = btrfs_item_size_nr(leaf, slot);
3429 item = btrfs_item_nr(leaf, slot);
3430 btrfs_set_item_size(leaf, item, old_size + data_size);
3431 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003432
Chris Mason5f39d392007-10-15 16:14:19 -04003433 if (btrfs_leaf_free_space(root, leaf) < 0) {
3434 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003435 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003436 }
Chris Mason6567e832007-04-16 09:22:45 -04003437}
3438
Chris Mason74123bd2007-02-02 11:05:29 -05003439/*
Chris Masond352ac62008-09-29 15:18:18 -04003440 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003441 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003442 * Returns the number of keys that were inserted.
3443 */
3444int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3445 struct btrfs_root *root,
3446 struct btrfs_path *path,
3447 struct btrfs_key *cpu_key, u32 *data_size,
3448 int nr)
3449{
3450 struct extent_buffer *leaf;
3451 struct btrfs_item *item;
3452 int ret = 0;
3453 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003454 int i;
3455 u32 nritems;
3456 u32 total_data = 0;
3457 u32 total_size = 0;
3458 unsigned int data_end;
3459 struct btrfs_disk_key disk_key;
3460 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003461 struct btrfs_map_token token;
3462
3463 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003464
Yan Zheng87b29b22008-12-17 10:21:48 -05003465 for (i = 0; i < nr; i++) {
3466 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3467 BTRFS_LEAF_DATA_SIZE(root)) {
3468 break;
3469 nr = i;
3470 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003471 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003472 total_size += data_size[i] + sizeof(struct btrfs_item);
3473 }
3474 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003475
Josef Bacikf3465ca2008-11-12 14:19:50 -05003476 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3477 if (ret == 0)
3478 return -EEXIST;
3479 if (ret < 0)
3480 goto out;
3481
Josef Bacikf3465ca2008-11-12 14:19:50 -05003482 leaf = path->nodes[0];
3483
3484 nritems = btrfs_header_nritems(leaf);
3485 data_end = leaf_data_end(root, leaf);
3486
3487 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3488 for (i = nr; i >= 0; i--) {
3489 total_data -= data_size[i];
3490 total_size -= data_size[i] + sizeof(struct btrfs_item);
3491 if (total_size < btrfs_leaf_free_space(root, leaf))
3492 break;
3493 }
3494 nr = i;
3495 }
3496
3497 slot = path->slots[0];
3498 BUG_ON(slot < 0);
3499
3500 if (slot != nritems) {
3501 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3502
3503 item = btrfs_item_nr(leaf, slot);
3504 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3505
3506 /* figure out how many keys we can insert in here */
3507 total_data = data_size[0];
3508 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003509 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003510 break;
3511 total_data += data_size[i];
3512 }
3513 nr = i;
3514
3515 if (old_data < data_end) {
3516 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003517 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003518 slot, old_data, data_end);
3519 BUG_ON(1);
3520 }
3521 /*
3522 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3523 */
3524 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003525 for (i = slot; i < nritems; i++) {
3526 u32 ioff;
3527
3528 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003529 ioff = btrfs_token_item_offset(leaf, item, &token);
3530 btrfs_set_token_item_offset(leaf, item,
3531 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003532 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003533 /* shift the items */
3534 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3535 btrfs_item_nr_offset(slot),
3536 (nritems - slot) * sizeof(struct btrfs_item));
3537
3538 /* shift the data */
3539 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3540 data_end - total_data, btrfs_leaf_data(leaf) +
3541 data_end, old_data - data_end);
3542 data_end = old_data;
3543 } else {
3544 /*
3545 * this sucks but it has to be done, if we are inserting at
3546 * the end of the leaf only insert 1 of the items, since we
3547 * have no way of knowing whats on the next leaf and we'd have
3548 * to drop our current locks to figure it out
3549 */
3550 nr = 1;
3551 }
3552
3553 /* setup the item for the new data */
3554 for (i = 0; i < nr; i++) {
3555 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3556 btrfs_set_item_key(leaf, &disk_key, slot + i);
3557 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003558 btrfs_set_token_item_offset(leaf, item,
3559 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003560 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003561 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003562 }
3563 btrfs_set_header_nritems(leaf, nritems + nr);
3564 btrfs_mark_buffer_dirty(leaf);
3565
3566 ret = 0;
3567 if (slot == 0) {
3568 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003569 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003570 }
3571
3572 if (btrfs_leaf_free_space(root, leaf) < 0) {
3573 btrfs_print_leaf(root, leaf);
3574 BUG();
3575 }
3576out:
3577 if (!ret)
3578 ret = nr;
3579 return ret;
3580}
3581
3582/*
Chris Mason44871b12009-03-13 10:04:31 -04003583 * this is a helper for btrfs_insert_empty_items, the main goal here is
3584 * to save stack depth by doing the bulk of the work in a function
3585 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003586 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003587void setup_items_for_insert(struct btrfs_trans_handle *trans,
3588 struct btrfs_root *root, struct btrfs_path *path,
3589 struct btrfs_key *cpu_key, u32 *data_size,
3590 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003591{
Chris Mason5f39d392007-10-15 16:14:19 -04003592 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003593 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003594 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003595 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003596 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003597 struct extent_buffer *leaf;
3598 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05003599 struct btrfs_map_token token;
3600
3601 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04003602
Chris Mason5f39d392007-10-15 16:14:19 -04003603 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003604 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003605
Chris Mason5f39d392007-10-15 16:14:19 -04003606 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003607 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003608
Chris Masonf25956c2008-09-12 15:32:53 -04003609 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003610 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003611 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003612 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003613 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003614 }
Chris Mason5f39d392007-10-15 16:14:19 -04003615
Chris Masonbe0e5c02007-01-26 15:51:26 -05003616 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003617 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003618
Chris Mason5f39d392007-10-15 16:14:19 -04003619 if (old_data < data_end) {
3620 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003621 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003622 slot, old_data, data_end);
3623 BUG_ON(1);
3624 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003625 /*
3626 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3627 */
3628 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003629 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003630 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003631
Chris Mason5f39d392007-10-15 16:14:19 -04003632 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003633 ioff = btrfs_token_item_offset(leaf, item, &token);
3634 btrfs_set_token_item_offset(leaf, item,
3635 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003636 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003637 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003638 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003639 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003640 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641
3642 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003643 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003644 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003645 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003646 data_end = old_data;
3647 }
Chris Mason5f39d392007-10-15 16:14:19 -04003648
Chris Mason62e27492007-03-15 12:56:47 -04003649 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003650 for (i = 0; i < nr; i++) {
3651 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3652 btrfs_set_item_key(leaf, &disk_key, slot + i);
3653 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003654 btrfs_set_token_item_offset(leaf, item,
3655 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003656 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003657 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003658 }
Chris Mason44871b12009-03-13 10:04:31 -04003659
Chris Mason9c583092008-01-29 15:15:18 -05003660 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003661
Chris Mason5a01a2e2008-01-30 11:43:54 -05003662 if (slot == 0) {
3663 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003664 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003665 }
Chris Masonb9473432009-03-13 11:00:37 -04003666 btrfs_unlock_up_safe(path, 1);
3667 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003668
Chris Mason5f39d392007-10-15 16:14:19 -04003669 if (btrfs_leaf_free_space(root, leaf) < 0) {
3670 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003671 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003672 }
Chris Mason44871b12009-03-13 10:04:31 -04003673}
3674
3675/*
3676 * Given a key and some data, insert items into the tree.
3677 * This does all the path init required, making room in the tree if needed.
3678 */
3679int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3680 struct btrfs_root *root,
3681 struct btrfs_path *path,
3682 struct btrfs_key *cpu_key, u32 *data_size,
3683 int nr)
3684{
Chris Mason44871b12009-03-13 10:04:31 -04003685 int ret = 0;
3686 int slot;
3687 int i;
3688 u32 total_size = 0;
3689 u32 total_data = 0;
3690
3691 for (i = 0; i < nr; i++)
3692 total_data += data_size[i];
3693
3694 total_size = total_data + (nr * sizeof(struct btrfs_item));
3695 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3696 if (ret == 0)
3697 return -EEXIST;
3698 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003699 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003700
Chris Mason44871b12009-03-13 10:04:31 -04003701 slot = path->slots[0];
3702 BUG_ON(slot < 0);
3703
Jeff Mahoney143bede2012-03-01 14:56:26 +01003704 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003705 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003706 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003707}
3708
3709/*
3710 * Given a key and some data, insert an item into the tree.
3711 * This does all the path init required, making room in the tree if needed.
3712 */
Chris Masone089f052007-03-16 16:20:31 -04003713int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3714 *root, struct btrfs_key *cpu_key, void *data, u32
3715 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003716{
3717 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003718 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003719 struct extent_buffer *leaf;
3720 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003721
Chris Mason2c90e5d2007-04-02 10:50:19 -04003722 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003723 if (!path)
3724 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003725 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003726 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003727 leaf = path->nodes[0];
3728 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3729 write_extent_buffer(leaf, data, ptr, data_size);
3730 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003731 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003732 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003733 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003734}
3735
Chris Mason74123bd2007-02-02 11:05:29 -05003736/*
Chris Mason5de08d72007-02-24 06:24:44 -05003737 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003738 *
Chris Masond352ac62008-09-29 15:18:18 -04003739 * the tree should have been previously balanced so the deletion does not
3740 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003741 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003742static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3743 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744{
Chris Mason5f39d392007-10-15 16:14:19 -04003745 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003746 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003747
Chris Mason5f39d392007-10-15 16:14:19 -04003748 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003749 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003750 memmove_extent_buffer(parent,
3751 btrfs_node_key_ptr_offset(slot),
3752 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003753 sizeof(struct btrfs_key_ptr) *
3754 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003755 }
Chris Mason7518a232007-03-12 12:01:18 -04003756 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003757 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003758 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003759 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003760 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003761 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003762 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003763 struct btrfs_disk_key disk_key;
3764
3765 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003766 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003767 }
Chris Masond6025572007-03-30 14:27:56 -04003768 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003769}
3770
Chris Mason74123bd2007-02-02 11:05:29 -05003771/*
Chris Mason323ac952008-10-01 19:05:46 -04003772 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003773 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003774 *
3775 * This deletes the pointer in path->nodes[1] and frees the leaf
3776 * block extent. zero is returned if it all worked out, < 0 otherwise.
3777 *
3778 * The path must have already been setup for deleting the leaf, including
3779 * all the proper balancing. path->nodes[1] must be locked.
3780 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003781static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3782 struct btrfs_root *root,
3783 struct btrfs_path *path,
3784 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003785{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003786 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003787 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003788
Chris Mason4d081c42009-02-04 09:31:28 -05003789 /*
3790 * btrfs_free_extent is expensive, we want to make sure we
3791 * aren't holding any locks when we call it
3792 */
3793 btrfs_unlock_up_safe(path, 0);
3794
Yan, Zhengf0486c62010-05-16 10:46:25 -04003795 root_sub_used(root, leaf->len);
3796
Josef Bacik3083ee22012-03-09 16:01:49 -05003797 extent_buffer_get(leaf);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003798 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05003799 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003800}
3801/*
Chris Mason74123bd2007-02-02 11:05:29 -05003802 * delete the item at the leaf level in path. If that empties
3803 * the leaf, remove it from the tree
3804 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003805int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3806 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003807{
Chris Mason5f39d392007-10-15 16:14:19 -04003808 struct extent_buffer *leaf;
3809 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003810 int last_off;
3811 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003812 int ret = 0;
3813 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003814 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003815 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05003816 struct btrfs_map_token token;
3817
3818 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003819
Chris Mason5f39d392007-10-15 16:14:19 -04003820 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003821 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3822
3823 for (i = 0; i < nr; i++)
3824 dsize += btrfs_item_size_nr(leaf, slot + i);
3825
Chris Mason5f39d392007-10-15 16:14:19 -04003826 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003827
Chris Mason85e21ba2008-01-29 15:11:36 -05003828 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003829 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003830
3831 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003832 data_end + dsize,
3833 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003834 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003835
Chris Mason85e21ba2008-01-29 15:11:36 -05003836 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003837 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003838
Chris Mason5f39d392007-10-15 16:14:19 -04003839 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003840 ioff = btrfs_token_item_offset(leaf, item, &token);
3841 btrfs_set_token_item_offset(leaf, item,
3842 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003843 }
Chris Masondb945352007-10-15 16:15:53 -04003844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003846 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003847 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003848 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003849 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003850 btrfs_set_header_nritems(leaf, nritems - nr);
3851 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003852
Chris Mason74123bd2007-02-02 11:05:29 -05003853 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003854 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003855 if (leaf == root->node) {
3856 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003857 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003858 btrfs_set_path_blocking(path);
3859 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003860 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003861 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003862 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003863 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003864 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003865 struct btrfs_disk_key disk_key;
3866
3867 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003868 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003869 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003870
Chris Mason74123bd2007-02-02 11:05:29 -05003871 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003872 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003873 /* push_leaf_left fixes the path.
3874 * make sure the path still points to our leaf
3875 * for possible call to del_ptr below
3876 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003877 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003878 extent_buffer_get(leaf);
3879
Chris Masonb9473432009-03-13 11:00:37 -04003880 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003881 wret = push_leaf_left(trans, root, path, 1, 1,
3882 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003883 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003884 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003885
3886 if (path->nodes[0] == leaf &&
3887 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003888 wret = push_leaf_right(trans, root, path, 1,
3889 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003890 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003891 ret = wret;
3892 }
Chris Mason5f39d392007-10-15 16:14:19 -04003893
3894 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003895 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003896 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003897 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003898 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003899 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003900 /* if we're still in the path, make sure
3901 * we're dirty. Otherwise, one of the
3902 * push_leaf functions must have already
3903 * dirtied this buffer
3904 */
3905 if (path->nodes[0] == leaf)
3906 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003908 }
Chris Masond5719762007-03-23 10:01:08 -04003909 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003910 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003911 }
3912 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003913 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003914}
3915
Chris Mason97571fd2007-02-24 13:39:08 -05003916/*
Chris Mason925baed2008-06-25 16:01:30 -04003917 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003918 * returns 0 if it found something or 1 if there are no lesser leaves.
3919 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003920 *
3921 * This may release the path, and so you may lose any locks held at the
3922 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003923 */
3924int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3925{
Chris Mason925baed2008-06-25 16:01:30 -04003926 struct btrfs_key key;
3927 struct btrfs_disk_key found_key;
3928 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003929
Chris Mason925baed2008-06-25 16:01:30 -04003930 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003931
Chris Mason925baed2008-06-25 16:01:30 -04003932 if (key.offset > 0)
3933 key.offset--;
3934 else if (key.type > 0)
3935 key.type--;
3936 else if (key.objectid > 0)
3937 key.objectid--;
3938 else
3939 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003940
David Sterbab3b4aa72011-04-21 01:20:15 +02003941 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003942 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3943 if (ret < 0)
3944 return ret;
3945 btrfs_item_key(path->nodes[0], &found_key, 0);
3946 ret = comp_keys(&found_key, &key);
3947 if (ret < 0)
3948 return 0;
3949 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003950}
3951
Chris Mason3f157a22008-06-25 16:01:31 -04003952/*
3953 * A helper function to walk down the tree starting at min_key, and looking
3954 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003955 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003956 *
3957 * This does not cow, but it does stuff the starting key it finds back
3958 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3959 * key and get a writable path.
3960 *
3961 * This does lock as it descends, and path->keep_locks should be set
3962 * to 1 by the caller.
3963 *
3964 * This honors path->lowest_level to prevent descent past a given level
3965 * of the tree.
3966 *
Chris Masond352ac62008-09-29 15:18:18 -04003967 * min_trans indicates the oldest transaction that you are interested
3968 * in walking through. Any nodes or leaves older than min_trans are
3969 * skipped over (without reading them).
3970 *
Chris Mason3f157a22008-06-25 16:01:31 -04003971 * returns zero if something useful was found, < 0 on error and 1 if there
3972 * was nothing in the tree that matched the search criteria.
3973 */
3974int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003975 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003976 struct btrfs_path *path, int cache_only,
3977 u64 min_trans)
3978{
3979 struct extent_buffer *cur;
3980 struct btrfs_key found_key;
3981 int slot;
Yan96524802008-07-24 12:19:49 -04003982 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003983 u32 nritems;
3984 int level;
3985 int ret = 1;
3986
Chris Mason934d3752008-12-08 16:43:10 -05003987 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003988again:
Chris Masonbd681512011-07-16 15:23:14 -04003989 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003990 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003991 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003992 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003993 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003994
3995 if (btrfs_header_generation(cur) < min_trans) {
3996 ret = 1;
3997 goto out;
3998 }
Chris Masond3977122009-01-05 21:25:51 -05003999 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004000 nritems = btrfs_header_nritems(cur);
4001 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004002 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004003
Chris Mason323ac952008-10-01 19:05:46 -04004004 /* at the lowest level, we're done, setup the path and exit */
4005 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004006 if (slot >= nritems)
4007 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004008 ret = 0;
4009 path->slots[level] = slot;
4010 btrfs_item_key_to_cpu(cur, &found_key, slot);
4011 goto out;
4012 }
Yan96524802008-07-24 12:19:49 -04004013 if (sret && slot > 0)
4014 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004015 /*
4016 * check this node pointer against the cache_only and
4017 * min_trans parameters. If it isn't in cache or is too
4018 * old, skip to the next one.
4019 */
Chris Masond3977122009-01-05 21:25:51 -05004020 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004021 u64 blockptr;
4022 u64 gen;
4023 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004024 struct btrfs_disk_key disk_key;
4025
Chris Mason3f157a22008-06-25 16:01:31 -04004026 blockptr = btrfs_node_blockptr(cur, slot);
4027 gen = btrfs_node_ptr_generation(cur, slot);
4028 if (gen < min_trans) {
4029 slot++;
4030 continue;
4031 }
4032 if (!cache_only)
4033 break;
4034
Chris Masone02119d2008-09-05 16:13:11 -04004035 if (max_key) {
4036 btrfs_node_key(cur, &disk_key, slot);
4037 if (comp_keys(&disk_key, max_key) >= 0) {
4038 ret = 1;
4039 goto out;
4040 }
4041 }
4042
Chris Mason3f157a22008-06-25 16:01:31 -04004043 tmp = btrfs_find_tree_block(root, blockptr,
4044 btrfs_level_size(root, level - 1));
4045
4046 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4047 free_extent_buffer(tmp);
4048 break;
4049 }
4050 if (tmp)
4051 free_extent_buffer(tmp);
4052 slot++;
4053 }
Chris Masone02119d2008-09-05 16:13:11 -04004054find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004055 /*
4056 * we didn't find a candidate key in this node, walk forward
4057 * and find another one
4058 */
4059 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004060 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004061 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004062 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004063 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004064 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004065 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004066 goto again;
4067 } else {
4068 goto out;
4069 }
4070 }
4071 /* save our key for returning back */
4072 btrfs_node_key_to_cpu(cur, &found_key, slot);
4073 path->slots[level] = slot;
4074 if (level == path->lowest_level) {
4075 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004076 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004077 goto out;
4078 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004079 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004080 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004081 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004082
Chris Masonbd681512011-07-16 15:23:14 -04004083 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004084
Chris Masonbd681512011-07-16 15:23:14 -04004085 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004086 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004087 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004088 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004089 }
4090out:
4091 if (ret == 0)
4092 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004093 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004094 return ret;
4095}
4096
4097/*
4098 * this is similar to btrfs_next_leaf, but does not try to preserve
4099 * and fixup the path. It looks for and returns the next key in the
4100 * tree based on the current path and the cache_only and min_trans
4101 * parameters.
4102 *
4103 * 0 is returned if another key is found, < 0 if there are any errors
4104 * and 1 is returned if there are no higher keys in the tree
4105 *
4106 * path->keep_locks should be set to 1 on the search made before
4107 * calling this function.
4108 */
Chris Masone7a84562008-06-25 16:01:31 -04004109int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004110 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004111 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004112{
Chris Masone7a84562008-06-25 16:01:31 -04004113 int slot;
4114 struct extent_buffer *c;
4115
Chris Mason934d3752008-12-08 16:43:10 -05004116 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004117 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004118 if (!path->nodes[level])
4119 return 1;
4120
4121 slot = path->slots[level] + 1;
4122 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004123next:
Chris Masone7a84562008-06-25 16:01:31 -04004124 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004125 int ret;
4126 int orig_lowest;
4127 struct btrfs_key cur_key;
4128 if (level + 1 >= BTRFS_MAX_LEVEL ||
4129 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004130 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004131
4132 if (path->locks[level + 1]) {
4133 level++;
4134 continue;
4135 }
4136
4137 slot = btrfs_header_nritems(c) - 1;
4138 if (level == 0)
4139 btrfs_item_key_to_cpu(c, &cur_key, slot);
4140 else
4141 btrfs_node_key_to_cpu(c, &cur_key, slot);
4142
4143 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004144 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004145 path->lowest_level = level;
4146 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4147 0, 0);
4148 path->lowest_level = orig_lowest;
4149 if (ret < 0)
4150 return ret;
4151
4152 c = path->nodes[level];
4153 slot = path->slots[level];
4154 if (ret == 0)
4155 slot++;
4156 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004157 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004158
Chris Masone7a84562008-06-25 16:01:31 -04004159 if (level == 0)
4160 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004161 else {
4162 u64 blockptr = btrfs_node_blockptr(c, slot);
4163 u64 gen = btrfs_node_ptr_generation(c, slot);
4164
4165 if (cache_only) {
4166 struct extent_buffer *cur;
4167 cur = btrfs_find_tree_block(root, blockptr,
4168 btrfs_level_size(root, level - 1));
4169 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4170 slot++;
4171 if (cur)
4172 free_extent_buffer(cur);
4173 goto next;
4174 }
4175 free_extent_buffer(cur);
4176 }
4177 if (gen < min_trans) {
4178 slot++;
4179 goto next;
4180 }
Chris Masone7a84562008-06-25 16:01:31 -04004181 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004182 }
Chris Masone7a84562008-06-25 16:01:31 -04004183 return 0;
4184 }
4185 return 1;
4186}
4187
Chris Mason7bb86312007-12-11 09:25:06 -05004188/*
Chris Mason925baed2008-06-25 16:01:30 -04004189 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004190 * returns 0 if it found something or 1 if there are no greater leaves.
4191 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004192 */
Chris Mason234b63a2007-03-13 10:46:10 -04004193int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004194{
4195 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004196 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004197 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004198 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004199 struct btrfs_key key;
4200 u32 nritems;
4201 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004202 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004203 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004204
4205 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004206 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004207 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004208
Chris Mason8e73f272009-04-03 10:14:18 -04004209 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4210again:
4211 level = 1;
4212 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004213 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004214 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004215
Chris Masona2135012008-06-25 16:01:30 -04004216 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004217 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004218
Chris Mason925baed2008-06-25 16:01:30 -04004219 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4220 path->keep_locks = 0;
4221
4222 if (ret < 0)
4223 return ret;
4224
Chris Masona2135012008-06-25 16:01:30 -04004225 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004226 /*
4227 * by releasing the path above we dropped all our locks. A balance
4228 * could have added more items next to the key that used to be
4229 * at the very end of the block. So, check again here and
4230 * advance the path if there are now more items available.
4231 */
Chris Masona2135012008-06-25 16:01:30 -04004232 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004233 if (ret == 0)
4234 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004235 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004236 goto done;
4237 }
Chris Masond97e63b2007-02-20 16:40:44 -05004238
Chris Masond3977122009-01-05 21:25:51 -05004239 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004240 if (!path->nodes[level]) {
4241 ret = 1;
4242 goto done;
4243 }
Chris Mason5f39d392007-10-15 16:14:19 -04004244
Chris Masond97e63b2007-02-20 16:40:44 -05004245 slot = path->slots[level] + 1;
4246 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004247 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004248 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004249 if (level == BTRFS_MAX_LEVEL) {
4250 ret = 1;
4251 goto done;
4252 }
Chris Masond97e63b2007-02-20 16:40:44 -05004253 continue;
4254 }
Chris Mason5f39d392007-10-15 16:14:19 -04004255
Chris Mason925baed2008-06-25 16:01:30 -04004256 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004257 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004258 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004259 }
Chris Mason5f39d392007-10-15 16:14:19 -04004260
Chris Mason8e73f272009-04-03 10:14:18 -04004261 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004262 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004263 ret = read_block_for_search(NULL, root, path, &next, level,
4264 slot, &key);
4265 if (ret == -EAGAIN)
4266 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004267
Chris Mason76a05b32009-05-14 13:24:30 -04004268 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004269 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004270 goto done;
4271 }
4272
Chris Mason5cd57b22008-06-25 16:01:30 -04004273 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004274 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004275 if (!ret) {
4276 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004277 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004278 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004279 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004280 }
Chris Mason31533fb2011-07-26 16:01:59 -04004281 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004282 }
Chris Masond97e63b2007-02-20 16:40:44 -05004283 break;
4284 }
4285 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004286 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004287 level--;
4288 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004289 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004290 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004291
Chris Mason5f39d392007-10-15 16:14:19 -04004292 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004293 path->nodes[level] = next;
4294 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004295 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004296 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004297 if (!level)
4298 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004299
Chris Mason8e73f272009-04-03 10:14:18 -04004300 ret = read_block_for_search(NULL, root, path, &next, level,
4301 0, &key);
4302 if (ret == -EAGAIN)
4303 goto again;
4304
Chris Mason76a05b32009-05-14 13:24:30 -04004305 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004306 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004307 goto done;
4308 }
4309
Chris Mason5cd57b22008-06-25 16:01:30 -04004310 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004311 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004312 if (!ret) {
4313 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004314 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004315 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004316 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004317 }
Chris Mason31533fb2011-07-26 16:01:59 -04004318 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004319 }
Chris Masond97e63b2007-02-20 16:40:44 -05004320 }
Chris Mason8e73f272009-04-03 10:14:18 -04004321 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004322done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004323 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004324 path->leave_spinning = old_spinning;
4325 if (!old_spinning)
4326 btrfs_set_path_blocking(path);
4327
4328 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004329}
Chris Mason0b86a832008-03-24 15:01:56 -04004330
Chris Mason3f157a22008-06-25 16:01:31 -04004331/*
4332 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4333 * searching until it gets past min_objectid or finds an item of 'type'
4334 *
4335 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4336 */
Chris Mason0b86a832008-03-24 15:01:56 -04004337int btrfs_previous_item(struct btrfs_root *root,
4338 struct btrfs_path *path, u64 min_objectid,
4339 int type)
4340{
4341 struct btrfs_key found_key;
4342 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004343 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004344 int ret;
4345
Chris Masond3977122009-01-05 21:25:51 -05004346 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004347 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004348 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004349 ret = btrfs_prev_leaf(root, path);
4350 if (ret != 0)
4351 return ret;
4352 } else {
4353 path->slots[0]--;
4354 }
4355 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004356 nritems = btrfs_header_nritems(leaf);
4357 if (nritems == 0)
4358 return 1;
4359 if (path->slots[0] == nritems)
4360 path->slots[0]--;
4361
Chris Mason0b86a832008-03-24 15:01:56 -04004362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004363 if (found_key.objectid < min_objectid)
4364 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004365 if (found_key.type == type)
4366 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004367 if (found_key.objectid == min_objectid &&
4368 found_key.type < type)
4369 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004370 }
4371 return 1;
4372}