blob: 26847999c64923ba346b7d55cd51570f32fb2d59 [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 Masonb9447ef82009-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{
Wang Sheng-Huif7757382012-03-30 15:14:27 +0800857 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -0400858 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);
Wang Sheng-Huif7757382012-03-30 15:14:27 +0800863 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
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
872 int level, int *slot)
873{
874 return bin_search(eb, key, level, slot);
875}
876
Yan, Zhengf0486c62010-05-16 10:46:25 -0400877static void root_add_used(struct btrfs_root *root, u32 size)
878{
879 spin_lock(&root->accounting_lock);
880 btrfs_set_root_used(&root->root_item,
881 btrfs_root_used(&root->root_item) + size);
882 spin_unlock(&root->accounting_lock);
883}
884
885static void root_sub_used(struct btrfs_root *root, u32 size)
886{
887 spin_lock(&root->accounting_lock);
888 btrfs_set_root_used(&root->root_item,
889 btrfs_root_used(&root->root_item) - size);
890 spin_unlock(&root->accounting_lock);
891}
892
Chris Masond352ac62008-09-29 15:18:18 -0400893/* given a node and slot number, this reads the blocks it points to. The
894 * extent buffer is returned with a reference taken (but unlocked).
895 * NULL is returned on error.
896 */
Chris Masone02119d2008-09-05 16:13:11 -0400897static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400898 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500899{
Chris Masonca7a79a2008-05-12 12:59:19 -0400900 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500901 if (slot < 0)
902 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400903 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500904 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400905
906 BUG_ON(level == 0);
907
Chris Masondb945352007-10-15 16:15:53 -0400908 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400909 btrfs_level_size(root, level - 1),
910 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500911}
912
Chris Masond352ac62008-09-29 15:18:18 -0400913/*
914 * node level balancing, used to make sure nodes are in proper order for
915 * item deletion. We balance from the top down, so we have to make sure
916 * that a deletion won't leave an node completely empty later on.
917 */
Chris Masone02119d2008-09-05 16:13:11 -0400918static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500919 struct btrfs_root *root,
920 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500921{
Chris Mason5f39d392007-10-15 16:14:19 -0400922 struct extent_buffer *right = NULL;
923 struct extent_buffer *mid;
924 struct extent_buffer *left = NULL;
925 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500926 int ret = 0;
927 int wret;
928 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500929 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500930 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500931
932 if (level == 0)
933 return 0;
934
Chris Mason5f39d392007-10-15 16:14:19 -0400935 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500936
Chris Masonbd681512011-07-16 15:23:14 -0400937 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
938 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500939 WARN_ON(btrfs_header_generation(mid) != trans->transid);
940
Chris Mason1d4f8a02007-03-13 09:28:32 -0400941 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500942
Li Zefana05a9bb2011-09-06 16:55:34 +0800943 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400944 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800945 pslot = path->slots[level + 1];
946 }
Chris Masonbb803952007-03-01 12:04:21 -0500947
Chris Mason40689472007-03-17 14:29:23 -0400948 /*
949 * deal with the case where there is only one pointer in the root
950 * by promoting the node below to a root
951 */
Chris Mason5f39d392007-10-15 16:14:19 -0400952 if (!parent) {
953 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500956 return 0;
957
958 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400959 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -0700960 if (!child) {
961 ret = -EROFS;
962 btrfs_std_error(root->fs_info, ret);
963 goto enospc;
964 }
965
Chris Mason925baed2008-06-25 16:01:30 -0400966 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500967 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400968 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400969 if (ret) {
970 btrfs_tree_unlock(child);
971 free_extent_buffer(child);
972 goto enospc;
973 }
Yan2f375ab2008-02-01 14:58:07 -0500974
Chris Mason240f62c2011-03-23 14:54:42 -0400975 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400976
Chris Mason0b86a832008-03-24 15:01:56 -0400977 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400978 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500979
Chris Mason925baed2008-06-25 16:01:30 -0400980 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500981 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400982 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400983 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500984 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400985 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400986
987 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200988 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500989 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -0500990 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400991 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500992 }
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400994 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500995 return 0;
996
Andi Kleen559af822010-10-29 15:14:37 -0400997 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400998
Chris Mason5f39d392007-10-15 16:14:19 -0400999 left = read_node_slot(root, parent, pslot - 1);
1000 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001001 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001002 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001003 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001004 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001005 if (wret) {
1006 ret = wret;
1007 goto enospc;
1008 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001009 }
Chris Mason5f39d392007-10-15 16:14:19 -04001010 right = read_node_slot(root, parent, pslot + 1);
1011 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001012 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001013 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001014 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001015 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001016 if (wret) {
1017 ret = wret;
1018 goto enospc;
1019 }
1020 }
1021
1022 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001023 if (left) {
1024 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001025 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001026 if (wret < 0)
1027 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001028 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001029 }
Chris Mason79f95c82007-03-01 15:16:26 -05001030
1031 /*
1032 * then try to empty the right most buffer into the middle
1033 */
Chris Mason5f39d392007-10-15 16:14:19 -04001034 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001035 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001036 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001037 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001038 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001039 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001040 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001041 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001042 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001043 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001044 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001045 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001046 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001047 struct btrfs_disk_key right_key;
1048 btrfs_node_key(right, &right_key, 0);
1049 btrfs_set_node_key(parent, &right_key, pslot + 1);
1050 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001051 }
1052 }
Chris Mason5f39d392007-10-15 16:14:19 -04001053 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001054 /*
1055 * we're not allowed to leave a node with one item in the
1056 * tree during a delete. A deletion from lower in the tree
1057 * could try to delete the only pointer in this node.
1058 * So, pull some keys from the left.
1059 * There has to be a left pointer at this point because
1060 * otherwise we would have pulled some pointers from the
1061 * right
1062 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001063 if (!left) {
1064 ret = -EROFS;
1065 btrfs_std_error(root->fs_info, ret);
1066 goto enospc;
1067 }
Chris Mason5f39d392007-10-15 16:14:19 -04001068 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001069 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001070 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001071 goto enospc;
1072 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001073 if (wret == 1) {
1074 wret = push_node_left(trans, root, left, mid, 1);
1075 if (wret < 0)
1076 ret = wret;
1077 }
Chris Mason79f95c82007-03-01 15:16:26 -05001078 BUG_ON(wret == 1);
1079 }
Chris Mason5f39d392007-10-15 16:14:19 -04001080 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001081 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001082 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001083 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001084 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001085 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001086 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001087 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001088 } else {
1089 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001090 struct btrfs_disk_key mid_key;
1091 btrfs_node_key(mid, &mid_key, 0);
1092 btrfs_set_node_key(parent, &mid_key, pslot);
1093 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001094 }
Chris Masonbb803952007-03-01 12:04:21 -05001095
Chris Mason79f95c82007-03-01 15:16:26 -05001096 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001097 if (left) {
1098 if (btrfs_header_nritems(left) > orig_slot) {
1099 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001100 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001101 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001102 path->slots[level + 1] -= 1;
1103 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001104 if (mid) {
1105 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001106 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001107 }
Chris Masonbb803952007-03-01 12:04:21 -05001108 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001109 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001110 path->slots[level] = orig_slot;
1111 }
1112 }
Chris Mason79f95c82007-03-01 15:16:26 -05001113 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001114 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001115 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001116 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001117enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001118 if (right) {
1119 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001120 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001121 }
1122 if (left) {
1123 if (path->nodes[level] != left)
1124 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001125 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001126 }
Chris Masonbb803952007-03-01 12:04:21 -05001127 return ret;
1128}
1129
Chris Masond352ac62008-09-29 15:18:18 -04001130/* Node balancing for insertion. Here we only split or push nodes around
1131 * when they are completely full. This is also done top down, so we
1132 * have to be pessimistic.
1133 */
Chris Masond3977122009-01-05 21:25:51 -05001134static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001135 struct btrfs_root *root,
1136 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001137{
Chris Mason5f39d392007-10-15 16:14:19 -04001138 struct extent_buffer *right = NULL;
1139 struct extent_buffer *mid;
1140 struct extent_buffer *left = NULL;
1141 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001142 int ret = 0;
1143 int wret;
1144 int pslot;
1145 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001146
1147 if (level == 0)
1148 return 1;
1149
Chris Mason5f39d392007-10-15 16:14:19 -04001150 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001151 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001152
Li Zefana05a9bb2011-09-06 16:55:34 +08001153 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001154 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001155 pslot = path->slots[level + 1];
1156 }
Chris Masone66f7092007-04-20 13:16:02 -04001157
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001159 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001160
Chris Mason5f39d392007-10-15 16:14:19 -04001161 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001162
1163 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001164 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001165 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001166
1167 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001168 btrfs_set_lock_blocking(left);
1169
Chris Mason5f39d392007-10-15 16:14:19 -04001170 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001171 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1172 wret = 1;
1173 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001174 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001175 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 if (ret)
1177 wret = 1;
1178 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001179 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001180 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001182 }
Chris Masone66f7092007-04-20 13:16:02 -04001183 if (wret < 0)
1184 ret = wret;
1185 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001187 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001188 btrfs_node_key(mid, &disk_key, 0);
1189 btrfs_set_node_key(parent, &disk_key, pslot);
1190 btrfs_mark_buffer_dirty(parent);
1191 if (btrfs_header_nritems(left) > orig_slot) {
1192 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001193 path->slots[level + 1] -= 1;
1194 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001195 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001196 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001197 } else {
1198 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001199 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001200 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001201 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001202 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001203 }
Chris Masone66f7092007-04-20 13:16:02 -04001204 return 0;
1205 }
Chris Mason925baed2008-06-25 16:01:30 -04001206 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001207 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001208 }
Chris Mason925baed2008-06-25 16:01:30 -04001209 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001210
1211 /*
1212 * then try to empty the right most buffer into the middle
1213 */
Chris Mason5f39d392007-10-15 16:14:19 -04001214 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001215 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001216
Chris Mason925baed2008-06-25 16:01:30 -04001217 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001218 btrfs_set_lock_blocking(right);
1219
Chris Mason5f39d392007-10-15 16:14:19 -04001220 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001221 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1222 wret = 1;
1223 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001224 ret = btrfs_cow_block(trans, root, right,
1225 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001226 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001227 if (ret)
1228 wret = 1;
1229 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001230 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001231 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001232 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001233 }
Chris Masone66f7092007-04-20 13:16:02 -04001234 if (wret < 0)
1235 ret = wret;
1236 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001237 struct btrfs_disk_key disk_key;
1238
1239 btrfs_node_key(right, &disk_key, 0);
1240 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1241 btrfs_mark_buffer_dirty(parent);
1242
1243 if (btrfs_header_nritems(mid) <= orig_slot) {
1244 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001245 path->slots[level + 1] += 1;
1246 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001247 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001248 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001249 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001250 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001251 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001252 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001253 }
Chris Masone66f7092007-04-20 13:16:02 -04001254 return 0;
1255 }
Chris Mason925baed2008-06-25 16:01:30 -04001256 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001257 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001258 }
Chris Masone66f7092007-04-20 13:16:02 -04001259 return 1;
1260}
1261
Chris Mason74123bd2007-02-02 11:05:29 -05001262/*
Chris Masond352ac62008-09-29 15:18:18 -04001263 * readahead one full node of leaves, finding things that are close
1264 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001265 */
Chris Masonc8c42862009-04-03 10:14:18 -04001266static void reada_for_search(struct btrfs_root *root,
1267 struct btrfs_path *path,
1268 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001269{
Chris Mason5f39d392007-10-15 16:14:19 -04001270 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001271 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001272 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001273 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001274 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001275 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001276 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001278 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001279 u32 nr;
1280 u32 blocksize;
1281 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001282
Chris Masona6b6e752007-10-15 16:22:39 -04001283 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001284 return;
1285
Chris Mason6702ed42007-08-07 16:15:09 -04001286 if (!path->nodes[level])
1287 return;
1288
Chris Mason5f39d392007-10-15 16:14:19 -04001289 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001290
Chris Mason3c69fae2007-08-07 15:52:22 -04001291 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001292 blocksize = btrfs_level_size(root, level - 1);
1293 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001294 if (eb) {
1295 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001296 return;
1297 }
1298
Chris Masona7175312009-01-22 09:23:10 -05001299 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001300
Chris Mason5f39d392007-10-15 16:14:19 -04001301 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001302 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001303
Chris Masond3977122009-01-05 21:25:51 -05001304 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001305 if (direction < 0) {
1306 if (nr == 0)
1307 break;
1308 nr--;
1309 } else if (direction > 0) {
1310 nr++;
1311 if (nr >= nritems)
1312 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001313 }
Chris Mason01f46652007-12-21 16:24:26 -05001314 if (path->reada < 0 && objectid) {
1315 btrfs_node_key(node, &disk_key, nr);
1316 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1317 break;
1318 }
Chris Mason6b800532007-10-15 16:17:34 -04001319 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001320 if ((search <= target && target - search <= 65536) ||
1321 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001322 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001323 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001324 nread += blocksize;
1325 }
1326 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001327 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001328 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001329 }
1330}
Chris Mason925baed2008-06-25 16:01:30 -04001331
Chris Masond352ac62008-09-29 15:18:18 -04001332/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001333 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1334 * cache
1335 */
1336static noinline int reada_for_balance(struct btrfs_root *root,
1337 struct btrfs_path *path, int level)
1338{
1339 int slot;
1340 int nritems;
1341 struct extent_buffer *parent;
1342 struct extent_buffer *eb;
1343 u64 gen;
1344 u64 block1 = 0;
1345 u64 block2 = 0;
1346 int ret = 0;
1347 int blocksize;
1348
Chris Mason8c594ea2009-04-20 15:50:10 -04001349 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001350 if (!parent)
1351 return 0;
1352
1353 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001354 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001355 blocksize = btrfs_level_size(root, level);
1356
1357 if (slot > 0) {
1358 block1 = btrfs_node_blockptr(parent, slot - 1);
1359 gen = btrfs_node_ptr_generation(parent, slot - 1);
1360 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001361 /*
1362 * if we get -eagain from btrfs_buffer_uptodate, we
1363 * don't want to return eagain here. That will loop
1364 * forever
1365 */
1366 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001367 block1 = 0;
1368 free_extent_buffer(eb);
1369 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001370 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001371 block2 = btrfs_node_blockptr(parent, slot + 1);
1372 gen = btrfs_node_ptr_generation(parent, slot + 1);
1373 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001374 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001375 block2 = 0;
1376 free_extent_buffer(eb);
1377 }
1378 if (block1 || block2) {
1379 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001380
1381 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001382 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001383
1384 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001385 if (block1)
1386 readahead_tree_block(root, block1, blocksize, 0);
1387 if (block2)
1388 readahead_tree_block(root, block2, blocksize, 0);
1389
1390 if (block1) {
1391 eb = read_tree_block(root, block1, blocksize, 0);
1392 free_extent_buffer(eb);
1393 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001394 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001395 eb = read_tree_block(root, block2, blocksize, 0);
1396 free_extent_buffer(eb);
1397 }
1398 }
1399 return ret;
1400}
1401
1402
1403/*
Chris Masond3977122009-01-05 21:25:51 -05001404 * when we walk down the tree, it is usually safe to unlock the higher layers
1405 * in the tree. The exceptions are when our path goes through slot 0, because
1406 * operations on the tree might require changing key pointers higher up in the
1407 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001408 *
Chris Masond3977122009-01-05 21:25:51 -05001409 * callers might also have set path->keep_locks, which tells this code to keep
1410 * the lock if the path points to the last slot in the block. This is part of
1411 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001412 *
Chris Masond3977122009-01-05 21:25:51 -05001413 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1414 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001415 */
Chris Masone02119d2008-09-05 16:13:11 -04001416static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001417 int lowest_unlock, int min_write_lock_level,
1418 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001419{
1420 int i;
1421 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001422 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001423 struct extent_buffer *t;
1424
1425 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1426 if (!path->nodes[i])
1427 break;
1428 if (!path->locks[i])
1429 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001430 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001431 skip_level = i + 1;
1432 continue;
1433 }
Chris Mason051e1b92008-06-25 16:01:30 -04001434 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001435 u32 nritems;
1436 t = path->nodes[i];
1437 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001438 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001439 skip_level = i + 1;
1440 continue;
1441 }
1442 }
Chris Mason051e1b92008-06-25 16:01:30 -04001443 if (skip_level < i && i >= lowest_unlock)
1444 no_skips = 1;
1445
Chris Mason925baed2008-06-25 16:01:30 -04001446 t = path->nodes[i];
1447 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001448 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001449 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001450 if (write_lock_level &&
1451 i > min_write_lock_level &&
1452 i <= *write_lock_level) {
1453 *write_lock_level = i - 1;
1454 }
Chris Mason925baed2008-06-25 16:01:30 -04001455 }
1456 }
1457}
1458
Chris Mason3c69fae2007-08-07 15:52:22 -04001459/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001460 * This releases any locks held in the path starting at level and
1461 * going all the way up to the root.
1462 *
1463 * btrfs_search_slot will keep the lock held on higher nodes in a few
1464 * corner cases, such as COW of the block at slot zero in the node. This
1465 * ignores those rules, and it should only be called when there are no
1466 * more updates to be done higher up in the tree.
1467 */
1468noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1469{
1470 int i;
1471
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001472 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001473 return;
1474
1475 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1476 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001477 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001478 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001479 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001480 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001481 path->locks[i] = 0;
1482 }
1483}
1484
1485/*
Chris Masonc8c42862009-04-03 10:14:18 -04001486 * helper function for btrfs_search_slot. The goal is to find a block
1487 * in cache without setting the path to blocking. If we find the block
1488 * we return zero and the path is unchanged.
1489 *
1490 * If we can't find the block, we set the path blocking and do some
1491 * reada. -EAGAIN is returned and the search must be repeated.
1492 */
1493static int
1494read_block_for_search(struct btrfs_trans_handle *trans,
1495 struct btrfs_root *root, struct btrfs_path *p,
1496 struct extent_buffer **eb_ret, int level, int slot,
1497 struct btrfs_key *key)
1498{
1499 u64 blocknr;
1500 u64 gen;
1501 u32 blocksize;
1502 struct extent_buffer *b = *eb_ret;
1503 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001504 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001505
1506 blocknr = btrfs_node_blockptr(b, slot);
1507 gen = btrfs_node_ptr_generation(b, slot);
1508 blocksize = btrfs_level_size(root, level - 1);
1509
1510 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001511 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04001512 /* first we do an atomic uptodate check */
1513 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
1514 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001515 /*
1516 * we found an up to date block without
1517 * sleeping, return
1518 * right away
1519 */
1520 *eb_ret = tmp;
1521 return 0;
1522 }
1523 /* the pages were up to date, but we failed
1524 * the generation number check. Do a full
1525 * read for the generation number that is correct.
1526 * We must do this without dropping locks so
1527 * we can trust our generation number
1528 */
1529 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001530 btrfs_set_path_blocking(p);
1531
Chris Masonb9fab912012-05-06 07:23:47 -04001532 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04001533 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04001534 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001535 *eb_ret = tmp;
1536 return 0;
1537 }
1538 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001539 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001540 return -EIO;
1541 }
Chris Masonc8c42862009-04-03 10:14:18 -04001542 }
1543
1544 /*
1545 * reduce lock contention at high levels
1546 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001547 * we read. Don't release the lock on the current
1548 * level because we need to walk this node to figure
1549 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001550 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001551 btrfs_unlock_up_safe(p, level + 1);
1552 btrfs_set_path_blocking(p);
1553
Chris Masoncb449212010-10-24 11:01:27 -04001554 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001555 if (p->reada)
1556 reada_for_search(root, p, level, slot, key->objectid);
1557
David Sterbab3b4aa72011-04-21 01:20:15 +02001558 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001559
1560 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001561 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001562 if (tmp) {
1563 /*
1564 * If the read above didn't mark this buffer up to date,
1565 * it will never end up being up to date. Set ret to EIO now
1566 * and give up so that our caller doesn't loop forever
1567 * on our EAGAINs.
1568 */
Chris Masonb9fab912012-05-06 07:23:47 -04001569 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04001570 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001571 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001572 }
1573 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001574}
1575
1576/*
1577 * helper function for btrfs_search_slot. This does all of the checks
1578 * for node-level blocks and does any balancing required based on
1579 * the ins_len.
1580 *
1581 * If no extra work was required, zero is returned. If we had to
1582 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1583 * start over
1584 */
1585static int
1586setup_nodes_for_search(struct btrfs_trans_handle *trans,
1587 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001588 struct extent_buffer *b, int level, int ins_len,
1589 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001590{
1591 int ret;
1592 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1593 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1594 int sret;
1595
Chris Masonbd681512011-07-16 15:23:14 -04001596 if (*write_lock_level < level + 1) {
1597 *write_lock_level = level + 1;
1598 btrfs_release_path(p);
1599 goto again;
1600 }
1601
Chris Masonc8c42862009-04-03 10:14:18 -04001602 sret = reada_for_balance(root, p, level);
1603 if (sret)
1604 goto again;
1605
1606 btrfs_set_path_blocking(p);
1607 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001608 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001609
1610 BUG_ON(sret > 0);
1611 if (sret) {
1612 ret = sret;
1613 goto done;
1614 }
1615 b = p->nodes[level];
1616 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001617 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001618 int sret;
1619
Chris Masonbd681512011-07-16 15:23:14 -04001620 if (*write_lock_level < level + 1) {
1621 *write_lock_level = level + 1;
1622 btrfs_release_path(p);
1623 goto again;
1624 }
1625
Chris Masonc8c42862009-04-03 10:14:18 -04001626 sret = reada_for_balance(root, p, level);
1627 if (sret)
1628 goto again;
1629
1630 btrfs_set_path_blocking(p);
1631 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001632 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001633
1634 if (sret) {
1635 ret = sret;
1636 goto done;
1637 }
1638 b = p->nodes[level];
1639 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001640 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001641 goto again;
1642 }
1643 BUG_ON(btrfs_header_nritems(b) == 1);
1644 }
1645 return 0;
1646
1647again:
1648 ret = -EAGAIN;
1649done:
1650 return ret;
1651}
1652
1653/*
Chris Mason74123bd2007-02-02 11:05:29 -05001654 * look for key in the tree. path is filled in with nodes along the way
1655 * if key is found, we return zero and you can find the item in the leaf
1656 * level of the path (level 0)
1657 *
1658 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001659 * be inserted, and 1 is returned. If there are other errors during the
1660 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001661 *
1662 * if ins_len > 0, nodes and leaves will be split as we walk down the
1663 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1664 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001665 */
Chris Masone089f052007-03-16 16:20:31 -04001666int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1667 *root, struct btrfs_key *key, struct btrfs_path *p, int
1668 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001669{
Chris Mason5f39d392007-10-15 16:14:19 -04001670 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001671 int slot;
1672 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001673 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001674 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001675 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001676 int root_lock;
1677 /* everything at write_lock_level or lower must be write locked */
1678 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001679 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001680 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001681
Chris Mason6702ed42007-08-07 16:15:09 -04001682 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001683 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001684 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001685
Chris Masonbd681512011-07-16 15:23:14 -04001686 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001687 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001688
Chris Masonbd681512011-07-16 15:23:14 -04001689 /* when we are removing items, we might have to go up to level
1690 * two as we update tree pointers Make sure we keep write
1691 * for those levels as well
1692 */
1693 write_lock_level = 2;
1694 } else if (ins_len > 0) {
1695 /*
1696 * for inserting items, make sure we have a write lock on
1697 * level 1 so we can update keys
1698 */
1699 write_lock_level = 1;
1700 }
1701
1702 if (!cow)
1703 write_lock_level = -1;
1704
1705 if (cow && (p->keep_locks || p->lowest_level))
1706 write_lock_level = BTRFS_MAX_LEVEL;
1707
Chris Masonf7c79f32012-03-19 15:54:38 -04001708 min_write_lock_level = write_lock_level;
1709
Chris Masonbb803952007-03-01 12:04:21 -05001710again:
Chris Masonbd681512011-07-16 15:23:14 -04001711 /*
1712 * we try very hard to do read locks on the root
1713 */
1714 root_lock = BTRFS_READ_LOCK;
1715 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001716 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001717 /*
1718 * the commit roots are read only
1719 * so we always do read locks
1720 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721 b = root->commit_root;
1722 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001723 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001724 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001725 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001726 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001727 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001728 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001729 level = btrfs_header_level(b);
1730 } else {
1731 /* we don't know the level of the root node
1732 * until we actually have it read locked
1733 */
1734 b = btrfs_read_lock_root_node(root);
1735 level = btrfs_header_level(b);
1736 if (level <= write_lock_level) {
1737 /* whoops, must trade for write lock */
1738 btrfs_tree_read_unlock(b);
1739 free_extent_buffer(b);
1740 b = btrfs_lock_root_node(root);
1741 root_lock = BTRFS_WRITE_LOCK;
1742
1743 /* the level might have changed, check again */
1744 level = btrfs_header_level(b);
1745 }
1746 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001747 }
Chris Masonbd681512011-07-16 15:23:14 -04001748 p->nodes[level] = b;
1749 if (!p->skip_locking)
1750 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001751
Chris Masoneb60cea2007-02-02 09:18:22 -05001752 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001753 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001754
1755 /*
1756 * setup the path here so we can release it under lock
1757 * contention with the cow code
1758 */
Chris Mason02217ed2007-03-02 16:08:05 -05001759 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001760 /*
1761 * if we don't really need to cow this block
1762 * then we don't want to set the path blocking,
1763 * so we test it here
1764 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001765 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001766 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001767
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768 btrfs_set_path_blocking(p);
1769
Chris Masonbd681512011-07-16 15:23:14 -04001770 /*
1771 * must have write locks on this node and the
1772 * parent
1773 */
1774 if (level + 1 > write_lock_level) {
1775 write_lock_level = level + 1;
1776 btrfs_release_path(p);
1777 goto again;
1778 }
1779
Yan Zheng33c66f42009-07-22 09:59:00 -04001780 err = btrfs_cow_block(trans, root, b,
1781 p->nodes[level + 1],
1782 p->slots[level + 1], &b);
1783 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001784 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001785 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001786 }
Chris Mason02217ed2007-03-02 16:08:05 -05001787 }
Chris Mason65b51a02008-08-01 15:11:20 -04001788cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001789 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001790
Chris Masoneb60cea2007-02-02 09:18:22 -05001791 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001792 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001793
1794 /*
1795 * we have a lock on b and as long as we aren't changing
1796 * the tree, there is no way to for the items in b to change.
1797 * It is safe to drop the lock on our parent before we
1798 * go through the expensive btree search on b.
1799 *
1800 * If cow is true, then we might be changing slot zero,
1801 * which may require changing the parent. So, we can't
1802 * drop the lock until after we know which slot we're
1803 * operating on.
1804 */
1805 if (!cow)
1806 btrfs_unlock_up_safe(p, level + 1);
1807
Chris Mason5f39d392007-10-15 16:14:19 -04001808 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001809
Chris Mason5f39d392007-10-15 16:14:19 -04001810 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001811 int dec = 0;
1812 if (ret && slot > 0) {
1813 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001814 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001815 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001816 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001817 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001818 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001820 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 if (err) {
1822 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001823 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001824 }
Chris Masonc8c42862009-04-03 10:14:18 -04001825 b = p->nodes[level];
1826 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001827
Chris Masonbd681512011-07-16 15:23:14 -04001828 /*
1829 * slot 0 is special, if we change the key
1830 * we have to update the parent pointer
1831 * which means we must have a write lock
1832 * on the parent
1833 */
1834 if (slot == 0 && cow &&
1835 write_lock_level < level + 1) {
1836 write_lock_level = level + 1;
1837 btrfs_release_path(p);
1838 goto again;
1839 }
1840
Chris Masonf7c79f32012-03-19 15:54:38 -04001841 unlock_up(p, level, lowest_unlock,
1842 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001843
Chris Mason925baed2008-06-25 16:01:30 -04001844 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001845 if (dec)
1846 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001847 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001848 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001849
Yan Zheng33c66f42009-07-22 09:59:00 -04001850 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001851 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001852 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001853 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001854 if (err) {
1855 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001856 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001857 }
Chris Mason76a05b32009-05-14 13:24:30 -04001858
Chris Masonb4ce94d2009-02-04 09:25:08 -05001859 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001860 level = btrfs_header_level(b);
1861 if (level <= write_lock_level) {
1862 err = btrfs_try_tree_write_lock(b);
1863 if (!err) {
1864 btrfs_set_path_blocking(p);
1865 btrfs_tree_lock(b);
1866 btrfs_clear_path_blocking(p, b,
1867 BTRFS_WRITE_LOCK);
1868 }
1869 p->locks[level] = BTRFS_WRITE_LOCK;
1870 } else {
1871 err = btrfs_try_tree_read_lock(b);
1872 if (!err) {
1873 btrfs_set_path_blocking(p);
1874 btrfs_tree_read_lock(b);
1875 btrfs_clear_path_blocking(p, b,
1876 BTRFS_READ_LOCK);
1877 }
1878 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001879 }
Chris Masonbd681512011-07-16 15:23:14 -04001880 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001881 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001882 } else {
1883 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001884 if (ins_len > 0 &&
1885 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001886 if (write_lock_level < 1) {
1887 write_lock_level = 1;
1888 btrfs_release_path(p);
1889 goto again;
1890 }
1891
Chris Masonb4ce94d2009-02-04 09:25:08 -05001892 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001893 err = split_leaf(trans, root, key,
1894 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001895 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001896
Yan Zheng33c66f42009-07-22 09:59:00 -04001897 BUG_ON(err > 0);
1898 if (err) {
1899 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001900 goto done;
1901 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001902 }
Chris Mason459931e2008-12-10 09:10:46 -05001903 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04001904 unlock_up(p, level, lowest_unlock,
1905 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04001906 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001907 }
1908 }
Chris Mason65b51a02008-08-01 15:11:20 -04001909 ret = 1;
1910done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001911 /*
1912 * we don't really know what they plan on doing with the path
1913 * from here on, so for now just mark it as blocking
1914 */
Chris Masonb9473432009-03-13 11:00:37 -04001915 if (!p->leave_spinning)
1916 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001917 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001918 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001919 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001920}
1921
Chris Mason74123bd2007-02-02 11:05:29 -05001922/*
1923 * adjust the pointers going up the tree, starting at level
1924 * making sure the right key of each node is points to 'key'.
1925 * This is used after shifting pointers to the left, so it stops
1926 * fixing up pointers when a given leaf/node is not in slot 0 of the
1927 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001928 *
Chris Mason74123bd2007-02-02 11:05:29 -05001929 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001930static void fixup_low_keys(struct btrfs_trans_handle *trans,
1931 struct btrfs_root *root, struct btrfs_path *path,
1932 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001933{
1934 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001935 struct extent_buffer *t;
1936
Chris Mason234b63a2007-03-13 10:46:10 -04001937 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001939 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001940 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001941 t = path->nodes[i];
1942 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001943 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001944 if (tslot != 0)
1945 break;
1946 }
1947}
1948
Chris Mason74123bd2007-02-02 11:05:29 -05001949/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001950 * update item key.
1951 *
1952 * This function isn't completely safe. It's the caller's responsibility
1953 * that the new key won't break the order
1954 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001955void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1956 struct btrfs_root *root, struct btrfs_path *path,
1957 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001958{
1959 struct btrfs_disk_key disk_key;
1960 struct extent_buffer *eb;
1961 int slot;
1962
1963 eb = path->nodes[0];
1964 slot = path->slots[0];
1965 if (slot > 0) {
1966 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001967 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001968 }
1969 if (slot < btrfs_header_nritems(eb) - 1) {
1970 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001971 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001972 }
1973
1974 btrfs_cpu_key_to_disk(&disk_key, new_key);
1975 btrfs_set_item_key(eb, &disk_key, slot);
1976 btrfs_mark_buffer_dirty(eb);
1977 if (slot == 0)
1978 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001979}
1980
1981/*
Chris Mason74123bd2007-02-02 11:05:29 -05001982 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001983 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001984 *
1985 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1986 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001987 */
Chris Mason98ed5172008-01-03 10:01:48 -05001988static int push_node_left(struct btrfs_trans_handle *trans,
1989 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001990 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001991{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001992 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001993 int src_nritems;
1994 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001995 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001996
Chris Mason5f39d392007-10-15 16:14:19 -04001997 src_nritems = btrfs_header_nritems(src);
1998 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001999 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002000 WARN_ON(btrfs_header_generation(src) != trans->transid);
2001 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002002
Chris Masonbce4eae2008-04-24 14:42:46 -04002003 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002004 return 1;
2005
Chris Masond3977122009-01-05 21:25:51 -05002006 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002007 return 1;
2008
Chris Masonbce4eae2008-04-24 14:42:46 -04002009 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002010 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002011 if (push_items < src_nritems) {
2012 /* leave at least 8 pointers in the node if
2013 * we aren't going to empty it
2014 */
2015 if (src_nritems - push_items < 8) {
2016 if (push_items <= 8)
2017 return 1;
2018 push_items -= 8;
2019 }
2020 }
2021 } else
2022 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002023
Chris Mason5f39d392007-10-15 16:14:19 -04002024 copy_extent_buffer(dst, src,
2025 btrfs_node_key_ptr_offset(dst_nritems),
2026 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002027 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002028
Chris Masonbb803952007-03-01 12:04:21 -05002029 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002030 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2031 btrfs_node_key_ptr_offset(push_items),
2032 (src_nritems - push_items) *
2033 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002034 }
Chris Mason5f39d392007-10-15 16:14:19 -04002035 btrfs_set_header_nritems(src, src_nritems - push_items);
2036 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2037 btrfs_mark_buffer_dirty(src);
2038 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002039
Chris Masonbb803952007-03-01 12:04:21 -05002040 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002041}
2042
Chris Mason97571fd2007-02-24 13:39:08 -05002043/*
Chris Mason79f95c82007-03-01 15:16:26 -05002044 * try to push data from one node into the next node right in the
2045 * tree.
2046 *
2047 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2048 * error, and > 0 if there was no room in the right hand block.
2049 *
2050 * this will only push up to 1/2 the contents of the left node over
2051 */
Chris Mason5f39d392007-10-15 16:14:19 -04002052static int balance_node_right(struct btrfs_trans_handle *trans,
2053 struct btrfs_root *root,
2054 struct extent_buffer *dst,
2055 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002056{
Chris Mason79f95c82007-03-01 15:16:26 -05002057 int push_items = 0;
2058 int max_push;
2059 int src_nritems;
2060 int dst_nritems;
2061 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002062
Chris Mason7bb86312007-12-11 09:25:06 -05002063 WARN_ON(btrfs_header_generation(src) != trans->transid);
2064 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2065
Chris Mason5f39d392007-10-15 16:14:19 -04002066 src_nritems = btrfs_header_nritems(src);
2067 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002068 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002069 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002070 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002071
Chris Masond3977122009-01-05 21:25:51 -05002072 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002073 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002074
2075 max_push = src_nritems / 2 + 1;
2076 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002077 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002078 return 1;
Yan252c38f2007-08-29 09:11:44 -04002079
Chris Mason79f95c82007-03-01 15:16:26 -05002080 if (max_push < push_items)
2081 push_items = max_push;
2082
Chris Mason5f39d392007-10-15 16:14:19 -04002083 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2084 btrfs_node_key_ptr_offset(0),
2085 (dst_nritems) *
2086 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002087
Chris Mason5f39d392007-10-15 16:14:19 -04002088 copy_extent_buffer(dst, src,
2089 btrfs_node_key_ptr_offset(0),
2090 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002091 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002092
Chris Mason5f39d392007-10-15 16:14:19 -04002093 btrfs_set_header_nritems(src, src_nritems - push_items);
2094 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002095
Chris Mason5f39d392007-10-15 16:14:19 -04002096 btrfs_mark_buffer_dirty(src);
2097 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002098
Chris Mason79f95c82007-03-01 15:16:26 -05002099 return ret;
2100}
2101
2102/*
Chris Mason97571fd2007-02-24 13:39:08 -05002103 * helper function to insert a new root level in the tree.
2104 * A new node is allocated, and a single item is inserted to
2105 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002106 *
2107 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002108 */
Chris Masond3977122009-01-05 21:25:51 -05002109static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002110 struct btrfs_root *root,
2111 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002112{
Chris Mason7bb86312007-12-11 09:25:06 -05002113 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002114 struct extent_buffer *lower;
2115 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002116 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002117 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002118
2119 BUG_ON(path->nodes[level]);
2120 BUG_ON(path->nodes[level-1] != root->node);
2121
Chris Mason7bb86312007-12-11 09:25:06 -05002122 lower = path->nodes[level-1];
2123 if (level == 1)
2124 btrfs_item_key(lower, &lower_key, 0);
2125 else
2126 btrfs_node_key(lower, &lower_key, 0);
2127
Zheng Yan31840ae2008-09-23 13:14:14 -04002128 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002129 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002130 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002131 if (IS_ERR(c))
2132 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002133
Yan, Zhengf0486c62010-05-16 10:46:25 -04002134 root_add_used(root, root->nodesize);
2135
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002136 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002137 btrfs_set_header_nritems(c, 1);
2138 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002139 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002141 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002143
Chris Mason5f39d392007-10-15 16:14:19 -04002144 write_extent_buffer(c, root->fs_info->fsid,
2145 (unsigned long)btrfs_header_fsid(c),
2146 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002147
2148 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2149 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2150 BTRFS_UUID_SIZE);
2151
Chris Mason5f39d392007-10-15 16:14:19 -04002152 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002153 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002154 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002155 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002156
2157 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002158
2159 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002160
Chris Mason925baed2008-06-25 16:01:30 -04002161 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002162 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002163
2164 /* the super has an extra ref to root->node */
2165 free_extent_buffer(old);
2166
Chris Mason0b86a832008-03-24 15:01:56 -04002167 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 extent_buffer_get(c);
2169 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002170 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002171 path->slots[level] = 0;
2172 return 0;
2173}
2174
Chris Mason74123bd2007-02-02 11:05:29 -05002175/*
2176 * worker function to insert a single pointer in a node.
2177 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002178 *
Chris Mason74123bd2007-02-02 11:05:29 -05002179 * slot and level indicate where you want the key to go, and
2180 * blocknr is the block the key points to.
2181 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002182static void insert_ptr(struct btrfs_trans_handle *trans,
2183 struct btrfs_root *root, struct btrfs_path *path,
2184 struct btrfs_disk_key *key, u64 bytenr,
2185 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002186{
Chris Mason5f39d392007-10-15 16:14:19 -04002187 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002188 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002189
2190 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002191 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002192 lower = path->nodes[level];
2193 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002194 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002195 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002196 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002197 memmove_extent_buffer(lower,
2198 btrfs_node_key_ptr_offset(slot + 1),
2199 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002200 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002201 }
Chris Mason5f39d392007-10-15 16:14:19 -04002202 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002203 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002204 WARN_ON(trans->transid == 0);
2205 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002206 btrfs_set_header_nritems(lower, nritems + 1);
2207 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002208}
2209
Chris Mason97571fd2007-02-24 13:39:08 -05002210/*
2211 * split the node at the specified level in path in two.
2212 * The path is corrected to point to the appropriate node after the split
2213 *
2214 * Before splitting this tries to make some room in the node by pushing
2215 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002216 *
2217 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002218 */
Chris Masone02119d2008-09-05 16:13:11 -04002219static noinline int split_node(struct btrfs_trans_handle *trans,
2220 struct btrfs_root *root,
2221 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002222{
Chris Mason5f39d392007-10-15 16:14:19 -04002223 struct extent_buffer *c;
2224 struct extent_buffer *split;
2225 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002226 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002227 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002228 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002229
Chris Mason5f39d392007-10-15 16:14:19 -04002230 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002231 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002232 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002233 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002234 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002235 if (ret)
2236 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002237 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002238 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002239 c = path->nodes[level];
2240 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002241 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002242 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002243 if (ret < 0)
2244 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002245 }
Chris Masone66f7092007-04-20 13:16:02 -04002246
Chris Mason5f39d392007-10-15 16:14:19 -04002247 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248 mid = (c_nritems + 1) / 2;
2249 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002250
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002251 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002252 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002253 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002254 if (IS_ERR(split))
2255 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002256
Yan, Zhengf0486c62010-05-16 10:46:25 -04002257 root_add_used(root, root->nodesize);
2258
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002260 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002261 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002262 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002263 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002264 btrfs_set_header_owner(split, root->root_key.objectid);
2265 write_extent_buffer(split, root->fs_info->fsid,
2266 (unsigned long)btrfs_header_fsid(split),
2267 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002268 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2269 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2270 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002271
Chris Mason5f39d392007-10-15 16:14:19 -04002272
2273 copy_extent_buffer(split, c,
2274 btrfs_node_key_ptr_offset(0),
2275 btrfs_node_key_ptr_offset(mid),
2276 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2277 btrfs_set_header_nritems(split, c_nritems - mid);
2278 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002279 ret = 0;
2280
Chris Mason5f39d392007-10-15 16:14:19 -04002281 btrfs_mark_buffer_dirty(c);
2282 btrfs_mark_buffer_dirty(split);
2283
Jeff Mahoney143bede2012-03-01 14:56:26 +01002284 insert_ptr(trans, root, path, &disk_key, split->start,
2285 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002286
Chris Mason5de08d72007-02-24 06:24:44 -05002287 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002288 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002289 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002290 free_extent_buffer(c);
2291 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002292 path->slots[level + 1] += 1;
2293 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002294 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002295 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002296 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002297 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002298}
2299
Chris Mason74123bd2007-02-02 11:05:29 -05002300/*
2301 * how many bytes are required to store the items in a leaf. start
2302 * and nr indicate which items in the leaf to check. This totals up the
2303 * space used both by the item structs and the item data
2304 */
Chris Mason5f39d392007-10-15 16:14:19 -04002305static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002306{
2307 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002308 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002309 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002310
2311 if (!nr)
2312 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002313 data_len = btrfs_item_end_nr(l, start);
2314 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002315 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002316 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002317 return data_len;
2318}
2319
Chris Mason74123bd2007-02-02 11:05:29 -05002320/*
Chris Masond4dbff92007-04-04 14:08:15 -04002321 * The space between the end of the leaf items and
2322 * the start of the leaf data. IOW, how much room
2323 * the leaf has left for both items and data
2324 */
Chris Masond3977122009-01-05 21:25:51 -05002325noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002326 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002327{
Chris Mason5f39d392007-10-15 16:14:19 -04002328 int nritems = btrfs_header_nritems(leaf);
2329 int ret;
2330 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2331 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002332 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2333 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002334 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002335 leaf_space_used(leaf, 0, nritems), nritems);
2336 }
2337 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002338}
2339
Chris Mason99d8f832010-07-07 10:51:48 -04002340/*
2341 * min slot controls the lowest index we're willing to push to the
2342 * right. We'll push up to and including min_slot, but no lower
2343 */
Chris Mason44871b12009-03-13 10:04:31 -04002344static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2345 struct btrfs_root *root,
2346 struct btrfs_path *path,
2347 int data_size, int empty,
2348 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002349 int free_space, u32 left_nritems,
2350 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002351{
Chris Mason5f39d392007-10-15 16:14:19 -04002352 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002353 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002354 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002355 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002356 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002357 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002358 int push_space = 0;
2359 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002360 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002361 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002362 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002363 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002364 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002365
Chris Masoncfed81a2012-03-03 07:40:03 -05002366 btrfs_init_map_token(&token);
2367
Chris Mason34a38212007-11-07 13:31:03 -05002368 if (empty)
2369 nr = 0;
2370 else
Chris Mason99d8f832010-07-07 10:51:48 -04002371 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002372
Zheng Yan31840ae2008-09-23 13:14:14 -04002373 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002374 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002375
Chris Mason44871b12009-03-13 10:04:31 -04002376 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002377 i = left_nritems - 1;
2378 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002379 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002380
Zheng Yan31840ae2008-09-23 13:14:14 -04002381 if (!empty && push_items > 0) {
2382 if (path->slots[0] > i)
2383 break;
2384 if (path->slots[0] == i) {
2385 int space = btrfs_leaf_free_space(root, left);
2386 if (space + push_space * 2 > free_space)
2387 break;
2388 }
2389 }
2390
Chris Mason00ec4c52007-02-24 12:47:20 -05002391 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002392 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002393
Chris Masondb945352007-10-15 16:15:53 -04002394 this_item_size = btrfs_item_size(left, item);
2395 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002396 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002397
Chris Mason00ec4c52007-02-24 12:47:20 -05002398 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002399 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002400 if (i == 0)
2401 break;
2402 i--;
Chris Masondb945352007-10-15 16:15:53 -04002403 }
Chris Mason5f39d392007-10-15 16:14:19 -04002404
Chris Mason925baed2008-06-25 16:01:30 -04002405 if (push_items == 0)
2406 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002407
Chris Mason34a38212007-11-07 13:31:03 -05002408 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002409 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002410
Chris Mason00ec4c52007-02-24 12:47:20 -05002411 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002412 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002413
Chris Mason5f39d392007-10-15 16:14:19 -04002414 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002415 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002416
Chris Mason00ec4c52007-02-24 12:47:20 -05002417 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002418 data_end = leaf_data_end(root, right);
2419 memmove_extent_buffer(right,
2420 btrfs_leaf_data(right) + data_end - push_space,
2421 btrfs_leaf_data(right) + data_end,
2422 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2423
Chris Mason00ec4c52007-02-24 12:47:20 -05002424 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002425 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002426 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2427 btrfs_leaf_data(left) + leaf_data_end(root, left),
2428 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002429
2430 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2431 btrfs_item_nr_offset(0),
2432 right_nritems * sizeof(struct btrfs_item));
2433
Chris Mason00ec4c52007-02-24 12:47:20 -05002434 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002435 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2436 btrfs_item_nr_offset(left_nritems - push_items),
2437 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002438
2439 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002440 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002441 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002442 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002443 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002444 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002445 push_space -= btrfs_token_item_size(right, item, &token);
2446 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002447 }
2448
Chris Mason7518a232007-03-12 12:01:18 -04002449 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002450 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002451
Chris Mason34a38212007-11-07 13:31:03 -05002452 if (left_nritems)
2453 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002454 else
2455 clean_tree_block(trans, root, left);
2456
Chris Mason5f39d392007-10-15 16:14:19 -04002457 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002458
Chris Mason5f39d392007-10-15 16:14:19 -04002459 btrfs_item_key(right, &disk_key, 0);
2460 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002461 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002462
Chris Mason00ec4c52007-02-24 12:47:20 -05002463 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002464 if (path->slots[0] >= left_nritems) {
2465 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002466 if (btrfs_header_nritems(path->nodes[0]) == 0)
2467 clean_tree_block(trans, root, path->nodes[0]);
2468 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002469 free_extent_buffer(path->nodes[0]);
2470 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002471 path->slots[1] += 1;
2472 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002473 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002474 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002475 }
2476 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002477
2478out_unlock:
2479 btrfs_tree_unlock(right);
2480 free_extent_buffer(right);
2481 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002482}
Chris Mason925baed2008-06-25 16:01:30 -04002483
Chris Mason00ec4c52007-02-24 12:47:20 -05002484/*
Chris Mason44871b12009-03-13 10:04:31 -04002485 * push some data in the path leaf to the right, trying to free up at
2486 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2487 *
2488 * returns 1 if the push failed because the other node didn't have enough
2489 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002490 *
2491 * this will push starting from min_slot to the end of the leaf. It won't
2492 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002493 */
2494static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002495 *root, struct btrfs_path *path,
2496 int min_data_size, int data_size,
2497 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002498{
2499 struct extent_buffer *left = path->nodes[0];
2500 struct extent_buffer *right;
2501 struct extent_buffer *upper;
2502 int slot;
2503 int free_space;
2504 u32 left_nritems;
2505 int ret;
2506
2507 if (!path->nodes[1])
2508 return 1;
2509
2510 slot = path->slots[1];
2511 upper = path->nodes[1];
2512 if (slot >= btrfs_header_nritems(upper) - 1)
2513 return 1;
2514
2515 btrfs_assert_tree_locked(path->nodes[1]);
2516
2517 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002518 if (right == NULL)
2519 return 1;
2520
Chris Mason44871b12009-03-13 10:04:31 -04002521 btrfs_tree_lock(right);
2522 btrfs_set_lock_blocking(right);
2523
2524 free_space = btrfs_leaf_free_space(root, right);
2525 if (free_space < data_size)
2526 goto out_unlock;
2527
2528 /* cow and double check */
2529 ret = btrfs_cow_block(trans, root, right, upper,
2530 slot + 1, &right);
2531 if (ret)
2532 goto out_unlock;
2533
2534 free_space = btrfs_leaf_free_space(root, right);
2535 if (free_space < data_size)
2536 goto out_unlock;
2537
2538 left_nritems = btrfs_header_nritems(left);
2539 if (left_nritems == 0)
2540 goto out_unlock;
2541
Chris Mason99d8f832010-07-07 10:51:48 -04002542 return __push_leaf_right(trans, root, path, min_data_size, empty,
2543 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002544out_unlock:
2545 btrfs_tree_unlock(right);
2546 free_extent_buffer(right);
2547 return 1;
2548}
2549
2550/*
Chris Mason74123bd2007-02-02 11:05:29 -05002551 * push some data in the path leaf to the left, trying to free up at
2552 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002553 *
2554 * max_slot can put a limit on how far into the leaf we'll push items. The
2555 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2556 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002557 */
Chris Mason44871b12009-03-13 10:04:31 -04002558static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2559 struct btrfs_root *root,
2560 struct btrfs_path *path, int data_size,
2561 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002562 int free_space, u32 right_nritems,
2563 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564{
Chris Mason5f39d392007-10-15 16:14:19 -04002565 struct btrfs_disk_key disk_key;
2566 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002568 int push_space = 0;
2569 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002570 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002571 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002572 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002573 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002574 u32 this_item_size;
2575 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002576 struct btrfs_map_token token;
2577
2578 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002579
Chris Mason34a38212007-11-07 13:31:03 -05002580 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002581 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002582 else
Chris Mason99d8f832010-07-07 10:51:48 -04002583 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002584
2585 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002586 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002587
Zheng Yan31840ae2008-09-23 13:14:14 -04002588 if (!empty && push_items > 0) {
2589 if (path->slots[0] < i)
2590 break;
2591 if (path->slots[0] == i) {
2592 int space = btrfs_leaf_free_space(root, right);
2593 if (space + push_space * 2 > free_space)
2594 break;
2595 }
2596 }
2597
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002599 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002600
2601 this_item_size = btrfs_item_size(right, item);
2602 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002603 break;
Chris Masondb945352007-10-15 16:15:53 -04002604
Chris Masonbe0e5c02007-01-26 15:51:26 -05002605 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002606 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002607 }
Chris Masondb945352007-10-15 16:15:53 -04002608
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002610 ret = 1;
2611 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002612 }
Chris Mason34a38212007-11-07 13:31:03 -05002613 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002614 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002615
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002617 copy_extent_buffer(left, right,
2618 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2619 btrfs_item_nr_offset(0),
2620 push_items * sizeof(struct btrfs_item));
2621
Chris Mason123abc82007-03-14 14:14:43 -04002622 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002623 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002624
2625 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002626 leaf_data_end(root, left) - push_space,
2627 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002628 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002629 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002630 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002631 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002632
Chris Masondb945352007-10-15 16:15:53 -04002633 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002634 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002635 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002636
Chris Mason5f39d392007-10-15 16:14:19 -04002637 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002638
Chris Masoncfed81a2012-03-03 07:40:03 -05002639 ioff = btrfs_token_item_offset(left, item, &token);
2640 btrfs_set_token_item_offset(left, item,
2641 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2642 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002643 }
Chris Mason5f39d392007-10-15 16:14:19 -04002644 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645
2646 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002647 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002648 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2649 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002650 WARN_ON(1);
2651 }
Chris Mason5f39d392007-10-15 16:14:19 -04002652
Chris Mason34a38212007-11-07 13:31:03 -05002653 if (push_items < right_nritems) {
2654 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2655 leaf_data_end(root, right);
2656 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2657 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2658 btrfs_leaf_data(right) +
2659 leaf_data_end(root, right), push_space);
2660
2661 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002662 btrfs_item_nr_offset(push_items),
2663 (btrfs_header_nritems(right) - push_items) *
2664 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002665 }
Yaneef1c492007-11-26 10:58:13 -05002666 right_nritems -= push_items;
2667 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002668 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002669 for (i = 0; i < right_nritems; i++) {
2670 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002671
Chris Masoncfed81a2012-03-03 07:40:03 -05002672 push_space = push_space - btrfs_token_item_size(right,
2673 item, &token);
2674 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002675 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002676
Chris Mason5f39d392007-10-15 16:14:19 -04002677 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002678 if (right_nritems)
2679 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002680 else
2681 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002682
Chris Mason5f39d392007-10-15 16:14:19 -04002683 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002684 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002685
2686 /* then fixup the leaf pointer in the path */
2687 if (path->slots[0] < push_items) {
2688 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002689 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002690 free_extent_buffer(path->nodes[0]);
2691 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002692 path->slots[1] -= 1;
2693 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002694 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002695 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002696 path->slots[0] -= push_items;
2697 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002698 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002699 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002700out:
2701 btrfs_tree_unlock(left);
2702 free_extent_buffer(left);
2703 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704}
2705
Chris Mason74123bd2007-02-02 11:05:29 -05002706/*
Chris Mason44871b12009-03-13 10:04:31 -04002707 * push some data in the path leaf to the left, trying to free up at
2708 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002709 *
2710 * max_slot can put a limit on how far into the leaf we'll push items. The
2711 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2712 * items
Chris Mason44871b12009-03-13 10:04:31 -04002713 */
2714static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002715 *root, struct btrfs_path *path, int min_data_size,
2716 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002717{
2718 struct extent_buffer *right = path->nodes[0];
2719 struct extent_buffer *left;
2720 int slot;
2721 int free_space;
2722 u32 right_nritems;
2723 int ret = 0;
2724
2725 slot = path->slots[1];
2726 if (slot == 0)
2727 return 1;
2728 if (!path->nodes[1])
2729 return 1;
2730
2731 right_nritems = btrfs_header_nritems(right);
2732 if (right_nritems == 0)
2733 return 1;
2734
2735 btrfs_assert_tree_locked(path->nodes[1]);
2736
2737 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002738 if (left == NULL)
2739 return 1;
2740
Chris Mason44871b12009-03-13 10:04:31 -04002741 btrfs_tree_lock(left);
2742 btrfs_set_lock_blocking(left);
2743
2744 free_space = btrfs_leaf_free_space(root, left);
2745 if (free_space < data_size) {
2746 ret = 1;
2747 goto out;
2748 }
2749
2750 /* cow and double check */
2751 ret = btrfs_cow_block(trans, root, left,
2752 path->nodes[1], slot - 1, &left);
2753 if (ret) {
2754 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002755 if (ret == -ENOSPC)
2756 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002757 goto out;
2758 }
2759
2760 free_space = btrfs_leaf_free_space(root, left);
2761 if (free_space < data_size) {
2762 ret = 1;
2763 goto out;
2764 }
2765
Chris Mason99d8f832010-07-07 10:51:48 -04002766 return __push_leaf_left(trans, root, path, min_data_size,
2767 empty, left, free_space, right_nritems,
2768 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002769out:
2770 btrfs_tree_unlock(left);
2771 free_extent_buffer(left);
2772 return ret;
2773}
2774
2775/*
Chris Mason74123bd2007-02-02 11:05:29 -05002776 * split the path's leaf in two, making sure there is at least data_size
2777 * available for the resulting leaf level of the path.
2778 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002779static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2780 struct btrfs_root *root,
2781 struct btrfs_path *path,
2782 struct extent_buffer *l,
2783 struct extent_buffer *right,
2784 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002785{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002786 int data_copy_size;
2787 int rt_data_off;
2788 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002789 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05002790 struct btrfs_map_token token;
2791
2792 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002793
Chris Mason5f39d392007-10-15 16:14:19 -04002794 nritems = nritems - mid;
2795 btrfs_set_header_nritems(right, nritems);
2796 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2797
2798 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2799 btrfs_item_nr_offset(mid),
2800 nritems * sizeof(struct btrfs_item));
2801
2802 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002803 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2804 data_copy_size, btrfs_leaf_data(l) +
2805 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002806
Chris Mason5f39d392007-10-15 16:14:19 -04002807 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2808 btrfs_item_end_nr(l, mid);
2809
2810 for (i = 0; i < nritems; i++) {
2811 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002812 u32 ioff;
2813
Chris Masoncfed81a2012-03-03 07:40:03 -05002814 ioff = btrfs_token_item_offset(right, item, &token);
2815 btrfs_set_token_item_offset(right, item,
2816 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002817 }
Chris Mason74123bd2007-02-02 11:05:29 -05002818
Chris Mason5f39d392007-10-15 16:14:19 -04002819 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002820 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002821 insert_ptr(trans, root, path, &disk_key, right->start,
2822 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002823
2824 btrfs_mark_buffer_dirty(right);
2825 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002826 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002827
Chris Masonbe0e5c02007-01-26 15:51:26 -05002828 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002829 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002830 free_extent_buffer(path->nodes[0]);
2831 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002832 path->slots[0] -= mid;
2833 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002834 } else {
2835 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002836 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002837 }
Chris Mason5f39d392007-10-15 16:14:19 -04002838
Chris Masoneb60cea2007-02-02 09:18:22 -05002839 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002840}
2841
2842/*
Chris Mason99d8f832010-07-07 10:51:48 -04002843 * double splits happen when we need to insert a big item in the middle
2844 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2845 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2846 * A B C
2847 *
2848 * We avoid this by trying to push the items on either side of our target
2849 * into the adjacent leaves. If all goes well we can avoid the double split
2850 * completely.
2851 */
2852static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2853 struct btrfs_root *root,
2854 struct btrfs_path *path,
2855 int data_size)
2856{
2857 int ret;
2858 int progress = 0;
2859 int slot;
2860 u32 nritems;
2861
2862 slot = path->slots[0];
2863
2864 /*
2865 * try to push all the items after our slot into the
2866 * right leaf
2867 */
2868 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2869 if (ret < 0)
2870 return ret;
2871
2872 if (ret == 0)
2873 progress++;
2874
2875 nritems = btrfs_header_nritems(path->nodes[0]);
2876 /*
2877 * our goal is to get our slot at the start or end of a leaf. If
2878 * we've done so we're done
2879 */
2880 if (path->slots[0] == 0 || path->slots[0] == nritems)
2881 return 0;
2882
2883 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2884 return 0;
2885
2886 /* try to push all the items before our slot into the next leaf */
2887 slot = path->slots[0];
2888 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2889 if (ret < 0)
2890 return ret;
2891
2892 if (ret == 0)
2893 progress++;
2894
2895 if (progress)
2896 return 0;
2897 return 1;
2898}
2899
2900/*
Chris Mason44871b12009-03-13 10:04:31 -04002901 * split the path's leaf in two, making sure there is at least data_size
2902 * available for the resulting leaf level of the path.
2903 *
2904 * returns 0 if all went well and < 0 on failure.
2905 */
2906static noinline int split_leaf(struct btrfs_trans_handle *trans,
2907 struct btrfs_root *root,
2908 struct btrfs_key *ins_key,
2909 struct btrfs_path *path, int data_size,
2910 int extend)
2911{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002913 struct extent_buffer *l;
2914 u32 nritems;
2915 int mid;
2916 int slot;
2917 struct extent_buffer *right;
2918 int ret = 0;
2919 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002920 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002921 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002922 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002923
Yan, Zhenga5719522009-09-24 09:17:31 -04002924 l = path->nodes[0];
2925 slot = path->slots[0];
2926 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2927 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2928 return -EOVERFLOW;
2929
Chris Mason44871b12009-03-13 10:04:31 -04002930 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002931 if (data_size) {
2932 wret = push_leaf_right(trans, root, path, data_size,
2933 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002934 if (wret < 0)
2935 return wret;
2936 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002937 wret = push_leaf_left(trans, root, path, data_size,
2938 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002939 if (wret < 0)
2940 return wret;
2941 }
2942 l = path->nodes[0];
2943
2944 /* did the pushes work? */
2945 if (btrfs_leaf_free_space(root, l) >= data_size)
2946 return 0;
2947 }
2948
2949 if (!path->nodes[1]) {
2950 ret = insert_new_root(trans, root, path, 1);
2951 if (ret)
2952 return ret;
2953 }
2954again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002955 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002956 l = path->nodes[0];
2957 slot = path->slots[0];
2958 nritems = btrfs_header_nritems(l);
2959 mid = (nritems + 1) / 2;
2960
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002961 if (mid <= slot) {
2962 if (nritems == 1 ||
2963 leaf_space_used(l, mid, nritems - mid) + data_size >
2964 BTRFS_LEAF_DATA_SIZE(root)) {
2965 if (slot >= nritems) {
2966 split = 0;
2967 } else {
2968 mid = slot;
2969 if (mid != nritems &&
2970 leaf_space_used(l, mid, nritems - mid) +
2971 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002972 if (data_size && !tried_avoid_double)
2973 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002974 split = 2;
2975 }
2976 }
2977 }
2978 } else {
2979 if (leaf_space_used(l, 0, mid) + data_size >
2980 BTRFS_LEAF_DATA_SIZE(root)) {
2981 if (!extend && data_size && slot == 0) {
2982 split = 0;
2983 } else if ((extend || !data_size) && slot == 0) {
2984 mid = 1;
2985 } else {
2986 mid = slot;
2987 if (mid != nritems &&
2988 leaf_space_used(l, mid, nritems - mid) +
2989 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002990 if (data_size && !tried_avoid_double)
2991 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002992 split = 2 ;
2993 }
2994 }
2995 }
2996 }
2997
2998 if (split == 0)
2999 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3000 else
3001 btrfs_item_key(l, &disk_key, mid);
3002
3003 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003004 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003005 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003006 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003007 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003008
3009 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003010
3011 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3012 btrfs_set_header_bytenr(right, right->start);
3013 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003015 btrfs_set_header_owner(right, root->root_key.objectid);
3016 btrfs_set_header_level(right, 0);
3017 write_extent_buffer(right, root->fs_info->fsid,
3018 (unsigned long)btrfs_header_fsid(right),
3019 BTRFS_FSID_SIZE);
3020
3021 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3022 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3023 BTRFS_UUID_SIZE);
3024
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003025 if (split == 0) {
3026 if (mid <= slot) {
3027 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003028 insert_ptr(trans, root, path, &disk_key, right->start,
3029 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003030 btrfs_tree_unlock(path->nodes[0]);
3031 free_extent_buffer(path->nodes[0]);
3032 path->nodes[0] = right;
3033 path->slots[0] = 0;
3034 path->slots[1] += 1;
3035 } else {
3036 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003037 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003038 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003039 btrfs_tree_unlock(path->nodes[0]);
3040 free_extent_buffer(path->nodes[0]);
3041 path->nodes[0] = right;
3042 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003043 if (path->slots[1] == 0)
3044 fixup_low_keys(trans, root, path,
3045 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003046 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003047 btrfs_mark_buffer_dirty(right);
3048 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003049 }
3050
Jeff Mahoney143bede2012-03-01 14:56:26 +01003051 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003052
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003053 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003054 BUG_ON(num_doubles != 0);
3055 num_doubles++;
3056 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003057 }
Chris Mason44871b12009-03-13 10:04:31 -04003058
Jeff Mahoney143bede2012-03-01 14:56:26 +01003059 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003060
3061push_for_double:
3062 push_for_double_split(trans, root, path, data_size);
3063 tried_avoid_double = 1;
3064 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3065 return 0;
3066 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003067}
3068
Yan, Zhengad48fd752009-11-12 09:33:58 +00003069static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3070 struct btrfs_root *root,
3071 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003072{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003073 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003074 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003075 struct btrfs_file_extent_item *fi;
3076 u64 extent_len = 0;
3077 u32 item_size;
3078 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003079
3080 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003081 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3082
3083 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3084 key.type != BTRFS_EXTENT_CSUM_KEY);
3085
3086 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3087 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003088
3089 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003090 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3091 fi = btrfs_item_ptr(leaf, path->slots[0],
3092 struct btrfs_file_extent_item);
3093 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3094 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003095 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003096
Chris Mason459931e2008-12-10 09:10:46 -05003097 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003098 path->search_for_split = 1;
3099 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003100 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003101 if (ret < 0)
3102 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003103
Yan, Zhengad48fd752009-11-12 09:33:58 +00003104 ret = -EAGAIN;
3105 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003106 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003107 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3108 goto err;
3109
Chris Mason109f6ae2010-04-02 09:20:18 -04003110 /* the leaf has changed, it now has room. return now */
3111 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3112 goto err;
3113
Yan, Zhengad48fd752009-11-12 09:33:58 +00003114 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3115 fi = btrfs_item_ptr(leaf, path->slots[0],
3116 struct btrfs_file_extent_item);
3117 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3118 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003119 }
3120
Chris Masonb9473432009-03-13 11:00:37 -04003121 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003122 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003123 if (ret)
3124 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003125
Yan, Zhengad48fd752009-11-12 09:33:58 +00003126 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003127 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003128 return 0;
3129err:
3130 path->keep_locks = 0;
3131 return ret;
3132}
3133
3134static noinline int split_item(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *root,
3136 struct btrfs_path *path,
3137 struct btrfs_key *new_key,
3138 unsigned long split_offset)
3139{
3140 struct extent_buffer *leaf;
3141 struct btrfs_item *item;
3142 struct btrfs_item *new_item;
3143 int slot;
3144 char *buf;
3145 u32 nritems;
3146 u32 item_size;
3147 u32 orig_offset;
3148 struct btrfs_disk_key disk_key;
3149
Chris Masonb9473432009-03-13 11:00:37 -04003150 leaf = path->nodes[0];
3151 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3152
Chris Masonb4ce94d2009-02-04 09:25:08 -05003153 btrfs_set_path_blocking(path);
3154
Chris Mason459931e2008-12-10 09:10:46 -05003155 item = btrfs_item_nr(leaf, path->slots[0]);
3156 orig_offset = btrfs_item_offset(leaf, item);
3157 item_size = btrfs_item_size(leaf, item);
3158
Chris Mason459931e2008-12-10 09:10:46 -05003159 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003160 if (!buf)
3161 return -ENOMEM;
3162
Chris Mason459931e2008-12-10 09:10:46 -05003163 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3164 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003165
Chris Mason459931e2008-12-10 09:10:46 -05003166 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003167 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003168 if (slot != nritems) {
3169 /* shift the items */
3170 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003171 btrfs_item_nr_offset(slot),
3172 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003173 }
3174
3175 btrfs_cpu_key_to_disk(&disk_key, new_key);
3176 btrfs_set_item_key(leaf, &disk_key, slot);
3177
3178 new_item = btrfs_item_nr(leaf, slot);
3179
3180 btrfs_set_item_offset(leaf, new_item, orig_offset);
3181 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3182
3183 btrfs_set_item_offset(leaf, item,
3184 orig_offset + item_size - split_offset);
3185 btrfs_set_item_size(leaf, item, split_offset);
3186
3187 btrfs_set_header_nritems(leaf, nritems + 1);
3188
3189 /* write the data for the start of the original item */
3190 write_extent_buffer(leaf, buf,
3191 btrfs_item_ptr_offset(leaf, path->slots[0]),
3192 split_offset);
3193
3194 /* write the data for the new item */
3195 write_extent_buffer(leaf, buf + split_offset,
3196 btrfs_item_ptr_offset(leaf, slot),
3197 item_size - split_offset);
3198 btrfs_mark_buffer_dirty(leaf);
3199
Yan, Zhengad48fd752009-11-12 09:33:58 +00003200 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003201 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003202 return 0;
3203}
3204
3205/*
3206 * This function splits a single item into two items,
3207 * giving 'new_key' to the new item and splitting the
3208 * old one at split_offset (from the start of the item).
3209 *
3210 * The path may be released by this operation. After
3211 * the split, the path is pointing to the old item. The
3212 * new item is going to be in the same node as the old one.
3213 *
3214 * Note, the item being split must be smaller enough to live alone on
3215 * a tree block with room for one extra struct btrfs_item
3216 *
3217 * This allows us to split the item in place, keeping a lock on the
3218 * leaf the entire time.
3219 */
3220int btrfs_split_item(struct btrfs_trans_handle *trans,
3221 struct btrfs_root *root,
3222 struct btrfs_path *path,
3223 struct btrfs_key *new_key,
3224 unsigned long split_offset)
3225{
3226 int ret;
3227 ret = setup_leaf_for_split(trans, root, path,
3228 sizeof(struct btrfs_item));
3229 if (ret)
3230 return ret;
3231
3232 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003233 return ret;
3234}
3235
3236/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003237 * This function duplicate a item, giving 'new_key' to the new item.
3238 * It guarantees both items live in the same tree leaf and the new item
3239 * is contiguous with the original item.
3240 *
3241 * This allows us to split file extent in place, keeping a lock on the
3242 * leaf the entire time.
3243 */
3244int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3245 struct btrfs_root *root,
3246 struct btrfs_path *path,
3247 struct btrfs_key *new_key)
3248{
3249 struct extent_buffer *leaf;
3250 int ret;
3251 u32 item_size;
3252
3253 leaf = path->nodes[0];
3254 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3255 ret = setup_leaf_for_split(trans, root, path,
3256 item_size + sizeof(struct btrfs_item));
3257 if (ret)
3258 return ret;
3259
3260 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003261 setup_items_for_insert(trans, root, path, new_key, &item_size,
3262 item_size, item_size +
3263 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003264 leaf = path->nodes[0];
3265 memcpy_extent_buffer(leaf,
3266 btrfs_item_ptr_offset(leaf, path->slots[0]),
3267 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3268 item_size);
3269 return 0;
3270}
3271
3272/*
Chris Masond352ac62008-09-29 15:18:18 -04003273 * make the item pointed to by the path smaller. new_size indicates
3274 * how small to make it, and from_end tells us if we just chop bytes
3275 * off the end of the item or if we shift the item to chop bytes off
3276 * the front.
3277 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003278void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3279 struct btrfs_root *root,
3280 struct btrfs_path *path,
3281 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003282{
Chris Masonb18c6682007-04-17 13:26:50 -04003283 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003284 struct extent_buffer *leaf;
3285 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003286 u32 nritems;
3287 unsigned int data_end;
3288 unsigned int old_data_start;
3289 unsigned int old_size;
3290 unsigned int size_diff;
3291 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003292 struct btrfs_map_token token;
3293
3294 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003295
Chris Mason5f39d392007-10-15 16:14:19 -04003296 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003297 slot = path->slots[0];
3298
3299 old_size = btrfs_item_size_nr(leaf, slot);
3300 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003301 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003302
Chris Mason5f39d392007-10-15 16:14:19 -04003303 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003304 data_end = leaf_data_end(root, leaf);
3305
Chris Mason5f39d392007-10-15 16:14:19 -04003306 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003307
Chris Masonb18c6682007-04-17 13:26:50 -04003308 size_diff = old_size - new_size;
3309
3310 BUG_ON(slot < 0);
3311 BUG_ON(slot >= nritems);
3312
3313 /*
3314 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3315 */
3316 /* first correct the data pointers */
3317 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003318 u32 ioff;
3319 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003320
Chris Masoncfed81a2012-03-03 07:40:03 -05003321 ioff = btrfs_token_item_offset(leaf, item, &token);
3322 btrfs_set_token_item_offset(leaf, item,
3323 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003324 }
Chris Masondb945352007-10-15 16:15:53 -04003325
Chris Masonb18c6682007-04-17 13:26:50 -04003326 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003327 if (from_end) {
3328 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3329 data_end + size_diff, btrfs_leaf_data(leaf) +
3330 data_end, old_data_start + new_size - data_end);
3331 } else {
3332 struct btrfs_disk_key disk_key;
3333 u64 offset;
3334
3335 btrfs_item_key(leaf, &disk_key, slot);
3336
3337 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3338 unsigned long ptr;
3339 struct btrfs_file_extent_item *fi;
3340
3341 fi = btrfs_item_ptr(leaf, slot,
3342 struct btrfs_file_extent_item);
3343 fi = (struct btrfs_file_extent_item *)(
3344 (unsigned long)fi - size_diff);
3345
3346 if (btrfs_file_extent_type(leaf, fi) ==
3347 BTRFS_FILE_EXTENT_INLINE) {
3348 ptr = btrfs_item_ptr_offset(leaf, slot);
3349 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003350 (unsigned long)fi,
3351 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003352 disk_bytenr));
3353 }
3354 }
3355
3356 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3357 data_end + size_diff, btrfs_leaf_data(leaf) +
3358 data_end, old_data_start - data_end);
3359
3360 offset = btrfs_disk_key_offset(&disk_key);
3361 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3362 btrfs_set_item_key(leaf, &disk_key, slot);
3363 if (slot == 0)
3364 fixup_low_keys(trans, root, path, &disk_key, 1);
3365 }
Chris Mason5f39d392007-10-15 16:14:19 -04003366
3367 item = btrfs_item_nr(leaf, slot);
3368 btrfs_set_item_size(leaf, item, new_size);
3369 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003370
Chris Mason5f39d392007-10-15 16:14:19 -04003371 if (btrfs_leaf_free_space(root, leaf) < 0) {
3372 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003373 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003374 }
Chris Masonb18c6682007-04-17 13:26:50 -04003375}
3376
Chris Masond352ac62008-09-29 15:18:18 -04003377/*
3378 * make the item pointed to by the path bigger, data_size is the new size.
3379 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003380void btrfs_extend_item(struct btrfs_trans_handle *trans,
3381 struct btrfs_root *root, struct btrfs_path *path,
3382 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003383{
Chris Mason6567e832007-04-16 09:22:45 -04003384 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003385 struct extent_buffer *leaf;
3386 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003387 u32 nritems;
3388 unsigned int data_end;
3389 unsigned int old_data;
3390 unsigned int old_size;
3391 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003392 struct btrfs_map_token token;
3393
3394 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003395
Chris Mason5f39d392007-10-15 16:14:19 -04003396 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003397
Chris Mason5f39d392007-10-15 16:14:19 -04003398 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003399 data_end = leaf_data_end(root, leaf);
3400
Chris Mason5f39d392007-10-15 16:14:19 -04003401 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3402 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003403 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003404 }
Chris Mason6567e832007-04-16 09:22:45 -04003405 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003406 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003407
3408 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003409 if (slot >= nritems) {
3410 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003411 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3412 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003413 BUG_ON(1);
3414 }
Chris Mason6567e832007-04-16 09:22:45 -04003415
3416 /*
3417 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3418 */
3419 /* first correct the data pointers */
3420 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003421 u32 ioff;
3422 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003423
Chris Masoncfed81a2012-03-03 07:40:03 -05003424 ioff = btrfs_token_item_offset(leaf, item, &token);
3425 btrfs_set_token_item_offset(leaf, item,
3426 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003427 }
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Chris Mason6567e832007-04-16 09:22:45 -04003429 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003430 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003431 data_end - data_size, btrfs_leaf_data(leaf) +
3432 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003433
Chris Mason6567e832007-04-16 09:22:45 -04003434 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003435 old_size = btrfs_item_size_nr(leaf, slot);
3436 item = btrfs_item_nr(leaf, slot);
3437 btrfs_set_item_size(leaf, item, old_size + data_size);
3438 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003439
Chris Mason5f39d392007-10-15 16:14:19 -04003440 if (btrfs_leaf_free_space(root, leaf) < 0) {
3441 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003442 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003443 }
Chris Mason6567e832007-04-16 09:22:45 -04003444}
3445
Chris Mason74123bd2007-02-02 11:05:29 -05003446/*
Chris Masond352ac62008-09-29 15:18:18 -04003447 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003448 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003449 * Returns the number of keys that were inserted.
3450 */
3451int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3452 struct btrfs_root *root,
3453 struct btrfs_path *path,
3454 struct btrfs_key *cpu_key, u32 *data_size,
3455 int nr)
3456{
3457 struct extent_buffer *leaf;
3458 struct btrfs_item *item;
3459 int ret = 0;
3460 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003461 int i;
3462 u32 nritems;
3463 u32 total_data = 0;
3464 u32 total_size = 0;
3465 unsigned int data_end;
3466 struct btrfs_disk_key disk_key;
3467 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003468 struct btrfs_map_token token;
3469
3470 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003471
Yan Zheng87b29b22008-12-17 10:21:48 -05003472 for (i = 0; i < nr; i++) {
3473 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3474 BTRFS_LEAF_DATA_SIZE(root)) {
3475 break;
3476 nr = i;
3477 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003478 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003479 total_size += data_size[i] + sizeof(struct btrfs_item);
3480 }
3481 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003482
Josef Bacikf3465ca2008-11-12 14:19:50 -05003483 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3484 if (ret == 0)
3485 return -EEXIST;
3486 if (ret < 0)
3487 goto out;
3488
Josef Bacikf3465ca2008-11-12 14:19:50 -05003489 leaf = path->nodes[0];
3490
3491 nritems = btrfs_header_nritems(leaf);
3492 data_end = leaf_data_end(root, leaf);
3493
3494 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3495 for (i = nr; i >= 0; i--) {
3496 total_data -= data_size[i];
3497 total_size -= data_size[i] + sizeof(struct btrfs_item);
3498 if (total_size < btrfs_leaf_free_space(root, leaf))
3499 break;
3500 }
3501 nr = i;
3502 }
3503
3504 slot = path->slots[0];
3505 BUG_ON(slot < 0);
3506
3507 if (slot != nritems) {
3508 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3509
3510 item = btrfs_item_nr(leaf, slot);
3511 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3512
3513 /* figure out how many keys we can insert in here */
3514 total_data = data_size[0];
3515 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003516 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003517 break;
3518 total_data += data_size[i];
3519 }
3520 nr = i;
3521
3522 if (old_data < data_end) {
3523 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003524 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003525 slot, old_data, data_end);
3526 BUG_ON(1);
3527 }
3528 /*
3529 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3530 */
3531 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003532 for (i = slot; i < nritems; i++) {
3533 u32 ioff;
3534
3535 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003536 ioff = btrfs_token_item_offset(leaf, item, &token);
3537 btrfs_set_token_item_offset(leaf, item,
3538 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003539 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003540 /* shift the items */
3541 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3542 btrfs_item_nr_offset(slot),
3543 (nritems - slot) * sizeof(struct btrfs_item));
3544
3545 /* shift the data */
3546 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3547 data_end - total_data, btrfs_leaf_data(leaf) +
3548 data_end, old_data - data_end);
3549 data_end = old_data;
3550 } else {
3551 /*
3552 * this sucks but it has to be done, if we are inserting at
3553 * the end of the leaf only insert 1 of the items, since we
3554 * have no way of knowing whats on the next leaf and we'd have
3555 * to drop our current locks to figure it out
3556 */
3557 nr = 1;
3558 }
3559
3560 /* setup the item for the new data */
3561 for (i = 0; i < nr; i++) {
3562 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3563 btrfs_set_item_key(leaf, &disk_key, slot + i);
3564 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003565 btrfs_set_token_item_offset(leaf, item,
3566 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003567 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003568 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003569 }
3570 btrfs_set_header_nritems(leaf, nritems + nr);
3571 btrfs_mark_buffer_dirty(leaf);
3572
3573 ret = 0;
3574 if (slot == 0) {
3575 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003576 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003577 }
3578
3579 if (btrfs_leaf_free_space(root, leaf) < 0) {
3580 btrfs_print_leaf(root, leaf);
3581 BUG();
3582 }
3583out:
3584 if (!ret)
3585 ret = nr;
3586 return ret;
3587}
3588
3589/*
Chris Mason44871b12009-03-13 10:04:31 -04003590 * this is a helper for btrfs_insert_empty_items, the main goal here is
3591 * to save stack depth by doing the bulk of the work in a function
3592 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003593 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003594void setup_items_for_insert(struct btrfs_trans_handle *trans,
3595 struct btrfs_root *root, struct btrfs_path *path,
3596 struct btrfs_key *cpu_key, u32 *data_size,
3597 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003598{
Chris Mason5f39d392007-10-15 16:14:19 -04003599 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003600 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003601 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003602 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003603 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003604 struct extent_buffer *leaf;
3605 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05003606 struct btrfs_map_token token;
3607
3608 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04003609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003611 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003612
Chris Mason5f39d392007-10-15 16:14:19 -04003613 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003614 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003615
Chris Masonf25956c2008-09-12 15:32:53 -04003616 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003617 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003618 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003619 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003620 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003621 }
Chris Mason5f39d392007-10-15 16:14:19 -04003622
Chris Masonbe0e5c02007-01-26 15:51:26 -05003623 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003624 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003625
Chris Mason5f39d392007-10-15 16:14:19 -04003626 if (old_data < data_end) {
3627 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003628 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003629 slot, old_data, data_end);
3630 BUG_ON(1);
3631 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632 /*
3633 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3634 */
3635 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003636 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003637 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003638
Chris Mason5f39d392007-10-15 16:14:19 -04003639 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003640 ioff = btrfs_token_item_offset(leaf, item, &token);
3641 btrfs_set_token_item_offset(leaf, item,
3642 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003643 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003644 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003645 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003646 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003647 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003648
3649 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003650 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003651 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003652 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003653 data_end = old_data;
3654 }
Chris Mason5f39d392007-10-15 16:14:19 -04003655
Chris Mason62e27492007-03-15 12:56:47 -04003656 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003657 for (i = 0; i < nr; i++) {
3658 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3659 btrfs_set_item_key(leaf, &disk_key, slot + i);
3660 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003661 btrfs_set_token_item_offset(leaf, item,
3662 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003663 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003664 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003665 }
Chris Mason44871b12009-03-13 10:04:31 -04003666
Chris Mason9c583092008-01-29 15:15:18 -05003667 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003668
Chris Mason5a01a2e2008-01-30 11:43:54 -05003669 if (slot == 0) {
3670 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003671 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003672 }
Chris Masonb9473432009-03-13 11:00:37 -04003673 btrfs_unlock_up_safe(path, 1);
3674 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003675
Chris Mason5f39d392007-10-15 16:14:19 -04003676 if (btrfs_leaf_free_space(root, leaf) < 0) {
3677 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003679 }
Chris Mason44871b12009-03-13 10:04:31 -04003680}
3681
3682/*
3683 * Given a key and some data, insert items into the tree.
3684 * This does all the path init required, making room in the tree if needed.
3685 */
3686int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3687 struct btrfs_root *root,
3688 struct btrfs_path *path,
3689 struct btrfs_key *cpu_key, u32 *data_size,
3690 int nr)
3691{
Chris Mason44871b12009-03-13 10:04:31 -04003692 int ret = 0;
3693 int slot;
3694 int i;
3695 u32 total_size = 0;
3696 u32 total_data = 0;
3697
3698 for (i = 0; i < nr; i++)
3699 total_data += data_size[i];
3700
3701 total_size = total_data + (nr * sizeof(struct btrfs_item));
3702 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3703 if (ret == 0)
3704 return -EEXIST;
3705 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003706 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003707
Chris Mason44871b12009-03-13 10:04:31 -04003708 slot = path->slots[0];
3709 BUG_ON(slot < 0);
3710
Jeff Mahoney143bede2012-03-01 14:56:26 +01003711 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003712 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003713 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003714}
3715
3716/*
3717 * Given a key and some data, insert an item into the tree.
3718 * This does all the path init required, making room in the tree if needed.
3719 */
Chris Masone089f052007-03-16 16:20:31 -04003720int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3721 *root, struct btrfs_key *cpu_key, void *data, u32
3722 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003723{
3724 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003725 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003726 struct extent_buffer *leaf;
3727 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003728
Chris Mason2c90e5d2007-04-02 10:50:19 -04003729 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003730 if (!path)
3731 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003732 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003733 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003734 leaf = path->nodes[0];
3735 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3736 write_extent_buffer(leaf, data, ptr, data_size);
3737 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003738 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003739 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003740 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003741}
3742
Chris Mason74123bd2007-02-02 11:05:29 -05003743/*
Chris Mason5de08d72007-02-24 06:24:44 -05003744 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003745 *
Chris Masond352ac62008-09-29 15:18:18 -04003746 * the tree should have been previously balanced so the deletion does not
3747 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003748 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003749static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3750 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003751{
Chris Mason5f39d392007-10-15 16:14:19 -04003752 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003753 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003754
Chris Mason5f39d392007-10-15 16:14:19 -04003755 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003756 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003757 memmove_extent_buffer(parent,
3758 btrfs_node_key_ptr_offset(slot),
3759 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003760 sizeof(struct btrfs_key_ptr) *
3761 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003762 }
Chris Mason7518a232007-03-12 12:01:18 -04003763 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003764 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003765 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003766 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003767 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003768 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003769 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003770 struct btrfs_disk_key disk_key;
3771
3772 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003773 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003774 }
Chris Masond6025572007-03-30 14:27:56 -04003775 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003776}
3777
Chris Mason74123bd2007-02-02 11:05:29 -05003778/*
Chris Mason323ac952008-10-01 19:05:46 -04003779 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003780 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003781 *
3782 * This deletes the pointer in path->nodes[1] and frees the leaf
3783 * block extent. zero is returned if it all worked out, < 0 otherwise.
3784 *
3785 * The path must have already been setup for deleting the leaf, including
3786 * all the proper balancing. path->nodes[1] must be locked.
3787 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003788static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3789 struct btrfs_root *root,
3790 struct btrfs_path *path,
3791 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003792{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003793 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003794 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003795
Chris Mason4d081c42009-02-04 09:31:28 -05003796 /*
3797 * btrfs_free_extent is expensive, we want to make sure we
3798 * aren't holding any locks when we call it
3799 */
3800 btrfs_unlock_up_safe(path, 0);
3801
Yan, Zhengf0486c62010-05-16 10:46:25 -04003802 root_sub_used(root, leaf->len);
3803
Josef Bacik3083ee22012-03-09 16:01:49 -05003804 extent_buffer_get(leaf);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003805 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05003806 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003807}
3808/*
Chris Mason74123bd2007-02-02 11:05:29 -05003809 * delete the item at the leaf level in path. If that empties
3810 * the leaf, remove it from the tree
3811 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003812int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3813 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814{
Chris Mason5f39d392007-10-15 16:14:19 -04003815 struct extent_buffer *leaf;
3816 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003817 int last_off;
3818 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003819 int ret = 0;
3820 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003821 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003822 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05003823 struct btrfs_map_token token;
3824
3825 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003826
Chris Mason5f39d392007-10-15 16:14:19 -04003827 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003828 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3829
3830 for (i = 0; i < nr; i++)
3831 dsize += btrfs_item_size_nr(leaf, slot + i);
3832
Chris Mason5f39d392007-10-15 16:14:19 -04003833 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834
Chris Mason85e21ba2008-01-29 15:11:36 -05003835 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003836 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003837
3838 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003839 data_end + dsize,
3840 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003841 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003842
Chris Mason85e21ba2008-01-29 15:11:36 -05003843 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003844 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003845
Chris Mason5f39d392007-10-15 16:14:19 -04003846 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003847 ioff = btrfs_token_item_offset(leaf, item, &token);
3848 btrfs_set_token_item_offset(leaf, item,
3849 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003850 }
Chris Masondb945352007-10-15 16:15:53 -04003851
Chris Mason5f39d392007-10-15 16:14:19 -04003852 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003853 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003854 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003855 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003856 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003857 btrfs_set_header_nritems(leaf, nritems - nr);
3858 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003859
Chris Mason74123bd2007-02-02 11:05:29 -05003860 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003861 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003862 if (leaf == root->node) {
3863 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003864 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003865 btrfs_set_path_blocking(path);
3866 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003867 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003868 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003869 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003870 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003871 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003872 struct btrfs_disk_key disk_key;
3873
3874 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003875 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003876 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003877
Chris Mason74123bd2007-02-02 11:05:29 -05003878 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003879 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003880 /* push_leaf_left fixes the path.
3881 * make sure the path still points to our leaf
3882 * for possible call to del_ptr below
3883 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003884 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003885 extent_buffer_get(leaf);
3886
Chris Masonb9473432009-03-13 11:00:37 -04003887 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003888 wret = push_leaf_left(trans, root, path, 1, 1,
3889 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003890 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003891 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003892
3893 if (path->nodes[0] == leaf &&
3894 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003895 wret = push_leaf_right(trans, root, path, 1,
3896 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003897 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003898 ret = wret;
3899 }
Chris Mason5f39d392007-10-15 16:14:19 -04003900
3901 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003902 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003903 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003904 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003905 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003906 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003907 /* if we're still in the path, make sure
3908 * we're dirty. Otherwise, one of the
3909 * push_leaf functions must have already
3910 * dirtied this buffer
3911 */
3912 if (path->nodes[0] == leaf)
3913 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003914 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003915 }
Chris Masond5719762007-03-23 10:01:08 -04003916 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003917 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003918 }
3919 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003920 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003921}
3922
Chris Mason97571fd2007-02-24 13:39:08 -05003923/*
Chris Mason925baed2008-06-25 16:01:30 -04003924 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003925 * returns 0 if it found something or 1 if there are no lesser leaves.
3926 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003927 *
3928 * This may release the path, and so you may lose any locks held at the
3929 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003930 */
3931int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3932{
Chris Mason925baed2008-06-25 16:01:30 -04003933 struct btrfs_key key;
3934 struct btrfs_disk_key found_key;
3935 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003936
Chris Mason925baed2008-06-25 16:01:30 -04003937 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003938
Chris Mason925baed2008-06-25 16:01:30 -04003939 if (key.offset > 0)
3940 key.offset--;
3941 else if (key.type > 0)
3942 key.type--;
3943 else if (key.objectid > 0)
3944 key.objectid--;
3945 else
3946 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003947
David Sterbab3b4aa72011-04-21 01:20:15 +02003948 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003949 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3950 if (ret < 0)
3951 return ret;
3952 btrfs_item_key(path->nodes[0], &found_key, 0);
3953 ret = comp_keys(&found_key, &key);
3954 if (ret < 0)
3955 return 0;
3956 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003957}
3958
Chris Mason3f157a22008-06-25 16:01:31 -04003959/*
3960 * A helper function to walk down the tree starting at min_key, and looking
3961 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003962 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003963 *
3964 * This does not cow, but it does stuff the starting key it finds back
3965 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3966 * key and get a writable path.
3967 *
3968 * This does lock as it descends, and path->keep_locks should be set
3969 * to 1 by the caller.
3970 *
3971 * This honors path->lowest_level to prevent descent past a given level
3972 * of the tree.
3973 *
Chris Masond352ac62008-09-29 15:18:18 -04003974 * min_trans indicates the oldest transaction that you are interested
3975 * in walking through. Any nodes or leaves older than min_trans are
3976 * skipped over (without reading them).
3977 *
Chris Mason3f157a22008-06-25 16:01:31 -04003978 * returns zero if something useful was found, < 0 on error and 1 if there
3979 * was nothing in the tree that matched the search criteria.
3980 */
3981int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003982 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003983 struct btrfs_path *path, int cache_only,
3984 u64 min_trans)
3985{
3986 struct extent_buffer *cur;
3987 struct btrfs_key found_key;
3988 int slot;
Yan96524802008-07-24 12:19:49 -04003989 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003990 u32 nritems;
3991 int level;
3992 int ret = 1;
3993
Chris Mason934d3752008-12-08 16:43:10 -05003994 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003995again:
Chris Masonbd681512011-07-16 15:23:14 -04003996 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003997 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003998 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003999 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004000 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004001
4002 if (btrfs_header_generation(cur) < min_trans) {
4003 ret = 1;
4004 goto out;
4005 }
Chris Masond3977122009-01-05 21:25:51 -05004006 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004007 nritems = btrfs_header_nritems(cur);
4008 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004009 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004010
Chris Mason323ac952008-10-01 19:05:46 -04004011 /* at the lowest level, we're done, setup the path and exit */
4012 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004013 if (slot >= nritems)
4014 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004015 ret = 0;
4016 path->slots[level] = slot;
4017 btrfs_item_key_to_cpu(cur, &found_key, slot);
4018 goto out;
4019 }
Yan96524802008-07-24 12:19:49 -04004020 if (sret && slot > 0)
4021 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004022 /*
4023 * check this node pointer against the cache_only and
4024 * min_trans parameters. If it isn't in cache or is too
4025 * old, skip to the next one.
4026 */
Chris Masond3977122009-01-05 21:25:51 -05004027 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004028 u64 blockptr;
4029 u64 gen;
4030 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004031 struct btrfs_disk_key disk_key;
4032
Chris Mason3f157a22008-06-25 16:01:31 -04004033 blockptr = btrfs_node_blockptr(cur, slot);
4034 gen = btrfs_node_ptr_generation(cur, slot);
4035 if (gen < min_trans) {
4036 slot++;
4037 continue;
4038 }
4039 if (!cache_only)
4040 break;
4041
Chris Masone02119d2008-09-05 16:13:11 -04004042 if (max_key) {
4043 btrfs_node_key(cur, &disk_key, slot);
4044 if (comp_keys(&disk_key, max_key) >= 0) {
4045 ret = 1;
4046 goto out;
4047 }
4048 }
4049
Chris Mason3f157a22008-06-25 16:01:31 -04004050 tmp = btrfs_find_tree_block(root, blockptr,
4051 btrfs_level_size(root, level - 1));
4052
Chris Masonb9fab912012-05-06 07:23:47 -04004053 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004054 free_extent_buffer(tmp);
4055 break;
4056 }
4057 if (tmp)
4058 free_extent_buffer(tmp);
4059 slot++;
4060 }
Chris Masone02119d2008-09-05 16:13:11 -04004061find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004062 /*
4063 * we didn't find a candidate key in this node, walk forward
4064 * and find another one
4065 */
4066 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004067 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004068 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004069 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004070 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004071 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004072 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004073 goto again;
4074 } else {
4075 goto out;
4076 }
4077 }
4078 /* save our key for returning back */
4079 btrfs_node_key_to_cpu(cur, &found_key, slot);
4080 path->slots[level] = slot;
4081 if (level == path->lowest_level) {
4082 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004083 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004084 goto out;
4085 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004086 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004087 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004088 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004089
Chris Masonbd681512011-07-16 15:23:14 -04004090 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004091
Chris Masonbd681512011-07-16 15:23:14 -04004092 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004093 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004094 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004095 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004096 }
4097out:
4098 if (ret == 0)
4099 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004100 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004101 return ret;
4102}
4103
4104/*
4105 * this is similar to btrfs_next_leaf, but does not try to preserve
4106 * and fixup the path. It looks for and returns the next key in the
4107 * tree based on the current path and the cache_only and min_trans
4108 * parameters.
4109 *
4110 * 0 is returned if another key is found, < 0 if there are any errors
4111 * and 1 is returned if there are no higher keys in the tree
4112 *
4113 * path->keep_locks should be set to 1 on the search made before
4114 * calling this function.
4115 */
Chris Masone7a84562008-06-25 16:01:31 -04004116int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004117 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004118 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004119{
Chris Masone7a84562008-06-25 16:01:31 -04004120 int slot;
4121 struct extent_buffer *c;
4122
Chris Mason934d3752008-12-08 16:43:10 -05004123 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004124 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004125 if (!path->nodes[level])
4126 return 1;
4127
4128 slot = path->slots[level] + 1;
4129 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004130next:
Chris Masone7a84562008-06-25 16:01:31 -04004131 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004132 int ret;
4133 int orig_lowest;
4134 struct btrfs_key cur_key;
4135 if (level + 1 >= BTRFS_MAX_LEVEL ||
4136 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004137 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004138
4139 if (path->locks[level + 1]) {
4140 level++;
4141 continue;
4142 }
4143
4144 slot = btrfs_header_nritems(c) - 1;
4145 if (level == 0)
4146 btrfs_item_key_to_cpu(c, &cur_key, slot);
4147 else
4148 btrfs_node_key_to_cpu(c, &cur_key, slot);
4149
4150 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004151 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004152 path->lowest_level = level;
4153 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4154 0, 0);
4155 path->lowest_level = orig_lowest;
4156 if (ret < 0)
4157 return ret;
4158
4159 c = path->nodes[level];
4160 slot = path->slots[level];
4161 if (ret == 0)
4162 slot++;
4163 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004164 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004165
Chris Masone7a84562008-06-25 16:01:31 -04004166 if (level == 0)
4167 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004168 else {
4169 u64 blockptr = btrfs_node_blockptr(c, slot);
4170 u64 gen = btrfs_node_ptr_generation(c, slot);
4171
4172 if (cache_only) {
4173 struct extent_buffer *cur;
4174 cur = btrfs_find_tree_block(root, blockptr,
4175 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004176 if (!cur ||
4177 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004178 slot++;
4179 if (cur)
4180 free_extent_buffer(cur);
4181 goto next;
4182 }
4183 free_extent_buffer(cur);
4184 }
4185 if (gen < min_trans) {
4186 slot++;
4187 goto next;
4188 }
Chris Masone7a84562008-06-25 16:01:31 -04004189 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004190 }
Chris Masone7a84562008-06-25 16:01:31 -04004191 return 0;
4192 }
4193 return 1;
4194}
4195
Chris Mason7bb86312007-12-11 09:25:06 -05004196/*
Chris Mason925baed2008-06-25 16:01:30 -04004197 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004198 * returns 0 if it found something or 1 if there are no greater leaves.
4199 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004200 */
Chris Mason234b63a2007-03-13 10:46:10 -04004201int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004202{
4203 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004204 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004205 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004206 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004207 struct btrfs_key key;
4208 u32 nritems;
4209 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004210 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004211 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004212
4213 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004214 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004215 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004216
Chris Mason8e73f272009-04-03 10:14:18 -04004217 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4218again:
4219 level = 1;
4220 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004221 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004222 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004223
Chris Masona2135012008-06-25 16:01:30 -04004224 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004225 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004226
Chris Mason925baed2008-06-25 16:01:30 -04004227 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4228 path->keep_locks = 0;
4229
4230 if (ret < 0)
4231 return ret;
4232
Chris Masona2135012008-06-25 16:01:30 -04004233 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004234 /*
4235 * by releasing the path above we dropped all our locks. A balance
4236 * could have added more items next to the key that used to be
4237 * at the very end of the block. So, check again here and
4238 * advance the path if there are now more items available.
4239 */
Chris Masona2135012008-06-25 16:01:30 -04004240 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004241 if (ret == 0)
4242 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004243 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004244 goto done;
4245 }
Chris Masond97e63b2007-02-20 16:40:44 -05004246
Chris Masond3977122009-01-05 21:25:51 -05004247 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004248 if (!path->nodes[level]) {
4249 ret = 1;
4250 goto done;
4251 }
Chris Mason5f39d392007-10-15 16:14:19 -04004252
Chris Masond97e63b2007-02-20 16:40:44 -05004253 slot = path->slots[level] + 1;
4254 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004255 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004256 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004257 if (level == BTRFS_MAX_LEVEL) {
4258 ret = 1;
4259 goto done;
4260 }
Chris Masond97e63b2007-02-20 16:40:44 -05004261 continue;
4262 }
Chris Mason5f39d392007-10-15 16:14:19 -04004263
Chris Mason925baed2008-06-25 16:01:30 -04004264 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004265 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004266 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004267 }
Chris Mason5f39d392007-10-15 16:14:19 -04004268
Chris Mason8e73f272009-04-03 10:14:18 -04004269 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004270 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004271 ret = read_block_for_search(NULL, root, path, &next, level,
4272 slot, &key);
4273 if (ret == -EAGAIN)
4274 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004275
Chris Mason76a05b32009-05-14 13:24:30 -04004276 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004277 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004278 goto done;
4279 }
4280
Chris Mason5cd57b22008-06-25 16:01:30 -04004281 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004282 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004283 if (!ret) {
4284 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004285 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004286 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004287 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004288 }
Chris Mason31533fb2011-07-26 16:01:59 -04004289 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004290 }
Chris Masond97e63b2007-02-20 16:40:44 -05004291 break;
4292 }
4293 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004294 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004295 level--;
4296 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004297 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004298 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004299
Chris Mason5f39d392007-10-15 16:14:19 -04004300 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004301 path->nodes[level] = next;
4302 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004303 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004304 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004305 if (!level)
4306 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004307
Chris Mason8e73f272009-04-03 10:14:18 -04004308 ret = read_block_for_search(NULL, root, path, &next, level,
4309 0, &key);
4310 if (ret == -EAGAIN)
4311 goto again;
4312
Chris Mason76a05b32009-05-14 13:24:30 -04004313 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004314 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004315 goto done;
4316 }
4317
Chris Mason5cd57b22008-06-25 16:01:30 -04004318 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004319 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004320 if (!ret) {
4321 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004322 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004323 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004324 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004325 }
Chris Mason31533fb2011-07-26 16:01:59 -04004326 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004327 }
Chris Masond97e63b2007-02-20 16:40:44 -05004328 }
Chris Mason8e73f272009-04-03 10:14:18 -04004329 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004330done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004331 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004332 path->leave_spinning = old_spinning;
4333 if (!old_spinning)
4334 btrfs_set_path_blocking(path);
4335
4336 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004337}
Chris Mason0b86a832008-03-24 15:01:56 -04004338
Chris Mason3f157a22008-06-25 16:01:31 -04004339/*
4340 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4341 * searching until it gets past min_objectid or finds an item of 'type'
4342 *
4343 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4344 */
Chris Mason0b86a832008-03-24 15:01:56 -04004345int btrfs_previous_item(struct btrfs_root *root,
4346 struct btrfs_path *path, u64 min_objectid,
4347 int type)
4348{
4349 struct btrfs_key found_key;
4350 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004351 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004352 int ret;
4353
Chris Masond3977122009-01-05 21:25:51 -05004354 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004355 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004356 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004357 ret = btrfs_prev_leaf(root, path);
4358 if (ret != 0)
4359 return ret;
4360 } else {
4361 path->slots[0]--;
4362 }
4363 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004364 nritems = btrfs_header_nritems(leaf);
4365 if (nritems == 0)
4366 return 1;
4367 if (path->slots[0] == nritems)
4368 path->slots[0]--;
4369
Chris Mason0b86a832008-03-24 15:01:56 -04004370 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004371 if (found_key.objectid < min_objectid)
4372 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004373 if (found_key.type == type)
4374 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004375 if (found_key.objectid == min_objectid &&
4376 found_key.type < type)
4377 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004378 }
4379 return 1;
4380}