blob: 086303b9be64e87af8ce5593b2f1a31dfb2a4eaa [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010039static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone089f052007-03-16 16:20:31 -040040 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040057 if (!p->nodes[i] || !p->locks[i])
58 continue;
59 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60 if (p->locks[i] == BTRFS_READ_LOCK)
61 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62 else if (p->locks[i] == BTRFS_WRITE_LOCK)
63 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050064 }
65}
66
67/*
68 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050069 *
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
73 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 */
Chris Mason4008c042009-02-12 14:09:45 -050075noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040076 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050077{
78 int i;
Chris Mason4008c042009-02-12 14:09:45 -050079
80#ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
85 * the path blocking.
86 */
Chris Masonbd681512011-07-16 15:23:14 -040087 if (held) {
88 btrfs_set_lock_blocking_rw(held, held_rw);
89 if (held_rw == BTRFS_WRITE_LOCK)
90 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91 else if (held_rw == BTRFS_READ_LOCK)
92 held_rw = BTRFS_READ_LOCK_BLOCKING;
93 }
Chris Mason4008c042009-02-12 14:09:45 -050094 btrfs_set_path_blocking(p);
95#endif
96
97 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040098 if (p->nodes[i] && p->locks[i]) {
99 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101 p->locks[i] = BTRFS_WRITE_LOCK;
102 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103 p->locks[i] = BTRFS_READ_LOCK;
104 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500105 }
Chris Mason4008c042009-02-12 14:09:45 -0500106
107#ifdef CONFIG_DEBUG_LOCK_ALLOC
108 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400109 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500110#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400114void btrfs_free_path(struct btrfs_path *p)
115{
Jesper Juhlff175d52010-12-25 21:22:30 +0000116 if (!p)
117 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200118 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400119 kmem_cache_free(btrfs_path_cachep, p);
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
125 *
126 * It is safe to call this on paths that no locks or extent buffers held.
127 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200128noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500129{
130 int i;
Chris Masona2135012008-06-25 16:01:30 -0400131
Chris Mason234b63a2007-03-13 10:46:10 -0400132 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400135 continue;
136 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400137 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400138 p->locks[i] = 0;
139 }
Chris Mason5f39d392007-10-15 16:14:19 -0400140 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400141 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 }
143}
144
Chris Masond352ac62008-09-29 15:18:18 -0400145/*
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
149 * looping required.
150 *
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400158
Josef Bacik3083ee22012-03-09 16:01:49 -0500159 while (1) {
160 rcu_read_lock();
161 eb = rcu_dereference(root->node);
162
163 /*
164 * RCU really hurts here, we could free up the root node because
165 * it was cow'ed but we may not get the new root node yet so do
166 * the inc_not_zero dance and if it doesn't work then
167 * synchronize_rcu and try again.
168 */
169 if (atomic_inc_not_zero(&eb->refs)) {
170 rcu_read_unlock();
171 break;
172 }
173 rcu_read_unlock();
174 synchronize_rcu();
175 }
Chris Mason925baed2008-06-25 16:01:30 -0400176 return eb;
177}
178
Chris Masond352ac62008-09-29 15:18:18 -0400179/* loop around taking references on and locking the root node of the
180 * tree until you end up with a lock on the root. A locked buffer
181 * is returned, with a reference held.
182 */
Chris Mason925baed2008-06-25 16:01:30 -0400183struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
184{
185 struct extent_buffer *eb;
186
Chris Masond3977122009-01-05 21:25:51 -0500187 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400188 eb = btrfs_root_node(root);
189 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400190 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400191 break;
Chris Mason925baed2008-06-25 16:01:30 -0400192 btrfs_tree_unlock(eb);
193 free_extent_buffer(eb);
194 }
195 return eb;
196}
197
Chris Masonbd681512011-07-16 15:23:14 -0400198/* loop around taking references on and locking the root node of the
199 * tree until you end up with a lock on the root. A locked buffer
200 * is returned, with a reference held.
201 */
202struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
203{
204 struct extent_buffer *eb;
205
206 while (1) {
207 eb = btrfs_root_node(root);
208 btrfs_tree_read_lock(eb);
209 if (eb == root->node)
210 break;
211 btrfs_tree_read_unlock(eb);
212 free_extent_buffer(eb);
213 }
214 return eb;
215}
216
Chris Masond352ac62008-09-29 15:18:18 -0400217/* cowonly root (everything not a reference counted cow subvolume), just get
218 * put onto a simple dirty list. transaction.c walks this to make sure they
219 * get properly updated on disk.
220 */
Chris Mason0b86a832008-03-24 15:01:56 -0400221static void add_root_to_dirty_list(struct btrfs_root *root)
222{
Chris Masone5846fc2012-05-03 12:08:48 -0400223 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400224 if (root->track_dirty && list_empty(&root->dirty_list)) {
225 list_add(&root->dirty_list,
226 &root->fs_info->dirty_cowonly_roots);
227 }
Chris Masone5846fc2012-05-03 12:08:48 -0400228 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400229}
230
Chris Masond352ac62008-09-29 15:18:18 -0400231/*
232 * used by snapshot creation to make a copy of a root for a tree with
233 * a given objectid. The buffer with the new root node is returned in
234 * cow_ret, and this func returns zero on success or a negative error code.
235 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500236int btrfs_copy_root(struct btrfs_trans_handle *trans,
237 struct btrfs_root *root,
238 struct extent_buffer *buf,
239 struct extent_buffer **cow_ret, u64 new_root_objectid)
240{
241 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500242 int ret = 0;
243 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400244 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245
246 WARN_ON(root->ref_cows && trans->transid !=
247 root->fs_info->running_transaction->transid);
248 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
249
250 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400251 if (level == 0)
252 btrfs_item_key(buf, &disk_key, 0);
253 else
254 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400255
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400256 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
257 new_root_objectid, &disk_key, level,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200258 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400259 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 return PTR_ERR(cow);
261
262 copy_extent_buffer(cow, buf, 0, 0, cow->len);
263 btrfs_set_header_bytenr(cow, cow->start);
264 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
266 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
267 BTRFS_HEADER_FLAG_RELOC);
268 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
269 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
270 else
271 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500272
Yan Zheng2b820322008-11-17 21:11:30 -0500273 write_extent_buffer(cow, root->fs_info->fsid,
274 (unsigned long)btrfs_header_fsid(cow),
275 BTRFS_FSID_SIZE);
276
Chris Masonbe20aa92007-12-17 20:14:01 -0500277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200279 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200281 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500282
Chris Masonbe20aa92007-12-17 20:14:01 -0500283 if (ret)
284 return ret;
285
286 btrfs_mark_buffer_dirty(cow);
287 *cow_ret = cow;
288 return 0;
289}
290
Chris Masond352ac62008-09-29 15:18:18 -0400291/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400292 * check if the tree block can be shared by multiple trees
293 */
294int btrfs_block_can_be_shared(struct btrfs_root *root,
295 struct extent_buffer *buf)
296{
297 /*
298 * Tree blocks not in refernece counted trees and tree roots
299 * are never shared. If a block was allocated after the last
300 * snapshot and the block was not allocated by tree relocation,
301 * we know the block is not shared.
302 */
303 if (root->ref_cows &&
304 buf != root->node && buf != root->commit_root &&
305 (btrfs_header_generation(buf) <=
306 btrfs_root_last_snapshot(&root->root_item) ||
307 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
308 return 1;
309#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
310 if (root->ref_cows &&
311 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
312 return 1;
313#endif
314 return 0;
315}
316
317static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
318 struct btrfs_root *root,
319 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400320 struct extent_buffer *cow,
321 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400322{
323 u64 refs;
324 u64 owner;
325 u64 flags;
326 u64 new_flags = 0;
327 int ret;
328
329 /*
330 * Backrefs update rules:
331 *
332 * Always use full backrefs for extent pointers in tree block
333 * allocated by tree relocation.
334 *
335 * If a shared tree block is no longer referenced by its owner
336 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
337 * use full backrefs for extent pointers in tree block.
338 *
339 * If a tree block is been relocating
340 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
341 * use full backrefs for extent pointers in tree block.
342 * The reason for this is some operations (such as drop tree)
343 * are only allowed for blocks use full backrefs.
344 */
345
346 if (btrfs_block_can_be_shared(root, buf)) {
347 ret = btrfs_lookup_extent_info(trans, root, buf->start,
348 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700349 if (ret)
350 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700351 if (refs == 0) {
352 ret = -EROFS;
353 btrfs_std_error(root->fs_info, ret);
354 return ret;
355 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400356 } else {
357 refs = 1;
358 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
359 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
360 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
361 else
362 flags = 0;
363 }
364
365 owner = btrfs_header_owner(buf);
366 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
367 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
368
369 if (refs > 1) {
370 if ((owner == root->root_key.objectid ||
371 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
372 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200373 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100374 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400375
376 if (root->root_key.objectid ==
377 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200378 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100379 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200380 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100381 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400382 }
383 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
384 } else {
385
386 if (root->root_key.objectid ==
387 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200388 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200390 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100391 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400392 }
393 if (new_flags != 0) {
394 ret = btrfs_set_disk_extent_flags(trans, root,
395 buf->start,
396 buf->len,
397 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700398 if (ret)
399 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400 }
401 } else {
402 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
403 if (root->root_key.objectid ==
404 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200405 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400406 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200407 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100408 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200409 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100410 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400411 }
412 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400413 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400414 }
415 return 0;
416}
417
418/*
Chris Masond3977122009-01-05 21:25:51 -0500419 * does the dirty work in cow of a single block. The parent block (if
420 * supplied) is updated to point to the new cow copy. The new buffer is marked
421 * dirty and returned locked. If you modify the block it needs to be marked
422 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400423 *
424 * search_start -- an allocation hint for the new block
425 *
Chris Masond3977122009-01-05 21:25:51 -0500426 * empty_size -- a hint that you plan on doing more cow. This is the size in
427 * bytes the allocator should try to find free next to the block it returns.
428 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400429 */
Chris Masond3977122009-01-05 21:25:51 -0500430static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400431 struct btrfs_root *root,
432 struct extent_buffer *buf,
433 struct extent_buffer *parent, int parent_slot,
434 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400435 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400436{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400437 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400438 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700439 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400440 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400441 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400442 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400443
Chris Mason925baed2008-06-25 16:01:30 -0400444 if (*cow_ret == buf)
445 unlock_orig = 1;
446
Chris Masonb9447ef2009-03-09 11:45:38 -0400447 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400448
Chris Mason7bb86312007-12-11 09:25:06 -0500449 WARN_ON(root->ref_cows && trans->transid !=
450 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400451 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400452
Chris Mason7bb86312007-12-11 09:25:06 -0500453 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400454
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400455 if (level == 0)
456 btrfs_item_key(buf, &disk_key, 0);
457 else
458 btrfs_node_key(buf, &disk_key, 0);
459
460 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
461 if (parent)
462 parent_start = parent->start;
463 else
464 parent_start = 0;
465 } else
466 parent_start = 0;
467
468 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
469 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200470 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400471 if (IS_ERR(cow))
472 return PTR_ERR(cow);
473
Chris Masonb4ce94d2009-02-04 09:25:08 -0500474 /* cow is set to blocking by btrfs_init_new_buffer */
475
Chris Mason5f39d392007-10-15 16:14:19 -0400476 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400477 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400478 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400479 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
480 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
481 BTRFS_HEADER_FLAG_RELOC);
482 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
483 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
484 else
485 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400486
Yan Zheng2b820322008-11-17 21:11:30 -0500487 write_extent_buffer(cow, root->fs_info->fsid,
488 (unsigned long)btrfs_header_fsid(cow),
489 BTRFS_FSID_SIZE);
490
Mark Fashehbe1a5562011-08-08 13:20:18 -0700491 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700492 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100493 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700494 return ret;
495 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400496
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400497 if (root->ref_cows)
498 btrfs_reloc_cow_block(trans, root, buf, cow);
499
Chris Mason6702ed42007-08-07 16:15:09 -0400500 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400501 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400502 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
503 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
504 parent_start = buf->start;
505 else
506 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400507
Chris Mason5f39d392007-10-15 16:14:19 -0400508 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400509 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400510
Yan, Zhengf0486c62010-05-16 10:46:25 -0400511 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200512 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400513 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400514 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400515 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400516 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
517 parent_start = parent->start;
518 else
519 parent_start = 0;
520
521 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400522 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400523 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500524 btrfs_set_node_ptr_generation(parent, parent_slot,
525 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400526 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400527 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200528 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400529 }
Chris Mason925baed2008-06-25 16:01:30 -0400530 if (unlock_orig)
531 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500532 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400533 btrfs_mark_buffer_dirty(cow);
534 *cow_ret = cow;
535 return 0;
536}
537
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400538static inline int should_cow_block(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct extent_buffer *buf)
541{
Liu Bof1ebcc72011-11-14 20:48:06 -0500542 /* ensure we can see the force_cow */
543 smp_rmb();
544
545 /*
546 * We do not need to cow a block if
547 * 1) this block is not created or changed in this transaction;
548 * 2) this block does not belong to TREE_RELOC tree;
549 * 3) the root is not forced COW.
550 *
551 * What is forced COW:
552 * when we create snapshot during commiting the transaction,
553 * after we've finished coping src root, we must COW the shared
554 * block to ensure the metadata consistency.
555 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400556 if (btrfs_header_generation(buf) == trans->transid &&
557 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
558 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500559 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
560 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400561 return 0;
562 return 1;
563}
564
Chris Masond352ac62008-09-29 15:18:18 -0400565/*
566 * cows a single block, see __btrfs_cow_block for the real work.
567 * This version of it has extra checks so that a block isn't cow'd more than
568 * once per transaction, as long as it hasn't been written yet
569 */
Chris Masond3977122009-01-05 21:25:51 -0500570noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400571 struct btrfs_root *root, struct extent_buffer *buf,
572 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400573 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500574{
Chris Mason6702ed42007-08-07 16:15:09 -0400575 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400576 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500577
Chris Masonccd467d2007-06-28 15:57:36 -0400578 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500579 printk(KERN_CRIT "trans %llu running %llu\n",
580 (unsigned long long)trans->transid,
581 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400582 root->fs_info->running_transaction->transid);
583 WARN_ON(1);
584 }
585 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500586 printk(KERN_CRIT "trans %llu running %llu\n",
587 (unsigned long long)trans->transid,
588 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400589 WARN_ON(1);
590 }
Chris Masondc17ff82008-01-08 15:46:30 -0500591
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400592 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500593 *cow_ret = buf;
594 return 0;
595 }
Chris Masonc4876852009-02-04 09:24:25 -0500596
Chris Mason0b86a832008-03-24 15:01:56 -0400597 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500598
599 if (parent)
600 btrfs_set_lock_blocking(parent);
601 btrfs_set_lock_blocking(buf);
602
Chris Masonf510cfe2007-10-15 16:14:48 -0400603 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400604 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000605
606 trace_btrfs_cow_block(root, buf, *cow_ret);
607
Chris Masonf510cfe2007-10-15 16:14:48 -0400608 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400609}
610
Chris Masond352ac62008-09-29 15:18:18 -0400611/*
612 * helper function for defrag to decide if two blocks pointed to by a
613 * node are actually close by
614 */
Chris Mason6b800532007-10-15 16:17:34 -0400615static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400616{
Chris Mason6b800532007-10-15 16:17:34 -0400617 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400618 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400619 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400620 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500621 return 0;
622}
623
Chris Mason081e9572007-11-06 10:26:24 -0500624/*
625 * compare two keys in a memcmp fashion
626 */
627static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
628{
629 struct btrfs_key k1;
630
631 btrfs_disk_key_to_cpu(&k1, disk);
632
Diego Calleja20736ab2009-07-24 11:06:52 -0400633 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500634}
635
Josef Bacikf3465ca2008-11-12 14:19:50 -0500636/*
637 * same as comp_keys only with two btrfs_key's
638 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400639int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500640{
641 if (k1->objectid > k2->objectid)
642 return 1;
643 if (k1->objectid < k2->objectid)
644 return -1;
645 if (k1->type > k2->type)
646 return 1;
647 if (k1->type < k2->type)
648 return -1;
649 if (k1->offset > k2->offset)
650 return 1;
651 if (k1->offset < k2->offset)
652 return -1;
653 return 0;
654}
Chris Mason081e9572007-11-06 10:26:24 -0500655
Chris Masond352ac62008-09-29 15:18:18 -0400656/*
657 * this is used by the defrag code to go through all the
658 * leaves pointed to by a node and reallocate them so that
659 * disk order is close to key order
660 */
Chris Mason6702ed42007-08-07 16:15:09 -0400661int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400662 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400663 int start_slot, int cache_only, u64 *last_ret,
664 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400665{
Chris Mason6b800532007-10-15 16:17:34 -0400666 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400667 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400668 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400669 u64 search_start = *last_ret;
670 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400671 u64 other;
672 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400673 int end_slot;
674 int i;
675 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400676 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400677 int uptodate;
678 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500679 int progress_passed = 0;
680 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400681
Chris Mason5708b952007-10-25 15:43:18 -0400682 parent_level = btrfs_header_level(parent);
683 if (cache_only && parent_level != 1)
684 return 0;
685
Chris Masond3977122009-01-05 21:25:51 -0500686 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400687 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500688 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400689 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400690
Chris Mason6b800532007-10-15 16:17:34 -0400691 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400692 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400693 end_slot = parent_nritems;
694
695 if (parent_nritems == 1)
696 return 0;
697
Chris Masonb4ce94d2009-02-04 09:25:08 -0500698 btrfs_set_lock_blocking(parent);
699
Chris Mason6702ed42007-08-07 16:15:09 -0400700 for (i = start_slot; i < end_slot; i++) {
701 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400702
Chris Mason081e9572007-11-06 10:26:24 -0500703 btrfs_node_key(parent, &disk_key, i);
704 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
705 continue;
706
707 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400708 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400709 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400710 if (last_block == 0)
711 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400712
Chris Mason6702ed42007-08-07 16:15:09 -0400713 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400714 other = btrfs_node_blockptr(parent, i - 1);
715 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400716 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400717 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400718 other = btrfs_node_blockptr(parent, i + 1);
719 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400720 }
Chris Masone9d0b132007-08-10 14:06:19 -0400721 if (close) {
722 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400723 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400724 }
Chris Mason6702ed42007-08-07 16:15:09 -0400725
Chris Mason6b800532007-10-15 16:17:34 -0400726 cur = btrfs_find_tree_block(root, blocknr, blocksize);
727 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400728 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400729 else
730 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400731 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400732 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400733 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400734 continue;
735 }
Chris Mason6b800532007-10-15 16:17:34 -0400736 if (!cur) {
737 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400738 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000739 if (!cur)
740 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400741 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400742 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400743 }
Chris Mason6702ed42007-08-07 16:15:09 -0400744 }
Chris Masone9d0b132007-08-10 14:06:19 -0400745 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400746 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400747
Chris Masone7a84562008-06-25 16:01:31 -0400748 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500749 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400750 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400751 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400752 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400753 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400754 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400755 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400756 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400757 break;
Yan252c38f2007-08-29 09:11:44 -0400758 }
Chris Masone7a84562008-06-25 16:01:31 -0400759 search_start = cur->start;
760 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400761 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400762 btrfs_tree_unlock(cur);
763 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400764 }
765 return err;
766}
767
Chris Mason74123bd2007-02-02 11:05:29 -0500768/*
769 * The leaf data grows from end-to-front in the node.
770 * this returns the address of the start of the last item,
771 * which is the stop of the leaf data stack
772 */
Chris Mason123abc82007-03-14 14:14:43 -0400773static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775{
Chris Mason5f39d392007-10-15 16:14:19 -0400776 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400778 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400779 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500780}
781
Chris Masonaa5d6be2007-02-28 16:35:06 -0500782
Chris Mason74123bd2007-02-02 11:05:29 -0500783/*
Chris Mason5f39d392007-10-15 16:14:19 -0400784 * search for key in the extent_buffer. The items start at offset p,
785 * and they are item_size apart. There are 'max' items in p.
786 *
Chris Mason74123bd2007-02-02 11:05:29 -0500787 * the slot in the array is returned via slot, and it points to
788 * the place where you would insert key if it is not found in
789 * the array.
790 *
791 * slot may point to max if the key is bigger than all of the keys
792 */
Chris Masone02119d2008-09-05 16:13:11 -0400793static noinline int generic_bin_search(struct extent_buffer *eb,
794 unsigned long p,
795 int item_size, struct btrfs_key *key,
796 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500797{
798 int low = 0;
799 int high = max;
800 int mid;
801 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400802 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400803 struct btrfs_disk_key unaligned;
804 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400805 char *kaddr = NULL;
806 unsigned long map_start = 0;
807 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400808 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500809
Chris Masond3977122009-01-05 21:25:51 -0500810 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500811 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400812 offset = p + mid * item_size;
813
Chris Masona6591712011-07-19 12:04:14 -0400814 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400815 (offset + sizeof(struct btrfs_disk_key)) >
816 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500817
818 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400819 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400820 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400821
Chris Mason479965d2007-10-15 16:14:27 -0400822 if (!err) {
823 tmp = (struct btrfs_disk_key *)(kaddr + offset -
824 map_start);
825 } else {
826 read_extent_buffer(eb, &unaligned,
827 offset, sizeof(unaligned));
828 tmp = &unaligned;
829 }
830
Chris Mason5f39d392007-10-15 16:14:19 -0400831 } else {
832 tmp = (struct btrfs_disk_key *)(kaddr + offset -
833 map_start);
834 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500835 ret = comp_keys(tmp, key);
836
837 if (ret < 0)
838 low = mid + 1;
839 else if (ret > 0)
840 high = mid;
841 else {
842 *slot = mid;
843 return 0;
844 }
845 }
846 *slot = low;
847 return 1;
848}
849
Chris Mason97571fd2007-02-24 13:39:08 -0500850/*
851 * simple bin_search frontend that does the right thing for
852 * leaves vs nodes
853 */
Chris Mason5f39d392007-10-15 16:14:19 -0400854static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
855 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500856{
Chris Mason5f39d392007-10-15 16:14:19 -0400857 if (level == 0) {
858 return generic_bin_search(eb,
859 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400860 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400861 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400862 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500863 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400864 return generic_bin_search(eb,
865 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400866 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400867 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400868 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500869 }
870 return -1;
871}
872
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
874 int level, int *slot)
875{
876 return bin_search(eb, key, level, slot);
877}
878
Yan, Zhengf0486c62010-05-16 10:46:25 -0400879static void root_add_used(struct btrfs_root *root, u32 size)
880{
881 spin_lock(&root->accounting_lock);
882 btrfs_set_root_used(&root->root_item,
883 btrfs_root_used(&root->root_item) + size);
884 spin_unlock(&root->accounting_lock);
885}
886
887static void root_sub_used(struct btrfs_root *root, u32 size)
888{
889 spin_lock(&root->accounting_lock);
890 btrfs_set_root_used(&root->root_item,
891 btrfs_root_used(&root->root_item) - size);
892 spin_unlock(&root->accounting_lock);
893}
894
Chris Masond352ac62008-09-29 15:18:18 -0400895/* given a node and slot number, this reads the blocks it points to. The
896 * extent buffer is returned with a reference taken (but unlocked).
897 * NULL is returned on error.
898 */
Chris Masone02119d2008-09-05 16:13:11 -0400899static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400900 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500901{
Chris Masonca7a79a2008-05-12 12:59:19 -0400902 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500903 if (slot < 0)
904 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400905 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500906 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400907
908 BUG_ON(level == 0);
909
Chris Masondb945352007-10-15 16:15:53 -0400910 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400911 btrfs_level_size(root, level - 1),
912 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500913}
914
Chris Masond352ac62008-09-29 15:18:18 -0400915/*
916 * node level balancing, used to make sure nodes are in proper order for
917 * item deletion. We balance from the top down, so we have to make sure
918 * that a deletion won't leave an node completely empty later on.
919 */
Chris Masone02119d2008-09-05 16:13:11 -0400920static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500921 struct btrfs_root *root,
922 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500923{
Chris Mason5f39d392007-10-15 16:14:19 -0400924 struct extent_buffer *right = NULL;
925 struct extent_buffer *mid;
926 struct extent_buffer *left = NULL;
927 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500928 int ret = 0;
929 int wret;
930 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500931 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500932 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500933
934 if (level == 0)
935 return 0;
936
Chris Mason5f39d392007-10-15 16:14:19 -0400937 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500938
Chris Masonbd681512011-07-16 15:23:14 -0400939 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
940 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500941 WARN_ON(btrfs_header_generation(mid) != trans->transid);
942
Chris Mason1d4f8a02007-03-13 09:28:32 -0400943 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500944
Li Zefana05a9bb2011-09-06 16:55:34 +0800945 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400946 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800947 pslot = path->slots[level + 1];
948 }
Chris Masonbb803952007-03-01 12:04:21 -0500949
Chris Mason40689472007-03-17 14:29:23 -0400950 /*
951 * deal with the case where there is only one pointer in the root
952 * by promoting the node below to a root
953 */
Chris Mason5f39d392007-10-15 16:14:19 -0400954 if (!parent) {
955 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500956
Chris Mason5f39d392007-10-15 16:14:19 -0400957 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500958 return 0;
959
960 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400961 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -0700962 if (!child) {
963 ret = -EROFS;
964 btrfs_std_error(root->fs_info, ret);
965 goto enospc;
966 }
967
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500969 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400970 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400971 if (ret) {
972 btrfs_tree_unlock(child);
973 free_extent_buffer(child);
974 goto enospc;
975 }
Yan2f375ab2008-02-01 14:58:07 -0500976
Chris Mason240f62c2011-03-23 14:54:42 -0400977 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400978
Chris Mason0b86a832008-03-24 15:01:56 -0400979 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400980 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500981
Chris Mason925baed2008-06-25 16:01:30 -0400982 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500983 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400984 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400985 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500986 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400987 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400988
989 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200990 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500991 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -0500992 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400993 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500994 }
Chris Mason5f39d392007-10-15 16:14:19 -0400995 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400996 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500997 return 0;
998
Andi Kleen559af822010-10-29 15:14:37 -0400999 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001000
Chris Mason5f39d392007-10-15 16:14:19 -04001001 left = read_node_slot(root, parent, pslot - 1);
1002 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001003 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001004 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001005 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001006 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 if (wret) {
1008 ret = wret;
1009 goto enospc;
1010 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001011 }
Chris Mason5f39d392007-10-15 16:14:19 -04001012 right = read_node_slot(root, parent, pslot + 1);
1013 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001014 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001015 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001016 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001017 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001018 if (wret) {
1019 ret = wret;
1020 goto enospc;
1021 }
1022 }
1023
1024 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001025 if (left) {
1026 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001027 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001028 if (wret < 0)
1029 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001030 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001031 }
Chris Mason79f95c82007-03-01 15:16:26 -05001032
1033 /*
1034 * then try to empty the right most buffer into the middle
1035 */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001037 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001038 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001039 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001040 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001041 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001042 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001043 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001044 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001045 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001046 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001047 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001048 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001049 struct btrfs_disk_key right_key;
1050 btrfs_node_key(right, &right_key, 0);
1051 btrfs_set_node_key(parent, &right_key, pslot + 1);
1052 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001053 }
1054 }
Chris Mason5f39d392007-10-15 16:14:19 -04001055 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001056 /*
1057 * we're not allowed to leave a node with one item in the
1058 * tree during a delete. A deletion from lower in the tree
1059 * could try to delete the only pointer in this node.
1060 * So, pull some keys from the left.
1061 * There has to be a left pointer at this point because
1062 * otherwise we would have pulled some pointers from the
1063 * right
1064 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001065 if (!left) {
1066 ret = -EROFS;
1067 btrfs_std_error(root->fs_info, ret);
1068 goto enospc;
1069 }
Chris Mason5f39d392007-10-15 16:14:19 -04001070 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001071 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001072 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001073 goto enospc;
1074 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001075 if (wret == 1) {
1076 wret = push_node_left(trans, root, left, mid, 1);
1077 if (wret < 0)
1078 ret = wret;
1079 }
Chris Mason79f95c82007-03-01 15:16:26 -05001080 BUG_ON(wret == 1);
1081 }
Chris Mason5f39d392007-10-15 16:14:19 -04001082 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001083 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001084 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001085 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001086 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001087 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001088 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001089 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001090 } else {
1091 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001092 struct btrfs_disk_key mid_key;
1093 btrfs_node_key(mid, &mid_key, 0);
1094 btrfs_set_node_key(parent, &mid_key, pslot);
1095 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001096 }
Chris Masonbb803952007-03-01 12:04:21 -05001097
Chris Mason79f95c82007-03-01 15:16:26 -05001098 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001099 if (left) {
1100 if (btrfs_header_nritems(left) > orig_slot) {
1101 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001102 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001103 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001104 path->slots[level + 1] -= 1;
1105 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001106 if (mid) {
1107 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001108 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001109 }
Chris Masonbb803952007-03-01 12:04:21 -05001110 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001111 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001112 path->slots[level] = orig_slot;
1113 }
1114 }
Chris Mason79f95c82007-03-01 15:16:26 -05001115 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001116 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001117 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001118 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001119enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001120 if (right) {
1121 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001122 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001123 }
1124 if (left) {
1125 if (path->nodes[level] != left)
1126 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001127 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001128 }
Chris Masonbb803952007-03-01 12:04:21 -05001129 return ret;
1130}
1131
Chris Masond352ac62008-09-29 15:18:18 -04001132/* Node balancing for insertion. Here we only split or push nodes around
1133 * when they are completely full. This is also done top down, so we
1134 * have to be pessimistic.
1135 */
Chris Masond3977122009-01-05 21:25:51 -05001136static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001137 struct btrfs_root *root,
1138 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001139{
Chris Mason5f39d392007-10-15 16:14:19 -04001140 struct extent_buffer *right = NULL;
1141 struct extent_buffer *mid;
1142 struct extent_buffer *left = NULL;
1143 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001144 int ret = 0;
1145 int wret;
1146 int pslot;
1147 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001148
1149 if (level == 0)
1150 return 1;
1151
Chris Mason5f39d392007-10-15 16:14:19 -04001152 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001153 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001154
Li Zefana05a9bb2011-09-06 16:55:34 +08001155 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001156 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001157 pslot = path->slots[level + 1];
1158 }
Chris Masone66f7092007-04-20 13:16:02 -04001159
Chris Mason5f39d392007-10-15 16:14:19 -04001160 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001161 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001162
Chris Mason5f39d392007-10-15 16:14:19 -04001163 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001164
1165 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001166 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001167 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001168
1169 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001170 btrfs_set_lock_blocking(left);
1171
Chris Mason5f39d392007-10-15 16:14:19 -04001172 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001173 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1174 wret = 1;
1175 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001176 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001177 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001178 if (ret)
1179 wret = 1;
1180 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001182 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001183 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001184 }
Chris Masone66f7092007-04-20 13:16:02 -04001185 if (wret < 0)
1186 ret = wret;
1187 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001188 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001189 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001190 btrfs_node_key(mid, &disk_key, 0);
1191 btrfs_set_node_key(parent, &disk_key, pslot);
1192 btrfs_mark_buffer_dirty(parent);
1193 if (btrfs_header_nritems(left) > orig_slot) {
1194 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001195 path->slots[level + 1] -= 1;
1196 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001197 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001198 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001199 } else {
1200 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001201 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001202 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001203 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001204 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001205 }
Chris Masone66f7092007-04-20 13:16:02 -04001206 return 0;
1207 }
Chris Mason925baed2008-06-25 16:01:30 -04001208 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001209 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001210 }
Chris Mason925baed2008-06-25 16:01:30 -04001211 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001212
1213 /*
1214 * then try to empty the right most buffer into the middle
1215 */
Chris Mason5f39d392007-10-15 16:14:19 -04001216 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001217 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001218
Chris Mason925baed2008-06-25 16:01:30 -04001219 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001220 btrfs_set_lock_blocking(right);
1221
Chris Mason5f39d392007-10-15 16:14:19 -04001222 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001223 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1224 wret = 1;
1225 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001226 ret = btrfs_cow_block(trans, root, right,
1227 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001228 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001229 if (ret)
1230 wret = 1;
1231 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001232 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001233 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001234 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001235 }
Chris Masone66f7092007-04-20 13:16:02 -04001236 if (wret < 0)
1237 ret = wret;
1238 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001239 struct btrfs_disk_key disk_key;
1240
1241 btrfs_node_key(right, &disk_key, 0);
1242 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1243 btrfs_mark_buffer_dirty(parent);
1244
1245 if (btrfs_header_nritems(mid) <= orig_slot) {
1246 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001247 path->slots[level + 1] += 1;
1248 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001249 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001250 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001251 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001252 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001253 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001254 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001255 }
Chris Masone66f7092007-04-20 13:16:02 -04001256 return 0;
1257 }
Chris Mason925baed2008-06-25 16:01:30 -04001258 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001259 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001260 }
Chris Masone66f7092007-04-20 13:16:02 -04001261 return 1;
1262}
1263
Chris Mason74123bd2007-02-02 11:05:29 -05001264/*
Chris Masond352ac62008-09-29 15:18:18 -04001265 * readahead one full node of leaves, finding things that are close
1266 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 */
Chris Masonc8c42862009-04-03 10:14:18 -04001268static void reada_for_search(struct btrfs_root *root,
1269 struct btrfs_path *path,
1270 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001271{
Chris Mason5f39d392007-10-15 16:14:19 -04001272 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001273 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001274 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001275 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001276 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001277 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001278 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001279 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001280 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001281 u32 nr;
1282 u32 blocksize;
1283 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001284
Chris Masona6b6e752007-10-15 16:22:39 -04001285 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001286 return;
1287
Chris Mason6702ed42007-08-07 16:15:09 -04001288 if (!path->nodes[level])
1289 return;
1290
Chris Mason5f39d392007-10-15 16:14:19 -04001291 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001292
Chris Mason3c69fae2007-08-07 15:52:22 -04001293 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001294 blocksize = btrfs_level_size(root, level - 1);
1295 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001296 if (eb) {
1297 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001298 return;
1299 }
1300
Chris Masona7175312009-01-22 09:23:10 -05001301 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001302
Chris Mason5f39d392007-10-15 16:14:19 -04001303 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001304 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001305
Chris Masond3977122009-01-05 21:25:51 -05001306 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001307 if (direction < 0) {
1308 if (nr == 0)
1309 break;
1310 nr--;
1311 } else if (direction > 0) {
1312 nr++;
1313 if (nr >= nritems)
1314 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001315 }
Chris Mason01f46652007-12-21 16:24:26 -05001316 if (path->reada < 0 && objectid) {
1317 btrfs_node_key(node, &disk_key, nr);
1318 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1319 break;
1320 }
Chris Mason6b800532007-10-15 16:17:34 -04001321 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001322 if ((search <= target && target - search <= 65536) ||
1323 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001324 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001325 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001326 nread += blocksize;
1327 }
1328 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001329 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001330 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001331 }
1332}
Chris Mason925baed2008-06-25 16:01:30 -04001333
Chris Masond352ac62008-09-29 15:18:18 -04001334/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001335 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1336 * cache
1337 */
1338static noinline int reada_for_balance(struct btrfs_root *root,
1339 struct btrfs_path *path, int level)
1340{
1341 int slot;
1342 int nritems;
1343 struct extent_buffer *parent;
1344 struct extent_buffer *eb;
1345 u64 gen;
1346 u64 block1 = 0;
1347 u64 block2 = 0;
1348 int ret = 0;
1349 int blocksize;
1350
Chris Mason8c594ea2009-04-20 15:50:10 -04001351 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001352 if (!parent)
1353 return 0;
1354
1355 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001356 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001357 blocksize = btrfs_level_size(root, level);
1358
1359 if (slot > 0) {
1360 block1 = btrfs_node_blockptr(parent, slot - 1);
1361 gen = btrfs_node_ptr_generation(parent, slot - 1);
1362 eb = btrfs_find_tree_block(root, block1, blocksize);
1363 if (eb && btrfs_buffer_uptodate(eb, gen))
1364 block1 = 0;
1365 free_extent_buffer(eb);
1366 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001367 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001368 block2 = btrfs_node_blockptr(parent, slot + 1);
1369 gen = btrfs_node_ptr_generation(parent, slot + 1);
1370 eb = btrfs_find_tree_block(root, block2, blocksize);
1371 if (eb && btrfs_buffer_uptodate(eb, gen))
1372 block2 = 0;
1373 free_extent_buffer(eb);
1374 }
1375 if (block1 || block2) {
1376 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001377
1378 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001379 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001380
1381 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001382 if (block1)
1383 readahead_tree_block(root, block1, blocksize, 0);
1384 if (block2)
1385 readahead_tree_block(root, block2, blocksize, 0);
1386
1387 if (block1) {
1388 eb = read_tree_block(root, block1, blocksize, 0);
1389 free_extent_buffer(eb);
1390 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001391 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001392 eb = read_tree_block(root, block2, blocksize, 0);
1393 free_extent_buffer(eb);
1394 }
1395 }
1396 return ret;
1397}
1398
1399
1400/*
Chris Masond3977122009-01-05 21:25:51 -05001401 * when we walk down the tree, it is usually safe to unlock the higher layers
1402 * in the tree. The exceptions are when our path goes through slot 0, because
1403 * operations on the tree might require changing key pointers higher up in the
1404 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001405 *
Chris Masond3977122009-01-05 21:25:51 -05001406 * callers might also have set path->keep_locks, which tells this code to keep
1407 * the lock if the path points to the last slot in the block. This is part of
1408 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001409 *
Chris Masond3977122009-01-05 21:25:51 -05001410 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1411 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001412 */
Chris Masone02119d2008-09-05 16:13:11 -04001413static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001414 int lowest_unlock, int min_write_lock_level,
1415 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001416{
1417 int i;
1418 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001419 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001420 struct extent_buffer *t;
1421
1422 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1423 if (!path->nodes[i])
1424 break;
1425 if (!path->locks[i])
1426 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001427 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001428 skip_level = i + 1;
1429 continue;
1430 }
Chris Mason051e1b92008-06-25 16:01:30 -04001431 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001432 u32 nritems;
1433 t = path->nodes[i];
1434 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001435 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001436 skip_level = i + 1;
1437 continue;
1438 }
1439 }
Chris Mason051e1b92008-06-25 16:01:30 -04001440 if (skip_level < i && i >= lowest_unlock)
1441 no_skips = 1;
1442
Chris Mason925baed2008-06-25 16:01:30 -04001443 t = path->nodes[i];
1444 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001445 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001446 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001447 if (write_lock_level &&
1448 i > min_write_lock_level &&
1449 i <= *write_lock_level) {
1450 *write_lock_level = i - 1;
1451 }
Chris Mason925baed2008-06-25 16:01:30 -04001452 }
1453 }
1454}
1455
Chris Mason3c69fae2007-08-07 15:52:22 -04001456/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001457 * This releases any locks held in the path starting at level and
1458 * going all the way up to the root.
1459 *
1460 * btrfs_search_slot will keep the lock held on higher nodes in a few
1461 * corner cases, such as COW of the block at slot zero in the node. This
1462 * ignores those rules, and it should only be called when there are no
1463 * more updates to be done higher up in the tree.
1464 */
1465noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1466{
1467 int i;
1468
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001469 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001470 return;
1471
1472 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1473 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001474 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001475 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001476 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001477 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001478 path->locks[i] = 0;
1479 }
1480}
1481
1482/*
Chris Masonc8c42862009-04-03 10:14:18 -04001483 * helper function for btrfs_search_slot. The goal is to find a block
1484 * in cache without setting the path to blocking. If we find the block
1485 * we return zero and the path is unchanged.
1486 *
1487 * If we can't find the block, we set the path blocking and do some
1488 * reada. -EAGAIN is returned and the search must be repeated.
1489 */
1490static int
1491read_block_for_search(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *root, struct btrfs_path *p,
1493 struct extent_buffer **eb_ret, int level, int slot,
1494 struct btrfs_key *key)
1495{
1496 u64 blocknr;
1497 u64 gen;
1498 u32 blocksize;
1499 struct extent_buffer *b = *eb_ret;
1500 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001501 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001502
1503 blocknr = btrfs_node_blockptr(b, slot);
1504 gen = btrfs_node_ptr_generation(b, slot);
1505 blocksize = btrfs_level_size(root, level - 1);
1506
1507 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001508 if (tmp) {
1509 if (btrfs_buffer_uptodate(tmp, 0)) {
1510 if (btrfs_buffer_uptodate(tmp, gen)) {
1511 /*
1512 * we found an up to date block without
1513 * sleeping, return
1514 * right away
1515 */
1516 *eb_ret = tmp;
1517 return 0;
1518 }
1519 /* the pages were up to date, but we failed
1520 * the generation number check. Do a full
1521 * read for the generation number that is correct.
1522 * We must do this without dropping locks so
1523 * we can trust our generation number
1524 */
1525 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001526 btrfs_set_path_blocking(p);
1527
Chris Masoncb449212010-10-24 11:01:27 -04001528 tmp = read_tree_block(root, blocknr, blocksize, gen);
1529 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1530 *eb_ret = tmp;
1531 return 0;
1532 }
1533 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001534 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001535 return -EIO;
1536 }
Chris Masonc8c42862009-04-03 10:14:18 -04001537 }
1538
1539 /*
1540 * reduce lock contention at high levels
1541 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001542 * we read. Don't release the lock on the current
1543 * level because we need to walk this node to figure
1544 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001545 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001546 btrfs_unlock_up_safe(p, level + 1);
1547 btrfs_set_path_blocking(p);
1548
Chris Masoncb449212010-10-24 11:01:27 -04001549 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001550 if (p->reada)
1551 reada_for_search(root, p, level, slot, key->objectid);
1552
David Sterbab3b4aa72011-04-21 01:20:15 +02001553 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001554
1555 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001556 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001557 if (tmp) {
1558 /*
1559 * If the read above didn't mark this buffer up to date,
1560 * it will never end up being up to date. Set ret to EIO now
1561 * and give up so that our caller doesn't loop forever
1562 * on our EAGAINs.
1563 */
1564 if (!btrfs_buffer_uptodate(tmp, 0))
1565 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001566 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001567 }
1568 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001569}
1570
1571/*
1572 * helper function for btrfs_search_slot. This does all of the checks
1573 * for node-level blocks and does any balancing required based on
1574 * the ins_len.
1575 *
1576 * If no extra work was required, zero is returned. If we had to
1577 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1578 * start over
1579 */
1580static int
1581setup_nodes_for_search(struct btrfs_trans_handle *trans,
1582 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001583 struct extent_buffer *b, int level, int ins_len,
1584 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001585{
1586 int ret;
1587 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1588 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1589 int sret;
1590
Chris Masonbd681512011-07-16 15:23:14 -04001591 if (*write_lock_level < level + 1) {
1592 *write_lock_level = level + 1;
1593 btrfs_release_path(p);
1594 goto again;
1595 }
1596
Chris Masonc8c42862009-04-03 10:14:18 -04001597 sret = reada_for_balance(root, p, level);
1598 if (sret)
1599 goto again;
1600
1601 btrfs_set_path_blocking(p);
1602 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001603 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001604
1605 BUG_ON(sret > 0);
1606 if (sret) {
1607 ret = sret;
1608 goto done;
1609 }
1610 b = p->nodes[level];
1611 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001612 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001613 int sret;
1614
Chris Masonbd681512011-07-16 15:23:14 -04001615 if (*write_lock_level < level + 1) {
1616 *write_lock_level = level + 1;
1617 btrfs_release_path(p);
1618 goto again;
1619 }
1620
Chris Masonc8c42862009-04-03 10:14:18 -04001621 sret = reada_for_balance(root, p, level);
1622 if (sret)
1623 goto again;
1624
1625 btrfs_set_path_blocking(p);
1626 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001627 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001628
1629 if (sret) {
1630 ret = sret;
1631 goto done;
1632 }
1633 b = p->nodes[level];
1634 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001635 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001636 goto again;
1637 }
1638 BUG_ON(btrfs_header_nritems(b) == 1);
1639 }
1640 return 0;
1641
1642again:
1643 ret = -EAGAIN;
1644done:
1645 return ret;
1646}
1647
1648/*
Chris Mason74123bd2007-02-02 11:05:29 -05001649 * look for key in the tree. path is filled in with nodes along the way
1650 * if key is found, we return zero and you can find the item in the leaf
1651 * level of the path (level 0)
1652 *
1653 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001654 * be inserted, and 1 is returned. If there are other errors during the
1655 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001656 *
1657 * if ins_len > 0, nodes and leaves will be split as we walk down the
1658 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1659 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001660 */
Chris Masone089f052007-03-16 16:20:31 -04001661int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1662 *root, struct btrfs_key *key, struct btrfs_path *p, int
1663 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001664{
Chris Mason5f39d392007-10-15 16:14:19 -04001665 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001666 int slot;
1667 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001668 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001669 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001670 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001671 int root_lock;
1672 /* everything at write_lock_level or lower must be write locked */
1673 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001674 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001675 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001676
Chris Mason6702ed42007-08-07 16:15:09 -04001677 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001678 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001679 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001680
Chris Masonbd681512011-07-16 15:23:14 -04001681 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001682 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001683
Chris Masonbd681512011-07-16 15:23:14 -04001684 /* when we are removing items, we might have to go up to level
1685 * two as we update tree pointers Make sure we keep write
1686 * for those levels as well
1687 */
1688 write_lock_level = 2;
1689 } else if (ins_len > 0) {
1690 /*
1691 * for inserting items, make sure we have a write lock on
1692 * level 1 so we can update keys
1693 */
1694 write_lock_level = 1;
1695 }
1696
1697 if (!cow)
1698 write_lock_level = -1;
1699
1700 if (cow && (p->keep_locks || p->lowest_level))
1701 write_lock_level = BTRFS_MAX_LEVEL;
1702
Chris Masonf7c79f32012-03-19 15:54:38 -04001703 min_write_lock_level = write_lock_level;
1704
Chris Masonbb803952007-03-01 12:04:21 -05001705again:
Chris Masonbd681512011-07-16 15:23:14 -04001706 /*
1707 * we try very hard to do read locks on the root
1708 */
1709 root_lock = BTRFS_READ_LOCK;
1710 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001711 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001712 /*
1713 * the commit roots are read only
1714 * so we always do read locks
1715 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001716 b = root->commit_root;
1717 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001718 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001719 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001720 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001722 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001723 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001724 level = btrfs_header_level(b);
1725 } else {
1726 /* we don't know the level of the root node
1727 * until we actually have it read locked
1728 */
1729 b = btrfs_read_lock_root_node(root);
1730 level = btrfs_header_level(b);
1731 if (level <= write_lock_level) {
1732 /* whoops, must trade for write lock */
1733 btrfs_tree_read_unlock(b);
1734 free_extent_buffer(b);
1735 b = btrfs_lock_root_node(root);
1736 root_lock = BTRFS_WRITE_LOCK;
1737
1738 /* the level might have changed, check again */
1739 level = btrfs_header_level(b);
1740 }
1741 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001742 }
Chris Masonbd681512011-07-16 15:23:14 -04001743 p->nodes[level] = b;
1744 if (!p->skip_locking)
1745 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001746
Chris Masoneb60cea2007-02-02 09:18:22 -05001747 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001748 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001749
1750 /*
1751 * setup the path here so we can release it under lock
1752 * contention with the cow code
1753 */
Chris Mason02217ed2007-03-02 16:08:05 -05001754 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001755 /*
1756 * if we don't really need to cow this block
1757 * then we don't want to set the path blocking,
1758 * so we test it here
1759 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001760 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001761 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762
Chris Masonb4ce94d2009-02-04 09:25:08 -05001763 btrfs_set_path_blocking(p);
1764
Chris Masonbd681512011-07-16 15:23:14 -04001765 /*
1766 * must have write locks on this node and the
1767 * parent
1768 */
1769 if (level + 1 > write_lock_level) {
1770 write_lock_level = level + 1;
1771 btrfs_release_path(p);
1772 goto again;
1773 }
1774
Yan Zheng33c66f42009-07-22 09:59:00 -04001775 err = btrfs_cow_block(trans, root, b,
1776 p->nodes[level + 1],
1777 p->slots[level + 1], &b);
1778 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001779 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001780 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001781 }
Chris Mason02217ed2007-03-02 16:08:05 -05001782 }
Chris Mason65b51a02008-08-01 15:11:20 -04001783cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001784 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001785
Chris Masoneb60cea2007-02-02 09:18:22 -05001786 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001787 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001788
1789 /*
1790 * we have a lock on b and as long as we aren't changing
1791 * the tree, there is no way to for the items in b to change.
1792 * It is safe to drop the lock on our parent before we
1793 * go through the expensive btree search on b.
1794 *
1795 * If cow is true, then we might be changing slot zero,
1796 * which may require changing the parent. So, we can't
1797 * drop the lock until after we know which slot we're
1798 * operating on.
1799 */
1800 if (!cow)
1801 btrfs_unlock_up_safe(p, level + 1);
1802
Chris Mason5f39d392007-10-15 16:14:19 -04001803 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001804
Chris Mason5f39d392007-10-15 16:14:19 -04001805 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001806 int dec = 0;
1807 if (ret && slot > 0) {
1808 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001809 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001810 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001811 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001813 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001815 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001816 if (err) {
1817 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001818 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 }
Chris Masonc8c42862009-04-03 10:14:18 -04001820 b = p->nodes[level];
1821 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001822
Chris Masonbd681512011-07-16 15:23:14 -04001823 /*
1824 * slot 0 is special, if we change the key
1825 * we have to update the parent pointer
1826 * which means we must have a write lock
1827 * on the parent
1828 */
1829 if (slot == 0 && cow &&
1830 write_lock_level < level + 1) {
1831 write_lock_level = level + 1;
1832 btrfs_release_path(p);
1833 goto again;
1834 }
1835
Chris Masonf7c79f32012-03-19 15:54:38 -04001836 unlock_up(p, level, lowest_unlock,
1837 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001838
Chris Mason925baed2008-06-25 16:01:30 -04001839 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001840 if (dec)
1841 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001842 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001843 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001844
Yan Zheng33c66f42009-07-22 09:59:00 -04001845 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001846 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001847 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001848 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001849 if (err) {
1850 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001851 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001852 }
Chris Mason76a05b32009-05-14 13:24:30 -04001853
Chris Masonb4ce94d2009-02-04 09:25:08 -05001854 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001855 level = btrfs_header_level(b);
1856 if (level <= write_lock_level) {
1857 err = btrfs_try_tree_write_lock(b);
1858 if (!err) {
1859 btrfs_set_path_blocking(p);
1860 btrfs_tree_lock(b);
1861 btrfs_clear_path_blocking(p, b,
1862 BTRFS_WRITE_LOCK);
1863 }
1864 p->locks[level] = BTRFS_WRITE_LOCK;
1865 } else {
1866 err = btrfs_try_tree_read_lock(b);
1867 if (!err) {
1868 btrfs_set_path_blocking(p);
1869 btrfs_tree_read_lock(b);
1870 btrfs_clear_path_blocking(p, b,
1871 BTRFS_READ_LOCK);
1872 }
1873 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001874 }
Chris Masonbd681512011-07-16 15:23:14 -04001875 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001876 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001877 } else {
1878 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001879 if (ins_len > 0 &&
1880 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001881 if (write_lock_level < 1) {
1882 write_lock_level = 1;
1883 btrfs_release_path(p);
1884 goto again;
1885 }
1886
Chris Masonb4ce94d2009-02-04 09:25:08 -05001887 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001888 err = split_leaf(trans, root, key,
1889 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001890 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001891
Yan Zheng33c66f42009-07-22 09:59:00 -04001892 BUG_ON(err > 0);
1893 if (err) {
1894 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001895 goto done;
1896 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001897 }
Chris Mason459931e2008-12-10 09:10:46 -05001898 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04001899 unlock_up(p, level, lowest_unlock,
1900 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04001901 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001902 }
1903 }
Chris Mason65b51a02008-08-01 15:11:20 -04001904 ret = 1;
1905done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001906 /*
1907 * we don't really know what they plan on doing with the path
1908 * from here on, so for now just mark it as blocking
1909 */
Chris Masonb9473432009-03-13 11:00:37 -04001910 if (!p->leave_spinning)
1911 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001912 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001913 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001914 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001915}
1916
Chris Mason74123bd2007-02-02 11:05:29 -05001917/*
1918 * adjust the pointers going up the tree, starting at level
1919 * making sure the right key of each node is points to 'key'.
1920 * This is used after shifting pointers to the left, so it stops
1921 * fixing up pointers when a given leaf/node is not in slot 0 of the
1922 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001923 *
Chris Mason74123bd2007-02-02 11:05:29 -05001924 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001925static void fixup_low_keys(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *root, struct btrfs_path *path,
1927 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001928{
1929 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04001930 struct extent_buffer *t;
1931
Chris Mason234b63a2007-03-13 10:46:10 -04001932 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001933 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001934 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001935 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001936 t = path->nodes[i];
1937 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001938 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001939 if (tslot != 0)
1940 break;
1941 }
1942}
1943
Chris Mason74123bd2007-02-02 11:05:29 -05001944/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001945 * update item key.
1946 *
1947 * This function isn't completely safe. It's the caller's responsibility
1948 * that the new key won't break the order
1949 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001950void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1951 struct btrfs_root *root, struct btrfs_path *path,
1952 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04001953{
1954 struct btrfs_disk_key disk_key;
1955 struct extent_buffer *eb;
1956 int slot;
1957
1958 eb = path->nodes[0];
1959 slot = path->slots[0];
1960 if (slot > 0) {
1961 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001962 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001963 }
1964 if (slot < btrfs_header_nritems(eb) - 1) {
1965 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001966 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001967 }
1968
1969 btrfs_cpu_key_to_disk(&disk_key, new_key);
1970 btrfs_set_item_key(eb, &disk_key, slot);
1971 btrfs_mark_buffer_dirty(eb);
1972 if (slot == 0)
1973 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001974}
1975
1976/*
Chris Mason74123bd2007-02-02 11:05:29 -05001977 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001978 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001979 *
1980 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1981 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001982 */
Chris Mason98ed5172008-01-03 10:01:48 -05001983static int push_node_left(struct btrfs_trans_handle *trans,
1984 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001985 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001986{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001987 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001988 int src_nritems;
1989 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001990 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001991
Chris Mason5f39d392007-10-15 16:14:19 -04001992 src_nritems = btrfs_header_nritems(src);
1993 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001994 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001995 WARN_ON(btrfs_header_generation(src) != trans->transid);
1996 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001997
Chris Masonbce4eae2008-04-24 14:42:46 -04001998 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001999 return 1;
2000
Chris Masond3977122009-01-05 21:25:51 -05002001 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002002 return 1;
2003
Chris Masonbce4eae2008-04-24 14:42:46 -04002004 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002005 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002006 if (push_items < src_nritems) {
2007 /* leave at least 8 pointers in the node if
2008 * we aren't going to empty it
2009 */
2010 if (src_nritems - push_items < 8) {
2011 if (push_items <= 8)
2012 return 1;
2013 push_items -= 8;
2014 }
2015 }
2016 } else
2017 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002018
Chris Mason5f39d392007-10-15 16:14:19 -04002019 copy_extent_buffer(dst, src,
2020 btrfs_node_key_ptr_offset(dst_nritems),
2021 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002022 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002023
Chris Masonbb803952007-03-01 12:04:21 -05002024 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002025 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2026 btrfs_node_key_ptr_offset(push_items),
2027 (src_nritems - push_items) *
2028 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002029 }
Chris Mason5f39d392007-10-15 16:14:19 -04002030 btrfs_set_header_nritems(src, src_nritems - push_items);
2031 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2032 btrfs_mark_buffer_dirty(src);
2033 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002034
Chris Masonbb803952007-03-01 12:04:21 -05002035 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002036}
2037
Chris Mason97571fd2007-02-24 13:39:08 -05002038/*
Chris Mason79f95c82007-03-01 15:16:26 -05002039 * try to push data from one node into the next node right in the
2040 * tree.
2041 *
2042 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2043 * error, and > 0 if there was no room in the right hand block.
2044 *
2045 * this will only push up to 1/2 the contents of the left node over
2046 */
Chris Mason5f39d392007-10-15 16:14:19 -04002047static int balance_node_right(struct btrfs_trans_handle *trans,
2048 struct btrfs_root *root,
2049 struct extent_buffer *dst,
2050 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002051{
Chris Mason79f95c82007-03-01 15:16:26 -05002052 int push_items = 0;
2053 int max_push;
2054 int src_nritems;
2055 int dst_nritems;
2056 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002057
Chris Mason7bb86312007-12-11 09:25:06 -05002058 WARN_ON(btrfs_header_generation(src) != trans->transid);
2059 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2060
Chris Mason5f39d392007-10-15 16:14:19 -04002061 src_nritems = btrfs_header_nritems(src);
2062 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002063 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002064 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002065 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002066
Chris Masond3977122009-01-05 21:25:51 -05002067 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002068 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002069
2070 max_push = src_nritems / 2 + 1;
2071 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002072 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002073 return 1;
Yan252c38f2007-08-29 09:11:44 -04002074
Chris Mason79f95c82007-03-01 15:16:26 -05002075 if (max_push < push_items)
2076 push_items = max_push;
2077
Chris Mason5f39d392007-10-15 16:14:19 -04002078 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2079 btrfs_node_key_ptr_offset(0),
2080 (dst_nritems) *
2081 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002082
Chris Mason5f39d392007-10-15 16:14:19 -04002083 copy_extent_buffer(dst, src,
2084 btrfs_node_key_ptr_offset(0),
2085 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002086 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002087
Chris Mason5f39d392007-10-15 16:14:19 -04002088 btrfs_set_header_nritems(src, src_nritems - push_items);
2089 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 btrfs_mark_buffer_dirty(src);
2092 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002093
Chris Mason79f95c82007-03-01 15:16:26 -05002094 return ret;
2095}
2096
2097/*
Chris Mason97571fd2007-02-24 13:39:08 -05002098 * helper function to insert a new root level in the tree.
2099 * A new node is allocated, and a single item is inserted to
2100 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002101 *
2102 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002103 */
Chris Masond3977122009-01-05 21:25:51 -05002104static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002105 struct btrfs_root *root,
2106 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002107{
Chris Mason7bb86312007-12-11 09:25:06 -05002108 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002109 struct extent_buffer *lower;
2110 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002111 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002112 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002113
2114 BUG_ON(path->nodes[level]);
2115 BUG_ON(path->nodes[level-1] != root->node);
2116
Chris Mason7bb86312007-12-11 09:25:06 -05002117 lower = path->nodes[level-1];
2118 if (level == 1)
2119 btrfs_item_key(lower, &lower_key, 0);
2120 else
2121 btrfs_node_key(lower, &lower_key, 0);
2122
Zheng Yan31840ae2008-09-23 13:14:14 -04002123 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002124 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002125 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002126 if (IS_ERR(c))
2127 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002128
Yan, Zhengf0486c62010-05-16 10:46:25 -04002129 root_add_used(root, root->nodesize);
2130
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002131 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002132 btrfs_set_header_nritems(c, 1);
2133 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002134 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002135 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002136 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002138
Chris Mason5f39d392007-10-15 16:14:19 -04002139 write_extent_buffer(c, root->fs_info->fsid,
2140 (unsigned long)btrfs_header_fsid(c),
2141 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002142
2143 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2144 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2145 BTRFS_UUID_SIZE);
2146
Chris Mason5f39d392007-10-15 16:14:19 -04002147 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002148 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002149 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002150 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002151
2152 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002153
2154 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002155
Chris Mason925baed2008-06-25 16:01:30 -04002156 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002157 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002158
2159 /* the super has an extra ref to root->node */
2160 free_extent_buffer(old);
2161
Chris Mason0b86a832008-03-24 15:01:56 -04002162 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002163 extent_buffer_get(c);
2164 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002165 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002166 path->slots[level] = 0;
2167 return 0;
2168}
2169
Chris Mason74123bd2007-02-02 11:05:29 -05002170/*
2171 * worker function to insert a single pointer in a node.
2172 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002173 *
Chris Mason74123bd2007-02-02 11:05:29 -05002174 * slot and level indicate where you want the key to go, and
2175 * blocknr is the block the key points to.
2176 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002177static void insert_ptr(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *root, struct btrfs_path *path,
2179 struct btrfs_disk_key *key, u64 bytenr,
2180 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002181{
Chris Mason5f39d392007-10-15 16:14:19 -04002182 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002183 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002184
2185 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002186 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002187 lower = path->nodes[level];
2188 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002189 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002190 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002191 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002192 memmove_extent_buffer(lower,
2193 btrfs_node_key_ptr_offset(slot + 1),
2194 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002195 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002196 }
Chris Mason5f39d392007-10-15 16:14:19 -04002197 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002198 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002199 WARN_ON(trans->transid == 0);
2200 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002201 btrfs_set_header_nritems(lower, nritems + 1);
2202 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002203}
2204
Chris Mason97571fd2007-02-24 13:39:08 -05002205/*
2206 * split the node at the specified level in path in two.
2207 * The path is corrected to point to the appropriate node after the split
2208 *
2209 * Before splitting this tries to make some room in the node by pushing
2210 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002211 *
2212 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002213 */
Chris Masone02119d2008-09-05 16:13:11 -04002214static noinline int split_node(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *root,
2216 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002217{
Chris Mason5f39d392007-10-15 16:14:19 -04002218 struct extent_buffer *c;
2219 struct extent_buffer *split;
2220 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002221 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002222 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002223 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002224
Chris Mason5f39d392007-10-15 16:14:19 -04002225 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002226 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002227 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002228 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002229 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002230 if (ret)
2231 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002232 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002233 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002234 c = path->nodes[level];
2235 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002236 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002237 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002238 if (ret < 0)
2239 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002240 }
Chris Masone66f7092007-04-20 13:16:02 -04002241
Chris Mason5f39d392007-10-15 16:14:19 -04002242 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002243 mid = (c_nritems + 1) / 2;
2244 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002247 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002248 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002249 if (IS_ERR(split))
2250 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002251
Yan, Zhengf0486c62010-05-16 10:46:25 -04002252 root_add_used(root, root->nodesize);
2253
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002254 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002255 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002256 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002257 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002258 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002259 btrfs_set_header_owner(split, root->root_key.objectid);
2260 write_extent_buffer(split, root->fs_info->fsid,
2261 (unsigned long)btrfs_header_fsid(split),
2262 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002263 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2264 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2265 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002266
Chris Mason5f39d392007-10-15 16:14:19 -04002267
2268 copy_extent_buffer(split, c,
2269 btrfs_node_key_ptr_offset(0),
2270 btrfs_node_key_ptr_offset(mid),
2271 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2272 btrfs_set_header_nritems(split, c_nritems - mid);
2273 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002274 ret = 0;
2275
Chris Mason5f39d392007-10-15 16:14:19 -04002276 btrfs_mark_buffer_dirty(c);
2277 btrfs_mark_buffer_dirty(split);
2278
Jeff Mahoney143bede2012-03-01 14:56:26 +01002279 insert_ptr(trans, root, path, &disk_key, split->start,
2280 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002281
Chris Mason5de08d72007-02-24 06:24:44 -05002282 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002283 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002284 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002285 free_extent_buffer(c);
2286 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002287 path->slots[level + 1] += 1;
2288 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002289 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002290 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002291 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002292 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002293}
2294
Chris Mason74123bd2007-02-02 11:05:29 -05002295/*
2296 * how many bytes are required to store the items in a leaf. start
2297 * and nr indicate which items in the leaf to check. This totals up the
2298 * space used both by the item structs and the item data
2299 */
Chris Mason5f39d392007-10-15 16:14:19 -04002300static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002301{
2302 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002303 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002304 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002305
2306 if (!nr)
2307 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002308 data_len = btrfs_item_end_nr(l, start);
2309 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002310 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002311 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002312 return data_len;
2313}
2314
Chris Mason74123bd2007-02-02 11:05:29 -05002315/*
Chris Masond4dbff92007-04-04 14:08:15 -04002316 * The space between the end of the leaf items and
2317 * the start of the leaf data. IOW, how much room
2318 * the leaf has left for both items and data
2319 */
Chris Masond3977122009-01-05 21:25:51 -05002320noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002321 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002322{
Chris Mason5f39d392007-10-15 16:14:19 -04002323 int nritems = btrfs_header_nritems(leaf);
2324 int ret;
2325 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2326 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002327 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2328 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002329 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002330 leaf_space_used(leaf, 0, nritems), nritems);
2331 }
2332 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002333}
2334
Chris Mason99d8f832010-07-07 10:51:48 -04002335/*
2336 * min slot controls the lowest index we're willing to push to the
2337 * right. We'll push up to and including min_slot, but no lower
2338 */
Chris Mason44871b12009-03-13 10:04:31 -04002339static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2340 struct btrfs_root *root,
2341 struct btrfs_path *path,
2342 int data_size, int empty,
2343 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002344 int free_space, u32 left_nritems,
2345 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002346{
Chris Mason5f39d392007-10-15 16:14:19 -04002347 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002348 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002349 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002350 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002351 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002352 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002353 int push_space = 0;
2354 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002355 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002356 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002357 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002358 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002359 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002360
Chris Masoncfed81a2012-03-03 07:40:03 -05002361 btrfs_init_map_token(&token);
2362
Chris Mason34a38212007-11-07 13:31:03 -05002363 if (empty)
2364 nr = 0;
2365 else
Chris Mason99d8f832010-07-07 10:51:48 -04002366 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002367
Zheng Yan31840ae2008-09-23 13:14:14 -04002368 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002369 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002370
Chris Mason44871b12009-03-13 10:04:31 -04002371 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002372 i = left_nritems - 1;
2373 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002374 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002375
Zheng Yan31840ae2008-09-23 13:14:14 -04002376 if (!empty && push_items > 0) {
2377 if (path->slots[0] > i)
2378 break;
2379 if (path->slots[0] == i) {
2380 int space = btrfs_leaf_free_space(root, left);
2381 if (space + push_space * 2 > free_space)
2382 break;
2383 }
2384 }
2385
Chris Mason00ec4c52007-02-24 12:47:20 -05002386 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002387 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002388
Chris Masondb945352007-10-15 16:15:53 -04002389 this_item_size = btrfs_item_size(left, item);
2390 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002391 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002392
Chris Mason00ec4c52007-02-24 12:47:20 -05002393 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002394 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002395 if (i == 0)
2396 break;
2397 i--;
Chris Masondb945352007-10-15 16:15:53 -04002398 }
Chris Mason5f39d392007-10-15 16:14:19 -04002399
Chris Mason925baed2008-06-25 16:01:30 -04002400 if (push_items == 0)
2401 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002402
Chris Mason34a38212007-11-07 13:31:03 -05002403 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002404 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002405
Chris Mason00ec4c52007-02-24 12:47:20 -05002406 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002407 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002408
Chris Mason5f39d392007-10-15 16:14:19 -04002409 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002410 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002411
Chris Mason00ec4c52007-02-24 12:47:20 -05002412 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002413 data_end = leaf_data_end(root, right);
2414 memmove_extent_buffer(right,
2415 btrfs_leaf_data(right) + data_end - push_space,
2416 btrfs_leaf_data(right) + data_end,
2417 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2418
Chris Mason00ec4c52007-02-24 12:47:20 -05002419 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002420 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002421 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2422 btrfs_leaf_data(left) + leaf_data_end(root, left),
2423 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002424
2425 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2426 btrfs_item_nr_offset(0),
2427 right_nritems * sizeof(struct btrfs_item));
2428
Chris Mason00ec4c52007-02-24 12:47:20 -05002429 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002430 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2431 btrfs_item_nr_offset(left_nritems - push_items),
2432 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002433
2434 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002435 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002436 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002437 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002438 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002439 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002440 push_space -= btrfs_token_item_size(right, item, &token);
2441 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002442 }
2443
Chris Mason7518a232007-03-12 12:01:18 -04002444 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002445 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002446
Chris Mason34a38212007-11-07 13:31:03 -05002447 if (left_nritems)
2448 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002449 else
2450 clean_tree_block(trans, root, left);
2451
Chris Mason5f39d392007-10-15 16:14:19 -04002452 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002453
Chris Mason5f39d392007-10-15 16:14:19 -04002454 btrfs_item_key(right, &disk_key, 0);
2455 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002456 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002457
Chris Mason00ec4c52007-02-24 12:47:20 -05002458 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002459 if (path->slots[0] >= left_nritems) {
2460 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002461 if (btrfs_header_nritems(path->nodes[0]) == 0)
2462 clean_tree_block(trans, root, path->nodes[0]);
2463 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002464 free_extent_buffer(path->nodes[0]);
2465 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002466 path->slots[1] += 1;
2467 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002468 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002469 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002470 }
2471 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002472
2473out_unlock:
2474 btrfs_tree_unlock(right);
2475 free_extent_buffer(right);
2476 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002477}
Chris Mason925baed2008-06-25 16:01:30 -04002478
Chris Mason00ec4c52007-02-24 12:47:20 -05002479/*
Chris Mason44871b12009-03-13 10:04:31 -04002480 * push some data in the path leaf to the right, trying to free up at
2481 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2482 *
2483 * returns 1 if the push failed because the other node didn't have enough
2484 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002485 *
2486 * this will push starting from min_slot to the end of the leaf. It won't
2487 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002488 */
2489static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002490 *root, struct btrfs_path *path,
2491 int min_data_size, int data_size,
2492 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002493{
2494 struct extent_buffer *left = path->nodes[0];
2495 struct extent_buffer *right;
2496 struct extent_buffer *upper;
2497 int slot;
2498 int free_space;
2499 u32 left_nritems;
2500 int ret;
2501
2502 if (!path->nodes[1])
2503 return 1;
2504
2505 slot = path->slots[1];
2506 upper = path->nodes[1];
2507 if (slot >= btrfs_header_nritems(upper) - 1)
2508 return 1;
2509
2510 btrfs_assert_tree_locked(path->nodes[1]);
2511
2512 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002513 if (right == NULL)
2514 return 1;
2515
Chris Mason44871b12009-03-13 10:04:31 -04002516 btrfs_tree_lock(right);
2517 btrfs_set_lock_blocking(right);
2518
2519 free_space = btrfs_leaf_free_space(root, right);
2520 if (free_space < data_size)
2521 goto out_unlock;
2522
2523 /* cow and double check */
2524 ret = btrfs_cow_block(trans, root, right, upper,
2525 slot + 1, &right);
2526 if (ret)
2527 goto out_unlock;
2528
2529 free_space = btrfs_leaf_free_space(root, right);
2530 if (free_space < data_size)
2531 goto out_unlock;
2532
2533 left_nritems = btrfs_header_nritems(left);
2534 if (left_nritems == 0)
2535 goto out_unlock;
2536
Chris Mason99d8f832010-07-07 10:51:48 -04002537 return __push_leaf_right(trans, root, path, min_data_size, empty,
2538 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002539out_unlock:
2540 btrfs_tree_unlock(right);
2541 free_extent_buffer(right);
2542 return 1;
2543}
2544
2545/*
Chris Mason74123bd2007-02-02 11:05:29 -05002546 * push some data in the path leaf to the left, trying to free up at
2547 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002548 *
2549 * max_slot can put a limit on how far into the leaf we'll push items. The
2550 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2551 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002552 */
Chris Mason44871b12009-03-13 10:04:31 -04002553static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2554 struct btrfs_root *root,
2555 struct btrfs_path *path, int data_size,
2556 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002557 int free_space, u32 right_nritems,
2558 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559{
Chris Mason5f39d392007-10-15 16:14:19 -04002560 struct btrfs_disk_key disk_key;
2561 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 int push_space = 0;
2564 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002565 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002566 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002567 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002568 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002569 u32 this_item_size;
2570 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002571 struct btrfs_map_token token;
2572
2573 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002574
Chris Mason34a38212007-11-07 13:31:03 -05002575 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002576 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002577 else
Chris Mason99d8f832010-07-07 10:51:48 -04002578 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002579
2580 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002581 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002582
Zheng Yan31840ae2008-09-23 13:14:14 -04002583 if (!empty && push_items > 0) {
2584 if (path->slots[0] < i)
2585 break;
2586 if (path->slots[0] == i) {
2587 int space = btrfs_leaf_free_space(root, right);
2588 if (space + push_space * 2 > free_space)
2589 break;
2590 }
2591 }
2592
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002594 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002595
2596 this_item_size = btrfs_item_size(right, item);
2597 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 break;
Chris Masondb945352007-10-15 16:15:53 -04002599
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002601 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 }
Chris Masondb945352007-10-15 16:15:53 -04002603
Chris Masonbe0e5c02007-01-26 15:51:26 -05002604 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002605 ret = 1;
2606 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002607 }
Chris Mason34a38212007-11-07 13:31:03 -05002608 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002609 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002610
Chris Masonbe0e5c02007-01-26 15:51:26 -05002611 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002612 copy_extent_buffer(left, right,
2613 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2614 btrfs_item_nr_offset(0),
2615 push_items * sizeof(struct btrfs_item));
2616
Chris Mason123abc82007-03-14 14:14:43 -04002617 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002618 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002619
2620 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002621 leaf_data_end(root, left) - push_space,
2622 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002623 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002624 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002625 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002626 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002627
Chris Masondb945352007-10-15 16:15:53 -04002628 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002629 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002630 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002631
Chris Mason5f39d392007-10-15 16:14:19 -04002632 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002633
Chris Masoncfed81a2012-03-03 07:40:03 -05002634 ioff = btrfs_token_item_offset(left, item, &token);
2635 btrfs_set_token_item_offset(left, item,
2636 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2637 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002638 }
Chris Mason5f39d392007-10-15 16:14:19 -04002639 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002640
2641 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002642 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002643 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2644 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002645 WARN_ON(1);
2646 }
Chris Mason5f39d392007-10-15 16:14:19 -04002647
Chris Mason34a38212007-11-07 13:31:03 -05002648 if (push_items < right_nritems) {
2649 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2650 leaf_data_end(root, right);
2651 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2652 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2653 btrfs_leaf_data(right) +
2654 leaf_data_end(root, right), push_space);
2655
2656 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002657 btrfs_item_nr_offset(push_items),
2658 (btrfs_header_nritems(right) - push_items) *
2659 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002660 }
Yaneef1c492007-11-26 10:58:13 -05002661 right_nritems -= push_items;
2662 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002663 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002664 for (i = 0; i < right_nritems; i++) {
2665 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002666
Chris Masoncfed81a2012-03-03 07:40:03 -05002667 push_space = push_space - btrfs_token_item_size(right,
2668 item, &token);
2669 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002670 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002671
Chris Mason5f39d392007-10-15 16:14:19 -04002672 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002673 if (right_nritems)
2674 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002675 else
2676 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002677
Chris Mason5f39d392007-10-15 16:14:19 -04002678 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002679 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680
2681 /* then fixup the leaf pointer in the path */
2682 if (path->slots[0] < push_items) {
2683 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002684 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002685 free_extent_buffer(path->nodes[0]);
2686 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002687 path->slots[1] -= 1;
2688 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002689 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002690 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002691 path->slots[0] -= push_items;
2692 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002693 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002694 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002695out:
2696 btrfs_tree_unlock(left);
2697 free_extent_buffer(left);
2698 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699}
2700
Chris Mason74123bd2007-02-02 11:05:29 -05002701/*
Chris Mason44871b12009-03-13 10:04:31 -04002702 * push some data in the path leaf to the left, trying to free up at
2703 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002704 *
2705 * max_slot can put a limit on how far into the leaf we'll push items. The
2706 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2707 * items
Chris Mason44871b12009-03-13 10:04:31 -04002708 */
2709static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002710 *root, struct btrfs_path *path, int min_data_size,
2711 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002712{
2713 struct extent_buffer *right = path->nodes[0];
2714 struct extent_buffer *left;
2715 int slot;
2716 int free_space;
2717 u32 right_nritems;
2718 int ret = 0;
2719
2720 slot = path->slots[1];
2721 if (slot == 0)
2722 return 1;
2723 if (!path->nodes[1])
2724 return 1;
2725
2726 right_nritems = btrfs_header_nritems(right);
2727 if (right_nritems == 0)
2728 return 1;
2729
2730 btrfs_assert_tree_locked(path->nodes[1]);
2731
2732 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002733 if (left == NULL)
2734 return 1;
2735
Chris Mason44871b12009-03-13 10:04:31 -04002736 btrfs_tree_lock(left);
2737 btrfs_set_lock_blocking(left);
2738
2739 free_space = btrfs_leaf_free_space(root, left);
2740 if (free_space < data_size) {
2741 ret = 1;
2742 goto out;
2743 }
2744
2745 /* cow and double check */
2746 ret = btrfs_cow_block(trans, root, left,
2747 path->nodes[1], slot - 1, &left);
2748 if (ret) {
2749 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002750 if (ret == -ENOSPC)
2751 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002752 goto out;
2753 }
2754
2755 free_space = btrfs_leaf_free_space(root, left);
2756 if (free_space < data_size) {
2757 ret = 1;
2758 goto out;
2759 }
2760
Chris Mason99d8f832010-07-07 10:51:48 -04002761 return __push_leaf_left(trans, root, path, min_data_size,
2762 empty, left, free_space, right_nritems,
2763 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002764out:
2765 btrfs_tree_unlock(left);
2766 free_extent_buffer(left);
2767 return ret;
2768}
2769
2770/*
Chris Mason74123bd2007-02-02 11:05:29 -05002771 * split the path's leaf in two, making sure there is at least data_size
2772 * available for the resulting leaf level of the path.
2773 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002774static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2775 struct btrfs_root *root,
2776 struct btrfs_path *path,
2777 struct extent_buffer *l,
2778 struct extent_buffer *right,
2779 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002780{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002781 int data_copy_size;
2782 int rt_data_off;
2783 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002784 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05002785 struct btrfs_map_token token;
2786
2787 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002788
Chris Mason5f39d392007-10-15 16:14:19 -04002789 nritems = nritems - mid;
2790 btrfs_set_header_nritems(right, nritems);
2791 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2792
2793 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2794 btrfs_item_nr_offset(mid),
2795 nritems * sizeof(struct btrfs_item));
2796
2797 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002798 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2799 data_copy_size, btrfs_leaf_data(l) +
2800 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002801
Chris Mason5f39d392007-10-15 16:14:19 -04002802 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2803 btrfs_item_end_nr(l, mid);
2804
2805 for (i = 0; i < nritems; i++) {
2806 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002807 u32 ioff;
2808
Chris Masoncfed81a2012-03-03 07:40:03 -05002809 ioff = btrfs_token_item_offset(right, item, &token);
2810 btrfs_set_token_item_offset(right, item,
2811 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002812 }
Chris Mason74123bd2007-02-02 11:05:29 -05002813
Chris Mason5f39d392007-10-15 16:14:19 -04002814 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002815 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002816 insert_ptr(trans, root, path, &disk_key, right->start,
2817 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002818
2819 btrfs_mark_buffer_dirty(right);
2820 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002821 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002822
Chris Masonbe0e5c02007-01-26 15:51:26 -05002823 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002824 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002825 free_extent_buffer(path->nodes[0]);
2826 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002827 path->slots[0] -= mid;
2828 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002829 } else {
2830 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002831 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002832 }
Chris Mason5f39d392007-10-15 16:14:19 -04002833
Chris Masoneb60cea2007-02-02 09:18:22 -05002834 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04002835}
2836
2837/*
Chris Mason99d8f832010-07-07 10:51:48 -04002838 * double splits happen when we need to insert a big item in the middle
2839 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2840 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2841 * A B C
2842 *
2843 * We avoid this by trying to push the items on either side of our target
2844 * into the adjacent leaves. If all goes well we can avoid the double split
2845 * completely.
2846 */
2847static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root,
2849 struct btrfs_path *path,
2850 int data_size)
2851{
2852 int ret;
2853 int progress = 0;
2854 int slot;
2855 u32 nritems;
2856
2857 slot = path->slots[0];
2858
2859 /*
2860 * try to push all the items after our slot into the
2861 * right leaf
2862 */
2863 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2864 if (ret < 0)
2865 return ret;
2866
2867 if (ret == 0)
2868 progress++;
2869
2870 nritems = btrfs_header_nritems(path->nodes[0]);
2871 /*
2872 * our goal is to get our slot at the start or end of a leaf. If
2873 * we've done so we're done
2874 */
2875 if (path->slots[0] == 0 || path->slots[0] == nritems)
2876 return 0;
2877
2878 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2879 return 0;
2880
2881 /* try to push all the items before our slot into the next leaf */
2882 slot = path->slots[0];
2883 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2884 if (ret < 0)
2885 return ret;
2886
2887 if (ret == 0)
2888 progress++;
2889
2890 if (progress)
2891 return 0;
2892 return 1;
2893}
2894
2895/*
Chris Mason44871b12009-03-13 10:04:31 -04002896 * split the path's leaf in two, making sure there is at least data_size
2897 * available for the resulting leaf level of the path.
2898 *
2899 * returns 0 if all went well and < 0 on failure.
2900 */
2901static noinline int split_leaf(struct btrfs_trans_handle *trans,
2902 struct btrfs_root *root,
2903 struct btrfs_key *ins_key,
2904 struct btrfs_path *path, int data_size,
2905 int extend)
2906{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002907 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002908 struct extent_buffer *l;
2909 u32 nritems;
2910 int mid;
2911 int slot;
2912 struct extent_buffer *right;
2913 int ret = 0;
2914 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002916 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002917 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002918
Yan, Zhenga5719522009-09-24 09:17:31 -04002919 l = path->nodes[0];
2920 slot = path->slots[0];
2921 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2922 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2923 return -EOVERFLOW;
2924
Chris Mason44871b12009-03-13 10:04:31 -04002925 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002926 if (data_size) {
2927 wret = push_leaf_right(trans, root, path, data_size,
2928 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002929 if (wret < 0)
2930 return wret;
2931 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002932 wret = push_leaf_left(trans, root, path, data_size,
2933 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002934 if (wret < 0)
2935 return wret;
2936 }
2937 l = path->nodes[0];
2938
2939 /* did the pushes work? */
2940 if (btrfs_leaf_free_space(root, l) >= data_size)
2941 return 0;
2942 }
2943
2944 if (!path->nodes[1]) {
2945 ret = insert_new_root(trans, root, path, 1);
2946 if (ret)
2947 return ret;
2948 }
2949again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002950 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002951 l = path->nodes[0];
2952 slot = path->slots[0];
2953 nritems = btrfs_header_nritems(l);
2954 mid = (nritems + 1) / 2;
2955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002956 if (mid <= slot) {
2957 if (nritems == 1 ||
2958 leaf_space_used(l, mid, nritems - mid) + data_size >
2959 BTRFS_LEAF_DATA_SIZE(root)) {
2960 if (slot >= nritems) {
2961 split = 0;
2962 } else {
2963 mid = slot;
2964 if (mid != nritems &&
2965 leaf_space_used(l, mid, nritems - mid) +
2966 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002967 if (data_size && !tried_avoid_double)
2968 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969 split = 2;
2970 }
2971 }
2972 }
2973 } else {
2974 if (leaf_space_used(l, 0, mid) + data_size >
2975 BTRFS_LEAF_DATA_SIZE(root)) {
2976 if (!extend && data_size && slot == 0) {
2977 split = 0;
2978 } else if ((extend || !data_size) && slot == 0) {
2979 mid = 1;
2980 } else {
2981 mid = slot;
2982 if (mid != nritems &&
2983 leaf_space_used(l, mid, nritems - mid) +
2984 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002985 if (data_size && !tried_avoid_double)
2986 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 split = 2 ;
2988 }
2989 }
2990 }
2991 }
2992
2993 if (split == 0)
2994 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2995 else
2996 btrfs_item_key(l, &disk_key, mid);
2997
2998 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002999 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003000 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003001 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003002 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003003
3004 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003005
3006 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3007 btrfs_set_header_bytenr(right, right->start);
3008 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003009 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003010 btrfs_set_header_owner(right, root->root_key.objectid);
3011 btrfs_set_header_level(right, 0);
3012 write_extent_buffer(right, root->fs_info->fsid,
3013 (unsigned long)btrfs_header_fsid(right),
3014 BTRFS_FSID_SIZE);
3015
3016 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3017 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3018 BTRFS_UUID_SIZE);
3019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 if (split == 0) {
3021 if (mid <= slot) {
3022 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003023 insert_ptr(trans, root, path, &disk_key, right->start,
3024 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003025 btrfs_tree_unlock(path->nodes[0]);
3026 free_extent_buffer(path->nodes[0]);
3027 path->nodes[0] = right;
3028 path->slots[0] = 0;
3029 path->slots[1] += 1;
3030 } else {
3031 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003032 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003033 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003034 btrfs_tree_unlock(path->nodes[0]);
3035 free_extent_buffer(path->nodes[0]);
3036 path->nodes[0] = right;
3037 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003038 if (path->slots[1] == 0)
3039 fixup_low_keys(trans, root, path,
3040 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003041 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003042 btrfs_mark_buffer_dirty(right);
3043 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003044 }
3045
Jeff Mahoney143bede2012-03-01 14:56:26 +01003046 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003049 BUG_ON(num_doubles != 0);
3050 num_doubles++;
3051 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003052 }
Chris Mason44871b12009-03-13 10:04:31 -04003053
Jeff Mahoney143bede2012-03-01 14:56:26 +01003054 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003055
3056push_for_double:
3057 push_for_double_split(trans, root, path, data_size);
3058 tried_avoid_double = 1;
3059 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3060 return 0;
3061 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003062}
3063
Yan, Zhengad48fd752009-11-12 09:33:58 +00003064static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3065 struct btrfs_root *root,
3066 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003067{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003068 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003069 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003070 struct btrfs_file_extent_item *fi;
3071 u64 extent_len = 0;
3072 u32 item_size;
3073 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003074
3075 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003076 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3077
3078 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3079 key.type != BTRFS_EXTENT_CSUM_KEY);
3080
3081 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3082 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003083
3084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003085 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3086 fi = btrfs_item_ptr(leaf, path->slots[0],
3087 struct btrfs_file_extent_item);
3088 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3089 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003090 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003091
Chris Mason459931e2008-12-10 09:10:46 -05003092 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003093 path->search_for_split = 1;
3094 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003095 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003096 if (ret < 0)
3097 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003098
Yan, Zhengad48fd752009-11-12 09:33:58 +00003099 ret = -EAGAIN;
3100 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003101 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003102 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3103 goto err;
3104
Chris Mason109f6ae2010-04-02 09:20:18 -04003105 /* the leaf has changed, it now has room. return now */
3106 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3107 goto err;
3108
Yan, Zhengad48fd752009-11-12 09:33:58 +00003109 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3110 fi = btrfs_item_ptr(leaf, path->slots[0],
3111 struct btrfs_file_extent_item);
3112 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3113 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003114 }
3115
Chris Masonb9473432009-03-13 11:00:37 -04003116 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003117 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003118 if (ret)
3119 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003120
Yan, Zhengad48fd752009-11-12 09:33:58 +00003121 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003122 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003123 return 0;
3124err:
3125 path->keep_locks = 0;
3126 return ret;
3127}
3128
3129static noinline int split_item(struct btrfs_trans_handle *trans,
3130 struct btrfs_root *root,
3131 struct btrfs_path *path,
3132 struct btrfs_key *new_key,
3133 unsigned long split_offset)
3134{
3135 struct extent_buffer *leaf;
3136 struct btrfs_item *item;
3137 struct btrfs_item *new_item;
3138 int slot;
3139 char *buf;
3140 u32 nritems;
3141 u32 item_size;
3142 u32 orig_offset;
3143 struct btrfs_disk_key disk_key;
3144
Chris Masonb9473432009-03-13 11:00:37 -04003145 leaf = path->nodes[0];
3146 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3147
Chris Masonb4ce94d2009-02-04 09:25:08 -05003148 btrfs_set_path_blocking(path);
3149
Chris Mason459931e2008-12-10 09:10:46 -05003150 item = btrfs_item_nr(leaf, path->slots[0]);
3151 orig_offset = btrfs_item_offset(leaf, item);
3152 item_size = btrfs_item_size(leaf, item);
3153
Chris Mason459931e2008-12-10 09:10:46 -05003154 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003155 if (!buf)
3156 return -ENOMEM;
3157
Chris Mason459931e2008-12-10 09:10:46 -05003158 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3159 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003160
Chris Mason459931e2008-12-10 09:10:46 -05003161 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003162 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003163 if (slot != nritems) {
3164 /* shift the items */
3165 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003166 btrfs_item_nr_offset(slot),
3167 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003168 }
3169
3170 btrfs_cpu_key_to_disk(&disk_key, new_key);
3171 btrfs_set_item_key(leaf, &disk_key, slot);
3172
3173 new_item = btrfs_item_nr(leaf, slot);
3174
3175 btrfs_set_item_offset(leaf, new_item, orig_offset);
3176 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3177
3178 btrfs_set_item_offset(leaf, item,
3179 orig_offset + item_size - split_offset);
3180 btrfs_set_item_size(leaf, item, split_offset);
3181
3182 btrfs_set_header_nritems(leaf, nritems + 1);
3183
3184 /* write the data for the start of the original item */
3185 write_extent_buffer(leaf, buf,
3186 btrfs_item_ptr_offset(leaf, path->slots[0]),
3187 split_offset);
3188
3189 /* write the data for the new item */
3190 write_extent_buffer(leaf, buf + split_offset,
3191 btrfs_item_ptr_offset(leaf, slot),
3192 item_size - split_offset);
3193 btrfs_mark_buffer_dirty(leaf);
3194
Yan, Zhengad48fd752009-11-12 09:33:58 +00003195 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003196 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003197 return 0;
3198}
3199
3200/*
3201 * This function splits a single item into two items,
3202 * giving 'new_key' to the new item and splitting the
3203 * old one at split_offset (from the start of the item).
3204 *
3205 * The path may be released by this operation. After
3206 * the split, the path is pointing to the old item. The
3207 * new item is going to be in the same node as the old one.
3208 *
3209 * Note, the item being split must be smaller enough to live alone on
3210 * a tree block with room for one extra struct btrfs_item
3211 *
3212 * This allows us to split the item in place, keeping a lock on the
3213 * leaf the entire time.
3214 */
3215int btrfs_split_item(struct btrfs_trans_handle *trans,
3216 struct btrfs_root *root,
3217 struct btrfs_path *path,
3218 struct btrfs_key *new_key,
3219 unsigned long split_offset)
3220{
3221 int ret;
3222 ret = setup_leaf_for_split(trans, root, path,
3223 sizeof(struct btrfs_item));
3224 if (ret)
3225 return ret;
3226
3227 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003228 return ret;
3229}
3230
3231/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003232 * This function duplicate a item, giving 'new_key' to the new item.
3233 * It guarantees both items live in the same tree leaf and the new item
3234 * is contiguous with the original item.
3235 *
3236 * This allows us to split file extent in place, keeping a lock on the
3237 * leaf the entire time.
3238 */
3239int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3240 struct btrfs_root *root,
3241 struct btrfs_path *path,
3242 struct btrfs_key *new_key)
3243{
3244 struct extent_buffer *leaf;
3245 int ret;
3246 u32 item_size;
3247
3248 leaf = path->nodes[0];
3249 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3250 ret = setup_leaf_for_split(trans, root, path,
3251 item_size + sizeof(struct btrfs_item));
3252 if (ret)
3253 return ret;
3254
3255 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003256 setup_items_for_insert(trans, root, path, new_key, &item_size,
3257 item_size, item_size +
3258 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003259 leaf = path->nodes[0];
3260 memcpy_extent_buffer(leaf,
3261 btrfs_item_ptr_offset(leaf, path->slots[0]),
3262 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3263 item_size);
3264 return 0;
3265}
3266
3267/*
Chris Masond352ac62008-09-29 15:18:18 -04003268 * make the item pointed to by the path smaller. new_size indicates
3269 * how small to make it, and from_end tells us if we just chop bytes
3270 * off the end of the item or if we shift the item to chop bytes off
3271 * the front.
3272 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003273void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root,
3275 struct btrfs_path *path,
3276 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003277{
Chris Masonb18c6682007-04-17 13:26:50 -04003278 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003279 struct extent_buffer *leaf;
3280 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003281 u32 nritems;
3282 unsigned int data_end;
3283 unsigned int old_data_start;
3284 unsigned int old_size;
3285 unsigned int size_diff;
3286 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003287 struct btrfs_map_token token;
3288
3289 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003290
Chris Mason5f39d392007-10-15 16:14:19 -04003291 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003292 slot = path->slots[0];
3293
3294 old_size = btrfs_item_size_nr(leaf, slot);
3295 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003296 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003297
Chris Mason5f39d392007-10-15 16:14:19 -04003298 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003299 data_end = leaf_data_end(root, leaf);
3300
Chris Mason5f39d392007-10-15 16:14:19 -04003301 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003302
Chris Masonb18c6682007-04-17 13:26:50 -04003303 size_diff = old_size - new_size;
3304
3305 BUG_ON(slot < 0);
3306 BUG_ON(slot >= nritems);
3307
3308 /*
3309 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3310 */
3311 /* first correct the data pointers */
3312 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003313 u32 ioff;
3314 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003315
Chris Masoncfed81a2012-03-03 07:40:03 -05003316 ioff = btrfs_token_item_offset(leaf, item, &token);
3317 btrfs_set_token_item_offset(leaf, item,
3318 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003319 }
Chris Masondb945352007-10-15 16:15:53 -04003320
Chris Masonb18c6682007-04-17 13:26:50 -04003321 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003322 if (from_end) {
3323 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3324 data_end + size_diff, btrfs_leaf_data(leaf) +
3325 data_end, old_data_start + new_size - data_end);
3326 } else {
3327 struct btrfs_disk_key disk_key;
3328 u64 offset;
3329
3330 btrfs_item_key(leaf, &disk_key, slot);
3331
3332 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3333 unsigned long ptr;
3334 struct btrfs_file_extent_item *fi;
3335
3336 fi = btrfs_item_ptr(leaf, slot,
3337 struct btrfs_file_extent_item);
3338 fi = (struct btrfs_file_extent_item *)(
3339 (unsigned long)fi - size_diff);
3340
3341 if (btrfs_file_extent_type(leaf, fi) ==
3342 BTRFS_FILE_EXTENT_INLINE) {
3343 ptr = btrfs_item_ptr_offset(leaf, slot);
3344 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003345 (unsigned long)fi,
3346 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003347 disk_bytenr));
3348 }
3349 }
3350
3351 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3352 data_end + size_diff, btrfs_leaf_data(leaf) +
3353 data_end, old_data_start - data_end);
3354
3355 offset = btrfs_disk_key_offset(&disk_key);
3356 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3357 btrfs_set_item_key(leaf, &disk_key, slot);
3358 if (slot == 0)
3359 fixup_low_keys(trans, root, path, &disk_key, 1);
3360 }
Chris Mason5f39d392007-10-15 16:14:19 -04003361
3362 item = btrfs_item_nr(leaf, slot);
3363 btrfs_set_item_size(leaf, item, new_size);
3364 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003365
Chris Mason5f39d392007-10-15 16:14:19 -04003366 if (btrfs_leaf_free_space(root, leaf) < 0) {
3367 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003368 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003369 }
Chris Masonb18c6682007-04-17 13:26:50 -04003370}
3371
Chris Masond352ac62008-09-29 15:18:18 -04003372/*
3373 * make the item pointed to by the path bigger, data_size is the new size.
3374 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003375void btrfs_extend_item(struct btrfs_trans_handle *trans,
3376 struct btrfs_root *root, struct btrfs_path *path,
3377 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003378{
Chris Mason6567e832007-04-16 09:22:45 -04003379 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003380 struct extent_buffer *leaf;
3381 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003382 u32 nritems;
3383 unsigned int data_end;
3384 unsigned int old_data;
3385 unsigned int old_size;
3386 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003387 struct btrfs_map_token token;
3388
3389 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003390
Chris Mason5f39d392007-10-15 16:14:19 -04003391 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003392
Chris Mason5f39d392007-10-15 16:14:19 -04003393 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003394 data_end = leaf_data_end(root, leaf);
3395
Chris Mason5f39d392007-10-15 16:14:19 -04003396 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3397 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003398 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003399 }
Chris Mason6567e832007-04-16 09:22:45 -04003400 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003401 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003402
3403 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003404 if (slot >= nritems) {
3405 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003406 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3407 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003408 BUG_ON(1);
3409 }
Chris Mason6567e832007-04-16 09:22:45 -04003410
3411 /*
3412 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3413 */
3414 /* first correct the data pointers */
3415 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003416 u32 ioff;
3417 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003418
Chris Masoncfed81a2012-03-03 07:40:03 -05003419 ioff = btrfs_token_item_offset(leaf, item, &token);
3420 btrfs_set_token_item_offset(leaf, item,
3421 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003422 }
Chris Mason5f39d392007-10-15 16:14:19 -04003423
Chris Mason6567e832007-04-16 09:22:45 -04003424 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003425 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003426 data_end - data_size, btrfs_leaf_data(leaf) +
3427 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Chris Mason6567e832007-04-16 09:22:45 -04003429 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003430 old_size = btrfs_item_size_nr(leaf, slot);
3431 item = btrfs_item_nr(leaf, slot);
3432 btrfs_set_item_size(leaf, item, old_size + data_size);
3433 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003434
Chris Mason5f39d392007-10-15 16:14:19 -04003435 if (btrfs_leaf_free_space(root, leaf) < 0) {
3436 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003437 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003438 }
Chris Mason6567e832007-04-16 09:22:45 -04003439}
3440
Chris Mason74123bd2007-02-02 11:05:29 -05003441/*
Chris Masond352ac62008-09-29 15:18:18 -04003442 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003443 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003444 * Returns the number of keys that were inserted.
3445 */
3446int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3447 struct btrfs_root *root,
3448 struct btrfs_path *path,
3449 struct btrfs_key *cpu_key, u32 *data_size,
3450 int nr)
3451{
3452 struct extent_buffer *leaf;
3453 struct btrfs_item *item;
3454 int ret = 0;
3455 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003456 int i;
3457 u32 nritems;
3458 u32 total_data = 0;
3459 u32 total_size = 0;
3460 unsigned int data_end;
3461 struct btrfs_disk_key disk_key;
3462 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003463 struct btrfs_map_token token;
3464
3465 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003466
Yan Zheng87b29b22008-12-17 10:21:48 -05003467 for (i = 0; i < nr; i++) {
3468 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3469 BTRFS_LEAF_DATA_SIZE(root)) {
3470 break;
3471 nr = i;
3472 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003473 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003474 total_size += data_size[i] + sizeof(struct btrfs_item);
3475 }
3476 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003477
Josef Bacikf3465ca2008-11-12 14:19:50 -05003478 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3479 if (ret == 0)
3480 return -EEXIST;
3481 if (ret < 0)
3482 goto out;
3483
Josef Bacikf3465ca2008-11-12 14:19:50 -05003484 leaf = path->nodes[0];
3485
3486 nritems = btrfs_header_nritems(leaf);
3487 data_end = leaf_data_end(root, leaf);
3488
3489 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3490 for (i = nr; i >= 0; i--) {
3491 total_data -= data_size[i];
3492 total_size -= data_size[i] + sizeof(struct btrfs_item);
3493 if (total_size < btrfs_leaf_free_space(root, leaf))
3494 break;
3495 }
3496 nr = i;
3497 }
3498
3499 slot = path->slots[0];
3500 BUG_ON(slot < 0);
3501
3502 if (slot != nritems) {
3503 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3504
3505 item = btrfs_item_nr(leaf, slot);
3506 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3507
3508 /* figure out how many keys we can insert in here */
3509 total_data = data_size[0];
3510 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003511 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003512 break;
3513 total_data += data_size[i];
3514 }
3515 nr = i;
3516
3517 if (old_data < data_end) {
3518 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003519 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003520 slot, old_data, data_end);
3521 BUG_ON(1);
3522 }
3523 /*
3524 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3525 */
3526 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003527 for (i = slot; i < nritems; i++) {
3528 u32 ioff;
3529
3530 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003531 ioff = btrfs_token_item_offset(leaf, item, &token);
3532 btrfs_set_token_item_offset(leaf, item,
3533 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003534 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003535 /* shift the items */
3536 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3537 btrfs_item_nr_offset(slot),
3538 (nritems - slot) * sizeof(struct btrfs_item));
3539
3540 /* shift the data */
3541 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3542 data_end - total_data, btrfs_leaf_data(leaf) +
3543 data_end, old_data - data_end);
3544 data_end = old_data;
3545 } else {
3546 /*
3547 * this sucks but it has to be done, if we are inserting at
3548 * the end of the leaf only insert 1 of the items, since we
3549 * have no way of knowing whats on the next leaf and we'd have
3550 * to drop our current locks to figure it out
3551 */
3552 nr = 1;
3553 }
3554
3555 /* setup the item for the new data */
3556 for (i = 0; i < nr; i++) {
3557 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3558 btrfs_set_item_key(leaf, &disk_key, slot + i);
3559 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003560 btrfs_set_token_item_offset(leaf, item,
3561 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003562 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003563 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003564 }
3565 btrfs_set_header_nritems(leaf, nritems + nr);
3566 btrfs_mark_buffer_dirty(leaf);
3567
3568 ret = 0;
3569 if (slot == 0) {
3570 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003571 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003572 }
3573
3574 if (btrfs_leaf_free_space(root, leaf) < 0) {
3575 btrfs_print_leaf(root, leaf);
3576 BUG();
3577 }
3578out:
3579 if (!ret)
3580 ret = nr;
3581 return ret;
3582}
3583
3584/*
Chris Mason44871b12009-03-13 10:04:31 -04003585 * this is a helper for btrfs_insert_empty_items, the main goal here is
3586 * to save stack depth by doing the bulk of the work in a function
3587 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003588 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003589void setup_items_for_insert(struct btrfs_trans_handle *trans,
3590 struct btrfs_root *root, struct btrfs_path *path,
3591 struct btrfs_key *cpu_key, u32 *data_size,
3592 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593{
Chris Mason5f39d392007-10-15 16:14:19 -04003594 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003595 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003596 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003598 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003599 struct extent_buffer *leaf;
3600 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05003601 struct btrfs_map_token token;
3602
3603 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003606 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003607
Chris Mason5f39d392007-10-15 16:14:19 -04003608 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003609 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003610
Chris Masonf25956c2008-09-12 15:32:53 -04003611 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003612 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003613 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003614 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003615 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003616 }
Chris Mason5f39d392007-10-15 16:14:19 -04003617
Chris Masonbe0e5c02007-01-26 15:51:26 -05003618 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003619 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003620
Chris Mason5f39d392007-10-15 16:14:19 -04003621 if (old_data < data_end) {
3622 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003623 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003624 slot, old_data, data_end);
3625 BUG_ON(1);
3626 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003627 /*
3628 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3629 */
3630 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003631 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003632 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003633
Chris Mason5f39d392007-10-15 16:14:19 -04003634 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003635 ioff = btrfs_token_item_offset(leaf, item, &token);
3636 btrfs_set_token_item_offset(leaf, item,
3637 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003638 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003639 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003640 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003641 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003642 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003643
3644 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003645 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003646 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003647 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003648 data_end = old_data;
3649 }
Chris Mason5f39d392007-10-15 16:14:19 -04003650
Chris Mason62e27492007-03-15 12:56:47 -04003651 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003652 for (i = 0; i < nr; i++) {
3653 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3654 btrfs_set_item_key(leaf, &disk_key, slot + i);
3655 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003656 btrfs_set_token_item_offset(leaf, item,
3657 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003658 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003659 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003660 }
Chris Mason44871b12009-03-13 10:04:31 -04003661
Chris Mason9c583092008-01-29 15:15:18 -05003662 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003663
Chris Mason5a01a2e2008-01-30 11:43:54 -05003664 if (slot == 0) {
3665 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003666 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003667 }
Chris Masonb9473432009-03-13 11:00:37 -04003668 btrfs_unlock_up_safe(path, 1);
3669 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003670
Chris Mason5f39d392007-10-15 16:14:19 -04003671 if (btrfs_leaf_free_space(root, leaf) < 0) {
3672 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003673 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003674 }
Chris Mason44871b12009-03-13 10:04:31 -04003675}
3676
3677/*
3678 * Given a key and some data, insert items into the tree.
3679 * This does all the path init required, making room in the tree if needed.
3680 */
3681int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3682 struct btrfs_root *root,
3683 struct btrfs_path *path,
3684 struct btrfs_key *cpu_key, u32 *data_size,
3685 int nr)
3686{
Chris Mason44871b12009-03-13 10:04:31 -04003687 int ret = 0;
3688 int slot;
3689 int i;
3690 u32 total_size = 0;
3691 u32 total_data = 0;
3692
3693 for (i = 0; i < nr; i++)
3694 total_data += data_size[i];
3695
3696 total_size = total_data + (nr * sizeof(struct btrfs_item));
3697 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3698 if (ret == 0)
3699 return -EEXIST;
3700 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003701 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003702
Chris Mason44871b12009-03-13 10:04:31 -04003703 slot = path->slots[0];
3704 BUG_ON(slot < 0);
3705
Jeff Mahoney143bede2012-03-01 14:56:26 +01003706 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003707 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003708 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003709}
3710
3711/*
3712 * Given a key and some data, insert an item into the tree.
3713 * This does all the path init required, making room in the tree if needed.
3714 */
Chris Masone089f052007-03-16 16:20:31 -04003715int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3716 *root, struct btrfs_key *cpu_key, void *data, u32
3717 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003718{
3719 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003720 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003721 struct extent_buffer *leaf;
3722 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003723
Chris Mason2c90e5d2007-04-02 10:50:19 -04003724 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003725 if (!path)
3726 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003727 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003728 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003729 leaf = path->nodes[0];
3730 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3731 write_extent_buffer(leaf, data, ptr, data_size);
3732 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003733 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003734 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003735 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003736}
3737
Chris Mason74123bd2007-02-02 11:05:29 -05003738/*
Chris Mason5de08d72007-02-24 06:24:44 -05003739 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003740 *
Chris Masond352ac62008-09-29 15:18:18 -04003741 * the tree should have been previously balanced so the deletion does not
3742 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003743 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003744static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3745 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003746{
Chris Mason5f39d392007-10-15 16:14:19 -04003747 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003748 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003749
Chris Mason5f39d392007-10-15 16:14:19 -04003750 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003751 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003752 memmove_extent_buffer(parent,
3753 btrfs_node_key_ptr_offset(slot),
3754 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003755 sizeof(struct btrfs_key_ptr) *
3756 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003757 }
Chris Mason7518a232007-03-12 12:01:18 -04003758 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003759 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003760 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003761 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003762 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003763 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003764 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003765 struct btrfs_disk_key disk_key;
3766
3767 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003768 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003769 }
Chris Masond6025572007-03-30 14:27:56 -04003770 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003771}
3772
Chris Mason74123bd2007-02-02 11:05:29 -05003773/*
Chris Mason323ac952008-10-01 19:05:46 -04003774 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003775 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003776 *
3777 * This deletes the pointer in path->nodes[1] and frees the leaf
3778 * block extent. zero is returned if it all worked out, < 0 otherwise.
3779 *
3780 * The path must have already been setup for deleting the leaf, including
3781 * all the proper balancing. path->nodes[1] must be locked.
3782 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003783static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3784 struct btrfs_root *root,
3785 struct btrfs_path *path,
3786 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003787{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003788 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003789 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003790
Chris Mason4d081c42009-02-04 09:31:28 -05003791 /*
3792 * btrfs_free_extent is expensive, we want to make sure we
3793 * aren't holding any locks when we call it
3794 */
3795 btrfs_unlock_up_safe(path, 0);
3796
Yan, Zhengf0486c62010-05-16 10:46:25 -04003797 root_sub_used(root, leaf->len);
3798
Josef Bacik3083ee22012-03-09 16:01:49 -05003799 extent_buffer_get(leaf);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003800 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05003801 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003802}
3803/*
Chris Mason74123bd2007-02-02 11:05:29 -05003804 * delete the item at the leaf level in path. If that empties
3805 * the leaf, remove it from the tree
3806 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003807int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3808 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003809{
Chris Mason5f39d392007-10-15 16:14:19 -04003810 struct extent_buffer *leaf;
3811 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003812 int last_off;
3813 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003814 int ret = 0;
3815 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003816 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003817 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05003818 struct btrfs_map_token token;
3819
3820 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003821
Chris Mason5f39d392007-10-15 16:14:19 -04003822 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003823 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3824
3825 for (i = 0; i < nr; i++)
3826 dsize += btrfs_item_size_nr(leaf, slot + i);
3827
Chris Mason5f39d392007-10-15 16:14:19 -04003828 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003829
Chris Mason85e21ba2008-01-29 15:11:36 -05003830 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003831 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003832
3833 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003834 data_end + dsize,
3835 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003836 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003837
Chris Mason85e21ba2008-01-29 15:11:36 -05003838 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003839 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003840
Chris Mason5f39d392007-10-15 16:14:19 -04003841 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003842 ioff = btrfs_token_item_offset(leaf, item, &token);
3843 btrfs_set_token_item_offset(leaf, item,
3844 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003845 }
Chris Masondb945352007-10-15 16:15:53 -04003846
Chris Mason5f39d392007-10-15 16:14:19 -04003847 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003848 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003849 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003850 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003852 btrfs_set_header_nritems(leaf, nritems - nr);
3853 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Chris Mason74123bd2007-02-02 11:05:29 -05003855 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003856 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003857 if (leaf == root->node) {
3858 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003859 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003860 btrfs_set_path_blocking(path);
3861 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003862 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05003863 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003864 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003865 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003866 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003867 struct btrfs_disk_key disk_key;
3868
3869 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003870 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003871 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003872
Chris Mason74123bd2007-02-02 11:05:29 -05003873 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003874 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003875 /* push_leaf_left fixes the path.
3876 * make sure the path still points to our leaf
3877 * for possible call to del_ptr below
3878 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003879 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003880 extent_buffer_get(leaf);
3881
Chris Masonb9473432009-03-13 11:00:37 -04003882 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003883 wret = push_leaf_left(trans, root, path, 1, 1,
3884 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003885 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003886 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003887
3888 if (path->nodes[0] == leaf &&
3889 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003890 wret = push_leaf_right(trans, root, path, 1,
3891 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003892 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003893 ret = wret;
3894 }
Chris Mason5f39d392007-10-15 16:14:19 -04003895
3896 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003897 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003898 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003899 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003900 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05003901 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003902 /* if we're still in the path, make sure
3903 * we're dirty. Otherwise, one of the
3904 * push_leaf functions must have already
3905 * dirtied this buffer
3906 */
3907 if (path->nodes[0] == leaf)
3908 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003909 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003910 }
Chris Masond5719762007-03-23 10:01:08 -04003911 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003912 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003913 }
3914 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003915 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003916}
3917
Chris Mason97571fd2007-02-24 13:39:08 -05003918/*
Chris Mason925baed2008-06-25 16:01:30 -04003919 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003920 * returns 0 if it found something or 1 if there are no lesser leaves.
3921 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003922 *
3923 * This may release the path, and so you may lose any locks held at the
3924 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003925 */
3926int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3927{
Chris Mason925baed2008-06-25 16:01:30 -04003928 struct btrfs_key key;
3929 struct btrfs_disk_key found_key;
3930 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003931
Chris Mason925baed2008-06-25 16:01:30 -04003932 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003933
Chris Mason925baed2008-06-25 16:01:30 -04003934 if (key.offset > 0)
3935 key.offset--;
3936 else if (key.type > 0)
3937 key.type--;
3938 else if (key.objectid > 0)
3939 key.objectid--;
3940 else
3941 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003942
David Sterbab3b4aa72011-04-21 01:20:15 +02003943 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003944 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3945 if (ret < 0)
3946 return ret;
3947 btrfs_item_key(path->nodes[0], &found_key, 0);
3948 ret = comp_keys(&found_key, &key);
3949 if (ret < 0)
3950 return 0;
3951 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003952}
3953
Chris Mason3f157a22008-06-25 16:01:31 -04003954/*
3955 * A helper function to walk down the tree starting at min_key, and looking
3956 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003957 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003958 *
3959 * This does not cow, but it does stuff the starting key it finds back
3960 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3961 * key and get a writable path.
3962 *
3963 * This does lock as it descends, and path->keep_locks should be set
3964 * to 1 by the caller.
3965 *
3966 * This honors path->lowest_level to prevent descent past a given level
3967 * of the tree.
3968 *
Chris Masond352ac62008-09-29 15:18:18 -04003969 * min_trans indicates the oldest transaction that you are interested
3970 * in walking through. Any nodes or leaves older than min_trans are
3971 * skipped over (without reading them).
3972 *
Chris Mason3f157a22008-06-25 16:01:31 -04003973 * returns zero if something useful was found, < 0 on error and 1 if there
3974 * was nothing in the tree that matched the search criteria.
3975 */
3976int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003977 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003978 struct btrfs_path *path, int cache_only,
3979 u64 min_trans)
3980{
3981 struct extent_buffer *cur;
3982 struct btrfs_key found_key;
3983 int slot;
Yan96524802008-07-24 12:19:49 -04003984 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003985 u32 nritems;
3986 int level;
3987 int ret = 1;
3988
Chris Mason934d3752008-12-08 16:43:10 -05003989 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003990again:
Chris Masonbd681512011-07-16 15:23:14 -04003991 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04003992 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003993 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003994 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04003995 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04003996
3997 if (btrfs_header_generation(cur) < min_trans) {
3998 ret = 1;
3999 goto out;
4000 }
Chris Masond3977122009-01-05 21:25:51 -05004001 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004002 nritems = btrfs_header_nritems(cur);
4003 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004004 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004005
Chris Mason323ac952008-10-01 19:05:46 -04004006 /* at the lowest level, we're done, setup the path and exit */
4007 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004008 if (slot >= nritems)
4009 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004010 ret = 0;
4011 path->slots[level] = slot;
4012 btrfs_item_key_to_cpu(cur, &found_key, slot);
4013 goto out;
4014 }
Yan96524802008-07-24 12:19:49 -04004015 if (sret && slot > 0)
4016 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004017 /*
4018 * check this node pointer against the cache_only and
4019 * min_trans parameters. If it isn't in cache or is too
4020 * old, skip to the next one.
4021 */
Chris Masond3977122009-01-05 21:25:51 -05004022 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004023 u64 blockptr;
4024 u64 gen;
4025 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004026 struct btrfs_disk_key disk_key;
4027
Chris Mason3f157a22008-06-25 16:01:31 -04004028 blockptr = btrfs_node_blockptr(cur, slot);
4029 gen = btrfs_node_ptr_generation(cur, slot);
4030 if (gen < min_trans) {
4031 slot++;
4032 continue;
4033 }
4034 if (!cache_only)
4035 break;
4036
Chris Masone02119d2008-09-05 16:13:11 -04004037 if (max_key) {
4038 btrfs_node_key(cur, &disk_key, slot);
4039 if (comp_keys(&disk_key, max_key) >= 0) {
4040 ret = 1;
4041 goto out;
4042 }
4043 }
4044
Chris Mason3f157a22008-06-25 16:01:31 -04004045 tmp = btrfs_find_tree_block(root, blockptr,
4046 btrfs_level_size(root, level - 1));
4047
4048 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4049 free_extent_buffer(tmp);
4050 break;
4051 }
4052 if (tmp)
4053 free_extent_buffer(tmp);
4054 slot++;
4055 }
Chris Masone02119d2008-09-05 16:13:11 -04004056find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004057 /*
4058 * we didn't find a candidate key in this node, walk forward
4059 * and find another one
4060 */
4061 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004062 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004063 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004064 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004065 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004066 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004067 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004068 goto again;
4069 } else {
4070 goto out;
4071 }
4072 }
4073 /* save our key for returning back */
4074 btrfs_node_key_to_cpu(cur, &found_key, slot);
4075 path->slots[level] = slot;
4076 if (level == path->lowest_level) {
4077 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004078 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004079 goto out;
4080 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004081 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004082 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004083 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004084
Chris Masonbd681512011-07-16 15:23:14 -04004085 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004086
Chris Masonbd681512011-07-16 15:23:14 -04004087 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004088 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004089 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004090 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004091 }
4092out:
4093 if (ret == 0)
4094 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004095 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004096 return ret;
4097}
4098
4099/*
4100 * this is similar to btrfs_next_leaf, but does not try to preserve
4101 * and fixup the path. It looks for and returns the next key in the
4102 * tree based on the current path and the cache_only and min_trans
4103 * parameters.
4104 *
4105 * 0 is returned if another key is found, < 0 if there are any errors
4106 * and 1 is returned if there are no higher keys in the tree
4107 *
4108 * path->keep_locks should be set to 1 on the search made before
4109 * calling this function.
4110 */
Chris Masone7a84562008-06-25 16:01:31 -04004111int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004112 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004113 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004114{
Chris Masone7a84562008-06-25 16:01:31 -04004115 int slot;
4116 struct extent_buffer *c;
4117
Chris Mason934d3752008-12-08 16:43:10 -05004118 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004119 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004120 if (!path->nodes[level])
4121 return 1;
4122
4123 slot = path->slots[level] + 1;
4124 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004125next:
Chris Masone7a84562008-06-25 16:01:31 -04004126 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004127 int ret;
4128 int orig_lowest;
4129 struct btrfs_key cur_key;
4130 if (level + 1 >= BTRFS_MAX_LEVEL ||
4131 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004132 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004133
4134 if (path->locks[level + 1]) {
4135 level++;
4136 continue;
4137 }
4138
4139 slot = btrfs_header_nritems(c) - 1;
4140 if (level == 0)
4141 btrfs_item_key_to_cpu(c, &cur_key, slot);
4142 else
4143 btrfs_node_key_to_cpu(c, &cur_key, slot);
4144
4145 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004146 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004147 path->lowest_level = level;
4148 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4149 0, 0);
4150 path->lowest_level = orig_lowest;
4151 if (ret < 0)
4152 return ret;
4153
4154 c = path->nodes[level];
4155 slot = path->slots[level];
4156 if (ret == 0)
4157 slot++;
4158 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004159 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004160
Chris Masone7a84562008-06-25 16:01:31 -04004161 if (level == 0)
4162 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004163 else {
4164 u64 blockptr = btrfs_node_blockptr(c, slot);
4165 u64 gen = btrfs_node_ptr_generation(c, slot);
4166
4167 if (cache_only) {
4168 struct extent_buffer *cur;
4169 cur = btrfs_find_tree_block(root, blockptr,
4170 btrfs_level_size(root, level - 1));
4171 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4172 slot++;
4173 if (cur)
4174 free_extent_buffer(cur);
4175 goto next;
4176 }
4177 free_extent_buffer(cur);
4178 }
4179 if (gen < min_trans) {
4180 slot++;
4181 goto next;
4182 }
Chris Masone7a84562008-06-25 16:01:31 -04004183 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004184 }
Chris Masone7a84562008-06-25 16:01:31 -04004185 return 0;
4186 }
4187 return 1;
4188}
4189
Chris Mason7bb86312007-12-11 09:25:06 -05004190/*
Chris Mason925baed2008-06-25 16:01:30 -04004191 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004192 * returns 0 if it found something or 1 if there are no greater leaves.
4193 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004194 */
Chris Mason234b63a2007-03-13 10:46:10 -04004195int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004196{
4197 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004198 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004199 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004200 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004201 struct btrfs_key key;
4202 u32 nritems;
4203 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004204 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004205 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004206
4207 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004208 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004209 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004210
Chris Mason8e73f272009-04-03 10:14:18 -04004211 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4212again:
4213 level = 1;
4214 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004215 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004216 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004217
Chris Masona2135012008-06-25 16:01:30 -04004218 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004219 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004220
Chris Mason925baed2008-06-25 16:01:30 -04004221 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4222 path->keep_locks = 0;
4223
4224 if (ret < 0)
4225 return ret;
4226
Chris Masona2135012008-06-25 16:01:30 -04004227 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004228 /*
4229 * by releasing the path above we dropped all our locks. A balance
4230 * could have added more items next to the key that used to be
4231 * at the very end of the block. So, check again here and
4232 * advance the path if there are now more items available.
4233 */
Chris Masona2135012008-06-25 16:01:30 -04004234 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004235 if (ret == 0)
4236 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004237 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004238 goto done;
4239 }
Chris Masond97e63b2007-02-20 16:40:44 -05004240
Chris Masond3977122009-01-05 21:25:51 -05004241 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004242 if (!path->nodes[level]) {
4243 ret = 1;
4244 goto done;
4245 }
Chris Mason5f39d392007-10-15 16:14:19 -04004246
Chris Masond97e63b2007-02-20 16:40:44 -05004247 slot = path->slots[level] + 1;
4248 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004249 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004250 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004251 if (level == BTRFS_MAX_LEVEL) {
4252 ret = 1;
4253 goto done;
4254 }
Chris Masond97e63b2007-02-20 16:40:44 -05004255 continue;
4256 }
Chris Mason5f39d392007-10-15 16:14:19 -04004257
Chris Mason925baed2008-06-25 16:01:30 -04004258 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004259 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004260 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004261 }
Chris Mason5f39d392007-10-15 16:14:19 -04004262
Chris Mason8e73f272009-04-03 10:14:18 -04004263 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004264 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004265 ret = read_block_for_search(NULL, root, path, &next, level,
4266 slot, &key);
4267 if (ret == -EAGAIN)
4268 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004269
Chris Mason76a05b32009-05-14 13:24:30 -04004270 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004271 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004272 goto done;
4273 }
4274
Chris Mason5cd57b22008-06-25 16:01:30 -04004275 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004276 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004277 if (!ret) {
4278 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004279 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004280 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004281 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004282 }
Chris Mason31533fb2011-07-26 16:01:59 -04004283 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004284 }
Chris Masond97e63b2007-02-20 16:40:44 -05004285 break;
4286 }
4287 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004288 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004289 level--;
4290 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004291 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004292 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004293
Chris Mason5f39d392007-10-15 16:14:19 -04004294 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004295 path->nodes[level] = next;
4296 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004297 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004298 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004299 if (!level)
4300 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004301
Chris Mason8e73f272009-04-03 10:14:18 -04004302 ret = read_block_for_search(NULL, root, path, &next, level,
4303 0, &key);
4304 if (ret == -EAGAIN)
4305 goto again;
4306
Chris Mason76a05b32009-05-14 13:24:30 -04004307 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004308 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004309 goto done;
4310 }
4311
Chris Mason5cd57b22008-06-25 16:01:30 -04004312 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004313 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004314 if (!ret) {
4315 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004316 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004317 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004318 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004319 }
Chris Mason31533fb2011-07-26 16:01:59 -04004320 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004321 }
Chris Masond97e63b2007-02-20 16:40:44 -05004322 }
Chris Mason8e73f272009-04-03 10:14:18 -04004323 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004324done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004325 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004326 path->leave_spinning = old_spinning;
4327 if (!old_spinning)
4328 btrfs_set_path_blocking(path);
4329
4330 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004331}
Chris Mason0b86a832008-03-24 15:01:56 -04004332
Chris Mason3f157a22008-06-25 16:01:31 -04004333/*
4334 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4335 * searching until it gets past min_objectid or finds an item of 'type'
4336 *
4337 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4338 */
Chris Mason0b86a832008-03-24 15:01:56 -04004339int btrfs_previous_item(struct btrfs_root *root,
4340 struct btrfs_path *path, u64 min_objectid,
4341 int type)
4342{
4343 struct btrfs_key found_key;
4344 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004345 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004346 int ret;
4347
Chris Masond3977122009-01-05 21:25:51 -05004348 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004349 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004350 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004351 ret = btrfs_prev_leaf(root, path);
4352 if (ret != 0)
4353 return ret;
4354 } else {
4355 path->slots[0]--;
4356 }
4357 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004358 nritems = btrfs_header_nritems(leaf);
4359 if (nritems == 0)
4360 return 1;
4361 if (path->slots[0] == nritems)
4362 path->slots[0]--;
4363
Chris Mason0b86a832008-03-24 15:01:56 -04004364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004365 if (found_key.objectid < min_objectid)
4366 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004367 if (found_key.type == type)
4368 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004369 if (found_key.objectid == min_objectid &&
4370 found_key.type < type)
4371 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004372 }
4373 return 1;
4374}