blob: 4106264fbc655ac79b26efa1177384ea92b72988 [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{
Chris Masone5846fc2012-05-03 12:08:48 -0400223 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400224 if (root->track_dirty && list_empty(&root->dirty_list)) {
225 list_add(&root->dirty_list,
226 &root->fs_info->dirty_cowonly_roots);
227 }
Chris Masone5846fc2012-05-03 12:08:48 -0400228 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400229}
230
Chris Masond352ac62008-09-29 15:18:18 -0400231/*
232 * used by snapshot creation to make a copy of a root for a tree with
233 * a given objectid. The buffer with the new root node is returned in
234 * cow_ret, and this func returns zero on success or a negative error code.
235 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500236int btrfs_copy_root(struct btrfs_trans_handle *trans,
237 struct btrfs_root *root,
238 struct extent_buffer *buf,
239 struct extent_buffer **cow_ret, u64 new_root_objectid)
240{
241 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500242 int ret = 0;
243 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400244 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245
246 WARN_ON(root->ref_cows && trans->transid !=
247 root->fs_info->running_transaction->transid);
248 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
249
250 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400251 if (level == 0)
252 btrfs_item_key(buf, &disk_key, 0);
253 else
254 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400255
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400256 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
257 new_root_objectid, &disk_key, level,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200258 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400259 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 return PTR_ERR(cow);
261
262 copy_extent_buffer(cow, buf, 0, 0, cow->len);
263 btrfs_set_header_bytenr(cow, cow->start);
264 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
266 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
267 BTRFS_HEADER_FLAG_RELOC);
268 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
269 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
270 else
271 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500272
Yan Zheng2b820322008-11-17 21:11:30 -0500273 write_extent_buffer(cow, root->fs_info->fsid,
274 (unsigned long)btrfs_header_fsid(cow),
275 BTRFS_FSID_SIZE);
276
Chris Masonbe20aa92007-12-17 20:14:01 -0500277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200279 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200281 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500282
Chris Masonbe20aa92007-12-17 20:14:01 -0500283 if (ret)
284 return ret;
285
286 btrfs_mark_buffer_dirty(cow);
287 *cow_ret = cow;
288 return 0;
289}
290
Chris Masond352ac62008-09-29 15:18:18 -0400291/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400292 * check if the tree block can be shared by multiple trees
293 */
294int btrfs_block_can_be_shared(struct btrfs_root *root,
295 struct extent_buffer *buf)
296{
297 /*
298 * Tree blocks not in refernece counted trees and tree roots
299 * are never shared. If a block was allocated after the last
300 * snapshot and the block was not allocated by tree relocation,
301 * we know the block is not shared.
302 */
303 if (root->ref_cows &&
304 buf != root->node && buf != root->commit_root &&
305 (btrfs_header_generation(buf) <=
306 btrfs_root_last_snapshot(&root->root_item) ||
307 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
308 return 1;
309#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
310 if (root->ref_cows &&
311 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
312 return 1;
313#endif
314 return 0;
315}
316
317static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
318 struct btrfs_root *root,
319 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400320 struct extent_buffer *cow,
321 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400322{
323 u64 refs;
324 u64 owner;
325 u64 flags;
326 u64 new_flags = 0;
327 int ret;
328
329 /*
330 * Backrefs update rules:
331 *
332 * Always use full backrefs for extent pointers in tree block
333 * allocated by tree relocation.
334 *
335 * If a shared tree block is no longer referenced by its owner
336 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
337 * use full backrefs for extent pointers in tree block.
338 *
339 * If a tree block is been relocating
340 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
341 * use full backrefs for extent pointers in tree block.
342 * The reason for this is some operations (such as drop tree)
343 * are only allowed for blocks use full backrefs.
344 */
345
346 if (btrfs_block_can_be_shared(root, buf)) {
347 ret = btrfs_lookup_extent_info(trans, root, buf->start,
348 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700349 if (ret)
350 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700351 if (refs == 0) {
352 ret = -EROFS;
353 btrfs_std_error(root->fs_info, ret);
354 return ret;
355 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400356 } else {
357 refs = 1;
358 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
359 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
360 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
361 else
362 flags = 0;
363 }
364
365 owner = btrfs_header_owner(buf);
366 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
367 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
368
369 if (refs > 1) {
370 if ((owner == root->root_key.objectid ||
371 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
372 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200373 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100374 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400375
376 if (root->root_key.objectid ==
377 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200378 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100379 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200380 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100381 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400382 }
383 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
384 } else {
385
386 if (root->root_key.objectid ==
387 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200388 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200390 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100391 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400392 }
393 if (new_flags != 0) {
394 ret = btrfs_set_disk_extent_flags(trans, root,
395 buf->start,
396 buf->len,
397 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700398 if (ret)
399 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400 }
401 } else {
402 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
403 if (root->root_key.objectid ==
404 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200405 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400406 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200407 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100408 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200409 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100410 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400411 }
412 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400413 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400414 }
415 return 0;
416}
417
418/*
Chris Masond3977122009-01-05 21:25:51 -0500419 * does the dirty work in cow of a single block. The parent block (if
420 * supplied) is updated to point to the new cow copy. The new buffer is marked
421 * dirty and returned locked. If you modify the block it needs to be marked
422 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400423 *
424 * search_start -- an allocation hint for the new block
425 *
Chris Masond3977122009-01-05 21:25:51 -0500426 * empty_size -- a hint that you plan on doing more cow. This is the size in
427 * bytes the allocator should try to find free next to the block it returns.
428 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400429 */
Chris Masond3977122009-01-05 21:25:51 -0500430static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400431 struct btrfs_root *root,
432 struct extent_buffer *buf,
433 struct extent_buffer *parent, int parent_slot,
434 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400435 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400436{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400437 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400438 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700439 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400440 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400441 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400442 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400443
Chris Mason925baed2008-06-25 16:01:30 -0400444 if (*cow_ret == buf)
445 unlock_orig = 1;
446
Chris Masonb9447ef2009-03-09 11:45:38 -0400447 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400448
Chris Mason7bb86312007-12-11 09:25:06 -0500449 WARN_ON(root->ref_cows && trans->transid !=
450 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400451 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400452
Chris Mason7bb86312007-12-11 09:25:06 -0500453 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400454
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400455 if (level == 0)
456 btrfs_item_key(buf, &disk_key, 0);
457 else
458 btrfs_node_key(buf, &disk_key, 0);
459
460 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
461 if (parent)
462 parent_start = parent->start;
463 else
464 parent_start = 0;
465 } else
466 parent_start = 0;
467
468 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
469 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200470 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400471 if (IS_ERR(cow))
472 return PTR_ERR(cow);
473
Chris Masonb4ce94d2009-02-04 09:25:08 -0500474 /* cow is set to blocking by btrfs_init_new_buffer */
475
Chris Mason5f39d392007-10-15 16:14:19 -0400476 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400477 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400478 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400479 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
480 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
481 BTRFS_HEADER_FLAG_RELOC);
482 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
483 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
484 else
485 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400486
Yan Zheng2b820322008-11-17 21:11:30 -0500487 write_extent_buffer(cow, root->fs_info->fsid,
488 (unsigned long)btrfs_header_fsid(cow),
489 BTRFS_FSID_SIZE);
490
Mark Fashehbe1a5562011-08-08 13:20:18 -0700491 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700492 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100493 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700494 return ret;
495 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400496
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400497 if (root->ref_cows)
498 btrfs_reloc_cow_block(trans, root, buf, cow);
499
Chris Mason6702ed42007-08-07 16:15:09 -0400500 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400501 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400502 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
503 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
504 parent_start = buf->start;
505 else
506 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400507
Chris Mason5f39d392007-10-15 16:14:19 -0400508 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400509 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400510
Yan, Zhengf0486c62010-05-16 10:46:25 -0400511 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200512 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400513 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400514 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400515 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
517 parent_start = parent->start;
518 else
519 parent_start = 0;
520
521 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400522 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400523 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500524 btrfs_set_node_ptr_generation(parent, parent_slot,
525 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400526 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400527 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200528 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400529 }
Chris Mason925baed2008-06-25 16:01:30 -0400530 if (unlock_orig)
531 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500532 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400533 btrfs_mark_buffer_dirty(cow);
534 *cow_ret = cow;
535 return 0;
536}
537
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400538static inline int should_cow_block(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct extent_buffer *buf)
541{
Liu Bof1ebcc72011-11-14 20:48:06 -0500542 /* ensure we can see the force_cow */
543 smp_rmb();
544
545 /*
546 * We do not need to cow a block if
547 * 1) this block is not created or changed in this transaction;
548 * 2) this block does not belong to TREE_RELOC tree;
549 * 3) the root is not forced COW.
550 *
551 * What is forced COW:
552 * when we create snapshot during commiting the transaction,
553 * after we've finished coping src root, we must COW the shared
554 * block to ensure the metadata consistency.
555 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 if (btrfs_header_generation(buf) == trans->transid &&
557 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
558 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500559 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
560 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400561 return 0;
562 return 1;
563}
564
Chris Masond352ac62008-09-29 15:18:18 -0400565/*
566 * cows a single block, see __btrfs_cow_block for the real work.
567 * This version of it has extra checks so that a block isn't cow'd more than
568 * once per transaction, as long as it hasn't been written yet
569 */
Chris Masond3977122009-01-05 21:25:51 -0500570noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400571 struct btrfs_root *root, struct extent_buffer *buf,
572 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400573 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500574{
Chris Mason6702ed42007-08-07 16:15:09 -0400575 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400576 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500577
Chris Masonccd467d2007-06-28 15:57:36 -0400578 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500579 printk(KERN_CRIT "trans %llu running %llu\n",
580 (unsigned long long)trans->transid,
581 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400582 root->fs_info->running_transaction->transid);
583 WARN_ON(1);
584 }
585 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500586 printk(KERN_CRIT "trans %llu running %llu\n",
587 (unsigned long long)trans->transid,
588 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400589 WARN_ON(1);
590 }
Chris Masondc17ff82008-01-08 15:46:30 -0500591
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400592 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500593 *cow_ret = buf;
594 return 0;
595 }
Chris Masonc4876852009-02-04 09:24:25 -0500596
Chris Mason0b86a832008-03-24 15:01:56 -0400597 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500598
599 if (parent)
600 btrfs_set_lock_blocking(parent);
601 btrfs_set_lock_blocking(buf);
602
Chris Masonf510cfe2007-10-15 16:14:48 -0400603 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400604 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000605
606 trace_btrfs_cow_block(root, buf, *cow_ret);
607
Chris Masonf510cfe2007-10-15 16:14:48 -0400608 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400609}
610
Chris Masond352ac62008-09-29 15:18:18 -0400611/*
612 * helper function for defrag to decide if two blocks pointed to by a
613 * node are actually close by
614 */
Chris Mason6b800532007-10-15 16:17:34 -0400615static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400616{
Chris Mason6b800532007-10-15 16:17:34 -0400617 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400618 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400619 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400620 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500621 return 0;
622}
623
Chris Mason081e9572007-11-06 10:26:24 -0500624/*
625 * compare two keys in a memcmp fashion
626 */
627static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
628{
629 struct btrfs_key k1;
630
631 btrfs_disk_key_to_cpu(&k1, disk);
632
Diego Calleja20736ab2009-07-24 11:06:52 -0400633 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500634}
635
Josef Bacikf3465ca2008-11-12 14:19:50 -0500636/*
637 * same as comp_keys only with two btrfs_key's
638 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400639int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500640{
641 if (k1->objectid > k2->objectid)
642 return 1;
643 if (k1->objectid < k2->objectid)
644 return -1;
645 if (k1->type > k2->type)
646 return 1;
647 if (k1->type < k2->type)
648 return -1;
649 if (k1->offset > k2->offset)
650 return 1;
651 if (k1->offset < k2->offset)
652 return -1;
653 return 0;
654}
Chris Mason081e9572007-11-06 10:26:24 -0500655
Chris Masond352ac62008-09-29 15:18:18 -0400656/*
657 * this is used by the defrag code to go through all the
658 * leaves pointed to by a node and reallocate them so that
659 * disk order is close to key order
660 */
Chris Mason6702ed42007-08-07 16:15:09 -0400661int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400662 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400663 int start_slot, int cache_only, u64 *last_ret,
664 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400665{
Chris Mason6b800532007-10-15 16:17:34 -0400666 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400667 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400668 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400669 u64 search_start = *last_ret;
670 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400671 u64 other;
672 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400673 int end_slot;
674 int i;
675 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400676 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400677 int uptodate;
678 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500679 int progress_passed = 0;
680 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400681
Chris Mason5708b952007-10-25 15:43:18 -0400682 parent_level = btrfs_header_level(parent);
683 if (cache_only && parent_level != 1)
684 return 0;
685
Chris Masond3977122009-01-05 21:25:51 -0500686 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400687 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500688 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400689 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400690
Chris Mason6b800532007-10-15 16:17:34 -0400691 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400692 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400693 end_slot = parent_nritems;
694
695 if (parent_nritems == 1)
696 return 0;
697
Chris Masonb4ce94d2009-02-04 09:25:08 -0500698 btrfs_set_lock_blocking(parent);
699
Chris Mason6702ed42007-08-07 16:15:09 -0400700 for (i = start_slot; i < end_slot; i++) {
701 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400702
Chris Mason081e9572007-11-06 10:26:24 -0500703 btrfs_node_key(parent, &disk_key, i);
704 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
705 continue;
706
707 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400708 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400709 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400710 if (last_block == 0)
711 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400712
Chris Mason6702ed42007-08-07 16:15:09 -0400713 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400714 other = btrfs_node_blockptr(parent, i - 1);
715 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400716 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400717 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400718 other = btrfs_node_blockptr(parent, i + 1);
719 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400720 }
Chris Masone9d0b132007-08-10 14:06:19 -0400721 if (close) {
722 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400723 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400724 }
Chris Mason6702ed42007-08-07 16:15:09 -0400725
Chris Mason6b800532007-10-15 16:17:34 -0400726 cur = btrfs_find_tree_block(root, blocknr, blocksize);
727 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -0400728 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -0400729 else
730 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400731 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400732 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400733 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400734 continue;
735 }
Chris Mason6b800532007-10-15 16:17:34 -0400736 if (!cur) {
737 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400738 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000739 if (!cur)
740 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400741 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400742 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400743 }
Chris Mason6702ed42007-08-07 16:15:09 -0400744 }
Chris Masone9d0b132007-08-10 14:06:19 -0400745 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400746 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400747
Chris Masone7a84562008-06-25 16:01:31 -0400748 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500749 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400750 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400751 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400752 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400753 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400754 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400755 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400756 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400757 break;
Yan252c38f2007-08-29 09:11:44 -0400758 }
Chris Masone7a84562008-06-25 16:01:31 -0400759 search_start = cur->start;
760 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400761 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400762 btrfs_tree_unlock(cur);
763 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400764 }
765 return err;
766}
767
Chris Mason74123bd2007-02-02 11:05:29 -0500768/*
769 * The leaf data grows from end-to-front in the node.
770 * this returns the address of the start of the last item,
771 * which is the stop of the leaf data stack
772 */
Chris Mason123abc82007-03-14 14:14:43 -0400773static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775{
Chris Mason5f39d392007-10-15 16:14:19 -0400776 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400778 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400779 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500780}
781
Chris Masonaa5d6be2007-02-28 16:35:06 -0500782
Chris Mason74123bd2007-02-02 11:05:29 -0500783/*
Chris Mason5f39d392007-10-15 16:14:19 -0400784 * search for key in the extent_buffer. The items start at offset p,
785 * and they are item_size apart. There are 'max' items in p.
786 *
Chris Mason74123bd2007-02-02 11:05:29 -0500787 * the slot in the array is returned via slot, and it points to
788 * the place where you would insert key if it is not found in
789 * the array.
790 *
791 * slot may point to max if the key is bigger than all of the keys
792 */
Chris Masone02119d2008-09-05 16:13:11 -0400793static noinline int generic_bin_search(struct extent_buffer *eb,
794 unsigned long p,
795 int item_size, struct btrfs_key *key,
796 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500797{
798 int low = 0;
799 int high = max;
800 int mid;
801 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400802 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400803 struct btrfs_disk_key unaligned;
804 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400805 char *kaddr = NULL;
806 unsigned long map_start = 0;
807 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400808 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500809
Chris Masond3977122009-01-05 21:25:51 -0500810 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500811 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400812 offset = p + mid * item_size;
813
Chris Masona6591712011-07-19 12:04:14 -0400814 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400815 (offset + sizeof(struct btrfs_disk_key)) >
816 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500817
818 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400819 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400820 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400821
Chris Mason479965d2007-10-15 16:14:27 -0400822 if (!err) {
823 tmp = (struct btrfs_disk_key *)(kaddr + offset -
824 map_start);
825 } else {
826 read_extent_buffer(eb, &unaligned,
827 offset, sizeof(unaligned));
828 tmp = &unaligned;
829 }
830
Chris Mason5f39d392007-10-15 16:14:19 -0400831 } else {
832 tmp = (struct btrfs_disk_key *)(kaddr + offset -
833 map_start);
834 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500835 ret = comp_keys(tmp, key);
836
837 if (ret < 0)
838 low = mid + 1;
839 else if (ret > 0)
840 high = mid;
841 else {
842 *slot = mid;
843 return 0;
844 }
845 }
846 *slot = low;
847 return 1;
848}
849
Chris Mason97571fd2007-02-24 13:39:08 -0500850/*
851 * simple bin_search frontend that does the right thing for
852 * leaves vs nodes
853 */
Chris Mason5f39d392007-10-15 16:14:19 -0400854static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
855 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500856{
Chris Mason5f39d392007-10-15 16:14:19 -0400857 if (level == 0) {
858 return generic_bin_search(eb,
859 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400860 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400861 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400862 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500863 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400864 return generic_bin_search(eb,
865 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400866 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400867 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400868 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500869 }
870 return -1;
871}
872
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
874 int level, int *slot)
875{
876 return bin_search(eb, key, level, slot);
877}
878
Yan, Zhengf0486c62010-05-16 10:46:25 -0400879static void root_add_used(struct btrfs_root *root, u32 size)
880{
881 spin_lock(&root->accounting_lock);
882 btrfs_set_root_used(&root->root_item,
883 btrfs_root_used(&root->root_item) + size);
884 spin_unlock(&root->accounting_lock);
885}
886
887static void root_sub_used(struct btrfs_root *root, u32 size)
888{
889 spin_lock(&root->accounting_lock);
890 btrfs_set_root_used(&root->root_item,
891 btrfs_root_used(&root->root_item) - size);
892 spin_unlock(&root->accounting_lock);
893}
894
Chris Masond352ac62008-09-29 15:18:18 -0400895/* given a node and slot number, this reads the blocks it points to. The
896 * extent buffer is returned with a reference taken (but unlocked).
897 * NULL is returned on error.
898 */
Chris Masone02119d2008-09-05 16:13:11 -0400899static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400900 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500901{
Chris Masonca7a79a2008-05-12 12:59:19 -0400902 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500903 if (slot < 0)
904 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400905 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500906 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400907
908 BUG_ON(level == 0);
909
Chris Masondb945352007-10-15 16:15:53 -0400910 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400911 btrfs_level_size(root, level - 1),
912 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500913}
914
Chris Masond352ac62008-09-29 15:18:18 -0400915/*
916 * node level balancing, used to make sure nodes are in proper order for
917 * item deletion. We balance from the top down, so we have to make sure
918 * that a deletion won't leave an node completely empty later on.
919 */
Chris Masone02119d2008-09-05 16:13:11 -0400920static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500921 struct btrfs_root *root,
922 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500923{
Chris Mason5f39d392007-10-15 16:14:19 -0400924 struct extent_buffer *right = NULL;
925 struct extent_buffer *mid;
926 struct extent_buffer *left = NULL;
927 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500928 int ret = 0;
929 int wret;
930 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500931 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500932 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500933
934 if (level == 0)
935 return 0;
936
Chris Mason5f39d392007-10-15 16:14:19 -0400937 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500938
Chris Masonbd681512011-07-16 15:23:14 -0400939 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
940 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500941 WARN_ON(btrfs_header_generation(mid) != trans->transid);
942
Chris Mason1d4f8a02007-03-13 09:28:32 -0400943 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500944
Li Zefana05a9bb2011-09-06 16:55:34 +0800945 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400946 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800947 pslot = path->slots[level + 1];
948 }
Chris Masonbb803952007-03-01 12:04:21 -0500949
Chris Mason40689472007-03-17 14:29:23 -0400950 /*
951 * deal with the case where there is only one pointer in the root
952 * by promoting the node below to a root
953 */
Chris Mason5f39d392007-10-15 16:14:19 -0400954 if (!parent) {
955 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500956
Chris Mason5f39d392007-10-15 16:14:19 -0400957 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500958 return 0;
959
960 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400961 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -0700962 if (!child) {
963 ret = -EROFS;
964 btrfs_std_error(root->fs_info, ret);
965 goto enospc;
966 }
967
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500969 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400970 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400971 if (ret) {
972 btrfs_tree_unlock(child);
973 free_extent_buffer(child);
974 goto enospc;
975 }
Yan2f375ab2008-02-01 14:58:07 -0500976
Chris Mason240f62c2011-03-23 14:54:42 -0400977 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400978
Chris Mason0b86a832008-03-24 15:01:56 -0400979 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400980 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500981
Chris Mason925baed2008-06-25 16:01:30 -0400982 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500983 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400984 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400985 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500986 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400987 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400988
989 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200990 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500991 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -0500992 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400993 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500994 }
Chris Mason5f39d392007-10-15 16:14:19 -0400995 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400996 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500997 return 0;
998
Andi Kleen559af822010-10-29 15:14:37 -0400999 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001000
Chris Mason5f39d392007-10-15 16:14:19 -04001001 left = read_node_slot(root, parent, pslot - 1);
1002 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001003 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001004 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001005 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001006 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 if (wret) {
1008 ret = wret;
1009 goto enospc;
1010 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001011 }
Chris Mason5f39d392007-10-15 16:14:19 -04001012 right = read_node_slot(root, parent, pslot + 1);
1013 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001014 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001015 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001016 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001017 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001018 if (wret) {
1019 ret = wret;
1020 goto enospc;
1021 }
1022 }
1023
1024 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001025 if (left) {
1026 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001027 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001028 if (wret < 0)
1029 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001030 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001031 }
Chris Mason79f95c82007-03-01 15:16:26 -05001032
1033 /*
1034 * then try to empty the right most buffer into the middle
1035 */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001037 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001038 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001039 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001040 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001041 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001042 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001043 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001044 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001045 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001046 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001047 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001048 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001049 struct btrfs_disk_key right_key;
1050 btrfs_node_key(right, &right_key, 0);
1051 btrfs_set_node_key(parent, &right_key, pslot + 1);
1052 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001053 }
1054 }
Chris Mason5f39d392007-10-15 16:14:19 -04001055 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001056 /*
1057 * we're not allowed to leave a node with one item in the
1058 * tree during a delete. A deletion from lower in the tree
1059 * could try to delete the only pointer in this node.
1060 * So, pull some keys from the left.
1061 * There has to be a left pointer at this point because
1062 * otherwise we would have pulled some pointers from the
1063 * right
1064 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001065 if (!left) {
1066 ret = -EROFS;
1067 btrfs_std_error(root->fs_info, ret);
1068 goto enospc;
1069 }
Chris Mason5f39d392007-10-15 16:14:19 -04001070 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001071 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001072 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001073 goto enospc;
1074 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001075 if (wret == 1) {
1076 wret = push_node_left(trans, root, left, mid, 1);
1077 if (wret < 0)
1078 ret = wret;
1079 }
Chris Mason79f95c82007-03-01 15:16:26 -05001080 BUG_ON(wret == 1);
1081 }
Chris Mason5f39d392007-10-15 16:14:19 -04001082 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001083 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001084 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001085 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001086 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001087 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001088 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001089 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001090 } else {
1091 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001092 struct btrfs_disk_key mid_key;
1093 btrfs_node_key(mid, &mid_key, 0);
1094 btrfs_set_node_key(parent, &mid_key, pslot);
1095 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001096 }
Chris Masonbb803952007-03-01 12:04:21 -05001097
Chris Mason79f95c82007-03-01 15:16:26 -05001098 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001099 if (left) {
1100 if (btrfs_header_nritems(left) > orig_slot) {
1101 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001102 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001103 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001104 path->slots[level + 1] -= 1;
1105 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001106 if (mid) {
1107 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001108 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001109 }
Chris Masonbb803952007-03-01 12:04:21 -05001110 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001111 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001112 path->slots[level] = orig_slot;
1113 }
1114 }
Chris Mason79f95c82007-03-01 15:16:26 -05001115 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001116 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001117 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001118 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001119enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001120 if (right) {
1121 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001122 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001123 }
1124 if (left) {
1125 if (path->nodes[level] != left)
1126 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001127 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001128 }
Chris Masonbb803952007-03-01 12:04:21 -05001129 return ret;
1130}
1131
Chris Masond352ac62008-09-29 15:18:18 -04001132/* Node balancing for insertion. Here we only split or push nodes around
1133 * when they are completely full. This is also done top down, so we
1134 * have to be pessimistic.
1135 */
Chris Masond3977122009-01-05 21:25:51 -05001136static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001137 struct btrfs_root *root,
1138 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001139{
Chris Mason5f39d392007-10-15 16:14:19 -04001140 struct extent_buffer *right = NULL;
1141 struct extent_buffer *mid;
1142 struct extent_buffer *left = NULL;
1143 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001144 int ret = 0;
1145 int wret;
1146 int pslot;
1147 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001148
1149 if (level == 0)
1150 return 1;
1151
Chris Mason5f39d392007-10-15 16:14:19 -04001152 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001153 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001154
Li Zefana05a9bb2011-09-06 16:55:34 +08001155 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001156 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001157 pslot = path->slots[level + 1];
1158 }
Chris Masone66f7092007-04-20 13:16:02 -04001159
Chris Mason5f39d392007-10-15 16:14:19 -04001160 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001161 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001162
Chris Mason5f39d392007-10-15 16:14:19 -04001163 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001164
1165 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001166 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001167 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001168
1169 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001170 btrfs_set_lock_blocking(left);
1171
Chris Mason5f39d392007-10-15 16:14:19 -04001172 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001173 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1174 wret = 1;
1175 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001176 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001177 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001178 if (ret)
1179 wret = 1;
1180 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001182 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001183 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001184 }
Chris Masone66f7092007-04-20 13:16:02 -04001185 if (wret < 0)
1186 ret = wret;
1187 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001188 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001189 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001190 btrfs_node_key(mid, &disk_key, 0);
1191 btrfs_set_node_key(parent, &disk_key, pslot);
1192 btrfs_mark_buffer_dirty(parent);
1193 if (btrfs_header_nritems(left) > orig_slot) {
1194 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001195 path->slots[level + 1] -= 1;
1196 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001197 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001198 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001199 } else {
1200 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001201 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001202 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001203 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001204 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001205 }
Chris Masone66f7092007-04-20 13:16:02 -04001206 return 0;
1207 }
Chris Mason925baed2008-06-25 16:01:30 -04001208 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001209 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001210 }
Chris Mason925baed2008-06-25 16:01:30 -04001211 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001212
1213 /*
1214 * then try to empty the right most buffer into the middle
1215 */
Chris Mason5f39d392007-10-15 16:14:19 -04001216 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001217 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001218
Chris Mason925baed2008-06-25 16:01:30 -04001219 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001220 btrfs_set_lock_blocking(right);
1221
Chris Mason5f39d392007-10-15 16:14:19 -04001222 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001223 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1224 wret = 1;
1225 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001226 ret = btrfs_cow_block(trans, root, right,
1227 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001228 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001229 if (ret)
1230 wret = 1;
1231 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001232 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001233 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001234 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001235 }
Chris Masone66f7092007-04-20 13:16:02 -04001236 if (wret < 0)
1237 ret = wret;
1238 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001239 struct btrfs_disk_key disk_key;
1240
1241 btrfs_node_key(right, &disk_key, 0);
1242 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1243 btrfs_mark_buffer_dirty(parent);
1244
1245 if (btrfs_header_nritems(mid) <= orig_slot) {
1246 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001247 path->slots[level + 1] += 1;
1248 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001249 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001250 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001251 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001252 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001253 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001254 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001255 }
Chris Masone66f7092007-04-20 13:16:02 -04001256 return 0;
1257 }
Chris Mason925baed2008-06-25 16:01:30 -04001258 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001259 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001260 }
Chris Masone66f7092007-04-20 13:16:02 -04001261 return 1;
1262}
1263
Chris Mason74123bd2007-02-02 11:05:29 -05001264/*
Chris Masond352ac62008-09-29 15:18:18 -04001265 * readahead one full node of leaves, finding things that are close
1266 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 */
Chris Masonc8c42862009-04-03 10:14:18 -04001268static void reada_for_search(struct btrfs_root *root,
1269 struct btrfs_path *path,
1270 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001271{
Chris Mason5f39d392007-10-15 16:14:19 -04001272 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001273 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001274 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001275 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001276 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001277 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001278 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001279 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001280 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001281 u32 nr;
1282 u32 blocksize;
1283 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001284
Chris Masona6b6e752007-10-15 16:22:39 -04001285 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001286 return;
1287
Chris Mason6702ed42007-08-07 16:15:09 -04001288 if (!path->nodes[level])
1289 return;
1290
Chris Mason5f39d392007-10-15 16:14:19 -04001291 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001292
Chris Mason3c69fae2007-08-07 15:52:22 -04001293 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001294 blocksize = btrfs_level_size(root, level - 1);
1295 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001296 if (eb) {
1297 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001298 return;
1299 }
1300
Chris Masona7175312009-01-22 09:23:10 -05001301 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001302
Chris Mason5f39d392007-10-15 16:14:19 -04001303 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001304 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001305
Chris Masond3977122009-01-05 21:25:51 -05001306 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001307 if (direction < 0) {
1308 if (nr == 0)
1309 break;
1310 nr--;
1311 } else if (direction > 0) {
1312 nr++;
1313 if (nr >= nritems)
1314 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001315 }
Chris Mason01f46652007-12-21 16:24:26 -05001316 if (path->reada < 0 && objectid) {
1317 btrfs_node_key(node, &disk_key, nr);
1318 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1319 break;
1320 }
Chris Mason6b800532007-10-15 16:17:34 -04001321 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001322 if ((search <= target && target - search <= 65536) ||
1323 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001324 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001325 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001326 nread += blocksize;
1327 }
1328 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001329 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001330 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001331 }
1332}
Chris Mason925baed2008-06-25 16:01:30 -04001333
Chris Masond352ac62008-09-29 15:18:18 -04001334/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001335 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1336 * cache
1337 */
1338static noinline int reada_for_balance(struct btrfs_root *root,
1339 struct btrfs_path *path, int level)
1340{
1341 int slot;
1342 int nritems;
1343 struct extent_buffer *parent;
1344 struct extent_buffer *eb;
1345 u64 gen;
1346 u64 block1 = 0;
1347 u64 block2 = 0;
1348 int ret = 0;
1349 int blocksize;
1350
Chris Mason8c594ea2009-04-20 15:50:10 -04001351 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001352 if (!parent)
1353 return 0;
1354
1355 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001356 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001357 blocksize = btrfs_level_size(root, level);
1358
1359 if (slot > 0) {
1360 block1 = btrfs_node_blockptr(parent, slot - 1);
1361 gen = btrfs_node_ptr_generation(parent, slot - 1);
1362 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001363 /*
1364 * if we get -eagain from btrfs_buffer_uptodate, we
1365 * don't want to return eagain here. That will loop
1366 * forever
1367 */
1368 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001369 block1 = 0;
1370 free_extent_buffer(eb);
1371 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001372 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001373 block2 = btrfs_node_blockptr(parent, slot + 1);
1374 gen = btrfs_node_ptr_generation(parent, slot + 1);
1375 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001376 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001377 block2 = 0;
1378 free_extent_buffer(eb);
1379 }
1380 if (block1 || block2) {
1381 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001382
1383 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001384 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001385
1386 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001387 if (block1)
1388 readahead_tree_block(root, block1, blocksize, 0);
1389 if (block2)
1390 readahead_tree_block(root, block2, blocksize, 0);
1391
1392 if (block1) {
1393 eb = read_tree_block(root, block1, blocksize, 0);
1394 free_extent_buffer(eb);
1395 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001396 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001397 eb = read_tree_block(root, block2, blocksize, 0);
1398 free_extent_buffer(eb);
1399 }
1400 }
1401 return ret;
1402}
1403
1404
1405/*
Chris Masond3977122009-01-05 21:25:51 -05001406 * when we walk down the tree, it is usually safe to unlock the higher layers
1407 * in the tree. The exceptions are when our path goes through slot 0, because
1408 * operations on the tree might require changing key pointers higher up in the
1409 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001410 *
Chris Masond3977122009-01-05 21:25:51 -05001411 * callers might also have set path->keep_locks, which tells this code to keep
1412 * the lock if the path points to the last slot in the block. This is part of
1413 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001414 *
Chris Masond3977122009-01-05 21:25:51 -05001415 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1416 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001417 */
Chris Masone02119d2008-09-05 16:13:11 -04001418static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001419 int lowest_unlock, int min_write_lock_level,
1420 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001421{
1422 int i;
1423 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001424 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001425 struct extent_buffer *t;
1426
1427 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1428 if (!path->nodes[i])
1429 break;
1430 if (!path->locks[i])
1431 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001432 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001433 skip_level = i + 1;
1434 continue;
1435 }
Chris Mason051e1b92008-06-25 16:01:30 -04001436 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001437 u32 nritems;
1438 t = path->nodes[i];
1439 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001440 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001441 skip_level = i + 1;
1442 continue;
1443 }
1444 }
Chris Mason051e1b92008-06-25 16:01:30 -04001445 if (skip_level < i && i >= lowest_unlock)
1446 no_skips = 1;
1447
Chris Mason925baed2008-06-25 16:01:30 -04001448 t = path->nodes[i];
1449 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001450 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001451 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001452 if (write_lock_level &&
1453 i > min_write_lock_level &&
1454 i <= *write_lock_level) {
1455 *write_lock_level = i - 1;
1456 }
Chris Mason925baed2008-06-25 16:01:30 -04001457 }
1458 }
1459}
1460
Chris Mason3c69fae2007-08-07 15:52:22 -04001461/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001462 * This releases any locks held in the path starting at level and
1463 * going all the way up to the root.
1464 *
1465 * btrfs_search_slot will keep the lock held on higher nodes in a few
1466 * corner cases, such as COW of the block at slot zero in the node. This
1467 * ignores those rules, and it should only be called when there are no
1468 * more updates to be done higher up in the tree.
1469 */
1470noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1471{
1472 int i;
1473
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001475 return;
1476
1477 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1478 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001479 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001480 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001481 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001482 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001483 path->locks[i] = 0;
1484 }
1485}
1486
1487/*
Chris Masonc8c42862009-04-03 10:14:18 -04001488 * helper function for btrfs_search_slot. The goal is to find a block
1489 * in cache without setting the path to blocking. If we find the block
1490 * we return zero and the path is unchanged.
1491 *
1492 * If we can't find the block, we set the path blocking and do some
1493 * reada. -EAGAIN is returned and the search must be repeated.
1494 */
1495static int
1496read_block_for_search(struct btrfs_trans_handle *trans,
1497 struct btrfs_root *root, struct btrfs_path *p,
1498 struct extent_buffer **eb_ret, int level, int slot,
1499 struct btrfs_key *key)
1500{
1501 u64 blocknr;
1502 u64 gen;
1503 u32 blocksize;
1504 struct extent_buffer *b = *eb_ret;
1505 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001506 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001507
1508 blocknr = btrfs_node_blockptr(b, slot);
1509 gen = btrfs_node_ptr_generation(b, slot);
1510 blocksize = btrfs_level_size(root, level - 1);
1511
1512 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001513 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04001514 /* first we do an atomic uptodate check */
1515 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
1516 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001517 /*
1518 * we found an up to date block without
1519 * sleeping, return
1520 * right away
1521 */
1522 *eb_ret = tmp;
1523 return 0;
1524 }
1525 /* the pages were up to date, but we failed
1526 * the generation number check. Do a full
1527 * read for the generation number that is correct.
1528 * We must do this without dropping locks so
1529 * we can trust our generation number
1530 */
1531 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001532 btrfs_set_path_blocking(p);
1533
Chris Masonb9fab912012-05-06 07:23:47 -04001534 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04001535 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04001536 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001537 *eb_ret = tmp;
1538 return 0;
1539 }
1540 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001541 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001542 return -EIO;
1543 }
Chris Masonc8c42862009-04-03 10:14:18 -04001544 }
1545
1546 /*
1547 * reduce lock contention at high levels
1548 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001549 * we read. Don't release the lock on the current
1550 * level because we need to walk this node to figure
1551 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001552 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001553 btrfs_unlock_up_safe(p, level + 1);
1554 btrfs_set_path_blocking(p);
1555
Chris Masoncb449212010-10-24 11:01:27 -04001556 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001557 if (p->reada)
1558 reada_for_search(root, p, level, slot, key->objectid);
1559
David Sterbab3b4aa72011-04-21 01:20:15 +02001560 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001561
1562 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001563 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001564 if (tmp) {
1565 /*
1566 * If the read above didn't mark this buffer up to date,
1567 * it will never end up being up to date. Set ret to EIO now
1568 * and give up so that our caller doesn't loop forever
1569 * on our EAGAINs.
1570 */
Chris Masonb9fab912012-05-06 07:23:47 -04001571 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04001572 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001573 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001574 }
1575 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001576}
1577
1578/*
1579 * helper function for btrfs_search_slot. This does all of the checks
1580 * for node-level blocks and does any balancing required based on
1581 * the ins_len.
1582 *
1583 * If no extra work was required, zero is returned. If we had to
1584 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1585 * start over
1586 */
1587static int
1588setup_nodes_for_search(struct btrfs_trans_handle *trans,
1589 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001590 struct extent_buffer *b, int level, int ins_len,
1591 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001592{
1593 int ret;
1594 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1595 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1596 int sret;
1597
Chris Masonbd681512011-07-16 15:23:14 -04001598 if (*write_lock_level < level + 1) {
1599 *write_lock_level = level + 1;
1600 btrfs_release_path(p);
1601 goto again;
1602 }
1603
Chris Masonc8c42862009-04-03 10:14:18 -04001604 sret = reada_for_balance(root, p, level);
1605 if (sret)
1606 goto again;
1607
1608 btrfs_set_path_blocking(p);
1609 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001610 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001611
1612 BUG_ON(sret > 0);
1613 if (sret) {
1614 ret = sret;
1615 goto done;
1616 }
1617 b = p->nodes[level];
1618 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001619 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001620 int sret;
1621
Chris Masonbd681512011-07-16 15:23:14 -04001622 if (*write_lock_level < level + 1) {
1623 *write_lock_level = level + 1;
1624 btrfs_release_path(p);
1625 goto again;
1626 }
1627
Chris Masonc8c42862009-04-03 10:14:18 -04001628 sret = reada_for_balance(root, p, level);
1629 if (sret)
1630 goto again;
1631
1632 btrfs_set_path_blocking(p);
1633 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001634 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001635
1636 if (sret) {
1637 ret = sret;
1638 goto done;
1639 }
1640 b = p->nodes[level];
1641 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001642 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001643 goto again;
1644 }
1645 BUG_ON(btrfs_header_nritems(b) == 1);
1646 }
1647 return 0;
1648
1649again:
1650 ret = -EAGAIN;
1651done:
1652 return ret;
1653}
1654
1655/*
Chris Mason74123bd2007-02-02 11:05:29 -05001656 * look for key in the tree. path is filled in with nodes along the way
1657 * if key is found, we return zero and you can find the item in the leaf
1658 * level of the path (level 0)
1659 *
1660 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001661 * be inserted, and 1 is returned. If there are other errors during the
1662 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001663 *
1664 * if ins_len > 0, nodes and leaves will be split as we walk down the
1665 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1666 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001667 */
Chris Masone089f052007-03-16 16:20:31 -04001668int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1669 *root, struct btrfs_key *key, struct btrfs_path *p, int
1670 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001671{
Chris Mason5f39d392007-10-15 16:14:19 -04001672 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001673 int slot;
1674 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001675 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001676 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001677 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001678 int root_lock;
1679 /* everything at write_lock_level or lower must be write locked */
1680 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001681 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001682 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001683
Chris Mason6702ed42007-08-07 16:15:09 -04001684 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001685 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001686 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001687
Chris Masonbd681512011-07-16 15:23:14 -04001688 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001689 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001690
Chris Masonbd681512011-07-16 15:23:14 -04001691 /* when we are removing items, we might have to go up to level
1692 * two as we update tree pointers Make sure we keep write
1693 * for those levels as well
1694 */
1695 write_lock_level = 2;
1696 } else if (ins_len > 0) {
1697 /*
1698 * for inserting items, make sure we have a write lock on
1699 * level 1 so we can update keys
1700 */
1701 write_lock_level = 1;
1702 }
1703
1704 if (!cow)
1705 write_lock_level = -1;
1706
1707 if (cow && (p->keep_locks || p->lowest_level))
1708 write_lock_level = BTRFS_MAX_LEVEL;
1709
Chris Masonf7c79f32012-03-19 15:54:38 -04001710 min_write_lock_level = write_lock_level;
1711
Chris Masonbb803952007-03-01 12:04:21 -05001712again:
Chris Masonbd681512011-07-16 15:23:14 -04001713 /*
1714 * we try very hard to do read locks on the root
1715 */
1716 root_lock = BTRFS_READ_LOCK;
1717 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001718 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001719 /*
1720 * the commit roots are read only
1721 * so we always do read locks
1722 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001723 b = root->commit_root;
1724 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001725 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001727 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001728 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001729 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001730 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001731 level = btrfs_header_level(b);
1732 } else {
1733 /* we don't know the level of the root node
1734 * until we actually have it read locked
1735 */
1736 b = btrfs_read_lock_root_node(root);
1737 level = btrfs_header_level(b);
1738 if (level <= write_lock_level) {
1739 /* whoops, must trade for write lock */
1740 btrfs_tree_read_unlock(b);
1741 free_extent_buffer(b);
1742 b = btrfs_lock_root_node(root);
1743 root_lock = BTRFS_WRITE_LOCK;
1744
1745 /* the level might have changed, check again */
1746 level = btrfs_header_level(b);
1747 }
1748 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001749 }
Chris Masonbd681512011-07-16 15:23:14 -04001750 p->nodes[level] = b;
1751 if (!p->skip_locking)
1752 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001753
Chris Masoneb60cea2007-02-02 09:18:22 -05001754 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001755 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001756
1757 /*
1758 * setup the path here so we can release it under lock
1759 * contention with the cow code
1760 */
Chris Mason02217ed2007-03-02 16:08:05 -05001761 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001762 /*
1763 * if we don't really need to cow this block
1764 * then we don't want to set the path blocking,
1765 * so we test it here
1766 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001767 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001768 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001769
Chris Masonb4ce94d2009-02-04 09:25:08 -05001770 btrfs_set_path_blocking(p);
1771
Chris Masonbd681512011-07-16 15:23:14 -04001772 /*
1773 * must have write locks on this node and the
1774 * parent
1775 */
1776 if (level + 1 > write_lock_level) {
1777 write_lock_level = level + 1;
1778 btrfs_release_path(p);
1779 goto again;
1780 }
1781
Yan Zheng33c66f42009-07-22 09:59:00 -04001782 err = btrfs_cow_block(trans, root, b,
1783 p->nodes[level + 1],
1784 p->slots[level + 1], &b);
1785 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001786 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001787 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001788 }
Chris Mason02217ed2007-03-02 16:08:05 -05001789 }
Chris Mason65b51a02008-08-01 15:11:20 -04001790cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001791 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001792
Chris Masoneb60cea2007-02-02 09:18:22 -05001793 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001794 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001795
1796 /*
1797 * we have a lock on b and as long as we aren't changing
1798 * the tree, there is no way to for the items in b to change.
1799 * It is safe to drop the lock on our parent before we
1800 * go through the expensive btree search on b.
1801 *
1802 * If cow is true, then we might be changing slot zero,
1803 * which may require changing the parent. So, we can't
1804 * drop the lock until after we know which slot we're
1805 * operating on.
1806 */
1807 if (!cow)
1808 btrfs_unlock_up_safe(p, level + 1);
1809
Chris Mason5f39d392007-10-15 16:14:19 -04001810 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001811
Chris Mason5f39d392007-10-15 16:14:19 -04001812 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001813 int dec = 0;
1814 if (ret && slot > 0) {
1815 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001816 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001817 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001820 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001822 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001823 if (err) {
1824 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001825 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001826 }
Chris Masonc8c42862009-04-03 10:14:18 -04001827 b = p->nodes[level];
1828 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001829
Chris Masonbd681512011-07-16 15:23:14 -04001830 /*
1831 * slot 0 is special, if we change the key
1832 * we have to update the parent pointer
1833 * which means we must have a write lock
1834 * on the parent
1835 */
1836 if (slot == 0 && cow &&
1837 write_lock_level < level + 1) {
1838 write_lock_level = level + 1;
1839 btrfs_release_path(p);
1840 goto again;
1841 }
1842
Chris Masonf7c79f32012-03-19 15:54:38 -04001843 unlock_up(p, level, lowest_unlock,
1844 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001845
Chris Mason925baed2008-06-25 16:01:30 -04001846 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001847 if (dec)
1848 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001849 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001850 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001851
Yan Zheng33c66f42009-07-22 09:59:00 -04001852 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001853 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001854 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001855 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001856 if (err) {
1857 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001858 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001859 }
Chris Mason76a05b32009-05-14 13:24:30 -04001860
Chris Masonb4ce94d2009-02-04 09:25:08 -05001861 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001862 level = btrfs_header_level(b);
1863 if (level <= write_lock_level) {
1864 err = btrfs_try_tree_write_lock(b);
1865 if (!err) {
1866 btrfs_set_path_blocking(p);
1867 btrfs_tree_lock(b);
1868 btrfs_clear_path_blocking(p, b,
1869 BTRFS_WRITE_LOCK);
1870 }
1871 p->locks[level] = BTRFS_WRITE_LOCK;
1872 } else {
1873 err = btrfs_try_tree_read_lock(b);
1874 if (!err) {
1875 btrfs_set_path_blocking(p);
1876 btrfs_tree_read_lock(b);
1877 btrfs_clear_path_blocking(p, b,
1878 BTRFS_READ_LOCK);
1879 }
1880 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001881 }
Chris Masonbd681512011-07-16 15:23:14 -04001882 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001883 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001884 } else {
1885 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001886 if (ins_len > 0 &&
1887 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001888 if (write_lock_level < 1) {
1889 write_lock_level = 1;
1890 btrfs_release_path(p);
1891 goto again;
1892 }
1893
Chris Masonb4ce94d2009-02-04 09:25:08 -05001894 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001895 err = split_leaf(trans, root, key,
1896 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001897 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001898
Yan Zheng33c66f42009-07-22 09:59:00 -04001899 BUG_ON(err > 0);
1900 if (err) {
1901 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001902 goto done;
1903 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001904 }
Chris Mason459931e2008-12-10 09:10:46 -05001905 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04001906 unlock_up(p, level, lowest_unlock,
1907 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04001908 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001909 }
1910 }
Chris Mason65b51a02008-08-01 15:11:20 -04001911 ret = 1;
1912done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001913 /*
1914 * we don't really know what they plan on doing with the path
1915 * from here on, so for now just mark it as blocking
1916 */
Chris Masonb9473432009-03-13 11:00:37 -04001917 if (!p->leave_spinning)
1918 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001919 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001920 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001921 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001922}
1923
Chris Mason74123bd2007-02-02 11:05:29 -05001924/*
1925 * adjust the pointers going up the tree, starting at level
1926 * making sure the right key of each node is points to 'key'.
1927 * This is used after shifting pointers to the left, so it stops
1928 * fixing up pointers when a given leaf/node is not in slot 0 of the
1929 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001930 *
Chris Mason74123bd2007-02-02 11:05:29 -05001931 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001932static void fixup_low_keys(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root, struct btrfs_path *path,
1934 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001935{
1936 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001937 struct extent_buffer *t;
1938
Chris Mason234b63a2007-03-13 10:46:10 -04001939 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001940 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001941 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001942 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001943 t = path->nodes[i];
1944 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001945 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946 if (tslot != 0)
1947 break;
1948 }
1949}
1950
Chris Mason74123bd2007-02-02 11:05:29 -05001951/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001952 * update item key.
1953 *
1954 * This function isn't completely safe. It's the caller's responsibility
1955 * that the new key won't break the order
1956 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001957void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1958 struct btrfs_root *root, struct btrfs_path *path,
1959 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001960{
1961 struct btrfs_disk_key disk_key;
1962 struct extent_buffer *eb;
1963 int slot;
1964
1965 eb = path->nodes[0];
1966 slot = path->slots[0];
1967 if (slot > 0) {
1968 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001969 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001970 }
1971 if (slot < btrfs_header_nritems(eb) - 1) {
1972 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001973 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001974 }
1975
1976 btrfs_cpu_key_to_disk(&disk_key, new_key);
1977 btrfs_set_item_key(eb, &disk_key, slot);
1978 btrfs_mark_buffer_dirty(eb);
1979 if (slot == 0)
1980 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001981}
1982
1983/*
Chris Mason74123bd2007-02-02 11:05:29 -05001984 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001985 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001986 *
1987 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1988 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001989 */
Chris Mason98ed5172008-01-03 10:01:48 -05001990static int push_node_left(struct btrfs_trans_handle *trans,
1991 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001992 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001993{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001994 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001995 int src_nritems;
1996 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001997 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998
Chris Mason5f39d392007-10-15 16:14:19 -04001999 src_nritems = btrfs_header_nritems(src);
2000 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002001 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002002 WARN_ON(btrfs_header_generation(src) != trans->transid);
2003 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002004
Chris Masonbce4eae2008-04-24 14:42:46 -04002005 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002006 return 1;
2007
Chris Masond3977122009-01-05 21:25:51 -05002008 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002009 return 1;
2010
Chris Masonbce4eae2008-04-24 14:42:46 -04002011 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002012 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002013 if (push_items < src_nritems) {
2014 /* leave at least 8 pointers in the node if
2015 * we aren't going to empty it
2016 */
2017 if (src_nritems - push_items < 8) {
2018 if (push_items <= 8)
2019 return 1;
2020 push_items -= 8;
2021 }
2022 }
2023 } else
2024 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002025
Chris Mason5f39d392007-10-15 16:14:19 -04002026 copy_extent_buffer(dst, src,
2027 btrfs_node_key_ptr_offset(dst_nritems),
2028 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002029 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002030
Chris Masonbb803952007-03-01 12:04:21 -05002031 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002032 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2033 btrfs_node_key_ptr_offset(push_items),
2034 (src_nritems - push_items) *
2035 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002036 }
Chris Mason5f39d392007-10-15 16:14:19 -04002037 btrfs_set_header_nritems(src, src_nritems - push_items);
2038 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2039 btrfs_mark_buffer_dirty(src);
2040 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002041
Chris Masonbb803952007-03-01 12:04:21 -05002042 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002043}
2044
Chris Mason97571fd2007-02-24 13:39:08 -05002045/*
Chris Mason79f95c82007-03-01 15:16:26 -05002046 * try to push data from one node into the next node right in the
2047 * tree.
2048 *
2049 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2050 * error, and > 0 if there was no room in the right hand block.
2051 *
2052 * this will only push up to 1/2 the contents of the left node over
2053 */
Chris Mason5f39d392007-10-15 16:14:19 -04002054static int balance_node_right(struct btrfs_trans_handle *trans,
2055 struct btrfs_root *root,
2056 struct extent_buffer *dst,
2057 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002058{
Chris Mason79f95c82007-03-01 15:16:26 -05002059 int push_items = 0;
2060 int max_push;
2061 int src_nritems;
2062 int dst_nritems;
2063 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002064
Chris Mason7bb86312007-12-11 09:25:06 -05002065 WARN_ON(btrfs_header_generation(src) != trans->transid);
2066 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2067
Chris Mason5f39d392007-10-15 16:14:19 -04002068 src_nritems = btrfs_header_nritems(src);
2069 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002070 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002071 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002072 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002073
Chris Masond3977122009-01-05 21:25:51 -05002074 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002075 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002076
2077 max_push = src_nritems / 2 + 1;
2078 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002079 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002080 return 1;
Yan252c38f2007-08-29 09:11:44 -04002081
Chris Mason79f95c82007-03-01 15:16:26 -05002082 if (max_push < push_items)
2083 push_items = max_push;
2084
Chris Mason5f39d392007-10-15 16:14:19 -04002085 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2086 btrfs_node_key_ptr_offset(0),
2087 (dst_nritems) *
2088 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002089
Chris Mason5f39d392007-10-15 16:14:19 -04002090 copy_extent_buffer(dst, src,
2091 btrfs_node_key_ptr_offset(0),
2092 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002093 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002094
Chris Mason5f39d392007-10-15 16:14:19 -04002095 btrfs_set_header_nritems(src, src_nritems - push_items);
2096 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002097
Chris Mason5f39d392007-10-15 16:14:19 -04002098 btrfs_mark_buffer_dirty(src);
2099 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002100
Chris Mason79f95c82007-03-01 15:16:26 -05002101 return ret;
2102}
2103
2104/*
Chris Mason97571fd2007-02-24 13:39:08 -05002105 * helper function to insert a new root level in the tree.
2106 * A new node is allocated, and a single item is inserted to
2107 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002108 *
2109 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002110 */
Chris Masond3977122009-01-05 21:25:51 -05002111static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002112 struct btrfs_root *root,
2113 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002114{
Chris Mason7bb86312007-12-11 09:25:06 -05002115 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002116 struct extent_buffer *lower;
2117 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002118 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002119 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002120
2121 BUG_ON(path->nodes[level]);
2122 BUG_ON(path->nodes[level-1] != root->node);
2123
Chris Mason7bb86312007-12-11 09:25:06 -05002124 lower = path->nodes[level-1];
2125 if (level == 1)
2126 btrfs_item_key(lower, &lower_key, 0);
2127 else
2128 btrfs_node_key(lower, &lower_key, 0);
2129
Zheng Yan31840ae2008-09-23 13:14:14 -04002130 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002131 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002132 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002133 if (IS_ERR(c))
2134 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002135
Yan, Zhengf0486c62010-05-16 10:46:25 -04002136 root_add_used(root, root->nodesize);
2137
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002139 btrfs_set_header_nritems(c, 1);
2140 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002141 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002143 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002144 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002145
Chris Mason5f39d392007-10-15 16:14:19 -04002146 write_extent_buffer(c, root->fs_info->fsid,
2147 (unsigned long)btrfs_header_fsid(c),
2148 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002149
2150 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2151 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2152 BTRFS_UUID_SIZE);
2153
Chris Mason5f39d392007-10-15 16:14:19 -04002154 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002155 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002156 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002157 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002158
2159 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002160
2161 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002162
Chris Mason925baed2008-06-25 16:01:30 -04002163 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002164 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002165
2166 /* the super has an extra ref to root->node */
2167 free_extent_buffer(old);
2168
Chris Mason0b86a832008-03-24 15:01:56 -04002169 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002170 extent_buffer_get(c);
2171 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002172 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002173 path->slots[level] = 0;
2174 return 0;
2175}
2176
Chris Mason74123bd2007-02-02 11:05:29 -05002177/*
2178 * worker function to insert a single pointer in a node.
2179 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002180 *
Chris Mason74123bd2007-02-02 11:05:29 -05002181 * slot and level indicate where you want the key to go, and
2182 * blocknr is the block the key points to.
2183 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002184static void insert_ptr(struct btrfs_trans_handle *trans,
2185 struct btrfs_root *root, struct btrfs_path *path,
2186 struct btrfs_disk_key *key, u64 bytenr,
2187 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002188{
Chris Mason5f39d392007-10-15 16:14:19 -04002189 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002190 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002191
2192 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002193 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002194 lower = path->nodes[level];
2195 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002196 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002197 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002198 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002199 memmove_extent_buffer(lower,
2200 btrfs_node_key_ptr_offset(slot + 1),
2201 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002202 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002203 }
Chris Mason5f39d392007-10-15 16:14:19 -04002204 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002205 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002206 WARN_ON(trans->transid == 0);
2207 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002208 btrfs_set_header_nritems(lower, nritems + 1);
2209 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002210}
2211
Chris Mason97571fd2007-02-24 13:39:08 -05002212/*
2213 * split the node at the specified level in path in two.
2214 * The path is corrected to point to the appropriate node after the split
2215 *
2216 * Before splitting this tries to make some room in the node by pushing
2217 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002218 *
2219 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002220 */
Chris Masone02119d2008-09-05 16:13:11 -04002221static noinline int split_node(struct btrfs_trans_handle *trans,
2222 struct btrfs_root *root,
2223 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002224{
Chris Mason5f39d392007-10-15 16:14:19 -04002225 struct extent_buffer *c;
2226 struct extent_buffer *split;
2227 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002228 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002229 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002230 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002233 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002234 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002235 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002236 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002237 if (ret)
2238 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002239 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002240 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002241 c = path->nodes[level];
2242 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002243 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002244 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002245 if (ret < 0)
2246 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002247 }
Chris Masone66f7092007-04-20 13:16:02 -04002248
Chris Mason5f39d392007-10-15 16:14:19 -04002249 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002250 mid = (c_nritems + 1) / 2;
2251 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002252
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002253 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002254 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002255 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002256 if (IS_ERR(split))
2257 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002258
Yan, Zhengf0486c62010-05-16 10:46:25 -04002259 root_add_used(root, root->nodesize);
2260
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002261 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002262 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002263 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002264 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002265 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002266 btrfs_set_header_owner(split, root->root_key.objectid);
2267 write_extent_buffer(split, root->fs_info->fsid,
2268 (unsigned long)btrfs_header_fsid(split),
2269 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002270 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2271 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2272 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002273
Chris Mason5f39d392007-10-15 16:14:19 -04002274
2275 copy_extent_buffer(split, c,
2276 btrfs_node_key_ptr_offset(0),
2277 btrfs_node_key_ptr_offset(mid),
2278 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2279 btrfs_set_header_nritems(split, c_nritems - mid);
2280 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002281 ret = 0;
2282
Chris Mason5f39d392007-10-15 16:14:19 -04002283 btrfs_mark_buffer_dirty(c);
2284 btrfs_mark_buffer_dirty(split);
2285
Jeff Mahoney143bede2012-03-01 14:56:26 +01002286 insert_ptr(trans, root, path, &disk_key, split->start,
2287 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002288
Chris Mason5de08d72007-02-24 06:24:44 -05002289 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002290 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002291 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002292 free_extent_buffer(c);
2293 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002294 path->slots[level + 1] += 1;
2295 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002296 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002297 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002298 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002299 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002300}
2301
Chris Mason74123bd2007-02-02 11:05:29 -05002302/*
2303 * how many bytes are required to store the items in a leaf. start
2304 * and nr indicate which items in the leaf to check. This totals up the
2305 * space used both by the item structs and the item data
2306 */
Chris Mason5f39d392007-10-15 16:14:19 -04002307static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002308{
2309 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002310 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002311 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002312
2313 if (!nr)
2314 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002315 data_len = btrfs_item_end_nr(l, start);
2316 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002317 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002318 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002319 return data_len;
2320}
2321
Chris Mason74123bd2007-02-02 11:05:29 -05002322/*
Chris Masond4dbff92007-04-04 14:08:15 -04002323 * The space between the end of the leaf items and
2324 * the start of the leaf data. IOW, how much room
2325 * the leaf has left for both items and data
2326 */
Chris Masond3977122009-01-05 21:25:51 -05002327noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002328 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002329{
Chris Mason5f39d392007-10-15 16:14:19 -04002330 int nritems = btrfs_header_nritems(leaf);
2331 int ret;
2332 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2333 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002334 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2335 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002336 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002337 leaf_space_used(leaf, 0, nritems), nritems);
2338 }
2339 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002340}
2341
Chris Mason99d8f832010-07-07 10:51:48 -04002342/*
2343 * min slot controls the lowest index we're willing to push to the
2344 * right. We'll push up to and including min_slot, but no lower
2345 */
Chris Mason44871b12009-03-13 10:04:31 -04002346static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2347 struct btrfs_root *root,
2348 struct btrfs_path *path,
2349 int data_size, int empty,
2350 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002351 int free_space, u32 left_nritems,
2352 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002353{
Chris Mason5f39d392007-10-15 16:14:19 -04002354 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002355 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002356 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002357 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002358 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002359 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002360 int push_space = 0;
2361 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002362 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002363 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002364 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002365 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002366 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002367
Chris Masoncfed81a2012-03-03 07:40:03 -05002368 btrfs_init_map_token(&token);
2369
Chris Mason34a38212007-11-07 13:31:03 -05002370 if (empty)
2371 nr = 0;
2372 else
Chris Mason99d8f832010-07-07 10:51:48 -04002373 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002374
Zheng Yan31840ae2008-09-23 13:14:14 -04002375 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002376 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002377
Chris Mason44871b12009-03-13 10:04:31 -04002378 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002379 i = left_nritems - 1;
2380 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002381 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002382
Zheng Yan31840ae2008-09-23 13:14:14 -04002383 if (!empty && push_items > 0) {
2384 if (path->slots[0] > i)
2385 break;
2386 if (path->slots[0] == i) {
2387 int space = btrfs_leaf_free_space(root, left);
2388 if (space + push_space * 2 > free_space)
2389 break;
2390 }
2391 }
2392
Chris Mason00ec4c52007-02-24 12:47:20 -05002393 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002394 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002395
Chris Masondb945352007-10-15 16:15:53 -04002396 this_item_size = btrfs_item_size(left, item);
2397 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002398 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002399
Chris Mason00ec4c52007-02-24 12:47:20 -05002400 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002401 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002402 if (i == 0)
2403 break;
2404 i--;
Chris Masondb945352007-10-15 16:15:53 -04002405 }
Chris Mason5f39d392007-10-15 16:14:19 -04002406
Chris Mason925baed2008-06-25 16:01:30 -04002407 if (push_items == 0)
2408 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002409
Chris Mason34a38212007-11-07 13:31:03 -05002410 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002411 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002412
Chris Mason00ec4c52007-02-24 12:47:20 -05002413 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002414 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002415
Chris Mason5f39d392007-10-15 16:14:19 -04002416 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002417 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002418
Chris Mason00ec4c52007-02-24 12:47:20 -05002419 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002420 data_end = leaf_data_end(root, right);
2421 memmove_extent_buffer(right,
2422 btrfs_leaf_data(right) + data_end - push_space,
2423 btrfs_leaf_data(right) + data_end,
2424 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2425
Chris Mason00ec4c52007-02-24 12:47:20 -05002426 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002427 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002428 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2429 btrfs_leaf_data(left) + leaf_data_end(root, left),
2430 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002431
2432 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2433 btrfs_item_nr_offset(0),
2434 right_nritems * sizeof(struct btrfs_item));
2435
Chris Mason00ec4c52007-02-24 12:47:20 -05002436 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002437 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2438 btrfs_item_nr_offset(left_nritems - push_items),
2439 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002440
2441 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002442 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002443 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002444 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002445 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002446 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002447 push_space -= btrfs_token_item_size(right, item, &token);
2448 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002449 }
2450
Chris Mason7518a232007-03-12 12:01:18 -04002451 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002452 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002453
Chris Mason34a38212007-11-07 13:31:03 -05002454 if (left_nritems)
2455 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002456 else
2457 clean_tree_block(trans, root, left);
2458
Chris Mason5f39d392007-10-15 16:14:19 -04002459 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002460
Chris Mason5f39d392007-10-15 16:14:19 -04002461 btrfs_item_key(right, &disk_key, 0);
2462 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002463 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002464
Chris Mason00ec4c52007-02-24 12:47:20 -05002465 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002466 if (path->slots[0] >= left_nritems) {
2467 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002468 if (btrfs_header_nritems(path->nodes[0]) == 0)
2469 clean_tree_block(trans, root, path->nodes[0]);
2470 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002471 free_extent_buffer(path->nodes[0]);
2472 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002473 path->slots[1] += 1;
2474 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002475 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002476 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002477 }
2478 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002479
2480out_unlock:
2481 btrfs_tree_unlock(right);
2482 free_extent_buffer(right);
2483 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002484}
Chris Mason925baed2008-06-25 16:01:30 -04002485
Chris Mason00ec4c52007-02-24 12:47:20 -05002486/*
Chris Mason44871b12009-03-13 10:04:31 -04002487 * push some data in the path leaf to the right, trying to free up at
2488 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2489 *
2490 * returns 1 if the push failed because the other node didn't have enough
2491 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002492 *
2493 * this will push starting from min_slot to the end of the leaf. It won't
2494 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002495 */
2496static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002497 *root, struct btrfs_path *path,
2498 int min_data_size, int data_size,
2499 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002500{
2501 struct extent_buffer *left = path->nodes[0];
2502 struct extent_buffer *right;
2503 struct extent_buffer *upper;
2504 int slot;
2505 int free_space;
2506 u32 left_nritems;
2507 int ret;
2508
2509 if (!path->nodes[1])
2510 return 1;
2511
2512 slot = path->slots[1];
2513 upper = path->nodes[1];
2514 if (slot >= btrfs_header_nritems(upper) - 1)
2515 return 1;
2516
2517 btrfs_assert_tree_locked(path->nodes[1]);
2518
2519 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002520 if (right == NULL)
2521 return 1;
2522
Chris Mason44871b12009-03-13 10:04:31 -04002523 btrfs_tree_lock(right);
2524 btrfs_set_lock_blocking(right);
2525
2526 free_space = btrfs_leaf_free_space(root, right);
2527 if (free_space < data_size)
2528 goto out_unlock;
2529
2530 /* cow and double check */
2531 ret = btrfs_cow_block(trans, root, right, upper,
2532 slot + 1, &right);
2533 if (ret)
2534 goto out_unlock;
2535
2536 free_space = btrfs_leaf_free_space(root, right);
2537 if (free_space < data_size)
2538 goto out_unlock;
2539
2540 left_nritems = btrfs_header_nritems(left);
2541 if (left_nritems == 0)
2542 goto out_unlock;
2543
Chris Mason99d8f832010-07-07 10:51:48 -04002544 return __push_leaf_right(trans, root, path, min_data_size, empty,
2545 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002546out_unlock:
2547 btrfs_tree_unlock(right);
2548 free_extent_buffer(right);
2549 return 1;
2550}
2551
2552/*
Chris Mason74123bd2007-02-02 11:05:29 -05002553 * push some data in the path leaf to the left, trying to free up at
2554 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002555 *
2556 * max_slot can put a limit on how far into the leaf we'll push items. The
2557 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2558 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002559 */
Chris Mason44871b12009-03-13 10:04:31 -04002560static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2561 struct btrfs_root *root,
2562 struct btrfs_path *path, int data_size,
2563 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002564 int free_space, u32 right_nritems,
2565 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002566{
Chris Mason5f39d392007-10-15 16:14:19 -04002567 struct btrfs_disk_key disk_key;
2568 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002569 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 int push_space = 0;
2571 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002572 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002573 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002574 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002575 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002576 u32 this_item_size;
2577 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002578 struct btrfs_map_token token;
2579
2580 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002581
Chris Mason34a38212007-11-07 13:31:03 -05002582 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002583 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002584 else
Chris Mason99d8f832010-07-07 10:51:48 -04002585 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002586
2587 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002588 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002589
Zheng Yan31840ae2008-09-23 13:14:14 -04002590 if (!empty && push_items > 0) {
2591 if (path->slots[0] < i)
2592 break;
2593 if (path->slots[0] == i) {
2594 int space = btrfs_leaf_free_space(root, right);
2595 if (space + push_space * 2 > free_space)
2596 break;
2597 }
2598 }
2599
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002601 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002602
2603 this_item_size = btrfs_item_size(right, item);
2604 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002605 break;
Chris Masondb945352007-10-15 16:15:53 -04002606
Chris Masonbe0e5c02007-01-26 15:51:26 -05002607 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002608 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 }
Chris Masondb945352007-10-15 16:15:53 -04002610
Chris Masonbe0e5c02007-01-26 15:51:26 -05002611 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002612 ret = 1;
2613 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614 }
Chris Mason34a38212007-11-07 13:31:03 -05002615 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002616 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002617
Chris Masonbe0e5c02007-01-26 15:51:26 -05002618 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002619 copy_extent_buffer(left, right,
2620 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2621 btrfs_item_nr_offset(0),
2622 push_items * sizeof(struct btrfs_item));
2623
Chris Mason123abc82007-03-14 14:14:43 -04002624 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002625 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002626
2627 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002628 leaf_data_end(root, left) - push_space,
2629 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002630 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002631 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002632 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002633 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002634
Chris Masondb945352007-10-15 16:15:53 -04002635 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002636 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002637 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002638
Chris Mason5f39d392007-10-15 16:14:19 -04002639 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002640
Chris Masoncfed81a2012-03-03 07:40:03 -05002641 ioff = btrfs_token_item_offset(left, item, &token);
2642 btrfs_set_token_item_offset(left, item,
2643 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2644 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645 }
Chris Mason5f39d392007-10-15 16:14:19 -04002646 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002647
2648 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002649 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002650 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2651 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002652 WARN_ON(1);
2653 }
Chris Mason5f39d392007-10-15 16:14:19 -04002654
Chris Mason34a38212007-11-07 13:31:03 -05002655 if (push_items < right_nritems) {
2656 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2657 leaf_data_end(root, right);
2658 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2659 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2660 btrfs_leaf_data(right) +
2661 leaf_data_end(root, right), push_space);
2662
2663 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002664 btrfs_item_nr_offset(push_items),
2665 (btrfs_header_nritems(right) - push_items) *
2666 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002667 }
Yaneef1c492007-11-26 10:58:13 -05002668 right_nritems -= push_items;
2669 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002670 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 for (i = 0; i < right_nritems; i++) {
2672 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002673
Chris Masoncfed81a2012-03-03 07:40:03 -05002674 push_space = push_space - btrfs_token_item_size(right,
2675 item, &token);
2676 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002677 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002678
Chris Mason5f39d392007-10-15 16:14:19 -04002679 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002680 if (right_nritems)
2681 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002682 else
2683 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002684
Chris Mason5f39d392007-10-15 16:14:19 -04002685 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002686 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002687
2688 /* then fixup the leaf pointer in the path */
2689 if (path->slots[0] < push_items) {
2690 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002691 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002692 free_extent_buffer(path->nodes[0]);
2693 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002694 path->slots[1] -= 1;
2695 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002696 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002697 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002698 path->slots[0] -= push_items;
2699 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002700 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002701 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002702out:
2703 btrfs_tree_unlock(left);
2704 free_extent_buffer(left);
2705 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002706}
2707
Chris Mason74123bd2007-02-02 11:05:29 -05002708/*
Chris Mason44871b12009-03-13 10:04:31 -04002709 * push some data in the path leaf to the left, trying to free up at
2710 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002711 *
2712 * max_slot can put a limit on how far into the leaf we'll push items. The
2713 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2714 * items
Chris Mason44871b12009-03-13 10:04:31 -04002715 */
2716static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002717 *root, struct btrfs_path *path, int min_data_size,
2718 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002719{
2720 struct extent_buffer *right = path->nodes[0];
2721 struct extent_buffer *left;
2722 int slot;
2723 int free_space;
2724 u32 right_nritems;
2725 int ret = 0;
2726
2727 slot = path->slots[1];
2728 if (slot == 0)
2729 return 1;
2730 if (!path->nodes[1])
2731 return 1;
2732
2733 right_nritems = btrfs_header_nritems(right);
2734 if (right_nritems == 0)
2735 return 1;
2736
2737 btrfs_assert_tree_locked(path->nodes[1]);
2738
2739 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002740 if (left == NULL)
2741 return 1;
2742
Chris Mason44871b12009-03-13 10:04:31 -04002743 btrfs_tree_lock(left);
2744 btrfs_set_lock_blocking(left);
2745
2746 free_space = btrfs_leaf_free_space(root, left);
2747 if (free_space < data_size) {
2748 ret = 1;
2749 goto out;
2750 }
2751
2752 /* cow and double check */
2753 ret = btrfs_cow_block(trans, root, left,
2754 path->nodes[1], slot - 1, &left);
2755 if (ret) {
2756 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002757 if (ret == -ENOSPC)
2758 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002759 goto out;
2760 }
2761
2762 free_space = btrfs_leaf_free_space(root, left);
2763 if (free_space < data_size) {
2764 ret = 1;
2765 goto out;
2766 }
2767
Chris Mason99d8f832010-07-07 10:51:48 -04002768 return __push_leaf_left(trans, root, path, min_data_size,
2769 empty, left, free_space, right_nritems,
2770 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002771out:
2772 btrfs_tree_unlock(left);
2773 free_extent_buffer(left);
2774 return ret;
2775}
2776
2777/*
Chris Mason74123bd2007-02-02 11:05:29 -05002778 * split the path's leaf in two, making sure there is at least data_size
2779 * available for the resulting leaf level of the path.
2780 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002781static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2782 struct btrfs_root *root,
2783 struct btrfs_path *path,
2784 struct extent_buffer *l,
2785 struct extent_buffer *right,
2786 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002787{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002788 int data_copy_size;
2789 int rt_data_off;
2790 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002791 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05002792 struct btrfs_map_token token;
2793
2794 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002795
Chris Mason5f39d392007-10-15 16:14:19 -04002796 nritems = nritems - mid;
2797 btrfs_set_header_nritems(right, nritems);
2798 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2799
2800 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2801 btrfs_item_nr_offset(mid),
2802 nritems * sizeof(struct btrfs_item));
2803
2804 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002805 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2806 data_copy_size, btrfs_leaf_data(l) +
2807 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002808
Chris Mason5f39d392007-10-15 16:14:19 -04002809 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2810 btrfs_item_end_nr(l, mid);
2811
2812 for (i = 0; i < nritems; i++) {
2813 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002814 u32 ioff;
2815
Chris Masoncfed81a2012-03-03 07:40:03 -05002816 ioff = btrfs_token_item_offset(right, item, &token);
2817 btrfs_set_token_item_offset(right, item,
2818 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002819 }
Chris Mason74123bd2007-02-02 11:05:29 -05002820
Chris Mason5f39d392007-10-15 16:14:19 -04002821 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002822 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002823 insert_ptr(trans, root, path, &disk_key, right->start,
2824 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002825
2826 btrfs_mark_buffer_dirty(right);
2827 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002828 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002829
Chris Masonbe0e5c02007-01-26 15:51:26 -05002830 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002831 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002832 free_extent_buffer(path->nodes[0]);
2833 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002834 path->slots[0] -= mid;
2835 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002836 } else {
2837 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002838 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002839 }
Chris Mason5f39d392007-10-15 16:14:19 -04002840
Chris Masoneb60cea2007-02-02 09:18:22 -05002841 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002842}
2843
2844/*
Chris Mason99d8f832010-07-07 10:51:48 -04002845 * double splits happen when we need to insert a big item in the middle
2846 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2847 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2848 * A B C
2849 *
2850 * We avoid this by trying to push the items on either side of our target
2851 * into the adjacent leaves. If all goes well we can avoid the double split
2852 * completely.
2853 */
2854static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2855 struct btrfs_root *root,
2856 struct btrfs_path *path,
2857 int data_size)
2858{
2859 int ret;
2860 int progress = 0;
2861 int slot;
2862 u32 nritems;
2863
2864 slot = path->slots[0];
2865
2866 /*
2867 * try to push all the items after our slot into the
2868 * right leaf
2869 */
2870 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2871 if (ret < 0)
2872 return ret;
2873
2874 if (ret == 0)
2875 progress++;
2876
2877 nritems = btrfs_header_nritems(path->nodes[0]);
2878 /*
2879 * our goal is to get our slot at the start or end of a leaf. If
2880 * we've done so we're done
2881 */
2882 if (path->slots[0] == 0 || path->slots[0] == nritems)
2883 return 0;
2884
2885 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2886 return 0;
2887
2888 /* try to push all the items before our slot into the next leaf */
2889 slot = path->slots[0];
2890 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2891 if (ret < 0)
2892 return ret;
2893
2894 if (ret == 0)
2895 progress++;
2896
2897 if (progress)
2898 return 0;
2899 return 1;
2900}
2901
2902/*
Chris Mason44871b12009-03-13 10:04:31 -04002903 * split the path's leaf in two, making sure there is at least data_size
2904 * available for the resulting leaf level of the path.
2905 *
2906 * returns 0 if all went well and < 0 on failure.
2907 */
2908static noinline int split_leaf(struct btrfs_trans_handle *trans,
2909 struct btrfs_root *root,
2910 struct btrfs_key *ins_key,
2911 struct btrfs_path *path, int data_size,
2912 int extend)
2913{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002914 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002915 struct extent_buffer *l;
2916 u32 nritems;
2917 int mid;
2918 int slot;
2919 struct extent_buffer *right;
2920 int ret = 0;
2921 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002922 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002923 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002924 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002925
Yan, Zhenga5719522009-09-24 09:17:31 -04002926 l = path->nodes[0];
2927 slot = path->slots[0];
2928 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2929 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2930 return -EOVERFLOW;
2931
Chris Mason44871b12009-03-13 10:04:31 -04002932 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002933 if (data_size) {
2934 wret = push_leaf_right(trans, root, path, data_size,
2935 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002936 if (wret < 0)
2937 return wret;
2938 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002939 wret = push_leaf_left(trans, root, path, data_size,
2940 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002941 if (wret < 0)
2942 return wret;
2943 }
2944 l = path->nodes[0];
2945
2946 /* did the pushes work? */
2947 if (btrfs_leaf_free_space(root, l) >= data_size)
2948 return 0;
2949 }
2950
2951 if (!path->nodes[1]) {
2952 ret = insert_new_root(trans, root, path, 1);
2953 if (ret)
2954 return ret;
2955 }
2956again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002957 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002958 l = path->nodes[0];
2959 slot = path->slots[0];
2960 nritems = btrfs_header_nritems(l);
2961 mid = (nritems + 1) / 2;
2962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963 if (mid <= slot) {
2964 if (nritems == 1 ||
2965 leaf_space_used(l, mid, nritems - mid) + data_size >
2966 BTRFS_LEAF_DATA_SIZE(root)) {
2967 if (slot >= nritems) {
2968 split = 0;
2969 } else {
2970 mid = slot;
2971 if (mid != nritems &&
2972 leaf_space_used(l, mid, nritems - mid) +
2973 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002974 if (data_size && !tried_avoid_double)
2975 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002976 split = 2;
2977 }
2978 }
2979 }
2980 } else {
2981 if (leaf_space_used(l, 0, mid) + data_size >
2982 BTRFS_LEAF_DATA_SIZE(root)) {
2983 if (!extend && data_size && slot == 0) {
2984 split = 0;
2985 } else if ((extend || !data_size) && slot == 0) {
2986 mid = 1;
2987 } else {
2988 mid = slot;
2989 if (mid != nritems &&
2990 leaf_space_used(l, mid, nritems - mid) +
2991 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002992 if (data_size && !tried_avoid_double)
2993 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002994 split = 2 ;
2995 }
2996 }
2997 }
2998 }
2999
3000 if (split == 0)
3001 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3002 else
3003 btrfs_item_key(l, &disk_key, mid);
3004
3005 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003006 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003007 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003008 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003009 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003010
3011 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003012
3013 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3014 btrfs_set_header_bytenr(right, right->start);
3015 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003016 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003017 btrfs_set_header_owner(right, root->root_key.objectid);
3018 btrfs_set_header_level(right, 0);
3019 write_extent_buffer(right, root->fs_info->fsid,
3020 (unsigned long)btrfs_header_fsid(right),
3021 BTRFS_FSID_SIZE);
3022
3023 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3024 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3025 BTRFS_UUID_SIZE);
3026
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003027 if (split == 0) {
3028 if (mid <= slot) {
3029 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003030 insert_ptr(trans, root, path, &disk_key, right->start,
3031 path->slots[1] + 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;
3036 path->slots[1] += 1;
3037 } else {
3038 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003039 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003040 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041 btrfs_tree_unlock(path->nodes[0]);
3042 free_extent_buffer(path->nodes[0]);
3043 path->nodes[0] = right;
3044 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003045 if (path->slots[1] == 0)
3046 fixup_low_keys(trans, root, path,
3047 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003048 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003049 btrfs_mark_buffer_dirty(right);
3050 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003051 }
3052
Jeff Mahoney143bede2012-03-01 14:56:26 +01003053 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003054
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003055 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003056 BUG_ON(num_doubles != 0);
3057 num_doubles++;
3058 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003059 }
Chris Mason44871b12009-03-13 10:04:31 -04003060
Jeff Mahoney143bede2012-03-01 14:56:26 +01003061 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003062
3063push_for_double:
3064 push_for_double_split(trans, root, path, data_size);
3065 tried_avoid_double = 1;
3066 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3067 return 0;
3068 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003069}
3070
Yan, Zhengad48fd752009-11-12 09:33:58 +00003071static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3072 struct btrfs_root *root,
3073 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003074{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003075 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003076 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003077 struct btrfs_file_extent_item *fi;
3078 u64 extent_len = 0;
3079 u32 item_size;
3080 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003081
3082 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003083 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3084
3085 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3086 key.type != BTRFS_EXTENT_CSUM_KEY);
3087
3088 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3089 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003090
3091 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003092 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3093 fi = btrfs_item_ptr(leaf, path->slots[0],
3094 struct btrfs_file_extent_item);
3095 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3096 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003097 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003098
Chris Mason459931e2008-12-10 09:10:46 -05003099 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003100 path->search_for_split = 1;
3101 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003102 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003103 if (ret < 0)
3104 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003105
Yan, Zhengad48fd752009-11-12 09:33:58 +00003106 ret = -EAGAIN;
3107 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003108 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003109 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3110 goto err;
3111
Chris Mason109f6ae2010-04-02 09:20:18 -04003112 /* the leaf has changed, it now has room. return now */
3113 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3114 goto err;
3115
Yan, Zhengad48fd752009-11-12 09:33:58 +00003116 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3117 fi = btrfs_item_ptr(leaf, path->slots[0],
3118 struct btrfs_file_extent_item);
3119 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3120 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003121 }
3122
Chris Masonb9473432009-03-13 11:00:37 -04003123 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003124 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003125 if (ret)
3126 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003127
Yan, Zhengad48fd752009-11-12 09:33:58 +00003128 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003129 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003130 return 0;
3131err:
3132 path->keep_locks = 0;
3133 return ret;
3134}
3135
3136static noinline int split_item(struct btrfs_trans_handle *trans,
3137 struct btrfs_root *root,
3138 struct btrfs_path *path,
3139 struct btrfs_key *new_key,
3140 unsigned long split_offset)
3141{
3142 struct extent_buffer *leaf;
3143 struct btrfs_item *item;
3144 struct btrfs_item *new_item;
3145 int slot;
3146 char *buf;
3147 u32 nritems;
3148 u32 item_size;
3149 u32 orig_offset;
3150 struct btrfs_disk_key disk_key;
3151
Chris Masonb9473432009-03-13 11:00:37 -04003152 leaf = path->nodes[0];
3153 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3154
Chris Masonb4ce94d2009-02-04 09:25:08 -05003155 btrfs_set_path_blocking(path);
3156
Chris Mason459931e2008-12-10 09:10:46 -05003157 item = btrfs_item_nr(leaf, path->slots[0]);
3158 orig_offset = btrfs_item_offset(leaf, item);
3159 item_size = btrfs_item_size(leaf, item);
3160
Chris Mason459931e2008-12-10 09:10:46 -05003161 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003162 if (!buf)
3163 return -ENOMEM;
3164
Chris Mason459931e2008-12-10 09:10:46 -05003165 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3166 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003167
Chris Mason459931e2008-12-10 09:10:46 -05003168 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003169 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003170 if (slot != nritems) {
3171 /* shift the items */
3172 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003173 btrfs_item_nr_offset(slot),
3174 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003175 }
3176
3177 btrfs_cpu_key_to_disk(&disk_key, new_key);
3178 btrfs_set_item_key(leaf, &disk_key, slot);
3179
3180 new_item = btrfs_item_nr(leaf, slot);
3181
3182 btrfs_set_item_offset(leaf, new_item, orig_offset);
3183 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3184
3185 btrfs_set_item_offset(leaf, item,
3186 orig_offset + item_size - split_offset);
3187 btrfs_set_item_size(leaf, item, split_offset);
3188
3189 btrfs_set_header_nritems(leaf, nritems + 1);
3190
3191 /* write the data for the start of the original item */
3192 write_extent_buffer(leaf, buf,
3193 btrfs_item_ptr_offset(leaf, path->slots[0]),
3194 split_offset);
3195
3196 /* write the data for the new item */
3197 write_extent_buffer(leaf, buf + split_offset,
3198 btrfs_item_ptr_offset(leaf, slot),
3199 item_size - split_offset);
3200 btrfs_mark_buffer_dirty(leaf);
3201
Yan, Zhengad48fd752009-11-12 09:33:58 +00003202 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003203 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003204 return 0;
3205}
3206
3207/*
3208 * This function splits a single item into two items,
3209 * giving 'new_key' to the new item and splitting the
3210 * old one at split_offset (from the start of the item).
3211 *
3212 * The path may be released by this operation. After
3213 * the split, the path is pointing to the old item. The
3214 * new item is going to be in the same node as the old one.
3215 *
3216 * Note, the item being split must be smaller enough to live alone on
3217 * a tree block with room for one extra struct btrfs_item
3218 *
3219 * This allows us to split the item in place, keeping a lock on the
3220 * leaf the entire time.
3221 */
3222int btrfs_split_item(struct btrfs_trans_handle *trans,
3223 struct btrfs_root *root,
3224 struct btrfs_path *path,
3225 struct btrfs_key *new_key,
3226 unsigned long split_offset)
3227{
3228 int ret;
3229 ret = setup_leaf_for_split(trans, root, path,
3230 sizeof(struct btrfs_item));
3231 if (ret)
3232 return ret;
3233
3234 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003235 return ret;
3236}
3237
3238/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003239 * This function duplicate a item, giving 'new_key' to the new item.
3240 * It guarantees both items live in the same tree leaf and the new item
3241 * is contiguous with the original item.
3242 *
3243 * This allows us to split file extent in place, keeping a lock on the
3244 * leaf the entire time.
3245 */
3246int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3247 struct btrfs_root *root,
3248 struct btrfs_path *path,
3249 struct btrfs_key *new_key)
3250{
3251 struct extent_buffer *leaf;
3252 int ret;
3253 u32 item_size;
3254
3255 leaf = path->nodes[0];
3256 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3257 ret = setup_leaf_for_split(trans, root, path,
3258 item_size + sizeof(struct btrfs_item));
3259 if (ret)
3260 return ret;
3261
3262 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003263 setup_items_for_insert(trans, root, path, new_key, &item_size,
3264 item_size, item_size +
3265 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003266 leaf = path->nodes[0];
3267 memcpy_extent_buffer(leaf,
3268 btrfs_item_ptr_offset(leaf, path->slots[0]),
3269 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3270 item_size);
3271 return 0;
3272}
3273
3274/*
Chris Masond352ac62008-09-29 15:18:18 -04003275 * make the item pointed to by the path smaller. new_size indicates
3276 * how small to make it, and from_end tells us if we just chop bytes
3277 * off the end of the item or if we shift the item to chop bytes off
3278 * the front.
3279 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003280void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3281 struct btrfs_root *root,
3282 struct btrfs_path *path,
3283 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003284{
Chris Masonb18c6682007-04-17 13:26:50 -04003285 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003286 struct extent_buffer *leaf;
3287 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003288 u32 nritems;
3289 unsigned int data_end;
3290 unsigned int old_data_start;
3291 unsigned int old_size;
3292 unsigned int size_diff;
3293 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003294 struct btrfs_map_token token;
3295
3296 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003297
Chris Mason5f39d392007-10-15 16:14:19 -04003298 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003299 slot = path->slots[0];
3300
3301 old_size = btrfs_item_size_nr(leaf, slot);
3302 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003303 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003304
Chris Mason5f39d392007-10-15 16:14:19 -04003305 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003306 data_end = leaf_data_end(root, leaf);
3307
Chris Mason5f39d392007-10-15 16:14:19 -04003308 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003309
Chris Masonb18c6682007-04-17 13:26:50 -04003310 size_diff = old_size - new_size;
3311
3312 BUG_ON(slot < 0);
3313 BUG_ON(slot >= nritems);
3314
3315 /*
3316 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3317 */
3318 /* first correct the data pointers */
3319 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003320 u32 ioff;
3321 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003322
Chris Masoncfed81a2012-03-03 07:40:03 -05003323 ioff = btrfs_token_item_offset(leaf, item, &token);
3324 btrfs_set_token_item_offset(leaf, item,
3325 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003326 }
Chris Masondb945352007-10-15 16:15:53 -04003327
Chris Masonb18c6682007-04-17 13:26:50 -04003328 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003329 if (from_end) {
3330 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3331 data_end + size_diff, btrfs_leaf_data(leaf) +
3332 data_end, old_data_start + new_size - data_end);
3333 } else {
3334 struct btrfs_disk_key disk_key;
3335 u64 offset;
3336
3337 btrfs_item_key(leaf, &disk_key, slot);
3338
3339 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3340 unsigned long ptr;
3341 struct btrfs_file_extent_item *fi;
3342
3343 fi = btrfs_item_ptr(leaf, slot,
3344 struct btrfs_file_extent_item);
3345 fi = (struct btrfs_file_extent_item *)(
3346 (unsigned long)fi - size_diff);
3347
3348 if (btrfs_file_extent_type(leaf, fi) ==
3349 BTRFS_FILE_EXTENT_INLINE) {
3350 ptr = btrfs_item_ptr_offset(leaf, slot);
3351 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003352 (unsigned long)fi,
3353 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003354 disk_bytenr));
3355 }
3356 }
3357
3358 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3359 data_end + size_diff, btrfs_leaf_data(leaf) +
3360 data_end, old_data_start - data_end);
3361
3362 offset = btrfs_disk_key_offset(&disk_key);
3363 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3364 btrfs_set_item_key(leaf, &disk_key, slot);
3365 if (slot == 0)
3366 fixup_low_keys(trans, root, path, &disk_key, 1);
3367 }
Chris Mason5f39d392007-10-15 16:14:19 -04003368
3369 item = btrfs_item_nr(leaf, slot);
3370 btrfs_set_item_size(leaf, item, new_size);
3371 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003372
Chris Mason5f39d392007-10-15 16:14:19 -04003373 if (btrfs_leaf_free_space(root, leaf) < 0) {
3374 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003375 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003376 }
Chris Masonb18c6682007-04-17 13:26:50 -04003377}
3378
Chris Masond352ac62008-09-29 15:18:18 -04003379/*
3380 * make the item pointed to by the path bigger, data_size is the new size.
3381 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003382void btrfs_extend_item(struct btrfs_trans_handle *trans,
3383 struct btrfs_root *root, struct btrfs_path *path,
3384 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003385{
Chris Mason6567e832007-04-16 09:22:45 -04003386 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003387 struct extent_buffer *leaf;
3388 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003389 u32 nritems;
3390 unsigned int data_end;
3391 unsigned int old_data;
3392 unsigned int old_size;
3393 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003394 struct btrfs_map_token token;
3395
3396 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003397
Chris Mason5f39d392007-10-15 16:14:19 -04003398 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003399
Chris Mason5f39d392007-10-15 16:14:19 -04003400 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003401 data_end = leaf_data_end(root, leaf);
3402
Chris Mason5f39d392007-10-15 16:14:19 -04003403 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3404 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003405 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003406 }
Chris Mason6567e832007-04-16 09:22:45 -04003407 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003408 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003409
3410 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003411 if (slot >= nritems) {
3412 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003413 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3414 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003415 BUG_ON(1);
3416 }
Chris Mason6567e832007-04-16 09:22:45 -04003417
3418 /*
3419 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3420 */
3421 /* first correct the data pointers */
3422 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003423 u32 ioff;
3424 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003425
Chris Masoncfed81a2012-03-03 07:40:03 -05003426 ioff = btrfs_token_item_offset(leaf, item, &token);
3427 btrfs_set_token_item_offset(leaf, item,
3428 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003429 }
Chris Mason5f39d392007-10-15 16:14:19 -04003430
Chris Mason6567e832007-04-16 09:22:45 -04003431 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003432 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003433 data_end - data_size, btrfs_leaf_data(leaf) +
3434 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003435
Chris Mason6567e832007-04-16 09:22:45 -04003436 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003437 old_size = btrfs_item_size_nr(leaf, slot);
3438 item = btrfs_item_nr(leaf, slot);
3439 btrfs_set_item_size(leaf, item, old_size + data_size);
3440 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003441
Chris Mason5f39d392007-10-15 16:14:19 -04003442 if (btrfs_leaf_free_space(root, leaf) < 0) {
3443 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003444 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003445 }
Chris Mason6567e832007-04-16 09:22:45 -04003446}
3447
Chris Mason74123bd2007-02-02 11:05:29 -05003448/*
Chris Masond352ac62008-09-29 15:18:18 -04003449 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003450 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003451 * Returns the number of keys that were inserted.
3452 */
3453int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3454 struct btrfs_root *root,
3455 struct btrfs_path *path,
3456 struct btrfs_key *cpu_key, u32 *data_size,
3457 int nr)
3458{
3459 struct extent_buffer *leaf;
3460 struct btrfs_item *item;
3461 int ret = 0;
3462 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003463 int i;
3464 u32 nritems;
3465 u32 total_data = 0;
3466 u32 total_size = 0;
3467 unsigned int data_end;
3468 struct btrfs_disk_key disk_key;
3469 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003470 struct btrfs_map_token token;
3471
3472 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003473
Yan Zheng87b29b22008-12-17 10:21:48 -05003474 for (i = 0; i < nr; i++) {
3475 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3476 BTRFS_LEAF_DATA_SIZE(root)) {
3477 break;
3478 nr = i;
3479 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003480 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003481 total_size += data_size[i] + sizeof(struct btrfs_item);
3482 }
3483 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003484
Josef Bacikf3465ca2008-11-12 14:19:50 -05003485 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3486 if (ret == 0)
3487 return -EEXIST;
3488 if (ret < 0)
3489 goto out;
3490
Josef Bacikf3465ca2008-11-12 14:19:50 -05003491 leaf = path->nodes[0];
3492
3493 nritems = btrfs_header_nritems(leaf);
3494 data_end = leaf_data_end(root, leaf);
3495
3496 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3497 for (i = nr; i >= 0; i--) {
3498 total_data -= data_size[i];
3499 total_size -= data_size[i] + sizeof(struct btrfs_item);
3500 if (total_size < btrfs_leaf_free_space(root, leaf))
3501 break;
3502 }
3503 nr = i;
3504 }
3505
3506 slot = path->slots[0];
3507 BUG_ON(slot < 0);
3508
3509 if (slot != nritems) {
3510 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3511
3512 item = btrfs_item_nr(leaf, slot);
3513 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3514
3515 /* figure out how many keys we can insert in here */
3516 total_data = data_size[0];
3517 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003518 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003519 break;
3520 total_data += data_size[i];
3521 }
3522 nr = i;
3523
3524 if (old_data < data_end) {
3525 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003526 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003527 slot, old_data, data_end);
3528 BUG_ON(1);
3529 }
3530 /*
3531 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3532 */
3533 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003534 for (i = slot; i < nritems; i++) {
3535 u32 ioff;
3536
3537 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003538 ioff = btrfs_token_item_offset(leaf, item, &token);
3539 btrfs_set_token_item_offset(leaf, item,
3540 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003541 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003542 /* shift the items */
3543 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3544 btrfs_item_nr_offset(slot),
3545 (nritems - slot) * sizeof(struct btrfs_item));
3546
3547 /* shift the data */
3548 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3549 data_end - total_data, btrfs_leaf_data(leaf) +
3550 data_end, old_data - data_end);
3551 data_end = old_data;
3552 } else {
3553 /*
3554 * this sucks but it has to be done, if we are inserting at
3555 * the end of the leaf only insert 1 of the items, since we
3556 * have no way of knowing whats on the next leaf and we'd have
3557 * to drop our current locks to figure it out
3558 */
3559 nr = 1;
3560 }
3561
3562 /* setup the item for the new data */
3563 for (i = 0; i < nr; i++) {
3564 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3565 btrfs_set_item_key(leaf, &disk_key, slot + i);
3566 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003567 btrfs_set_token_item_offset(leaf, item,
3568 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003569 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003570 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003571 }
3572 btrfs_set_header_nritems(leaf, nritems + nr);
3573 btrfs_mark_buffer_dirty(leaf);
3574
3575 ret = 0;
3576 if (slot == 0) {
3577 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003578 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003579 }
3580
3581 if (btrfs_leaf_free_space(root, leaf) < 0) {
3582 btrfs_print_leaf(root, leaf);
3583 BUG();
3584 }
3585out:
3586 if (!ret)
3587 ret = nr;
3588 return ret;
3589}
3590
3591/*
Chris Mason44871b12009-03-13 10:04:31 -04003592 * this is a helper for btrfs_insert_empty_items, the main goal here is
3593 * to save stack depth by doing the bulk of the work in a function
3594 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003595 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003596void setup_items_for_insert(struct btrfs_trans_handle *trans,
3597 struct btrfs_root *root, struct btrfs_path *path,
3598 struct btrfs_key *cpu_key, u32 *data_size,
3599 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600{
Chris Mason5f39d392007-10-15 16:14:19 -04003601 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003602 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003603 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003604 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003605 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003606 struct extent_buffer *leaf;
3607 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05003608 struct btrfs_map_token token;
3609
3610 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04003611
Chris Mason5f39d392007-10-15 16:14:19 -04003612 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003613 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003616 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003617
Chris Masonf25956c2008-09-12 15:32:53 -04003618 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003619 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003620 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003621 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003622 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003623 }
Chris Mason5f39d392007-10-15 16:14:19 -04003624
Chris Masonbe0e5c02007-01-26 15:51:26 -05003625 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003626 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003627
Chris Mason5f39d392007-10-15 16:14:19 -04003628 if (old_data < data_end) {
3629 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003630 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003631 slot, old_data, data_end);
3632 BUG_ON(1);
3633 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003634 /*
3635 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3636 */
3637 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003638 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003639 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003640
Chris Mason5f39d392007-10-15 16:14:19 -04003641 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003642 ioff = btrfs_token_item_offset(leaf, item, &token);
3643 btrfs_set_token_item_offset(leaf, item,
3644 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003645 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003646 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003647 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003648 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003649 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003650
3651 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003652 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003653 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003654 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003655 data_end = old_data;
3656 }
Chris Mason5f39d392007-10-15 16:14:19 -04003657
Chris Mason62e27492007-03-15 12:56:47 -04003658 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003659 for (i = 0; i < nr; i++) {
3660 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3661 btrfs_set_item_key(leaf, &disk_key, slot + i);
3662 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003663 btrfs_set_token_item_offset(leaf, item,
3664 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003665 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003666 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003667 }
Chris Mason44871b12009-03-13 10:04:31 -04003668
Chris Mason9c583092008-01-29 15:15:18 -05003669 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003670
Chris Mason5a01a2e2008-01-30 11:43:54 -05003671 if (slot == 0) {
3672 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003673 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003674 }
Chris Masonb9473432009-03-13 11:00:37 -04003675 btrfs_unlock_up_safe(path, 1);
3676 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003677
Chris Mason5f39d392007-10-15 16:14:19 -04003678 if (btrfs_leaf_free_space(root, leaf) < 0) {
3679 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003680 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003681 }
Chris Mason44871b12009-03-13 10:04:31 -04003682}
3683
3684/*
3685 * Given a key and some data, insert items into the tree.
3686 * This does all the path init required, making room in the tree if needed.
3687 */
3688int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3689 struct btrfs_root *root,
3690 struct btrfs_path *path,
3691 struct btrfs_key *cpu_key, u32 *data_size,
3692 int nr)
3693{
Chris Mason44871b12009-03-13 10:04:31 -04003694 int ret = 0;
3695 int slot;
3696 int i;
3697 u32 total_size = 0;
3698 u32 total_data = 0;
3699
3700 for (i = 0; i < nr; i++)
3701 total_data += data_size[i];
3702
3703 total_size = total_data + (nr * sizeof(struct btrfs_item));
3704 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3705 if (ret == 0)
3706 return -EEXIST;
3707 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003708 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003709
Chris Mason44871b12009-03-13 10:04:31 -04003710 slot = path->slots[0];
3711 BUG_ON(slot < 0);
3712
Jeff Mahoney143bede2012-03-01 14:56:26 +01003713 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003714 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003715 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003716}
3717
3718/*
3719 * Given a key and some data, insert an item into the tree.
3720 * This does all the path init required, making room in the tree if needed.
3721 */
Chris Masone089f052007-03-16 16:20:31 -04003722int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3723 *root, struct btrfs_key *cpu_key, void *data, u32
3724 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003725{
3726 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003727 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003728 struct extent_buffer *leaf;
3729 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003730
Chris Mason2c90e5d2007-04-02 10:50:19 -04003731 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003732 if (!path)
3733 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003734 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003735 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003736 leaf = path->nodes[0];
3737 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3738 write_extent_buffer(leaf, data, ptr, data_size);
3739 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003740 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003741 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003742 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003743}
3744
Chris Mason74123bd2007-02-02 11:05:29 -05003745/*
Chris Mason5de08d72007-02-24 06:24:44 -05003746 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003747 *
Chris Masond352ac62008-09-29 15:18:18 -04003748 * the tree should have been previously balanced so the deletion does not
3749 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003750 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003751static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3752 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003753{
Chris Mason5f39d392007-10-15 16:14:19 -04003754 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003755 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003756
Chris Mason5f39d392007-10-15 16:14:19 -04003757 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003758 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003759 memmove_extent_buffer(parent,
3760 btrfs_node_key_ptr_offset(slot),
3761 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003762 sizeof(struct btrfs_key_ptr) *
3763 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003764 }
Chris Mason7518a232007-03-12 12:01:18 -04003765 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003766 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003767 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003768 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003769 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003770 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003771 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003772 struct btrfs_disk_key disk_key;
3773
3774 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003775 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003776 }
Chris Masond6025572007-03-30 14:27:56 -04003777 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003778}
3779
Chris Mason74123bd2007-02-02 11:05:29 -05003780/*
Chris Mason323ac952008-10-01 19:05:46 -04003781 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003782 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003783 *
3784 * This deletes the pointer in path->nodes[1] and frees the leaf
3785 * block extent. zero is returned if it all worked out, < 0 otherwise.
3786 *
3787 * The path must have already been setup for deleting the leaf, including
3788 * all the proper balancing. path->nodes[1] must be locked.
3789 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003790static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3791 struct btrfs_root *root,
3792 struct btrfs_path *path,
3793 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003794{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003795 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003796 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003797
Chris Mason4d081c42009-02-04 09:31:28 -05003798 /*
3799 * btrfs_free_extent is expensive, we want to make sure we
3800 * aren't holding any locks when we call it
3801 */
3802 btrfs_unlock_up_safe(path, 0);
3803
Yan, Zhengf0486c62010-05-16 10:46:25 -04003804 root_sub_used(root, leaf->len);
3805
Josef Bacik3083ee22012-03-09 16:01:49 -05003806 extent_buffer_get(leaf);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003807 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05003808 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003809}
3810/*
Chris Mason74123bd2007-02-02 11:05:29 -05003811 * delete the item at the leaf level in path. If that empties
3812 * the leaf, remove it from the tree
3813 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003814int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3815 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003816{
Chris Mason5f39d392007-10-15 16:14:19 -04003817 struct extent_buffer *leaf;
3818 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003819 int last_off;
3820 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003821 int ret = 0;
3822 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003823 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003824 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05003825 struct btrfs_map_token token;
3826
3827 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003828
Chris Mason5f39d392007-10-15 16:14:19 -04003829 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003830 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3831
3832 for (i = 0; i < nr; i++)
3833 dsize += btrfs_item_size_nr(leaf, slot + i);
3834
Chris Mason5f39d392007-10-15 16:14:19 -04003835 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003836
Chris Mason85e21ba2008-01-29 15:11:36 -05003837 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003838 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003839
3840 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003841 data_end + dsize,
3842 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003843 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003844
Chris Mason85e21ba2008-01-29 15:11:36 -05003845 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003846 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003847
Chris Mason5f39d392007-10-15 16:14:19 -04003848 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003849 ioff = btrfs_token_item_offset(leaf, item, &token);
3850 btrfs_set_token_item_offset(leaf, item,
3851 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003852 }
Chris Masondb945352007-10-15 16:15:53 -04003853
Chris Mason5f39d392007-10-15 16:14:19 -04003854 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003855 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003856 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003857 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003858 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003859 btrfs_set_header_nritems(leaf, nritems - nr);
3860 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003861
Chris Mason74123bd2007-02-02 11:05:29 -05003862 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003863 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003864 if (leaf == root->node) {
3865 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003866 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003867 btrfs_set_path_blocking(path);
3868 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003869 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003870 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003871 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003872 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003873 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003874 struct btrfs_disk_key disk_key;
3875
3876 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003877 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003878 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003879
Chris Mason74123bd2007-02-02 11:05:29 -05003880 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003881 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003882 /* push_leaf_left fixes the path.
3883 * make sure the path still points to our leaf
3884 * for possible call to del_ptr below
3885 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003886 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003887 extent_buffer_get(leaf);
3888
Chris Masonb9473432009-03-13 11:00:37 -04003889 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003890 wret = push_leaf_left(trans, root, path, 1, 1,
3891 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003892 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003893 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003894
3895 if (path->nodes[0] == leaf &&
3896 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003897 wret = push_leaf_right(trans, root, path, 1,
3898 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003899 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003900 ret = wret;
3901 }
Chris Mason5f39d392007-10-15 16:14:19 -04003902
3903 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003904 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003905 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003906 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003907 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003908 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003909 /* if we're still in the path, make sure
3910 * we're dirty. Otherwise, one of the
3911 * push_leaf functions must have already
3912 * dirtied this buffer
3913 */
3914 if (path->nodes[0] == leaf)
3915 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003916 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003917 }
Chris Masond5719762007-03-23 10:01:08 -04003918 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003919 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003920 }
3921 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003922 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003923}
3924
Chris Mason97571fd2007-02-24 13:39:08 -05003925/*
Chris Mason925baed2008-06-25 16:01:30 -04003926 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003927 * returns 0 if it found something or 1 if there are no lesser leaves.
3928 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003929 *
3930 * This may release the path, and so you may lose any locks held at the
3931 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003932 */
3933int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3934{
Chris Mason925baed2008-06-25 16:01:30 -04003935 struct btrfs_key key;
3936 struct btrfs_disk_key found_key;
3937 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003938
Chris Mason925baed2008-06-25 16:01:30 -04003939 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003940
Chris Mason925baed2008-06-25 16:01:30 -04003941 if (key.offset > 0)
3942 key.offset--;
3943 else if (key.type > 0)
3944 key.type--;
3945 else if (key.objectid > 0)
3946 key.objectid--;
3947 else
3948 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003949
David Sterbab3b4aa72011-04-21 01:20:15 +02003950 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3952 if (ret < 0)
3953 return ret;
3954 btrfs_item_key(path->nodes[0], &found_key, 0);
3955 ret = comp_keys(&found_key, &key);
3956 if (ret < 0)
3957 return 0;
3958 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003959}
3960
Chris Mason3f157a22008-06-25 16:01:31 -04003961/*
3962 * A helper function to walk down the tree starting at min_key, and looking
3963 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003964 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003965 *
3966 * This does not cow, but it does stuff the starting key it finds back
3967 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3968 * key and get a writable path.
3969 *
3970 * This does lock as it descends, and path->keep_locks should be set
3971 * to 1 by the caller.
3972 *
3973 * This honors path->lowest_level to prevent descent past a given level
3974 * of the tree.
3975 *
Chris Masond352ac62008-09-29 15:18:18 -04003976 * min_trans indicates the oldest transaction that you are interested
3977 * in walking through. Any nodes or leaves older than min_trans are
3978 * skipped over (without reading them).
3979 *
Chris Mason3f157a22008-06-25 16:01:31 -04003980 * returns zero if something useful was found, < 0 on error and 1 if there
3981 * was nothing in the tree that matched the search criteria.
3982 */
3983int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003984 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003985 struct btrfs_path *path, int cache_only,
3986 u64 min_trans)
3987{
3988 struct extent_buffer *cur;
3989 struct btrfs_key found_key;
3990 int slot;
Yan96524802008-07-24 12:19:49 -04003991 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003992 u32 nritems;
3993 int level;
3994 int ret = 1;
3995
Chris Mason934d3752008-12-08 16:43:10 -05003996 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003997again:
Chris Masonbd681512011-07-16 15:23:14 -04003998 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003999 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004000 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004001 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004002 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004003
4004 if (btrfs_header_generation(cur) < min_trans) {
4005 ret = 1;
4006 goto out;
4007 }
Chris Masond3977122009-01-05 21:25:51 -05004008 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004009 nritems = btrfs_header_nritems(cur);
4010 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004011 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004012
Chris Mason323ac952008-10-01 19:05:46 -04004013 /* at the lowest level, we're done, setup the path and exit */
4014 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004015 if (slot >= nritems)
4016 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004017 ret = 0;
4018 path->slots[level] = slot;
4019 btrfs_item_key_to_cpu(cur, &found_key, slot);
4020 goto out;
4021 }
Yan96524802008-07-24 12:19:49 -04004022 if (sret && slot > 0)
4023 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004024 /*
4025 * check this node pointer against the cache_only and
4026 * min_trans parameters. If it isn't in cache or is too
4027 * old, skip to the next one.
4028 */
Chris Masond3977122009-01-05 21:25:51 -05004029 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004030 u64 blockptr;
4031 u64 gen;
4032 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004033 struct btrfs_disk_key disk_key;
4034
Chris Mason3f157a22008-06-25 16:01:31 -04004035 blockptr = btrfs_node_blockptr(cur, slot);
4036 gen = btrfs_node_ptr_generation(cur, slot);
4037 if (gen < min_trans) {
4038 slot++;
4039 continue;
4040 }
4041 if (!cache_only)
4042 break;
4043
Chris Masone02119d2008-09-05 16:13:11 -04004044 if (max_key) {
4045 btrfs_node_key(cur, &disk_key, slot);
4046 if (comp_keys(&disk_key, max_key) >= 0) {
4047 ret = 1;
4048 goto out;
4049 }
4050 }
4051
Chris Mason3f157a22008-06-25 16:01:31 -04004052 tmp = btrfs_find_tree_block(root, blockptr,
4053 btrfs_level_size(root, level - 1));
4054
Chris Masonb9fab912012-05-06 07:23:47 -04004055 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004056 free_extent_buffer(tmp);
4057 break;
4058 }
4059 if (tmp)
4060 free_extent_buffer(tmp);
4061 slot++;
4062 }
Chris Masone02119d2008-09-05 16:13:11 -04004063find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004064 /*
4065 * we didn't find a candidate key in this node, walk forward
4066 * and find another one
4067 */
4068 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004069 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004070 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004071 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004072 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004073 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004074 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004075 goto again;
4076 } else {
4077 goto out;
4078 }
4079 }
4080 /* save our key for returning back */
4081 btrfs_node_key_to_cpu(cur, &found_key, slot);
4082 path->slots[level] = slot;
4083 if (level == path->lowest_level) {
4084 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004085 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004086 goto out;
4087 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004088 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004089 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004090 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004091
Chris Masonbd681512011-07-16 15:23:14 -04004092 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004093
Chris Masonbd681512011-07-16 15:23:14 -04004094 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004095 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004096 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004097 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004098 }
4099out:
4100 if (ret == 0)
4101 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004102 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004103 return ret;
4104}
4105
4106/*
4107 * this is similar to btrfs_next_leaf, but does not try to preserve
4108 * and fixup the path. It looks for and returns the next key in the
4109 * tree based on the current path and the cache_only and min_trans
4110 * parameters.
4111 *
4112 * 0 is returned if another key is found, < 0 if there are any errors
4113 * and 1 is returned if there are no higher keys in the tree
4114 *
4115 * path->keep_locks should be set to 1 on the search made before
4116 * calling this function.
4117 */
Chris Masone7a84562008-06-25 16:01:31 -04004118int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004119 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004120 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004121{
Chris Masone7a84562008-06-25 16:01:31 -04004122 int slot;
4123 struct extent_buffer *c;
4124
Chris Mason934d3752008-12-08 16:43:10 -05004125 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004126 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004127 if (!path->nodes[level])
4128 return 1;
4129
4130 slot = path->slots[level] + 1;
4131 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004132next:
Chris Masone7a84562008-06-25 16:01:31 -04004133 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004134 int ret;
4135 int orig_lowest;
4136 struct btrfs_key cur_key;
4137 if (level + 1 >= BTRFS_MAX_LEVEL ||
4138 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004139 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004140
4141 if (path->locks[level + 1]) {
4142 level++;
4143 continue;
4144 }
4145
4146 slot = btrfs_header_nritems(c) - 1;
4147 if (level == 0)
4148 btrfs_item_key_to_cpu(c, &cur_key, slot);
4149 else
4150 btrfs_node_key_to_cpu(c, &cur_key, slot);
4151
4152 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004153 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004154 path->lowest_level = level;
4155 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4156 0, 0);
4157 path->lowest_level = orig_lowest;
4158 if (ret < 0)
4159 return ret;
4160
4161 c = path->nodes[level];
4162 slot = path->slots[level];
4163 if (ret == 0)
4164 slot++;
4165 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004166 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004167
Chris Masone7a84562008-06-25 16:01:31 -04004168 if (level == 0)
4169 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004170 else {
4171 u64 blockptr = btrfs_node_blockptr(c, slot);
4172 u64 gen = btrfs_node_ptr_generation(c, slot);
4173
4174 if (cache_only) {
4175 struct extent_buffer *cur;
4176 cur = btrfs_find_tree_block(root, blockptr,
4177 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004178 if (!cur ||
4179 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004180 slot++;
4181 if (cur)
4182 free_extent_buffer(cur);
4183 goto next;
4184 }
4185 free_extent_buffer(cur);
4186 }
4187 if (gen < min_trans) {
4188 slot++;
4189 goto next;
4190 }
Chris Masone7a84562008-06-25 16:01:31 -04004191 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004192 }
Chris Masone7a84562008-06-25 16:01:31 -04004193 return 0;
4194 }
4195 return 1;
4196}
4197
Chris Mason7bb86312007-12-11 09:25:06 -05004198/*
Chris Mason925baed2008-06-25 16:01:30 -04004199 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004200 * returns 0 if it found something or 1 if there are no greater leaves.
4201 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004202 */
Chris Mason234b63a2007-03-13 10:46:10 -04004203int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004204{
4205 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004206 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004207 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004208 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004209 struct btrfs_key key;
4210 u32 nritems;
4211 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004212 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004213 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004214
4215 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004216 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004217 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004218
Chris Mason8e73f272009-04-03 10:14:18 -04004219 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4220again:
4221 level = 1;
4222 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004223 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004224 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004225
Chris Masona2135012008-06-25 16:01:30 -04004226 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004227 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004228
Chris Mason925baed2008-06-25 16:01:30 -04004229 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4230 path->keep_locks = 0;
4231
4232 if (ret < 0)
4233 return ret;
4234
Chris Masona2135012008-06-25 16:01:30 -04004235 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004236 /*
4237 * by releasing the path above we dropped all our locks. A balance
4238 * could have added more items next to the key that used to be
4239 * at the very end of the block. So, check again here and
4240 * advance the path if there are now more items available.
4241 */
Chris Masona2135012008-06-25 16:01:30 -04004242 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004243 if (ret == 0)
4244 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004245 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004246 goto done;
4247 }
Chris Masond97e63b2007-02-20 16:40:44 -05004248
Chris Masond3977122009-01-05 21:25:51 -05004249 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004250 if (!path->nodes[level]) {
4251 ret = 1;
4252 goto done;
4253 }
Chris Mason5f39d392007-10-15 16:14:19 -04004254
Chris Masond97e63b2007-02-20 16:40:44 -05004255 slot = path->slots[level] + 1;
4256 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004257 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004258 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004259 if (level == BTRFS_MAX_LEVEL) {
4260 ret = 1;
4261 goto done;
4262 }
Chris Masond97e63b2007-02-20 16:40:44 -05004263 continue;
4264 }
Chris Mason5f39d392007-10-15 16:14:19 -04004265
Chris Mason925baed2008-06-25 16:01:30 -04004266 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004267 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004268 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004269 }
Chris Mason5f39d392007-10-15 16:14:19 -04004270
Chris Mason8e73f272009-04-03 10:14:18 -04004271 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004272 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004273 ret = read_block_for_search(NULL, root, path, &next, level,
4274 slot, &key);
4275 if (ret == -EAGAIN)
4276 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004277
Chris Mason76a05b32009-05-14 13:24:30 -04004278 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004279 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004280 goto done;
4281 }
4282
Chris Mason5cd57b22008-06-25 16:01:30 -04004283 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004284 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004285 if (!ret) {
4286 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004287 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004288 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004289 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004290 }
Chris Mason31533fb2011-07-26 16:01:59 -04004291 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004292 }
Chris Masond97e63b2007-02-20 16:40:44 -05004293 break;
4294 }
4295 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004296 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004297 level--;
4298 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004299 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004300 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004301
Chris Mason5f39d392007-10-15 16:14:19 -04004302 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004303 path->nodes[level] = next;
4304 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004305 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004306 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004307 if (!level)
4308 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004309
Chris Mason8e73f272009-04-03 10:14:18 -04004310 ret = read_block_for_search(NULL, root, path, &next, level,
4311 0, &key);
4312 if (ret == -EAGAIN)
4313 goto again;
4314
Chris Mason76a05b32009-05-14 13:24:30 -04004315 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004316 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004317 goto done;
4318 }
4319
Chris Mason5cd57b22008-06-25 16:01:30 -04004320 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004321 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004322 if (!ret) {
4323 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004324 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004325 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004326 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004327 }
Chris Mason31533fb2011-07-26 16:01:59 -04004328 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004329 }
Chris Masond97e63b2007-02-20 16:40:44 -05004330 }
Chris Mason8e73f272009-04-03 10:14:18 -04004331 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004332done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004333 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004334 path->leave_spinning = old_spinning;
4335 if (!old_spinning)
4336 btrfs_set_path_blocking(path);
4337
4338 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004339}
Chris Mason0b86a832008-03-24 15:01:56 -04004340
Chris Mason3f157a22008-06-25 16:01:31 -04004341/*
4342 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4343 * searching until it gets past min_objectid or finds an item of 'type'
4344 *
4345 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4346 */
Chris Mason0b86a832008-03-24 15:01:56 -04004347int btrfs_previous_item(struct btrfs_root *root,
4348 struct btrfs_path *path, u64 min_objectid,
4349 int type)
4350{
4351 struct btrfs_key found_key;
4352 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004353 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004354 int ret;
4355
Chris Masond3977122009-01-05 21:25:51 -05004356 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004357 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004358 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004359 ret = btrfs_prev_leaf(root, path);
4360 if (ret != 0)
4361 return ret;
4362 } else {
4363 path->slots[0]--;
4364 }
4365 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004366 nritems = btrfs_header_nritems(leaf);
4367 if (nritems == 0)
4368 return 1;
4369 if (path->slots[0] == nritems)
4370 path->slots[0]--;
4371
Chris Mason0b86a832008-03-24 15:01:56 -04004372 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004373 if (found_key.objectid < min_objectid)
4374 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004375 if (found_key.type == type)
4376 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004377 if (found_key.objectid == min_objectid &&
4378 found_key.type < type)
4379 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004380 }
4381 return 1;
4382}