blob: 8af24da983b1156e888ac0a95f58e7aa0a5bb9bb [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);
Chris Masone089f052007-03-16 16:20:31 -040039static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040057 if (!p->nodes[i] || !p->locks[i])
58 continue;
59 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60 if (p->locks[i] == BTRFS_READ_LOCK)
61 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62 else if (p->locks[i] == BTRFS_WRITE_LOCK)
63 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050064 }
65}
66
67/*
68 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050069 *
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
73 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 */
Chris Mason4008c042009-02-12 14:09:45 -050075noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040076 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050077{
78 int i;
Chris Mason4008c042009-02-12 14:09:45 -050079
80#ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
85 * the path blocking.
86 */
Chris Masonbd681512011-07-16 15:23:14 -040087 if (held) {
88 btrfs_set_lock_blocking_rw(held, held_rw);
89 if (held_rw == BTRFS_WRITE_LOCK)
90 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91 else if (held_rw == BTRFS_READ_LOCK)
92 held_rw = BTRFS_READ_LOCK_BLOCKING;
93 }
Chris Mason4008c042009-02-12 14:09:45 -050094 btrfs_set_path_blocking(p);
95#endif
96
97 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040098 if (p->nodes[i] && p->locks[i]) {
99 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101 p->locks[i] = BTRFS_WRITE_LOCK;
102 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103 p->locks[i] = BTRFS_READ_LOCK;
104 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500105 }
Chris Mason4008c042009-02-12 14:09:45 -0500106
107#ifdef CONFIG_DEBUG_LOCK_ALLOC
108 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400109 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500110#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400114void btrfs_free_path(struct btrfs_path *p)
115{
Jesper Juhlff175d52010-12-25 21:22:30 +0000116 if (!p)
117 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200118 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400119 kmem_cache_free(btrfs_path_cachep, p);
120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/*
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
125 *
126 * It is safe to call this on paths that no locks or extent buffers held.
127 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200128noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500129{
130 int i;
Chris Masona2135012008-06-25 16:01:30 -0400131
Chris Mason234b63a2007-03-13 10:46:10 -0400132 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400135 continue;
136 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400137 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400138 p->locks[i] = 0;
139 }
Chris Mason5f39d392007-10-15 16:14:19 -0400140 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400141 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500142 }
143}
144
Chris Masond352ac62008-09-29 15:18:18 -0400145/*
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
149 * looping required.
150 *
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400158
Josef Bacik3083ee22012-03-09 16:01:49 -0500159 while (1) {
160 rcu_read_lock();
161 eb = rcu_dereference(root->node);
162
163 /*
164 * RCU really hurts here, we could free up the root node because
165 * it was cow'ed but we may not get the new root node yet so do
166 * the inc_not_zero dance and if it doesn't work then
167 * synchronize_rcu and try again.
168 */
169 if (atomic_inc_not_zero(&eb->refs)) {
170 rcu_read_unlock();
171 break;
172 }
173 rcu_read_unlock();
174 synchronize_rcu();
175 }
Chris Mason925baed2008-06-25 16:01:30 -0400176 return eb;
177}
178
Chris Masond352ac62008-09-29 15:18:18 -0400179/* loop around taking references on and locking the root node of the
180 * tree until you end up with a lock on the root. A locked buffer
181 * is returned, with a reference held.
182 */
Chris Mason925baed2008-06-25 16:01:30 -0400183struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
184{
185 struct extent_buffer *eb;
186
Chris Masond3977122009-01-05 21:25:51 -0500187 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400188 eb = btrfs_root_node(root);
189 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400190 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400191 break;
Chris Mason925baed2008-06-25 16:01:30 -0400192 btrfs_tree_unlock(eb);
193 free_extent_buffer(eb);
194 }
195 return eb;
196}
197
Chris Masonbd681512011-07-16 15:23:14 -0400198/* loop around taking references on and locking the root node of the
199 * tree until you end up with a lock on the root. A locked buffer
200 * is returned, with a reference held.
201 */
202struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
203{
204 struct extent_buffer *eb;
205
206 while (1) {
207 eb = btrfs_root_node(root);
208 btrfs_tree_read_lock(eb);
209 if (eb == root->node)
210 break;
211 btrfs_tree_read_unlock(eb);
212 free_extent_buffer(eb);
213 }
214 return eb;
215}
216
Chris Masond352ac62008-09-29 15:18:18 -0400217/* cowonly root (everything not a reference counted cow subvolume), just get
218 * put onto a simple dirty list. transaction.c walks this to make sure they
219 * get properly updated on disk.
220 */
Chris Mason0b86a832008-03-24 15:01:56 -0400221static void add_root_to_dirty_list(struct btrfs_root *root)
222{
223 if (root->track_dirty && list_empty(&root->dirty_list)) {
224 list_add(&root->dirty_list,
225 &root->fs_info->dirty_cowonly_roots);
226 }
227}
228
Chris Masond352ac62008-09-29 15:18:18 -0400229/*
230 * used by snapshot creation to make a copy of a root for a tree with
231 * a given objectid. The buffer with the new root node is returned in
232 * cow_ret, and this func returns zero on success or a negative error code.
233 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500234int btrfs_copy_root(struct btrfs_trans_handle *trans,
235 struct btrfs_root *root,
236 struct extent_buffer *buf,
237 struct extent_buffer **cow_ret, u64 new_root_objectid)
238{
239 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 int ret = 0;
241 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400242 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243
244 WARN_ON(root->ref_cows && trans->transid !=
245 root->fs_info->running_transaction->transid);
246 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
247
248 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400249 if (level == 0)
250 btrfs_item_key(buf, &disk_key, 0);
251 else
252 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400253
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
255 new_root_objectid, &disk_key, level,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200256 buf->start, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400257 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 return PTR_ERR(cow);
259
260 copy_extent_buffer(cow, buf, 0, 0, cow->len);
261 btrfs_set_header_bytenr(cow, cow->start);
262 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
264 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
265 BTRFS_HEADER_FLAG_RELOC);
266 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
267 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
268 else
269 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500270
Yan Zheng2b820322008-11-17 21:11:30 -0500271 write_extent_buffer(cow, root->fs_info->fsid,
272 (unsigned long)btrfs_header_fsid(cow),
273 BTRFS_FSID_SIZE);
274
Chris Masonbe20aa92007-12-17 20:14:01 -0500275 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400276 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200277 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400278 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200279 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 if (ret)
282 return ret;
283
284 btrfs_mark_buffer_dirty(cow);
285 *cow_ret = cow;
286 return 0;
287}
288
Chris Masond352ac62008-09-29 15:18:18 -0400289/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 * check if the tree block can be shared by multiple trees
291 */
292int btrfs_block_can_be_shared(struct btrfs_root *root,
293 struct extent_buffer *buf)
294{
295 /*
296 * Tree blocks not in refernece counted trees and tree roots
297 * are never shared. If a block was allocated after the last
298 * snapshot and the block was not allocated by tree relocation,
299 * we know the block is not shared.
300 */
301 if (root->ref_cows &&
302 buf != root->node && buf != root->commit_root &&
303 (btrfs_header_generation(buf) <=
304 btrfs_root_last_snapshot(&root->root_item) ||
305 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
306 return 1;
307#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
308 if (root->ref_cows &&
309 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
310 return 1;
311#endif
312 return 0;
313}
314
315static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
316 struct btrfs_root *root,
317 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400318 struct extent_buffer *cow,
319 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400320{
321 u64 refs;
322 u64 owner;
323 u64 flags;
324 u64 new_flags = 0;
325 int ret;
326
327 /*
328 * Backrefs update rules:
329 *
330 * Always use full backrefs for extent pointers in tree block
331 * allocated by tree relocation.
332 *
333 * If a shared tree block is no longer referenced by its owner
334 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
335 * use full backrefs for extent pointers in tree block.
336 *
337 * If a tree block is been relocating
338 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
339 * use full backrefs for extent pointers in tree block.
340 * The reason for this is some operations (such as drop tree)
341 * are only allowed for blocks use full backrefs.
342 */
343
344 if (btrfs_block_can_be_shared(root, buf)) {
345 ret = btrfs_lookup_extent_info(trans, root, buf->start,
346 buf->len, &refs, &flags);
347 BUG_ON(ret);
348 BUG_ON(refs == 0);
349 } else {
350 refs = 1;
351 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
352 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
353 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
354 else
355 flags = 0;
356 }
357
358 owner = btrfs_header_owner(buf);
359 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
360 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
361
362 if (refs > 1) {
363 if ((owner == root->root_key.objectid ||
364 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
365 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200366 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400367 BUG_ON(ret);
368
369 if (root->root_key.objectid ==
370 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200371 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400372 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200373 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400374 BUG_ON(ret);
375 }
376 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
377 } else {
378
379 if (root->root_key.objectid ==
380 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200381 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400382 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200383 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400384 BUG_ON(ret);
385 }
386 if (new_flags != 0) {
387 ret = btrfs_set_disk_extent_flags(trans, root,
388 buf->start,
389 buf->len,
390 new_flags, 0);
391 BUG_ON(ret);
392 }
393 } else {
394 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
395 if (root->root_key.objectid ==
396 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200397 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400398 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200399 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400 BUG_ON(ret);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200401 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400402 BUG_ON(ret);
403 }
404 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400405 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400406 }
407 return 0;
408}
409
410/*
Chris Masond3977122009-01-05 21:25:51 -0500411 * does the dirty work in cow of a single block. The parent block (if
412 * supplied) is updated to point to the new cow copy. The new buffer is marked
413 * dirty and returned locked. If you modify the block it needs to be marked
414 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400415 *
416 * search_start -- an allocation hint for the new block
417 *
Chris Masond3977122009-01-05 21:25:51 -0500418 * empty_size -- a hint that you plan on doing more cow. This is the size in
419 * bytes the allocator should try to find free next to the block it returns.
420 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400421 */
Chris Masond3977122009-01-05 21:25:51 -0500422static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400423 struct btrfs_root *root,
424 struct extent_buffer *buf,
425 struct extent_buffer *parent, int parent_slot,
426 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400427 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400428{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400429 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400430 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500431 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400432 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400433 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400434 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400435
Chris Mason925baed2008-06-25 16:01:30 -0400436 if (*cow_ret == buf)
437 unlock_orig = 1;
438
Chris Masonb9447ef82009-03-09 11:45:38 -0400439 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400440
Chris Mason7bb86312007-12-11 09:25:06 -0500441 WARN_ON(root->ref_cows && trans->transid !=
442 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400443 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400444
Chris Mason7bb86312007-12-11 09:25:06 -0500445 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400446
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400447 if (level == 0)
448 btrfs_item_key(buf, &disk_key, 0);
449 else
450 btrfs_node_key(buf, &disk_key, 0);
451
452 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
453 if (parent)
454 parent_start = parent->start;
455 else
456 parent_start = 0;
457 } else
458 parent_start = 0;
459
460 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
461 root->root_key.objectid, &disk_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200462 level, search_start, empty_size, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400463 if (IS_ERR(cow))
464 return PTR_ERR(cow);
465
Chris Masonb4ce94d2009-02-04 09:25:08 -0500466 /* cow is set to blocking by btrfs_init_new_buffer */
467
Chris Mason5f39d392007-10-15 16:14:19 -0400468 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400469 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400470 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400471 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
472 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
473 BTRFS_HEADER_FLAG_RELOC);
474 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
475 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
476 else
477 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400478
Yan Zheng2b820322008-11-17 21:11:30 -0500479 write_extent_buffer(cow, root->fs_info->fsid,
480 (unsigned long)btrfs_header_fsid(cow),
481 BTRFS_FSID_SIZE);
482
Yan, Zhengf0486c62010-05-16 10:46:25 -0400483 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400484
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400485 if (root->ref_cows)
486 btrfs_reloc_cow_block(trans, root, buf, cow);
487
Chris Mason6702ed42007-08-07 16:15:09 -0400488 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400489 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400490 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
491 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
492 parent_start = buf->start;
493 else
494 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400495
Chris Mason5f39d392007-10-15 16:14:19 -0400496 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400497 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400498
Yan, Zhengf0486c62010-05-16 10:46:25 -0400499 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200500 last_ref, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400501 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400502 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400503 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400504 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
505 parent_start = parent->start;
506 else
507 parent_start = 0;
508
509 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400510 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400511 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500512 btrfs_set_node_ptr_generation(parent, parent_slot,
513 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400514 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400515 btrfs_free_tree_block(trans, root, buf, parent_start,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200516 last_ref, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400517 }
Chris Mason925baed2008-06-25 16:01:30 -0400518 if (unlock_orig)
519 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500520 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400521 btrfs_mark_buffer_dirty(cow);
522 *cow_ret = cow;
523 return 0;
524}
525
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400526static inline int should_cow_block(struct btrfs_trans_handle *trans,
527 struct btrfs_root *root,
528 struct extent_buffer *buf)
529{
Liu Bof1ebcc72011-11-14 20:48:06 -0500530 /* ensure we can see the force_cow */
531 smp_rmb();
532
533 /*
534 * We do not need to cow a block if
535 * 1) this block is not created or changed in this transaction;
536 * 2) this block does not belong to TREE_RELOC tree;
537 * 3) the root is not forced COW.
538 *
539 * What is forced COW:
540 * when we create snapshot during commiting the transaction,
541 * after we've finished coping src root, we must COW the shared
542 * block to ensure the metadata consistency.
543 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400544 if (btrfs_header_generation(buf) == trans->transid &&
545 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
546 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500547 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
548 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549 return 0;
550 return 1;
551}
552
Chris Masond352ac62008-09-29 15:18:18 -0400553/*
554 * cows a single block, see __btrfs_cow_block for the real work.
555 * This version of it has extra checks so that a block isn't cow'd more than
556 * once per transaction, as long as it hasn't been written yet
557 */
Chris Masond3977122009-01-05 21:25:51 -0500558noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400559 struct btrfs_root *root, struct extent_buffer *buf,
560 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400561 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500562{
Chris Mason6702ed42007-08-07 16:15:09 -0400563 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400564 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500565
Chris Masonccd467d2007-06-28 15:57:36 -0400566 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500567 printk(KERN_CRIT "trans %llu running %llu\n",
568 (unsigned long long)trans->transid,
569 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400570 root->fs_info->running_transaction->transid);
571 WARN_ON(1);
572 }
573 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500574 printk(KERN_CRIT "trans %llu running %llu\n",
575 (unsigned long long)trans->transid,
576 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400577 WARN_ON(1);
578 }
Chris Masondc17ff82008-01-08 15:46:30 -0500579
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400580 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500581 *cow_ret = buf;
582 return 0;
583 }
Chris Masonc4876852009-02-04 09:24:25 -0500584
Chris Mason0b86a832008-03-24 15:01:56 -0400585 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500586
587 if (parent)
588 btrfs_set_lock_blocking(parent);
589 btrfs_set_lock_blocking(buf);
590
Chris Masonf510cfe2007-10-15 16:14:48 -0400591 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400592 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000593
594 trace_btrfs_cow_block(root, buf, *cow_ret);
595
Chris Masonf510cfe2007-10-15 16:14:48 -0400596 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400597}
598
Chris Masond352ac62008-09-29 15:18:18 -0400599/*
600 * helper function for defrag to decide if two blocks pointed to by a
601 * node are actually close by
602 */
Chris Mason6b800532007-10-15 16:17:34 -0400603static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400604{
Chris Mason6b800532007-10-15 16:17:34 -0400605 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400606 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400607 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400608 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500609 return 0;
610}
611
Chris Mason081e9572007-11-06 10:26:24 -0500612/*
613 * compare two keys in a memcmp fashion
614 */
615static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
616{
617 struct btrfs_key k1;
618
619 btrfs_disk_key_to_cpu(&k1, disk);
620
Diego Calleja20736ab2009-07-24 11:06:52 -0400621 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500622}
623
Josef Bacikf3465ca2008-11-12 14:19:50 -0500624/*
625 * same as comp_keys only with two btrfs_key's
626 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400627int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500628{
629 if (k1->objectid > k2->objectid)
630 return 1;
631 if (k1->objectid < k2->objectid)
632 return -1;
633 if (k1->type > k2->type)
634 return 1;
635 if (k1->type < k2->type)
636 return -1;
637 if (k1->offset > k2->offset)
638 return 1;
639 if (k1->offset < k2->offset)
640 return -1;
641 return 0;
642}
Chris Mason081e9572007-11-06 10:26:24 -0500643
Chris Masond352ac62008-09-29 15:18:18 -0400644/*
645 * this is used by the defrag code to go through all the
646 * leaves pointed to by a node and reallocate them so that
647 * disk order is close to key order
648 */
Chris Mason6702ed42007-08-07 16:15:09 -0400649int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400650 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400651 int start_slot, int cache_only, u64 *last_ret,
652 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400653{
Chris Mason6b800532007-10-15 16:17:34 -0400654 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400655 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400656 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400657 u64 search_start = *last_ret;
658 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400659 u64 other;
660 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400661 int end_slot;
662 int i;
663 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400664 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400665 int uptodate;
666 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500667 int progress_passed = 0;
668 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400669
Chris Mason5708b952007-10-25 15:43:18 -0400670 parent_level = btrfs_header_level(parent);
671 if (cache_only && parent_level != 1)
672 return 0;
673
Chris Masond3977122009-01-05 21:25:51 -0500674 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400675 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500676 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400677 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400678
Chris Mason6b800532007-10-15 16:17:34 -0400679 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400680 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400681 end_slot = parent_nritems;
682
683 if (parent_nritems == 1)
684 return 0;
685
Chris Masonb4ce94d2009-02-04 09:25:08 -0500686 btrfs_set_lock_blocking(parent);
687
Chris Mason6702ed42007-08-07 16:15:09 -0400688 for (i = start_slot; i < end_slot; i++) {
689 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400690
Chris Mason081e9572007-11-06 10:26:24 -0500691 btrfs_node_key(parent, &disk_key, i);
692 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
693 continue;
694
695 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400696 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400697 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400698 if (last_block == 0)
699 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400700
Chris Mason6702ed42007-08-07 16:15:09 -0400701 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400702 other = btrfs_node_blockptr(parent, i - 1);
703 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400704 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400705 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400706 other = btrfs_node_blockptr(parent, i + 1);
707 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400708 }
Chris Masone9d0b132007-08-10 14:06:19 -0400709 if (close) {
710 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400711 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400712 }
Chris Mason6702ed42007-08-07 16:15:09 -0400713
Chris Mason6b800532007-10-15 16:17:34 -0400714 cur = btrfs_find_tree_block(root, blocknr, blocksize);
715 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400716 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400717 else
718 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400719 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400720 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400721 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400722 continue;
723 }
Chris Mason6b800532007-10-15 16:17:34 -0400724 if (!cur) {
725 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400726 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000727 if (!cur)
728 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400729 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400730 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400731 }
Chris Mason6702ed42007-08-07 16:15:09 -0400732 }
Chris Masone9d0b132007-08-10 14:06:19 -0400733 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400734 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400735
Chris Masone7a84562008-06-25 16:01:31 -0400736 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500737 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400738 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400739 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400740 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400741 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400742 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400743 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400744 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400745 break;
Yan252c38f2007-08-29 09:11:44 -0400746 }
Chris Masone7a84562008-06-25 16:01:31 -0400747 search_start = cur->start;
748 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400749 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400750 btrfs_tree_unlock(cur);
751 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400752 }
753 return err;
754}
755
Chris Mason74123bd2007-02-02 11:05:29 -0500756/*
757 * The leaf data grows from end-to-front in the node.
758 * this returns the address of the start of the last item,
759 * which is the stop of the leaf data stack
760 */
Chris Mason123abc82007-03-14 14:14:43 -0400761static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400762 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500763{
Chris Mason5f39d392007-10-15 16:14:19 -0400764 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500765 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400766 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400767 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500768}
769
Chris Masonaa5d6be2007-02-28 16:35:06 -0500770
Chris Mason74123bd2007-02-02 11:05:29 -0500771/*
Chris Mason5f39d392007-10-15 16:14:19 -0400772 * search for key in the extent_buffer. The items start at offset p,
773 * and they are item_size apart. There are 'max' items in p.
774 *
Chris Mason74123bd2007-02-02 11:05:29 -0500775 * the slot in the array is returned via slot, and it points to
776 * the place where you would insert key if it is not found in
777 * the array.
778 *
779 * slot may point to max if the key is bigger than all of the keys
780 */
Chris Masone02119d2008-09-05 16:13:11 -0400781static noinline int generic_bin_search(struct extent_buffer *eb,
782 unsigned long p,
783 int item_size, struct btrfs_key *key,
784 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500785{
786 int low = 0;
787 int high = max;
788 int mid;
789 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400790 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400791 struct btrfs_disk_key unaligned;
792 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400793 char *kaddr = NULL;
794 unsigned long map_start = 0;
795 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400796 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500797
Chris Masond3977122009-01-05 21:25:51 -0500798 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500799 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400800 offset = p + mid * item_size;
801
Chris Masona6591712011-07-19 12:04:14 -0400802 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400803 (offset + sizeof(struct btrfs_disk_key)) >
804 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500805
806 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400807 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400808 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400809
Chris Mason479965d2007-10-15 16:14:27 -0400810 if (!err) {
811 tmp = (struct btrfs_disk_key *)(kaddr + offset -
812 map_start);
813 } else {
814 read_extent_buffer(eb, &unaligned,
815 offset, sizeof(unaligned));
816 tmp = &unaligned;
817 }
818
Chris Mason5f39d392007-10-15 16:14:19 -0400819 } else {
820 tmp = (struct btrfs_disk_key *)(kaddr + offset -
821 map_start);
822 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500823 ret = comp_keys(tmp, key);
824
825 if (ret < 0)
826 low = mid + 1;
827 else if (ret > 0)
828 high = mid;
829 else {
830 *slot = mid;
831 return 0;
832 }
833 }
834 *slot = low;
835 return 1;
836}
837
Chris Mason97571fd2007-02-24 13:39:08 -0500838/*
839 * simple bin_search frontend that does the right thing for
840 * leaves vs nodes
841 */
Chris Mason5f39d392007-10-15 16:14:19 -0400842static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
843 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500844{
Chris Mason5f39d392007-10-15 16:14:19 -0400845 if (level == 0) {
846 return generic_bin_search(eb,
847 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400848 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400849 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400850 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500851 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400852 return generic_bin_search(eb,
853 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400854 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400855 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400856 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500857 }
858 return -1;
859}
860
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
862 int level, int *slot)
863{
864 return bin_search(eb, key, level, slot);
865}
866
Yan, Zhengf0486c62010-05-16 10:46:25 -0400867static void root_add_used(struct btrfs_root *root, u32 size)
868{
869 spin_lock(&root->accounting_lock);
870 btrfs_set_root_used(&root->root_item,
871 btrfs_root_used(&root->root_item) + size);
872 spin_unlock(&root->accounting_lock);
873}
874
875static void root_sub_used(struct btrfs_root *root, u32 size)
876{
877 spin_lock(&root->accounting_lock);
878 btrfs_set_root_used(&root->root_item,
879 btrfs_root_used(&root->root_item) - size);
880 spin_unlock(&root->accounting_lock);
881}
882
Chris Masond352ac62008-09-29 15:18:18 -0400883/* given a node and slot number, this reads the blocks it points to. The
884 * extent buffer is returned with a reference taken (but unlocked).
885 * NULL is returned on error.
886 */
Chris Masone02119d2008-09-05 16:13:11 -0400887static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400888 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500889{
Chris Masonca7a79a2008-05-12 12:59:19 -0400890 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500891 if (slot < 0)
892 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400893 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500894 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400895
896 BUG_ON(level == 0);
897
Chris Masondb945352007-10-15 16:15:53 -0400898 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400899 btrfs_level_size(root, level - 1),
900 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500901}
902
Chris Masond352ac62008-09-29 15:18:18 -0400903/*
904 * node level balancing, used to make sure nodes are in proper order for
905 * item deletion. We balance from the top down, so we have to make sure
906 * that a deletion won't leave an node completely empty later on.
907 */
Chris Masone02119d2008-09-05 16:13:11 -0400908static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500909 struct btrfs_root *root,
910 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500911{
Chris Mason5f39d392007-10-15 16:14:19 -0400912 struct extent_buffer *right = NULL;
913 struct extent_buffer *mid;
914 struct extent_buffer *left = NULL;
915 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500916 int ret = 0;
917 int wret;
918 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500919 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500920 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500921
922 if (level == 0)
923 return 0;
924
Chris Mason5f39d392007-10-15 16:14:19 -0400925 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500926
Chris Masonbd681512011-07-16 15:23:14 -0400927 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
928 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -0500929 WARN_ON(btrfs_header_generation(mid) != trans->transid);
930
Chris Mason1d4f8a02007-03-13 09:28:32 -0400931 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500932
Li Zefana05a9bb2011-09-06 16:55:34 +0800933 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400934 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800935 pslot = path->slots[level + 1];
936 }
Chris Masonbb803952007-03-01 12:04:21 -0500937
Chris Mason40689472007-03-17 14:29:23 -0400938 /*
939 * deal with the case where there is only one pointer in the root
940 * by promoting the node below to a root
941 */
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (!parent) {
943 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500944
Chris Mason5f39d392007-10-15 16:14:19 -0400945 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500946 return 0;
947
948 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400949 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500950 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400951 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500952 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400953 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400954 if (ret) {
955 btrfs_tree_unlock(child);
956 free_extent_buffer(child);
957 goto enospc;
958 }
Yan2f375ab2008-02-01 14:58:07 -0500959
Chris Mason240f62c2011-03-23 14:54:42 -0400960 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400961
Chris Mason0b86a832008-03-24 15:01:56 -0400962 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400963 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500964
Chris Mason925baed2008-06-25 16:01:30 -0400965 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500966 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400967 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500969 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400970 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400971
972 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200973 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500974 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -0500975 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400976 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500977 }
Chris Mason5f39d392007-10-15 16:14:19 -0400978 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400979 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500980 return 0;
981
Andi Kleen559af822010-10-29 15:14:37 -0400982 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400983
Chris Mason5f39d392007-10-15 16:14:19 -0400984 left = read_node_slot(root, parent, pslot - 1);
985 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400986 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500987 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400988 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400989 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 if (wret) {
991 ret = wret;
992 goto enospc;
993 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400994 }
Chris Mason5f39d392007-10-15 16:14:19 -0400995 right = read_node_slot(root, parent, pslot + 1);
996 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400997 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500998 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400999 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001000 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001001 if (wret) {
1002 ret = wret;
1003 goto enospc;
1004 }
1005 }
1006
1007 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001008 if (left) {
1009 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001010 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001011 if (wret < 0)
1012 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001013 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001014 }
Chris Mason79f95c82007-03-01 15:16:26 -05001015
1016 /*
1017 * then try to empty the right most buffer into the middle
1018 */
Chris Mason5f39d392007-10-15 16:14:19 -04001019 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001020 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001021 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001022 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001023 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001024 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001025 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -04001026 wret = del_ptr(trans, root, path, level + 1, pslot +
1027 1);
Chris Masonbb803952007-03-01 12:04:21 -05001028 if (wret)
1029 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001030 root_sub_used(root, right->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001031 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001032 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001033 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001034 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001035 struct btrfs_disk_key right_key;
1036 btrfs_node_key(right, &right_key, 0);
1037 btrfs_set_node_key(parent, &right_key, pslot + 1);
1038 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001039 }
1040 }
Chris Mason5f39d392007-10-15 16:14:19 -04001041 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001042 /*
1043 * we're not allowed to leave a node with one item in the
1044 * tree during a delete. A deletion from lower in the tree
1045 * could try to delete the only pointer in this node.
1046 * So, pull some keys from the left.
1047 * There has to be a left pointer at this point because
1048 * otherwise we would have pulled some pointers from the
1049 * right
1050 */
Chris Mason5f39d392007-10-15 16:14:19 -04001051 BUG_ON(!left);
1052 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001053 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001054 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001055 goto enospc;
1056 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001057 if (wret == 1) {
1058 wret = push_node_left(trans, root, left, mid, 1);
1059 if (wret < 0)
1060 ret = wret;
1061 }
Chris Mason79f95c82007-03-01 15:16:26 -05001062 BUG_ON(wret == 1);
1063 }
Chris Mason5f39d392007-10-15 16:14:19 -04001064 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001065 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001066 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001067 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001068 if (wret)
1069 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001070 root_sub_used(root, mid->len);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001071 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05001072 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001073 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001074 } else {
1075 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001076 struct btrfs_disk_key mid_key;
1077 btrfs_node_key(mid, &mid_key, 0);
1078 btrfs_set_node_key(parent, &mid_key, pslot);
1079 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001080 }
Chris Masonbb803952007-03-01 12:04:21 -05001081
Chris Mason79f95c82007-03-01 15:16:26 -05001082 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001083 if (left) {
1084 if (btrfs_header_nritems(left) > orig_slot) {
1085 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001086 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001087 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001088 path->slots[level + 1] -= 1;
1089 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001090 if (mid) {
1091 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001092 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001093 }
Chris Masonbb803952007-03-01 12:04:21 -05001094 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001095 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001096 path->slots[level] = orig_slot;
1097 }
1098 }
Chris Mason79f95c82007-03-01 15:16:26 -05001099 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001100 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001101 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001102 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001103enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001104 if (right) {
1105 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001106 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001107 }
1108 if (left) {
1109 if (path->nodes[level] != left)
1110 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001111 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001112 }
Chris Masonbb803952007-03-01 12:04:21 -05001113 return ret;
1114}
1115
Chris Masond352ac62008-09-29 15:18:18 -04001116/* Node balancing for insertion. Here we only split or push nodes around
1117 * when they are completely full. This is also done top down, so we
1118 * have to be pessimistic.
1119 */
Chris Masond3977122009-01-05 21:25:51 -05001120static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001121 struct btrfs_root *root,
1122 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001123{
Chris Mason5f39d392007-10-15 16:14:19 -04001124 struct extent_buffer *right = NULL;
1125 struct extent_buffer *mid;
1126 struct extent_buffer *left = NULL;
1127 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001128 int ret = 0;
1129 int wret;
1130 int pslot;
1131 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001132
1133 if (level == 0)
1134 return 1;
1135
Chris Mason5f39d392007-10-15 16:14:19 -04001136 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001137 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001138
Li Zefana05a9bb2011-09-06 16:55:34 +08001139 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001140 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001141 pslot = path->slots[level + 1];
1142 }
Chris Masone66f7092007-04-20 13:16:02 -04001143
Chris Mason5f39d392007-10-15 16:14:19 -04001144 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001145 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001146
Chris Mason5f39d392007-10-15 16:14:19 -04001147 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001148
1149 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001150 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001151 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001152
1153 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001154 btrfs_set_lock_blocking(left);
1155
Chris Mason5f39d392007-10-15 16:14:19 -04001156 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001157 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1158 wret = 1;
1159 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001160 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001161 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001162 if (ret)
1163 wret = 1;
1164 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001165 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001166 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001167 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001168 }
Chris Masone66f7092007-04-20 13:16:02 -04001169 if (wret < 0)
1170 ret = wret;
1171 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001172 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001173 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001174 btrfs_node_key(mid, &disk_key, 0);
1175 btrfs_set_node_key(parent, &disk_key, pslot);
1176 btrfs_mark_buffer_dirty(parent);
1177 if (btrfs_header_nritems(left) > orig_slot) {
1178 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001179 path->slots[level + 1] -= 1;
1180 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001181 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001182 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001183 } else {
1184 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001185 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001186 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001187 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001188 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001189 }
Chris Masone66f7092007-04-20 13:16:02 -04001190 return 0;
1191 }
Chris Mason925baed2008-06-25 16:01:30 -04001192 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001193 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001194 }
Chris Mason925baed2008-06-25 16:01:30 -04001195 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001196
1197 /*
1198 * then try to empty the right most buffer into the middle
1199 */
Chris Mason5f39d392007-10-15 16:14:19 -04001200 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001201 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001202
Chris Mason925baed2008-06-25 16:01:30 -04001203 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001204 btrfs_set_lock_blocking(right);
1205
Chris Mason5f39d392007-10-15 16:14:19 -04001206 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001207 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1208 wret = 1;
1209 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001210 ret = btrfs_cow_block(trans, root, right,
1211 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001212 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001213 if (ret)
1214 wret = 1;
1215 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001216 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001217 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001218 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001219 }
Chris Masone66f7092007-04-20 13:16:02 -04001220 if (wret < 0)
1221 ret = wret;
1222 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001223 struct btrfs_disk_key disk_key;
1224
1225 btrfs_node_key(right, &disk_key, 0);
1226 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1227 btrfs_mark_buffer_dirty(parent);
1228
1229 if (btrfs_header_nritems(mid) <= orig_slot) {
1230 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001231 path->slots[level + 1] += 1;
1232 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001233 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001234 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001235 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001236 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001237 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001238 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001239 }
Chris Masone66f7092007-04-20 13:16:02 -04001240 return 0;
1241 }
Chris Mason925baed2008-06-25 16:01:30 -04001242 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001243 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001244 }
Chris Masone66f7092007-04-20 13:16:02 -04001245 return 1;
1246}
1247
Chris Mason74123bd2007-02-02 11:05:29 -05001248/*
Chris Masond352ac62008-09-29 15:18:18 -04001249 * readahead one full node of leaves, finding things that are close
1250 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001251 */
Chris Masonc8c42862009-04-03 10:14:18 -04001252static void reada_for_search(struct btrfs_root *root,
1253 struct btrfs_path *path,
1254 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001255{
Chris Mason5f39d392007-10-15 16:14:19 -04001256 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001257 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001258 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001259 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001260 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001261 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001262 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001263 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001264 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001265 u32 nr;
1266 u32 blocksize;
1267 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001268
Chris Masona6b6e752007-10-15 16:22:39 -04001269 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001270 return;
1271
Chris Mason6702ed42007-08-07 16:15:09 -04001272 if (!path->nodes[level])
1273 return;
1274
Chris Mason5f39d392007-10-15 16:14:19 -04001275 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001276
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001278 blocksize = btrfs_level_size(root, level - 1);
1279 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001280 if (eb) {
1281 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001282 return;
1283 }
1284
Chris Masona7175312009-01-22 09:23:10 -05001285 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001286
Chris Mason5f39d392007-10-15 16:14:19 -04001287 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001288 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001289
Chris Masond3977122009-01-05 21:25:51 -05001290 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001291 if (direction < 0) {
1292 if (nr == 0)
1293 break;
1294 nr--;
1295 } else if (direction > 0) {
1296 nr++;
1297 if (nr >= nritems)
1298 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001299 }
Chris Mason01f46652007-12-21 16:24:26 -05001300 if (path->reada < 0 && objectid) {
1301 btrfs_node_key(node, &disk_key, nr);
1302 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1303 break;
1304 }
Chris Mason6b800532007-10-15 16:17:34 -04001305 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001306 if ((search <= target && target - search <= 65536) ||
1307 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001308 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001309 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001310 nread += blocksize;
1311 }
1312 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001313 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001314 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001315 }
1316}
Chris Mason925baed2008-06-25 16:01:30 -04001317
Chris Masond352ac62008-09-29 15:18:18 -04001318/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001319 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1320 * cache
1321 */
1322static noinline int reada_for_balance(struct btrfs_root *root,
1323 struct btrfs_path *path, int level)
1324{
1325 int slot;
1326 int nritems;
1327 struct extent_buffer *parent;
1328 struct extent_buffer *eb;
1329 u64 gen;
1330 u64 block1 = 0;
1331 u64 block2 = 0;
1332 int ret = 0;
1333 int blocksize;
1334
Chris Mason8c594ea2009-04-20 15:50:10 -04001335 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001336 if (!parent)
1337 return 0;
1338
1339 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001340 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001341 blocksize = btrfs_level_size(root, level);
1342
1343 if (slot > 0) {
1344 block1 = btrfs_node_blockptr(parent, slot - 1);
1345 gen = btrfs_node_ptr_generation(parent, slot - 1);
1346 eb = btrfs_find_tree_block(root, block1, blocksize);
1347 if (eb && btrfs_buffer_uptodate(eb, gen))
1348 block1 = 0;
1349 free_extent_buffer(eb);
1350 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001351 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001352 block2 = btrfs_node_blockptr(parent, slot + 1);
1353 gen = btrfs_node_ptr_generation(parent, slot + 1);
1354 eb = btrfs_find_tree_block(root, block2, blocksize);
1355 if (eb && btrfs_buffer_uptodate(eb, gen))
1356 block2 = 0;
1357 free_extent_buffer(eb);
1358 }
1359 if (block1 || block2) {
1360 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001361
1362 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001363 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001364
1365 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001366 if (block1)
1367 readahead_tree_block(root, block1, blocksize, 0);
1368 if (block2)
1369 readahead_tree_block(root, block2, blocksize, 0);
1370
1371 if (block1) {
1372 eb = read_tree_block(root, block1, blocksize, 0);
1373 free_extent_buffer(eb);
1374 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001375 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001376 eb = read_tree_block(root, block2, blocksize, 0);
1377 free_extent_buffer(eb);
1378 }
1379 }
1380 return ret;
1381}
1382
1383
1384/*
Chris Masond3977122009-01-05 21:25:51 -05001385 * when we walk down the tree, it is usually safe to unlock the higher layers
1386 * in the tree. The exceptions are when our path goes through slot 0, because
1387 * operations on the tree might require changing key pointers higher up in the
1388 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001389 *
Chris Masond3977122009-01-05 21:25:51 -05001390 * callers might also have set path->keep_locks, which tells this code to keep
1391 * the lock if the path points to the last slot in the block. This is part of
1392 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001393 *
Chris Masond3977122009-01-05 21:25:51 -05001394 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1395 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001396 */
Chris Masone02119d2008-09-05 16:13:11 -04001397static noinline void unlock_up(struct btrfs_path *path, int level,
1398 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001399{
1400 int i;
1401 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001402 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001403 struct extent_buffer *t;
1404
1405 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1406 if (!path->nodes[i])
1407 break;
1408 if (!path->locks[i])
1409 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001410 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001411 skip_level = i + 1;
1412 continue;
1413 }
Chris Mason051e1b92008-06-25 16:01:30 -04001414 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001415 u32 nritems;
1416 t = path->nodes[i];
1417 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001418 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001419 skip_level = i + 1;
1420 continue;
1421 }
1422 }
Chris Mason051e1b92008-06-25 16:01:30 -04001423 if (skip_level < i && i >= lowest_unlock)
1424 no_skips = 1;
1425
Chris Mason925baed2008-06-25 16:01:30 -04001426 t = path->nodes[i];
1427 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001428 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001429 path->locks[i] = 0;
1430 }
1431 }
1432}
1433
Chris Mason3c69fae2007-08-07 15:52:22 -04001434/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435 * This releases any locks held in the path starting at level and
1436 * going all the way up to the root.
1437 *
1438 * btrfs_search_slot will keep the lock held on higher nodes in a few
1439 * corner cases, such as COW of the block at slot zero in the node. This
1440 * ignores those rules, and it should only be called when there are no
1441 * more updates to be done higher up in the tree.
1442 */
1443noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1444{
1445 int i;
1446
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001447 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001448 return;
1449
1450 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1451 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001452 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001453 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001454 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001455 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001456 path->locks[i] = 0;
1457 }
1458}
1459
1460/*
Chris Masonc8c42862009-04-03 10:14:18 -04001461 * helper function for btrfs_search_slot. The goal is to find a block
1462 * in cache without setting the path to blocking. If we find the block
1463 * we return zero and the path is unchanged.
1464 *
1465 * If we can't find the block, we set the path blocking and do some
1466 * reada. -EAGAIN is returned and the search must be repeated.
1467 */
1468static int
1469read_block_for_search(struct btrfs_trans_handle *trans,
1470 struct btrfs_root *root, struct btrfs_path *p,
1471 struct extent_buffer **eb_ret, int level, int slot,
1472 struct btrfs_key *key)
1473{
1474 u64 blocknr;
1475 u64 gen;
1476 u32 blocksize;
1477 struct extent_buffer *b = *eb_ret;
1478 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001479 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001480
1481 blocknr = btrfs_node_blockptr(b, slot);
1482 gen = btrfs_node_ptr_generation(b, slot);
1483 blocksize = btrfs_level_size(root, level - 1);
1484
1485 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001486 if (tmp) {
1487 if (btrfs_buffer_uptodate(tmp, 0)) {
1488 if (btrfs_buffer_uptodate(tmp, gen)) {
1489 /*
1490 * we found an up to date block without
1491 * sleeping, return
1492 * right away
1493 */
1494 *eb_ret = tmp;
1495 return 0;
1496 }
1497 /* the pages were up to date, but we failed
1498 * the generation number check. Do a full
1499 * read for the generation number that is correct.
1500 * We must do this without dropping locks so
1501 * we can trust our generation number
1502 */
1503 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001504 btrfs_set_path_blocking(p);
1505
Chris Masoncb449212010-10-24 11:01:27 -04001506 tmp = read_tree_block(root, blocknr, blocksize, gen);
1507 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1508 *eb_ret = tmp;
1509 return 0;
1510 }
1511 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001512 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001513 return -EIO;
1514 }
Chris Masonc8c42862009-04-03 10:14:18 -04001515 }
1516
1517 /*
1518 * reduce lock contention at high levels
1519 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001520 * we read. Don't release the lock on the current
1521 * level because we need to walk this node to figure
1522 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001523 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001524 btrfs_unlock_up_safe(p, level + 1);
1525 btrfs_set_path_blocking(p);
1526
Chris Masoncb449212010-10-24 11:01:27 -04001527 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001528 if (p->reada)
1529 reada_for_search(root, p, level, slot, key->objectid);
1530
David Sterbab3b4aa72011-04-21 01:20:15 +02001531 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001532
1533 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001534 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001535 if (tmp) {
1536 /*
1537 * If the read above didn't mark this buffer up to date,
1538 * it will never end up being up to date. Set ret to EIO now
1539 * and give up so that our caller doesn't loop forever
1540 * on our EAGAINs.
1541 */
1542 if (!btrfs_buffer_uptodate(tmp, 0))
1543 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001544 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001545 }
1546 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001547}
1548
1549/*
1550 * helper function for btrfs_search_slot. This does all of the checks
1551 * for node-level blocks and does any balancing required based on
1552 * the ins_len.
1553 *
1554 * If no extra work was required, zero is returned. If we had to
1555 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1556 * start over
1557 */
1558static int
1559setup_nodes_for_search(struct btrfs_trans_handle *trans,
1560 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001561 struct extent_buffer *b, int level, int ins_len,
1562 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001563{
1564 int ret;
1565 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1566 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1567 int sret;
1568
Chris Masonbd681512011-07-16 15:23:14 -04001569 if (*write_lock_level < level + 1) {
1570 *write_lock_level = level + 1;
1571 btrfs_release_path(p);
1572 goto again;
1573 }
1574
Chris Masonc8c42862009-04-03 10:14:18 -04001575 sret = reada_for_balance(root, p, level);
1576 if (sret)
1577 goto again;
1578
1579 btrfs_set_path_blocking(p);
1580 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001581 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001582
1583 BUG_ON(sret > 0);
1584 if (sret) {
1585 ret = sret;
1586 goto done;
1587 }
1588 b = p->nodes[level];
1589 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001590 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001591 int sret;
1592
Chris Masonbd681512011-07-16 15:23:14 -04001593 if (*write_lock_level < level + 1) {
1594 *write_lock_level = level + 1;
1595 btrfs_release_path(p);
1596 goto again;
1597 }
1598
Chris Masonc8c42862009-04-03 10:14:18 -04001599 sret = reada_for_balance(root, p, level);
1600 if (sret)
1601 goto again;
1602
1603 btrfs_set_path_blocking(p);
1604 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04001605 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04001606
1607 if (sret) {
1608 ret = sret;
1609 goto done;
1610 }
1611 b = p->nodes[level];
1612 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001613 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001614 goto again;
1615 }
1616 BUG_ON(btrfs_header_nritems(b) == 1);
1617 }
1618 return 0;
1619
1620again:
1621 ret = -EAGAIN;
1622done:
1623 return ret;
1624}
1625
1626/*
Chris Mason74123bd2007-02-02 11:05:29 -05001627 * look for key in the tree. path is filled in with nodes along the way
1628 * if key is found, we return zero and you can find the item in the leaf
1629 * level of the path (level 0)
1630 *
1631 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001632 * be inserted, and 1 is returned. If there are other errors during the
1633 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001634 *
1635 * if ins_len > 0, nodes and leaves will be split as we walk down the
1636 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1637 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001638 */
Chris Masone089f052007-03-16 16:20:31 -04001639int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1640 *root, struct btrfs_key *key, struct btrfs_path *p, int
1641 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001642{
Chris Mason5f39d392007-10-15 16:14:19 -04001643 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001644 int slot;
1645 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001646 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001647 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001648 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001649 int root_lock;
1650 /* everything at write_lock_level or lower must be write locked */
1651 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001652 u8 lowest_level = 0;
1653
Chris Mason6702ed42007-08-07 16:15:09 -04001654 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001655 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001656 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001657
Chris Masonbd681512011-07-16 15:23:14 -04001658 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001659 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001660
Chris Masonbd681512011-07-16 15:23:14 -04001661 /* when we are removing items, we might have to go up to level
1662 * two as we update tree pointers Make sure we keep write
1663 * for those levels as well
1664 */
1665 write_lock_level = 2;
1666 } else if (ins_len > 0) {
1667 /*
1668 * for inserting items, make sure we have a write lock on
1669 * level 1 so we can update keys
1670 */
1671 write_lock_level = 1;
1672 }
1673
1674 if (!cow)
1675 write_lock_level = -1;
1676
1677 if (cow && (p->keep_locks || p->lowest_level))
1678 write_lock_level = BTRFS_MAX_LEVEL;
1679
Chris Masonbb803952007-03-01 12:04:21 -05001680again:
Chris Masonbd681512011-07-16 15:23:14 -04001681 /*
1682 * we try very hard to do read locks on the root
1683 */
1684 root_lock = BTRFS_READ_LOCK;
1685 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001686 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04001687 /*
1688 * the commit roots are read only
1689 * so we always do read locks
1690 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001691 b = root->commit_root;
1692 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04001693 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001694 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04001695 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001696 } else {
Chris Masonbd681512011-07-16 15:23:14 -04001697 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04001699 level = btrfs_header_level(b);
1700 } else {
1701 /* we don't know the level of the root node
1702 * until we actually have it read locked
1703 */
1704 b = btrfs_read_lock_root_node(root);
1705 level = btrfs_header_level(b);
1706 if (level <= write_lock_level) {
1707 /* whoops, must trade for write lock */
1708 btrfs_tree_read_unlock(b);
1709 free_extent_buffer(b);
1710 b = btrfs_lock_root_node(root);
1711 root_lock = BTRFS_WRITE_LOCK;
1712
1713 /* the level might have changed, check again */
1714 level = btrfs_header_level(b);
1715 }
1716 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717 }
Chris Masonbd681512011-07-16 15:23:14 -04001718 p->nodes[level] = b;
1719 if (!p->skip_locking)
1720 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04001721
Chris Masoneb60cea2007-02-02 09:18:22 -05001722 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001723 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001724
1725 /*
1726 * setup the path here so we can release it under lock
1727 * contention with the cow code
1728 */
Chris Mason02217ed2007-03-02 16:08:05 -05001729 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001730 /*
1731 * if we don't really need to cow this block
1732 * then we don't want to set the path blocking,
1733 * so we test it here
1734 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001736 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001737
Chris Masonb4ce94d2009-02-04 09:25:08 -05001738 btrfs_set_path_blocking(p);
1739
Chris Masonbd681512011-07-16 15:23:14 -04001740 /*
1741 * must have write locks on this node and the
1742 * parent
1743 */
1744 if (level + 1 > write_lock_level) {
1745 write_lock_level = level + 1;
1746 btrfs_release_path(p);
1747 goto again;
1748 }
1749
Yan Zheng33c66f42009-07-22 09:59:00 -04001750 err = btrfs_cow_block(trans, root, b,
1751 p->nodes[level + 1],
1752 p->slots[level + 1], &b);
1753 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001754 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001755 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001756 }
Chris Mason02217ed2007-03-02 16:08:05 -05001757 }
Chris Mason65b51a02008-08-01 15:11:20 -04001758cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001759 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001760
Chris Masoneb60cea2007-02-02 09:18:22 -05001761 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04001762 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001763
1764 /*
1765 * we have a lock on b and as long as we aren't changing
1766 * the tree, there is no way to for the items in b to change.
1767 * It is safe to drop the lock on our parent before we
1768 * go through the expensive btree search on b.
1769 *
1770 * If cow is true, then we might be changing slot zero,
1771 * which may require changing the parent. So, we can't
1772 * drop the lock until after we know which slot we're
1773 * operating on.
1774 */
1775 if (!cow)
1776 btrfs_unlock_up_safe(p, level + 1);
1777
Chris Mason5f39d392007-10-15 16:14:19 -04001778 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001779
Chris Mason5f39d392007-10-15 16:14:19 -04001780 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001781 int dec = 0;
1782 if (ret && slot > 0) {
1783 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001784 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001785 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001786 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001787 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04001788 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04001789 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001790 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001791 if (err) {
1792 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001793 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001794 }
Chris Masonc8c42862009-04-03 10:14:18 -04001795 b = p->nodes[level];
1796 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001797
Chris Masonbd681512011-07-16 15:23:14 -04001798 /*
1799 * slot 0 is special, if we change the key
1800 * we have to update the parent pointer
1801 * which means we must have a write lock
1802 * on the parent
1803 */
1804 if (slot == 0 && cow &&
1805 write_lock_level < level + 1) {
1806 write_lock_level = level + 1;
1807 btrfs_release_path(p);
1808 goto again;
1809 }
1810
Chris Masonf9efa9c2008-06-25 16:14:04 -04001811 unlock_up(p, level, lowest_unlock);
1812
Chris Mason925baed2008-06-25 16:01:30 -04001813 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 if (dec)
1815 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001816 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001817 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001818
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001820 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001822 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001823 if (err) {
1824 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001825 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001826 }
Chris Mason76a05b32009-05-14 13:24:30 -04001827
Chris Masonb4ce94d2009-02-04 09:25:08 -05001828 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04001829 level = btrfs_header_level(b);
1830 if (level <= write_lock_level) {
1831 err = btrfs_try_tree_write_lock(b);
1832 if (!err) {
1833 btrfs_set_path_blocking(p);
1834 btrfs_tree_lock(b);
1835 btrfs_clear_path_blocking(p, b,
1836 BTRFS_WRITE_LOCK);
1837 }
1838 p->locks[level] = BTRFS_WRITE_LOCK;
1839 } else {
1840 err = btrfs_try_tree_read_lock(b);
1841 if (!err) {
1842 btrfs_set_path_blocking(p);
1843 btrfs_tree_read_lock(b);
1844 btrfs_clear_path_blocking(p, b,
1845 BTRFS_READ_LOCK);
1846 }
1847 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001848 }
Chris Masonbd681512011-07-16 15:23:14 -04001849 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001850 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001851 } else {
1852 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001853 if (ins_len > 0 &&
1854 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001855 if (write_lock_level < 1) {
1856 write_lock_level = 1;
1857 btrfs_release_path(p);
1858 goto again;
1859 }
1860
Chris Masonb4ce94d2009-02-04 09:25:08 -05001861 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001862 err = split_leaf(trans, root, key,
1863 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04001864 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001865
Yan Zheng33c66f42009-07-22 09:59:00 -04001866 BUG_ON(err > 0);
1867 if (err) {
1868 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001869 goto done;
1870 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001871 }
Chris Mason459931e2008-12-10 09:10:46 -05001872 if (!p->search_for_split)
1873 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001874 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001875 }
1876 }
Chris Mason65b51a02008-08-01 15:11:20 -04001877 ret = 1;
1878done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001879 /*
1880 * we don't really know what they plan on doing with the path
1881 * from here on, so for now just mark it as blocking
1882 */
Chris Masonb9473432009-03-13 11:00:37 -04001883 if (!p->leave_spinning)
1884 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001885 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001886 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001887 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001888}
1889
Chris Mason74123bd2007-02-02 11:05:29 -05001890/*
1891 * adjust the pointers going up the tree, starting at level
1892 * making sure the right key of each node is points to 'key'.
1893 * This is used after shifting pointers to the left, so it stops
1894 * fixing up pointers when a given leaf/node is not in slot 0 of the
1895 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001896 *
1897 * If this fails to write a tree block, it returns -1, but continues
1898 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001899 */
Chris Mason5f39d392007-10-15 16:14:19 -04001900static int fixup_low_keys(struct btrfs_trans_handle *trans,
1901 struct btrfs_root *root, struct btrfs_path *path,
1902 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001903{
1904 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001905 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001906 struct extent_buffer *t;
1907
Chris Mason234b63a2007-03-13 10:46:10 -04001908 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001909 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001910 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001911 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001912 t = path->nodes[i];
1913 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001914 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001915 if (tslot != 0)
1916 break;
1917 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001918 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001919}
1920
Chris Mason74123bd2007-02-02 11:05:29 -05001921/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001922 * update item key.
1923 *
1924 * This function isn't completely safe. It's the caller's responsibility
1925 * that the new key won't break the order
1926 */
1927int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1928 struct btrfs_root *root, struct btrfs_path *path,
1929 struct btrfs_key *new_key)
1930{
1931 struct btrfs_disk_key disk_key;
1932 struct extent_buffer *eb;
1933 int slot;
1934
1935 eb = path->nodes[0];
1936 slot = path->slots[0];
1937 if (slot > 0) {
1938 btrfs_item_key(eb, &disk_key, slot - 1);
1939 if (comp_keys(&disk_key, new_key) >= 0)
1940 return -1;
1941 }
1942 if (slot < btrfs_header_nritems(eb) - 1) {
1943 btrfs_item_key(eb, &disk_key, slot + 1);
1944 if (comp_keys(&disk_key, new_key) <= 0)
1945 return -1;
1946 }
1947
1948 btrfs_cpu_key_to_disk(&disk_key, new_key);
1949 btrfs_set_item_key(eb, &disk_key, slot);
1950 btrfs_mark_buffer_dirty(eb);
1951 if (slot == 0)
1952 fixup_low_keys(trans, root, path, &disk_key, 1);
1953 return 0;
1954}
1955
1956/*
Chris Mason74123bd2007-02-02 11:05:29 -05001957 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001958 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001959 *
1960 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1961 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001962 */
Chris Mason98ed5172008-01-03 10:01:48 -05001963static int push_node_left(struct btrfs_trans_handle *trans,
1964 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001965 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001966{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001967 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001968 int src_nritems;
1969 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001970 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 src_nritems = btrfs_header_nritems(src);
1973 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001974 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001975 WARN_ON(btrfs_header_generation(src) != trans->transid);
1976 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001977
Chris Masonbce4eae2008-04-24 14:42:46 -04001978 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001979 return 1;
1980
Chris Masond3977122009-01-05 21:25:51 -05001981 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001982 return 1;
1983
Chris Masonbce4eae2008-04-24 14:42:46 -04001984 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001985 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001986 if (push_items < src_nritems) {
1987 /* leave at least 8 pointers in the node if
1988 * we aren't going to empty it
1989 */
1990 if (src_nritems - push_items < 8) {
1991 if (push_items <= 8)
1992 return 1;
1993 push_items -= 8;
1994 }
1995 }
1996 } else
1997 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001998
Chris Mason5f39d392007-10-15 16:14:19 -04001999 copy_extent_buffer(dst, src,
2000 btrfs_node_key_ptr_offset(dst_nritems),
2001 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002002 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002003
Chris Masonbb803952007-03-01 12:04:21 -05002004 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002005 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2006 btrfs_node_key_ptr_offset(push_items),
2007 (src_nritems - push_items) *
2008 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002009 }
Chris Mason5f39d392007-10-15 16:14:19 -04002010 btrfs_set_header_nritems(src, src_nritems - push_items);
2011 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2012 btrfs_mark_buffer_dirty(src);
2013 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002014
Chris Masonbb803952007-03-01 12:04:21 -05002015 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002016}
2017
Chris Mason97571fd2007-02-24 13:39:08 -05002018/*
Chris Mason79f95c82007-03-01 15:16:26 -05002019 * try to push data from one node into the next node right in the
2020 * tree.
2021 *
2022 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2023 * error, and > 0 if there was no room in the right hand block.
2024 *
2025 * this will only push up to 1/2 the contents of the left node over
2026 */
Chris Mason5f39d392007-10-15 16:14:19 -04002027static int balance_node_right(struct btrfs_trans_handle *trans,
2028 struct btrfs_root *root,
2029 struct extent_buffer *dst,
2030 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002031{
Chris Mason79f95c82007-03-01 15:16:26 -05002032 int push_items = 0;
2033 int max_push;
2034 int src_nritems;
2035 int dst_nritems;
2036 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002037
Chris Mason7bb86312007-12-11 09:25:06 -05002038 WARN_ON(btrfs_header_generation(src) != trans->transid);
2039 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2040
Chris Mason5f39d392007-10-15 16:14:19 -04002041 src_nritems = btrfs_header_nritems(src);
2042 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002043 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002044 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002045 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002046
Chris Masond3977122009-01-05 21:25:51 -05002047 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002048 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002049
2050 max_push = src_nritems / 2 + 1;
2051 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002052 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002053 return 1;
Yan252c38f2007-08-29 09:11:44 -04002054
Chris Mason79f95c82007-03-01 15:16:26 -05002055 if (max_push < push_items)
2056 push_items = max_push;
2057
Chris Mason5f39d392007-10-15 16:14:19 -04002058 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2059 btrfs_node_key_ptr_offset(0),
2060 (dst_nritems) *
2061 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002062
Chris Mason5f39d392007-10-15 16:14:19 -04002063 copy_extent_buffer(dst, src,
2064 btrfs_node_key_ptr_offset(0),
2065 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002066 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002067
Chris Mason5f39d392007-10-15 16:14:19 -04002068 btrfs_set_header_nritems(src, src_nritems - push_items);
2069 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002070
Chris Mason5f39d392007-10-15 16:14:19 -04002071 btrfs_mark_buffer_dirty(src);
2072 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002073
Chris Mason79f95c82007-03-01 15:16:26 -05002074 return ret;
2075}
2076
2077/*
Chris Mason97571fd2007-02-24 13:39:08 -05002078 * helper function to insert a new root level in the tree.
2079 * A new node is allocated, and a single item is inserted to
2080 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002081 *
2082 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002083 */
Chris Masond3977122009-01-05 21:25:51 -05002084static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002085 struct btrfs_root *root,
2086 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002087{
Chris Mason7bb86312007-12-11 09:25:06 -05002088 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002089 struct extent_buffer *lower;
2090 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002091 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002092 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002093
2094 BUG_ON(path->nodes[level]);
2095 BUG_ON(path->nodes[level-1] != root->node);
2096
Chris Mason7bb86312007-12-11 09:25:06 -05002097 lower = path->nodes[level-1];
2098 if (level == 1)
2099 btrfs_item_key(lower, &lower_key, 0);
2100 else
2101 btrfs_node_key(lower, &lower_key, 0);
2102
Zheng Yan31840ae2008-09-23 13:14:14 -04002103 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002104 root->root_key.objectid, &lower_key,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002105 level, root->node->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 if (IS_ERR(c))
2107 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002108
Yan, Zhengf0486c62010-05-16 10:46:25 -04002109 root_add_used(root, root->nodesize);
2110
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002111 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002112 btrfs_set_header_nritems(c, 1);
2113 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002114 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002115 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002117 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002118
Chris Mason5f39d392007-10-15 16:14:19 -04002119 write_extent_buffer(c, root->fs_info->fsid,
2120 (unsigned long)btrfs_header_fsid(c),
2121 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002122
2123 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2124 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2125 BTRFS_UUID_SIZE);
2126
Chris Mason5f39d392007-10-15 16:14:19 -04002127 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002128 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002129 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002130 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002131
2132 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002133
2134 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002135
Chris Mason925baed2008-06-25 16:01:30 -04002136 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002137 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002138
2139 /* the super has an extra ref to root->node */
2140 free_extent_buffer(old);
2141
Chris Mason0b86a832008-03-24 15:01:56 -04002142 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 extent_buffer_get(c);
2144 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002145 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002146 path->slots[level] = 0;
2147 return 0;
2148}
2149
Chris Mason74123bd2007-02-02 11:05:29 -05002150/*
2151 * worker function to insert a single pointer in a node.
2152 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002153 *
Chris Mason74123bd2007-02-02 11:05:29 -05002154 * slot and level indicate where you want the key to go, and
2155 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002156 *
2157 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002158 */
Chris Masone089f052007-03-16 16:20:31 -04002159static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2160 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002161 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002162{
Chris Mason5f39d392007-10-15 16:14:19 -04002163 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002164 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002165
2166 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002167 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 lower = path->nodes[level];
2169 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002170 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002171 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002172 BUG();
2173 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002174 memmove_extent_buffer(lower,
2175 btrfs_node_key_ptr_offset(slot + 1),
2176 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002177 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002178 }
Chris Mason5f39d392007-10-15 16:14:19 -04002179 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002180 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002181 WARN_ON(trans->transid == 0);
2182 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002183 btrfs_set_header_nritems(lower, nritems + 1);
2184 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002185 return 0;
2186}
2187
Chris Mason97571fd2007-02-24 13:39:08 -05002188/*
2189 * split the node at the specified level in path in two.
2190 * The path is corrected to point to the appropriate node after the split
2191 *
2192 * Before splitting this tries to make some room in the node by pushing
2193 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002194 *
2195 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002196 */
Chris Masone02119d2008-09-05 16:13:11 -04002197static noinline int split_node(struct btrfs_trans_handle *trans,
2198 struct btrfs_root *root,
2199 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002200{
Chris Mason5f39d392007-10-15 16:14:19 -04002201 struct extent_buffer *c;
2202 struct extent_buffer *split;
2203 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002204 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002205 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002206 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002207 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002208
Chris Mason5f39d392007-10-15 16:14:19 -04002209 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002210 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002211 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002212 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002213 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002214 if (ret)
2215 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002216 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002217 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002218 c = path->nodes[level];
2219 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002220 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002221 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002222 if (ret < 0)
2223 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002224 }
Chris Masone66f7092007-04-20 13:16:02 -04002225
Chris Mason5f39d392007-10-15 16:14:19 -04002226 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002227 mid = (c_nritems + 1) / 2;
2228 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002229
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002230 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002231 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002232 &disk_key, level, c->start, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002233 if (IS_ERR(split))
2234 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002235
Yan, Zhengf0486c62010-05-16 10:46:25 -04002236 root_add_used(root, root->nodesize);
2237
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002238 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002239 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002240 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002241 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002243 btrfs_set_header_owner(split, root->root_key.objectid);
2244 write_extent_buffer(split, root->fs_info->fsid,
2245 (unsigned long)btrfs_header_fsid(split),
2246 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002247 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2248 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2249 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002250
Chris Mason5f39d392007-10-15 16:14:19 -04002251
2252 copy_extent_buffer(split, c,
2253 btrfs_node_key_ptr_offset(0),
2254 btrfs_node_key_ptr_offset(mid),
2255 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2256 btrfs_set_header_nritems(split, c_nritems - mid);
2257 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002258 ret = 0;
2259
Chris Mason5f39d392007-10-15 16:14:19 -04002260 btrfs_mark_buffer_dirty(c);
2261 btrfs_mark_buffer_dirty(split);
2262
Chris Masondb945352007-10-15 16:15:53 -04002263 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002264 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002265 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002266 if (wret)
2267 ret = wret;
2268
Chris Mason5de08d72007-02-24 06:24:44 -05002269 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002270 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002271 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002272 free_extent_buffer(c);
2273 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002274 path->slots[level + 1] += 1;
2275 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002276 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002277 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002278 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002279 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002280}
2281
Chris Mason74123bd2007-02-02 11:05:29 -05002282/*
2283 * how many bytes are required to store the items in a leaf. start
2284 * and nr indicate which items in the leaf to check. This totals up the
2285 * space used both by the item structs and the item data
2286 */
Chris Mason5f39d392007-10-15 16:14:19 -04002287static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002288{
2289 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002290 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002291 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002292
2293 if (!nr)
2294 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002295 data_len = btrfs_item_end_nr(l, start);
2296 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002297 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002298 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002299 return data_len;
2300}
2301
Chris Mason74123bd2007-02-02 11:05:29 -05002302/*
Chris Masond4dbff92007-04-04 14:08:15 -04002303 * The space between the end of the leaf items and
2304 * the start of the leaf data. IOW, how much room
2305 * the leaf has left for both items and data
2306 */
Chris Masond3977122009-01-05 21:25:51 -05002307noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002308 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002309{
Chris Mason5f39d392007-10-15 16:14:19 -04002310 int nritems = btrfs_header_nritems(leaf);
2311 int ret;
2312 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2313 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002314 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2315 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002316 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002317 leaf_space_used(leaf, 0, nritems), nritems);
2318 }
2319 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002320}
2321
Chris Mason99d8f832010-07-07 10:51:48 -04002322/*
2323 * min slot controls the lowest index we're willing to push to the
2324 * right. We'll push up to and including min_slot, but no lower
2325 */
Chris Mason44871b12009-03-13 10:04:31 -04002326static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *root,
2328 struct btrfs_path *path,
2329 int data_size, int empty,
2330 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002331 int free_space, u32 left_nritems,
2332 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002333{
Chris Mason5f39d392007-10-15 16:14:19 -04002334 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002335 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002336 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002337 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002339 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002340 int push_space = 0;
2341 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002342 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002343 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002344 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002345 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002346 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002347
Chris Masoncfed81a2012-03-03 07:40:03 -05002348 btrfs_init_map_token(&token);
2349
Chris Mason34a38212007-11-07 13:31:03 -05002350 if (empty)
2351 nr = 0;
2352 else
Chris Mason99d8f832010-07-07 10:51:48 -04002353 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002354
Zheng Yan31840ae2008-09-23 13:14:14 -04002355 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002356 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002357
Chris Mason44871b12009-03-13 10:04:31 -04002358 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002359 i = left_nritems - 1;
2360 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002361 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002362
Zheng Yan31840ae2008-09-23 13:14:14 -04002363 if (!empty && push_items > 0) {
2364 if (path->slots[0] > i)
2365 break;
2366 if (path->slots[0] == i) {
2367 int space = btrfs_leaf_free_space(root, left);
2368 if (space + push_space * 2 > free_space)
2369 break;
2370 }
2371 }
2372
Chris Mason00ec4c52007-02-24 12:47:20 -05002373 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002374 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002375
Chris Masondb945352007-10-15 16:15:53 -04002376 this_item_size = btrfs_item_size(left, item);
2377 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002378 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002379
Chris Mason00ec4c52007-02-24 12:47:20 -05002380 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002381 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002382 if (i == 0)
2383 break;
2384 i--;
Chris Masondb945352007-10-15 16:15:53 -04002385 }
Chris Mason5f39d392007-10-15 16:14:19 -04002386
Chris Mason925baed2008-06-25 16:01:30 -04002387 if (push_items == 0)
2388 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002389
Chris Mason34a38212007-11-07 13:31:03 -05002390 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002391 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002392
Chris Mason00ec4c52007-02-24 12:47:20 -05002393 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002394 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002395
Chris Mason5f39d392007-10-15 16:14:19 -04002396 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002397 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002398
Chris Mason00ec4c52007-02-24 12:47:20 -05002399 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002400 data_end = leaf_data_end(root, right);
2401 memmove_extent_buffer(right,
2402 btrfs_leaf_data(right) + data_end - push_space,
2403 btrfs_leaf_data(right) + data_end,
2404 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2405
Chris Mason00ec4c52007-02-24 12:47:20 -05002406 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002407 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002408 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2409 btrfs_leaf_data(left) + leaf_data_end(root, left),
2410 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002411
2412 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2413 btrfs_item_nr_offset(0),
2414 right_nritems * sizeof(struct btrfs_item));
2415
Chris Mason00ec4c52007-02-24 12:47:20 -05002416 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002417 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2418 btrfs_item_nr_offset(left_nritems - push_items),
2419 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002420
2421 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002422 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002423 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002424 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002425 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002426 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002427 push_space -= btrfs_token_item_size(right, item, &token);
2428 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002429 }
2430
Chris Mason7518a232007-03-12 12:01:18 -04002431 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002432 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002433
Chris Mason34a38212007-11-07 13:31:03 -05002434 if (left_nritems)
2435 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002436 else
2437 clean_tree_block(trans, root, left);
2438
Chris Mason5f39d392007-10-15 16:14:19 -04002439 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002440
Chris Mason5f39d392007-10-15 16:14:19 -04002441 btrfs_item_key(right, &disk_key, 0);
2442 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002443 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002444
Chris Mason00ec4c52007-02-24 12:47:20 -05002445 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002446 if (path->slots[0] >= left_nritems) {
2447 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002448 if (btrfs_header_nritems(path->nodes[0]) == 0)
2449 clean_tree_block(trans, root, path->nodes[0]);
2450 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002451 free_extent_buffer(path->nodes[0]);
2452 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002453 path->slots[1] += 1;
2454 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002455 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002456 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002457 }
2458 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002459
2460out_unlock:
2461 btrfs_tree_unlock(right);
2462 free_extent_buffer(right);
2463 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002464}
Chris Mason925baed2008-06-25 16:01:30 -04002465
Chris Mason00ec4c52007-02-24 12:47:20 -05002466/*
Chris Mason44871b12009-03-13 10:04:31 -04002467 * push some data in the path leaf to the right, trying to free up at
2468 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2469 *
2470 * returns 1 if the push failed because the other node didn't have enough
2471 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002472 *
2473 * this will push starting from min_slot to the end of the leaf. It won't
2474 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002475 */
2476static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002477 *root, struct btrfs_path *path,
2478 int min_data_size, int data_size,
2479 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002480{
2481 struct extent_buffer *left = path->nodes[0];
2482 struct extent_buffer *right;
2483 struct extent_buffer *upper;
2484 int slot;
2485 int free_space;
2486 u32 left_nritems;
2487 int ret;
2488
2489 if (!path->nodes[1])
2490 return 1;
2491
2492 slot = path->slots[1];
2493 upper = path->nodes[1];
2494 if (slot >= btrfs_header_nritems(upper) - 1)
2495 return 1;
2496
2497 btrfs_assert_tree_locked(path->nodes[1]);
2498
2499 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002500 if (right == NULL)
2501 return 1;
2502
Chris Mason44871b12009-03-13 10:04:31 -04002503 btrfs_tree_lock(right);
2504 btrfs_set_lock_blocking(right);
2505
2506 free_space = btrfs_leaf_free_space(root, right);
2507 if (free_space < data_size)
2508 goto out_unlock;
2509
2510 /* cow and double check */
2511 ret = btrfs_cow_block(trans, root, right, upper,
2512 slot + 1, &right);
2513 if (ret)
2514 goto out_unlock;
2515
2516 free_space = btrfs_leaf_free_space(root, right);
2517 if (free_space < data_size)
2518 goto out_unlock;
2519
2520 left_nritems = btrfs_header_nritems(left);
2521 if (left_nritems == 0)
2522 goto out_unlock;
2523
Chris Mason99d8f832010-07-07 10:51:48 -04002524 return __push_leaf_right(trans, root, path, min_data_size, empty,
2525 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002526out_unlock:
2527 btrfs_tree_unlock(right);
2528 free_extent_buffer(right);
2529 return 1;
2530}
2531
2532/*
Chris Mason74123bd2007-02-02 11:05:29 -05002533 * push some data in the path leaf to the left, trying to free up at
2534 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002535 *
2536 * max_slot can put a limit on how far into the leaf we'll push items. The
2537 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2538 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002539 */
Chris Mason44871b12009-03-13 10:04:31 -04002540static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2541 struct btrfs_root *root,
2542 struct btrfs_path *path, int data_size,
2543 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002544 int free_space, u32 right_nritems,
2545 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002546{
Chris Mason5f39d392007-10-15 16:14:19 -04002547 struct btrfs_disk_key disk_key;
2548 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002549 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002550 int push_space = 0;
2551 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002552 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002553 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002554 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002555 int ret = 0;
2556 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002557 u32 this_item_size;
2558 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002559 struct btrfs_map_token token;
2560
2561 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562
Chris Mason34a38212007-11-07 13:31:03 -05002563 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002564 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002565 else
Chris Mason99d8f832010-07-07 10:51:48 -04002566 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002567
2568 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002569 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002570
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 if (!empty && push_items > 0) {
2572 if (path->slots[0] < i)
2573 break;
2574 if (path->slots[0] == i) {
2575 int space = btrfs_leaf_free_space(root, right);
2576 if (space + push_space * 2 > free_space)
2577 break;
2578 }
2579 }
2580
Chris Masonbe0e5c02007-01-26 15:51:26 -05002581 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002582 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002583
2584 this_item_size = btrfs_item_size(right, item);
2585 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 break;
Chris Masondb945352007-10-15 16:15:53 -04002587
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002589 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002590 }
Chris Masondb945352007-10-15 16:15:53 -04002591
Chris Masonbe0e5c02007-01-26 15:51:26 -05002592 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002593 ret = 1;
2594 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595 }
Chris Mason34a38212007-11-07 13:31:03 -05002596 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002597 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002598
Chris Masonbe0e5c02007-01-26 15:51:26 -05002599 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002600 copy_extent_buffer(left, right,
2601 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2602 btrfs_item_nr_offset(0),
2603 push_items * sizeof(struct btrfs_item));
2604
Chris Mason123abc82007-03-14 14:14:43 -04002605 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002606 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002607
2608 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002609 leaf_data_end(root, left) - push_space,
2610 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002611 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002612 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002613 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002614 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002615
Chris Masondb945352007-10-15 16:15:53 -04002616 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002617 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002618 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002619
Chris Mason5f39d392007-10-15 16:14:19 -04002620 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002621
Chris Masoncfed81a2012-03-03 07:40:03 -05002622 ioff = btrfs_token_item_offset(left, item, &token);
2623 btrfs_set_token_item_offset(left, item,
2624 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2625 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002626 }
Chris Mason5f39d392007-10-15 16:14:19 -04002627 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002628
2629 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002630 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002631 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2632 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002633 WARN_ON(1);
2634 }
Chris Mason5f39d392007-10-15 16:14:19 -04002635
Chris Mason34a38212007-11-07 13:31:03 -05002636 if (push_items < right_nritems) {
2637 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2638 leaf_data_end(root, right);
2639 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2640 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2641 btrfs_leaf_data(right) +
2642 leaf_data_end(root, right), push_space);
2643
2644 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002645 btrfs_item_nr_offset(push_items),
2646 (btrfs_header_nritems(right) - push_items) *
2647 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002648 }
Yaneef1c492007-11-26 10:58:13 -05002649 right_nritems -= push_items;
2650 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002651 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002652 for (i = 0; i < right_nritems; i++) {
2653 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002654
Chris Masoncfed81a2012-03-03 07:40:03 -05002655 push_space = push_space - btrfs_token_item_size(right,
2656 item, &token);
2657 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002658 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002659
Chris Mason5f39d392007-10-15 16:14:19 -04002660 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002661 if (right_nritems)
2662 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002663 else
2664 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002665
Chris Mason5f39d392007-10-15 16:14:19 -04002666 btrfs_item_key(right, &disk_key, 0);
2667 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002668 if (wret)
2669 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670
2671 /* then fixup the leaf pointer in the path */
2672 if (path->slots[0] < push_items) {
2673 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002674 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002675 free_extent_buffer(path->nodes[0]);
2676 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002677 path->slots[1] -= 1;
2678 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002679 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002680 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002681 path->slots[0] -= push_items;
2682 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002683 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002684 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002685out:
2686 btrfs_tree_unlock(left);
2687 free_extent_buffer(left);
2688 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689}
2690
Chris Mason74123bd2007-02-02 11:05:29 -05002691/*
Chris Mason44871b12009-03-13 10:04:31 -04002692 * push some data in the path leaf to the left, trying to free up at
2693 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002694 *
2695 * max_slot can put a limit on how far into the leaf we'll push items. The
2696 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2697 * items
Chris Mason44871b12009-03-13 10:04:31 -04002698 */
2699static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002700 *root, struct btrfs_path *path, int min_data_size,
2701 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002702{
2703 struct extent_buffer *right = path->nodes[0];
2704 struct extent_buffer *left;
2705 int slot;
2706 int free_space;
2707 u32 right_nritems;
2708 int ret = 0;
2709
2710 slot = path->slots[1];
2711 if (slot == 0)
2712 return 1;
2713 if (!path->nodes[1])
2714 return 1;
2715
2716 right_nritems = btrfs_header_nritems(right);
2717 if (right_nritems == 0)
2718 return 1;
2719
2720 btrfs_assert_tree_locked(path->nodes[1]);
2721
2722 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002723 if (left == NULL)
2724 return 1;
2725
Chris Mason44871b12009-03-13 10:04:31 -04002726 btrfs_tree_lock(left);
2727 btrfs_set_lock_blocking(left);
2728
2729 free_space = btrfs_leaf_free_space(root, left);
2730 if (free_space < data_size) {
2731 ret = 1;
2732 goto out;
2733 }
2734
2735 /* cow and double check */
2736 ret = btrfs_cow_block(trans, root, left,
2737 path->nodes[1], slot - 1, &left);
2738 if (ret) {
2739 /* we hit -ENOSPC, but it isn't fatal here */
2740 ret = 1;
2741 goto out;
2742 }
2743
2744 free_space = btrfs_leaf_free_space(root, left);
2745 if (free_space < data_size) {
2746 ret = 1;
2747 goto out;
2748 }
2749
Chris Mason99d8f832010-07-07 10:51:48 -04002750 return __push_leaf_left(trans, root, path, min_data_size,
2751 empty, left, free_space, right_nritems,
2752 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002753out:
2754 btrfs_tree_unlock(left);
2755 free_extent_buffer(left);
2756 return ret;
2757}
2758
2759/*
Chris Mason74123bd2007-02-02 11:05:29 -05002760 * split the path's leaf in two, making sure there is at least data_size
2761 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002762 *
2763 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002764 */
Chris Mason44871b12009-03-13 10:04:31 -04002765static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002766 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002767 struct btrfs_path *path,
2768 struct extent_buffer *l,
2769 struct extent_buffer *right,
2770 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002771{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002772 int data_copy_size;
2773 int rt_data_off;
2774 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002775 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002776 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002777 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05002778 struct btrfs_map_token token;
2779
2780 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002781
Chris Mason5f39d392007-10-15 16:14:19 -04002782 nritems = nritems - mid;
2783 btrfs_set_header_nritems(right, nritems);
2784 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2785
2786 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2787 btrfs_item_nr_offset(mid),
2788 nritems * sizeof(struct btrfs_item));
2789
2790 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002791 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2792 data_copy_size, btrfs_leaf_data(l) +
2793 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002794
Chris Mason5f39d392007-10-15 16:14:19 -04002795 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2796 btrfs_item_end_nr(l, mid);
2797
2798 for (i = 0; i < nritems; i++) {
2799 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002800 u32 ioff;
2801
Chris Masoncfed81a2012-03-03 07:40:03 -05002802 ioff = btrfs_token_item_offset(right, item, &token);
2803 btrfs_set_token_item_offset(right, item,
2804 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002805 }
Chris Mason74123bd2007-02-02 11:05:29 -05002806
Chris Mason5f39d392007-10-15 16:14:19 -04002807 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002808 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002809 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002810 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2811 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002812 if (wret)
2813 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002814
2815 btrfs_mark_buffer_dirty(right);
2816 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002817 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002818
Chris Masonbe0e5c02007-01-26 15:51:26 -05002819 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002820 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002821 free_extent_buffer(path->nodes[0]);
2822 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002823 path->slots[0] -= mid;
2824 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002825 } else {
2826 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002827 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002828 }
Chris Mason5f39d392007-10-15 16:14:19 -04002829
Chris Masoneb60cea2007-02-02 09:18:22 -05002830 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002831
Chris Mason44871b12009-03-13 10:04:31 -04002832 return ret;
2833}
2834
2835/*
Chris Mason99d8f832010-07-07 10:51:48 -04002836 * double splits happen when we need to insert a big item in the middle
2837 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2838 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2839 * A B C
2840 *
2841 * We avoid this by trying to push the items on either side of our target
2842 * into the adjacent leaves. If all goes well we can avoid the double split
2843 * completely.
2844 */
2845static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2846 struct btrfs_root *root,
2847 struct btrfs_path *path,
2848 int data_size)
2849{
2850 int ret;
2851 int progress = 0;
2852 int slot;
2853 u32 nritems;
2854
2855 slot = path->slots[0];
2856
2857 /*
2858 * try to push all the items after our slot into the
2859 * right leaf
2860 */
2861 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2862 if (ret < 0)
2863 return ret;
2864
2865 if (ret == 0)
2866 progress++;
2867
2868 nritems = btrfs_header_nritems(path->nodes[0]);
2869 /*
2870 * our goal is to get our slot at the start or end of a leaf. If
2871 * we've done so we're done
2872 */
2873 if (path->slots[0] == 0 || path->slots[0] == nritems)
2874 return 0;
2875
2876 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2877 return 0;
2878
2879 /* try to push all the items before our slot into the next leaf */
2880 slot = path->slots[0];
2881 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2882 if (ret < 0)
2883 return ret;
2884
2885 if (ret == 0)
2886 progress++;
2887
2888 if (progress)
2889 return 0;
2890 return 1;
2891}
2892
2893/*
Chris Mason44871b12009-03-13 10:04:31 -04002894 * split the path's leaf in two, making sure there is at least data_size
2895 * available for the resulting leaf level of the path.
2896 *
2897 * returns 0 if all went well and < 0 on failure.
2898 */
2899static noinline int split_leaf(struct btrfs_trans_handle *trans,
2900 struct btrfs_root *root,
2901 struct btrfs_key *ins_key,
2902 struct btrfs_path *path, int data_size,
2903 int extend)
2904{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002906 struct extent_buffer *l;
2907 u32 nritems;
2908 int mid;
2909 int slot;
2910 struct extent_buffer *right;
2911 int ret = 0;
2912 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002913 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002914 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002915 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002916
Yan, Zhenga5719522009-09-24 09:17:31 -04002917 l = path->nodes[0];
2918 slot = path->slots[0];
2919 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2920 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2921 return -EOVERFLOW;
2922
Chris Mason44871b12009-03-13 10:04:31 -04002923 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002924 if (data_size) {
2925 wret = push_leaf_right(trans, root, path, data_size,
2926 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002927 if (wret < 0)
2928 return wret;
2929 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002930 wret = push_leaf_left(trans, root, path, data_size,
2931 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002932 if (wret < 0)
2933 return wret;
2934 }
2935 l = path->nodes[0];
2936
2937 /* did the pushes work? */
2938 if (btrfs_leaf_free_space(root, l) >= data_size)
2939 return 0;
2940 }
2941
2942 if (!path->nodes[1]) {
2943 ret = insert_new_root(trans, root, path, 1);
2944 if (ret)
2945 return ret;
2946 }
2947again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002948 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002949 l = path->nodes[0];
2950 slot = path->slots[0];
2951 nritems = btrfs_header_nritems(l);
2952 mid = (nritems + 1) / 2;
2953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 if (mid <= slot) {
2955 if (nritems == 1 ||
2956 leaf_space_used(l, mid, nritems - mid) + data_size >
2957 BTRFS_LEAF_DATA_SIZE(root)) {
2958 if (slot >= nritems) {
2959 split = 0;
2960 } else {
2961 mid = slot;
2962 if (mid != nritems &&
2963 leaf_space_used(l, mid, nritems - mid) +
2964 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002965 if (data_size && !tried_avoid_double)
2966 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002967 split = 2;
2968 }
2969 }
2970 }
2971 } else {
2972 if (leaf_space_used(l, 0, mid) + data_size >
2973 BTRFS_LEAF_DATA_SIZE(root)) {
2974 if (!extend && data_size && slot == 0) {
2975 split = 0;
2976 } else if ((extend || !data_size) && slot == 0) {
2977 mid = 1;
2978 } else {
2979 mid = slot;
2980 if (mid != nritems &&
2981 leaf_space_used(l, mid, nritems - mid) +
2982 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002983 if (data_size && !tried_avoid_double)
2984 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 split = 2 ;
2986 }
2987 }
2988 }
2989 }
2990
2991 if (split == 0)
2992 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2993 else
2994 btrfs_item_key(l, &disk_key, mid);
2995
2996 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002997 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002998 &disk_key, 0, l->start, 0, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002999 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003000 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003001
3002 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003003
3004 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3005 btrfs_set_header_bytenr(right, right->start);
3006 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003007 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003008 btrfs_set_header_owner(right, root->root_key.objectid);
3009 btrfs_set_header_level(right, 0);
3010 write_extent_buffer(right, root->fs_info->fsid,
3011 (unsigned long)btrfs_header_fsid(right),
3012 BTRFS_FSID_SIZE);
3013
3014 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3015 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3016 BTRFS_UUID_SIZE);
3017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 if (split == 0) {
3019 if (mid <= slot) {
3020 btrfs_set_header_nritems(right, 0);
3021 wret = insert_ptr(trans, root, path,
3022 &disk_key, right->start,
3023 path->slots[1] + 1, 1);
3024 if (wret)
3025 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003026
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003027 btrfs_tree_unlock(path->nodes[0]);
3028 free_extent_buffer(path->nodes[0]);
3029 path->nodes[0] = right;
3030 path->slots[0] = 0;
3031 path->slots[1] += 1;
3032 } else {
3033 btrfs_set_header_nritems(right, 0);
3034 wret = insert_ptr(trans, root, path,
3035 &disk_key,
3036 right->start,
3037 path->slots[1], 1);
3038 if (wret)
3039 ret = wret;
3040 btrfs_tree_unlock(path->nodes[0]);
3041 free_extent_buffer(path->nodes[0]);
3042 path->nodes[0] = right;
3043 path->slots[0] = 0;
3044 if (path->slots[1] == 0) {
3045 wret = fixup_low_keys(trans, root,
3046 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003047 if (wret)
3048 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003049 }
3050 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003051 btrfs_mark_buffer_dirty(right);
3052 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003053 }
3054
3055 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3056 BUG_ON(ret);
3057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003059 BUG_ON(num_doubles != 0);
3060 num_doubles++;
3061 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003062 }
Chris Mason44871b12009-03-13 10:04:31 -04003063
Chris Masonbe0e5c02007-01-26 15:51:26 -05003064 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003065
3066push_for_double:
3067 push_for_double_split(trans, root, path, data_size);
3068 tried_avoid_double = 1;
3069 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3070 return 0;
3071 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003072}
3073
Yan, Zhengad48fd752009-11-12 09:33:58 +00003074static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3075 struct btrfs_root *root,
3076 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003077{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003078 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003079 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003080 struct btrfs_file_extent_item *fi;
3081 u64 extent_len = 0;
3082 u32 item_size;
3083 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003084
3085 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003086 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3087
3088 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3089 key.type != BTRFS_EXTENT_CSUM_KEY);
3090
3091 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3092 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003093
3094 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003095 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3096 fi = btrfs_item_ptr(leaf, path->slots[0],
3097 struct btrfs_file_extent_item);
3098 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3099 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003100 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003101
Chris Mason459931e2008-12-10 09:10:46 -05003102 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003103 path->search_for_split = 1;
3104 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003105 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003106 if (ret < 0)
3107 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003108
Yan, Zhengad48fd752009-11-12 09:33:58 +00003109 ret = -EAGAIN;
3110 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003111 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003112 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3113 goto err;
3114
Chris Mason109f6ae2010-04-02 09:20:18 -04003115 /* the leaf has changed, it now has room. return now */
3116 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3117 goto err;
3118
Yan, Zhengad48fd752009-11-12 09:33:58 +00003119 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3120 fi = btrfs_item_ptr(leaf, path->slots[0],
3121 struct btrfs_file_extent_item);
3122 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3123 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003124 }
3125
Chris Masonb9473432009-03-13 11:00:37 -04003126 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003127 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003128 if (ret)
3129 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003130
Yan, Zhengad48fd752009-11-12 09:33:58 +00003131 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003132 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003133 return 0;
3134err:
3135 path->keep_locks = 0;
3136 return ret;
3137}
3138
3139static noinline int split_item(struct btrfs_trans_handle *trans,
3140 struct btrfs_root *root,
3141 struct btrfs_path *path,
3142 struct btrfs_key *new_key,
3143 unsigned long split_offset)
3144{
3145 struct extent_buffer *leaf;
3146 struct btrfs_item *item;
3147 struct btrfs_item *new_item;
3148 int slot;
3149 char *buf;
3150 u32 nritems;
3151 u32 item_size;
3152 u32 orig_offset;
3153 struct btrfs_disk_key disk_key;
3154
Chris Masonb9473432009-03-13 11:00:37 -04003155 leaf = path->nodes[0];
3156 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3157
Chris Masonb4ce94d2009-02-04 09:25:08 -05003158 btrfs_set_path_blocking(path);
3159
Chris Mason459931e2008-12-10 09:10:46 -05003160 item = btrfs_item_nr(leaf, path->slots[0]);
3161 orig_offset = btrfs_item_offset(leaf, item);
3162 item_size = btrfs_item_size(leaf, item);
3163
Chris Mason459931e2008-12-10 09:10:46 -05003164 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003165 if (!buf)
3166 return -ENOMEM;
3167
Chris Mason459931e2008-12-10 09:10:46 -05003168 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3169 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003170
Chris Mason459931e2008-12-10 09:10:46 -05003171 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003172 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003173 if (slot != nritems) {
3174 /* shift the items */
3175 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003176 btrfs_item_nr_offset(slot),
3177 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003178 }
3179
3180 btrfs_cpu_key_to_disk(&disk_key, new_key);
3181 btrfs_set_item_key(leaf, &disk_key, slot);
3182
3183 new_item = btrfs_item_nr(leaf, slot);
3184
3185 btrfs_set_item_offset(leaf, new_item, orig_offset);
3186 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3187
3188 btrfs_set_item_offset(leaf, item,
3189 orig_offset + item_size - split_offset);
3190 btrfs_set_item_size(leaf, item, split_offset);
3191
3192 btrfs_set_header_nritems(leaf, nritems + 1);
3193
3194 /* write the data for the start of the original item */
3195 write_extent_buffer(leaf, buf,
3196 btrfs_item_ptr_offset(leaf, path->slots[0]),
3197 split_offset);
3198
3199 /* write the data for the new item */
3200 write_extent_buffer(leaf, buf + split_offset,
3201 btrfs_item_ptr_offset(leaf, slot),
3202 item_size - split_offset);
3203 btrfs_mark_buffer_dirty(leaf);
3204
Yan, Zhengad48fd752009-11-12 09:33:58 +00003205 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003206 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003207 return 0;
3208}
3209
3210/*
3211 * This function splits a single item into two items,
3212 * giving 'new_key' to the new item and splitting the
3213 * old one at split_offset (from the start of the item).
3214 *
3215 * The path may be released by this operation. After
3216 * the split, the path is pointing to the old item. The
3217 * new item is going to be in the same node as the old one.
3218 *
3219 * Note, the item being split must be smaller enough to live alone on
3220 * a tree block with room for one extra struct btrfs_item
3221 *
3222 * This allows us to split the item in place, keeping a lock on the
3223 * leaf the entire time.
3224 */
3225int btrfs_split_item(struct btrfs_trans_handle *trans,
3226 struct btrfs_root *root,
3227 struct btrfs_path *path,
3228 struct btrfs_key *new_key,
3229 unsigned long split_offset)
3230{
3231 int ret;
3232 ret = setup_leaf_for_split(trans, root, path,
3233 sizeof(struct btrfs_item));
3234 if (ret)
3235 return ret;
3236
3237 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003238 return ret;
3239}
3240
3241/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003242 * This function duplicate a item, giving 'new_key' to the new item.
3243 * It guarantees both items live in the same tree leaf and the new item
3244 * is contiguous with the original item.
3245 *
3246 * This allows us to split file extent in place, keeping a lock on the
3247 * leaf the entire time.
3248 */
3249int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3250 struct btrfs_root *root,
3251 struct btrfs_path *path,
3252 struct btrfs_key *new_key)
3253{
3254 struct extent_buffer *leaf;
3255 int ret;
3256 u32 item_size;
3257
3258 leaf = path->nodes[0];
3259 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3260 ret = setup_leaf_for_split(trans, root, path,
3261 item_size + sizeof(struct btrfs_item));
3262 if (ret)
3263 return ret;
3264
3265 path->slots[0]++;
3266 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3267 item_size, item_size +
3268 sizeof(struct btrfs_item), 1);
3269 BUG_ON(ret);
3270
3271 leaf = path->nodes[0];
3272 memcpy_extent_buffer(leaf,
3273 btrfs_item_ptr_offset(leaf, path->slots[0]),
3274 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3275 item_size);
3276 return 0;
3277}
3278
3279/*
Chris Masond352ac62008-09-29 15:18:18 -04003280 * make the item pointed to by the path smaller. new_size indicates
3281 * how small to make it, and from_end tells us if we just chop bytes
3282 * off the end of the item or if we shift the item to chop bytes off
3283 * the front.
3284 */
Chris Masonb18c6682007-04-17 13:26:50 -04003285int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3286 struct btrfs_root *root,
3287 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003288 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003289{
Chris Masonb18c6682007-04-17 13:26:50 -04003290 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003291 struct extent_buffer *leaf;
3292 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003293 u32 nritems;
3294 unsigned int data_end;
3295 unsigned int old_data_start;
3296 unsigned int old_size;
3297 unsigned int size_diff;
3298 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003299 struct btrfs_map_token token;
3300
3301 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003302
Chris Mason5f39d392007-10-15 16:14:19 -04003303 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003304 slot = path->slots[0];
3305
3306 old_size = btrfs_item_size_nr(leaf, slot);
3307 if (old_size == new_size)
3308 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003309
Chris Mason5f39d392007-10-15 16:14:19 -04003310 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003311 data_end = leaf_data_end(root, leaf);
3312
Chris Mason5f39d392007-10-15 16:14:19 -04003313 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003314
Chris Masonb18c6682007-04-17 13:26:50 -04003315 size_diff = old_size - new_size;
3316
3317 BUG_ON(slot < 0);
3318 BUG_ON(slot >= nritems);
3319
3320 /*
3321 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3322 */
3323 /* first correct the data pointers */
3324 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003325 u32 ioff;
3326 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003327
Chris Masoncfed81a2012-03-03 07:40:03 -05003328 ioff = btrfs_token_item_offset(leaf, item, &token);
3329 btrfs_set_token_item_offset(leaf, item,
3330 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003331 }
Chris Masondb945352007-10-15 16:15:53 -04003332
Chris Masonb18c6682007-04-17 13:26:50 -04003333 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003334 if (from_end) {
3335 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3336 data_end + size_diff, btrfs_leaf_data(leaf) +
3337 data_end, old_data_start + new_size - data_end);
3338 } else {
3339 struct btrfs_disk_key disk_key;
3340 u64 offset;
3341
3342 btrfs_item_key(leaf, &disk_key, slot);
3343
3344 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3345 unsigned long ptr;
3346 struct btrfs_file_extent_item *fi;
3347
3348 fi = btrfs_item_ptr(leaf, slot,
3349 struct btrfs_file_extent_item);
3350 fi = (struct btrfs_file_extent_item *)(
3351 (unsigned long)fi - size_diff);
3352
3353 if (btrfs_file_extent_type(leaf, fi) ==
3354 BTRFS_FILE_EXTENT_INLINE) {
3355 ptr = btrfs_item_ptr_offset(leaf, slot);
3356 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003357 (unsigned long)fi,
3358 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003359 disk_bytenr));
3360 }
3361 }
3362
3363 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3364 data_end + size_diff, btrfs_leaf_data(leaf) +
3365 data_end, old_data_start - data_end);
3366
3367 offset = btrfs_disk_key_offset(&disk_key);
3368 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3369 btrfs_set_item_key(leaf, &disk_key, slot);
3370 if (slot == 0)
3371 fixup_low_keys(trans, root, path, &disk_key, 1);
3372 }
Chris Mason5f39d392007-10-15 16:14:19 -04003373
3374 item = btrfs_item_nr(leaf, slot);
3375 btrfs_set_item_size(leaf, item, new_size);
3376 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003377
Chris Mason5f39d392007-10-15 16:14:19 -04003378 if (btrfs_leaf_free_space(root, leaf) < 0) {
3379 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003380 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003381 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003382 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003383}
3384
Chris Masond352ac62008-09-29 15:18:18 -04003385/*
3386 * make the item pointed to by the path bigger, data_size is the new size.
3387 */
Chris Mason5f39d392007-10-15 16:14:19 -04003388int btrfs_extend_item(struct btrfs_trans_handle *trans,
3389 struct btrfs_root *root, struct btrfs_path *path,
3390 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003391{
Chris Mason6567e832007-04-16 09:22:45 -04003392 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003393 struct extent_buffer *leaf;
3394 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003395 u32 nritems;
3396 unsigned int data_end;
3397 unsigned int old_data;
3398 unsigned int old_size;
3399 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003400 struct btrfs_map_token token;
3401
3402 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003403
Chris Mason5f39d392007-10-15 16:14:19 -04003404 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003405
Chris Mason5f39d392007-10-15 16:14:19 -04003406 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003407 data_end = leaf_data_end(root, leaf);
3408
Chris Mason5f39d392007-10-15 16:14:19 -04003409 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3410 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003411 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003412 }
Chris Mason6567e832007-04-16 09:22:45 -04003413 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003414 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003415
3416 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003417 if (slot >= nritems) {
3418 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003419 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3420 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003421 BUG_ON(1);
3422 }
Chris Mason6567e832007-04-16 09:22:45 -04003423
3424 /*
3425 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3426 */
3427 /* first correct the data pointers */
3428 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003429 u32 ioff;
3430 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003431
Chris Masoncfed81a2012-03-03 07:40:03 -05003432 ioff = btrfs_token_item_offset(leaf, item, &token);
3433 btrfs_set_token_item_offset(leaf, item,
3434 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003435 }
Chris Mason5f39d392007-10-15 16:14:19 -04003436
Chris Mason6567e832007-04-16 09:22:45 -04003437 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003438 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003439 data_end - data_size, btrfs_leaf_data(leaf) +
3440 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003441
Chris Mason6567e832007-04-16 09:22:45 -04003442 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003443 old_size = btrfs_item_size_nr(leaf, slot);
3444 item = btrfs_item_nr(leaf, slot);
3445 btrfs_set_item_size(leaf, item, old_size + data_size);
3446 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003447
Chris Mason5f39d392007-10-15 16:14:19 -04003448 if (btrfs_leaf_free_space(root, leaf) < 0) {
3449 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003450 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003451 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003452 return 0;
Chris Mason6567e832007-04-16 09:22:45 -04003453}
3454
Chris Mason74123bd2007-02-02 11:05:29 -05003455/*
Chris Masond352ac62008-09-29 15:18:18 -04003456 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003457 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003458 * Returns the number of keys that were inserted.
3459 */
3460int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3461 struct btrfs_root *root,
3462 struct btrfs_path *path,
3463 struct btrfs_key *cpu_key, u32 *data_size,
3464 int nr)
3465{
3466 struct extent_buffer *leaf;
3467 struct btrfs_item *item;
3468 int ret = 0;
3469 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003470 int i;
3471 u32 nritems;
3472 u32 total_data = 0;
3473 u32 total_size = 0;
3474 unsigned int data_end;
3475 struct btrfs_disk_key disk_key;
3476 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003477 struct btrfs_map_token token;
3478
3479 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003480
Yan Zheng87b29b22008-12-17 10:21:48 -05003481 for (i = 0; i < nr; i++) {
3482 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3483 BTRFS_LEAF_DATA_SIZE(root)) {
3484 break;
3485 nr = i;
3486 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003487 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003488 total_size += data_size[i] + sizeof(struct btrfs_item);
3489 }
3490 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003491
Josef Bacikf3465ca2008-11-12 14:19:50 -05003492 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3493 if (ret == 0)
3494 return -EEXIST;
3495 if (ret < 0)
3496 goto out;
3497
Josef Bacikf3465ca2008-11-12 14:19:50 -05003498 leaf = path->nodes[0];
3499
3500 nritems = btrfs_header_nritems(leaf);
3501 data_end = leaf_data_end(root, leaf);
3502
3503 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3504 for (i = nr; i >= 0; i--) {
3505 total_data -= data_size[i];
3506 total_size -= data_size[i] + sizeof(struct btrfs_item);
3507 if (total_size < btrfs_leaf_free_space(root, leaf))
3508 break;
3509 }
3510 nr = i;
3511 }
3512
3513 slot = path->slots[0];
3514 BUG_ON(slot < 0);
3515
3516 if (slot != nritems) {
3517 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3518
3519 item = btrfs_item_nr(leaf, slot);
3520 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3521
3522 /* figure out how many keys we can insert in here */
3523 total_data = data_size[0];
3524 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003525 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003526 break;
3527 total_data += data_size[i];
3528 }
3529 nr = i;
3530
3531 if (old_data < data_end) {
3532 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003533 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003534 slot, old_data, data_end);
3535 BUG_ON(1);
3536 }
3537 /*
3538 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3539 */
3540 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003541 for (i = slot; i < nritems; i++) {
3542 u32 ioff;
3543
3544 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003545 ioff = btrfs_token_item_offset(leaf, item, &token);
3546 btrfs_set_token_item_offset(leaf, item,
3547 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003548 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003549 /* shift the items */
3550 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3551 btrfs_item_nr_offset(slot),
3552 (nritems - slot) * sizeof(struct btrfs_item));
3553
3554 /* shift the data */
3555 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3556 data_end - total_data, btrfs_leaf_data(leaf) +
3557 data_end, old_data - data_end);
3558 data_end = old_data;
3559 } else {
3560 /*
3561 * this sucks but it has to be done, if we are inserting at
3562 * the end of the leaf only insert 1 of the items, since we
3563 * have no way of knowing whats on the next leaf and we'd have
3564 * to drop our current locks to figure it out
3565 */
3566 nr = 1;
3567 }
3568
3569 /* setup the item for the new data */
3570 for (i = 0; i < nr; i++) {
3571 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3572 btrfs_set_item_key(leaf, &disk_key, slot + i);
3573 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003574 btrfs_set_token_item_offset(leaf, item,
3575 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003576 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003577 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003578 }
3579 btrfs_set_header_nritems(leaf, nritems + nr);
3580 btrfs_mark_buffer_dirty(leaf);
3581
3582 ret = 0;
3583 if (slot == 0) {
3584 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3585 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3586 }
3587
3588 if (btrfs_leaf_free_space(root, leaf) < 0) {
3589 btrfs_print_leaf(root, leaf);
3590 BUG();
3591 }
3592out:
3593 if (!ret)
3594 ret = nr;
3595 return ret;
3596}
3597
3598/*
Chris Mason44871b12009-03-13 10:04:31 -04003599 * this is a helper for btrfs_insert_empty_items, the main goal here is
3600 * to save stack depth by doing the bulk of the work in a function
3601 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003602 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003603int setup_items_for_insert(struct btrfs_trans_handle *trans,
3604 struct btrfs_root *root, struct btrfs_path *path,
3605 struct btrfs_key *cpu_key, u32 *data_size,
3606 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003607{
Chris Mason5f39d392007-10-15 16:14:19 -04003608 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003609 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003610 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003611 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003612 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003613 int ret;
3614 struct extent_buffer *leaf;
3615 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05003616 struct btrfs_map_token token;
3617
3618 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04003619
Chris Mason5f39d392007-10-15 16:14:19 -04003620 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003621 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003622
Chris Mason5f39d392007-10-15 16:14:19 -04003623 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003624 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003625
Chris Masonf25956c2008-09-12 15:32:53 -04003626 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003627 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003628 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003629 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003630 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003631 }
Chris Mason5f39d392007-10-15 16:14:19 -04003632
Chris Masonbe0e5c02007-01-26 15:51:26 -05003633 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003634 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003635
Chris Mason5f39d392007-10-15 16:14:19 -04003636 if (old_data < data_end) {
3637 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003638 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003639 slot, old_data, data_end);
3640 BUG_ON(1);
3641 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003642 /*
3643 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3644 */
3645 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003646 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003647 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003648
Chris Mason5f39d392007-10-15 16:14:19 -04003649 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003650 ioff = btrfs_token_item_offset(leaf, item, &token);
3651 btrfs_set_token_item_offset(leaf, item,
3652 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003653 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003654 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003655 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003656 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003657 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003658
3659 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003660 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003661 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003662 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003663 data_end = old_data;
3664 }
Chris Mason5f39d392007-10-15 16:14:19 -04003665
Chris Mason62e27492007-03-15 12:56:47 -04003666 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003667 for (i = 0; i < nr; i++) {
3668 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3669 btrfs_set_item_key(leaf, &disk_key, slot + i);
3670 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003671 btrfs_set_token_item_offset(leaf, item,
3672 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003673 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003674 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05003675 }
Chris Mason44871b12009-03-13 10:04:31 -04003676
Chris Mason9c583092008-01-29 15:15:18 -05003677 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003678
3679 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003680 if (slot == 0) {
3681 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003682 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003683 }
Chris Masonb9473432009-03-13 11:00:37 -04003684 btrfs_unlock_up_safe(path, 1);
3685 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003686
Chris Mason5f39d392007-10-15 16:14:19 -04003687 if (btrfs_leaf_free_space(root, leaf) < 0) {
3688 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003689 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003690 }
Chris Mason44871b12009-03-13 10:04:31 -04003691 return ret;
3692}
3693
3694/*
3695 * Given a key and some data, insert items into the tree.
3696 * This does all the path init required, making room in the tree if needed.
3697 */
3698int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root,
3700 struct btrfs_path *path,
3701 struct btrfs_key *cpu_key, u32 *data_size,
3702 int nr)
3703{
Chris Mason44871b12009-03-13 10:04:31 -04003704 int ret = 0;
3705 int slot;
3706 int i;
3707 u32 total_size = 0;
3708 u32 total_data = 0;
3709
3710 for (i = 0; i < nr; i++)
3711 total_data += data_size[i];
3712
3713 total_size = total_data + (nr * sizeof(struct btrfs_item));
3714 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3715 if (ret == 0)
3716 return -EEXIST;
3717 if (ret < 0)
3718 goto out;
3719
Chris Mason44871b12009-03-13 10:04:31 -04003720 slot = path->slots[0];
3721 BUG_ON(slot < 0);
3722
3723 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3724 total_data, total_size, nr);
3725
Chris Masoned2ff2c2007-03-01 18:59:40 -05003726out:
Chris Mason62e27492007-03-15 12:56:47 -04003727 return ret;
3728}
3729
3730/*
3731 * Given a key and some data, insert an item into the tree.
3732 * This does all the path init required, making room in the tree if needed.
3733 */
Chris Masone089f052007-03-16 16:20:31 -04003734int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3735 *root, struct btrfs_key *cpu_key, void *data, u32
3736 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003737{
3738 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003739 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003740 struct extent_buffer *leaf;
3741 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003742
Chris Mason2c90e5d2007-04-02 10:50:19 -04003743 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003744 if (!path)
3745 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003746 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003747 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003748 leaf = path->nodes[0];
3749 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3750 write_extent_buffer(leaf, data, ptr, data_size);
3751 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003752 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003753 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003754 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003755}
3756
Chris Mason74123bd2007-02-02 11:05:29 -05003757/*
Chris Mason5de08d72007-02-24 06:24:44 -05003758 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003759 *
Chris Masond352ac62008-09-29 15:18:18 -04003760 * the tree should have been previously balanced so the deletion does not
3761 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003762 */
Chris Masone089f052007-03-16 16:20:31 -04003763static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3764 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003765{
Chris Mason5f39d392007-10-15 16:14:19 -04003766 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003767 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003768 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003769 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003770
Chris Mason5f39d392007-10-15 16:14:19 -04003771 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003772 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003773 memmove_extent_buffer(parent,
3774 btrfs_node_key_ptr_offset(slot),
3775 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003776 sizeof(struct btrfs_key_ptr) *
3777 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003778 }
Chris Mason7518a232007-03-12 12:01:18 -04003779 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003780 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003781 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003782 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003783 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003784 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003785 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003786 struct btrfs_disk_key disk_key;
3787
3788 btrfs_node_key(parent, &disk_key, 0);
3789 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003790 if (wret)
3791 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003792 }
Chris Masond6025572007-03-30 14:27:56 -04003793 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003794 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003795}
3796
Chris Mason74123bd2007-02-02 11:05:29 -05003797/*
Chris Mason323ac952008-10-01 19:05:46 -04003798 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003799 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003800 *
3801 * This deletes the pointer in path->nodes[1] and frees the leaf
3802 * block extent. zero is returned if it all worked out, < 0 otherwise.
3803 *
3804 * The path must have already been setup for deleting the leaf, including
3805 * all the proper balancing. path->nodes[1] must be locked.
3806 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003807static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3808 struct btrfs_root *root,
3809 struct btrfs_path *path,
3810 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003811{
3812 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003813
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003814 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003815 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3816 if (ret)
3817 return ret;
3818
Chris Mason4d081c42009-02-04 09:31:28 -05003819 /*
3820 * btrfs_free_extent is expensive, we want to make sure we
3821 * aren't holding any locks when we call it
3822 */
3823 btrfs_unlock_up_safe(path, 0);
3824
Yan, Zhengf0486c62010-05-16 10:46:25 -04003825 root_sub_used(root, leaf->len);
3826
Josef Bacik3083ee22012-03-09 16:01:49 -05003827 extent_buffer_get(leaf);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003828 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05003829 free_extent_buffer_stale(leaf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003830 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003831}
3832/*
Chris Mason74123bd2007-02-02 11:05:29 -05003833 * delete the item at the leaf level in path. If that empties
3834 * the leaf, remove it from the tree
3835 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003836int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3837 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003838{
Chris Mason5f39d392007-10-15 16:14:19 -04003839 struct extent_buffer *leaf;
3840 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003841 int last_off;
3842 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003843 int ret = 0;
3844 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003845 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003846 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05003847 struct btrfs_map_token token;
3848
3849 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003850
Chris Mason5f39d392007-10-15 16:14:19 -04003851 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003852 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3853
3854 for (i = 0; i < nr; i++)
3855 dsize += btrfs_item_size_nr(leaf, slot + i);
3856
Chris Mason5f39d392007-10-15 16:14:19 -04003857 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003858
Chris Mason85e21ba2008-01-29 15:11:36 -05003859 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003860 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003861
3862 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003863 data_end + dsize,
3864 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003865 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003866
Chris Mason85e21ba2008-01-29 15:11:36 -05003867 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003868 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003869
Chris Mason5f39d392007-10-15 16:14:19 -04003870 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003871 ioff = btrfs_token_item_offset(leaf, item, &token);
3872 btrfs_set_token_item_offset(leaf, item,
3873 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003874 }
Chris Masondb945352007-10-15 16:15:53 -04003875
Chris Mason5f39d392007-10-15 16:14:19 -04003876 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003877 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003878 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003879 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003880 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003881 btrfs_set_header_nritems(leaf, nritems - nr);
3882 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003883
Chris Mason74123bd2007-02-02 11:05:29 -05003884 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003885 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003886 if (leaf == root->node) {
3887 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003888 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003889 btrfs_set_path_blocking(path);
3890 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003891 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003892 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003893 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003894 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003895 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003896 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003897 struct btrfs_disk_key disk_key;
3898
3899 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003900 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003901 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003902 if (wret)
3903 ret = wret;
3904 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003905
Chris Mason74123bd2007-02-02 11:05:29 -05003906 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003907 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003908 /* push_leaf_left fixes the path.
3909 * make sure the path still points to our leaf
3910 * for possible call to del_ptr below
3911 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003912 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003913 extent_buffer_get(leaf);
3914
Chris Masonb9473432009-03-13 11:00:37 -04003915 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003916 wret = push_leaf_left(trans, root, path, 1, 1,
3917 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003918 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003919 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003920
3921 if (path->nodes[0] == leaf &&
3922 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003923 wret = push_leaf_right(trans, root, path, 1,
3924 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003925 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003926 ret = wret;
3927 }
Chris Mason5f39d392007-10-15 16:14:19 -04003928
3929 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003930 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003931 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003932 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003933 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003934 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003935 /* if we're still in the path, make sure
3936 * we're dirty. Otherwise, one of the
3937 * push_leaf functions must have already
3938 * dirtied this buffer
3939 */
3940 if (path->nodes[0] == leaf)
3941 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003942 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003943 }
Chris Masond5719762007-03-23 10:01:08 -04003944 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003945 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003946 }
3947 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003948 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003949}
3950
Chris Mason97571fd2007-02-24 13:39:08 -05003951/*
Chris Mason925baed2008-06-25 16:01:30 -04003952 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003953 * returns 0 if it found something or 1 if there are no lesser leaves.
3954 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003955 *
3956 * This may release the path, and so you may lose any locks held at the
3957 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003958 */
3959int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3960{
Chris Mason925baed2008-06-25 16:01:30 -04003961 struct btrfs_key key;
3962 struct btrfs_disk_key found_key;
3963 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003964
Chris Mason925baed2008-06-25 16:01:30 -04003965 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003966
Chris Mason925baed2008-06-25 16:01:30 -04003967 if (key.offset > 0)
3968 key.offset--;
3969 else if (key.type > 0)
3970 key.type--;
3971 else if (key.objectid > 0)
3972 key.objectid--;
3973 else
3974 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003975
David Sterbab3b4aa72011-04-21 01:20:15 +02003976 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003977 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3978 if (ret < 0)
3979 return ret;
3980 btrfs_item_key(path->nodes[0], &found_key, 0);
3981 ret = comp_keys(&found_key, &key);
3982 if (ret < 0)
3983 return 0;
3984 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003985}
3986
Chris Mason3f157a22008-06-25 16:01:31 -04003987/*
3988 * A helper function to walk down the tree starting at min_key, and looking
3989 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003990 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003991 *
3992 * This does not cow, but it does stuff the starting key it finds back
3993 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3994 * key and get a writable path.
3995 *
3996 * This does lock as it descends, and path->keep_locks should be set
3997 * to 1 by the caller.
3998 *
3999 * This honors path->lowest_level to prevent descent past a given level
4000 * of the tree.
4001 *
Chris Masond352ac62008-09-29 15:18:18 -04004002 * min_trans indicates the oldest transaction that you are interested
4003 * in walking through. Any nodes or leaves older than min_trans are
4004 * skipped over (without reading them).
4005 *
Chris Mason3f157a22008-06-25 16:01:31 -04004006 * returns zero if something useful was found, < 0 on error and 1 if there
4007 * was nothing in the tree that matched the search criteria.
4008 */
4009int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004010 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004011 struct btrfs_path *path, int cache_only,
4012 u64 min_trans)
4013{
4014 struct extent_buffer *cur;
4015 struct btrfs_key found_key;
4016 int slot;
Yan96524802008-07-24 12:19:49 -04004017 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004018 u32 nritems;
4019 int level;
4020 int ret = 1;
4021
Chris Mason934d3752008-12-08 16:43:10 -05004022 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004023again:
Chris Masonbd681512011-07-16 15:23:14 -04004024 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004025 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004026 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004027 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004028 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004029
4030 if (btrfs_header_generation(cur) < min_trans) {
4031 ret = 1;
4032 goto out;
4033 }
Chris Masond3977122009-01-05 21:25:51 -05004034 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004035 nritems = btrfs_header_nritems(cur);
4036 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004037 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004038
Chris Mason323ac952008-10-01 19:05:46 -04004039 /* at the lowest level, we're done, setup the path and exit */
4040 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004041 if (slot >= nritems)
4042 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004043 ret = 0;
4044 path->slots[level] = slot;
4045 btrfs_item_key_to_cpu(cur, &found_key, slot);
4046 goto out;
4047 }
Yan96524802008-07-24 12:19:49 -04004048 if (sret && slot > 0)
4049 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004050 /*
4051 * check this node pointer against the cache_only and
4052 * min_trans parameters. If it isn't in cache or is too
4053 * old, skip to the next one.
4054 */
Chris Masond3977122009-01-05 21:25:51 -05004055 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004056 u64 blockptr;
4057 u64 gen;
4058 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004059 struct btrfs_disk_key disk_key;
4060
Chris Mason3f157a22008-06-25 16:01:31 -04004061 blockptr = btrfs_node_blockptr(cur, slot);
4062 gen = btrfs_node_ptr_generation(cur, slot);
4063 if (gen < min_trans) {
4064 slot++;
4065 continue;
4066 }
4067 if (!cache_only)
4068 break;
4069
Chris Masone02119d2008-09-05 16:13:11 -04004070 if (max_key) {
4071 btrfs_node_key(cur, &disk_key, slot);
4072 if (comp_keys(&disk_key, max_key) >= 0) {
4073 ret = 1;
4074 goto out;
4075 }
4076 }
4077
Chris Mason3f157a22008-06-25 16:01:31 -04004078 tmp = btrfs_find_tree_block(root, blockptr,
4079 btrfs_level_size(root, level - 1));
4080
4081 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4082 free_extent_buffer(tmp);
4083 break;
4084 }
4085 if (tmp)
4086 free_extent_buffer(tmp);
4087 slot++;
4088 }
Chris Masone02119d2008-09-05 16:13:11 -04004089find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004090 /*
4091 * we didn't find a candidate key in this node, walk forward
4092 * and find another one
4093 */
4094 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004095 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004096 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004097 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004098 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004099 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004100 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004101 goto again;
4102 } else {
4103 goto out;
4104 }
4105 }
4106 /* save our key for returning back */
4107 btrfs_node_key_to_cpu(cur, &found_key, slot);
4108 path->slots[level] = slot;
4109 if (level == path->lowest_level) {
4110 ret = 0;
4111 unlock_up(path, level, 1);
4112 goto out;
4113 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004114 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004115 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004116 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004117
Chris Masonbd681512011-07-16 15:23:14 -04004118 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004119
Chris Masonbd681512011-07-16 15:23:14 -04004120 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004121 path->nodes[level - 1] = cur;
4122 unlock_up(path, level, 1);
Chris Masonbd681512011-07-16 15:23:14 -04004123 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004124 }
4125out:
4126 if (ret == 0)
4127 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004128 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004129 return ret;
4130}
4131
4132/*
4133 * this is similar to btrfs_next_leaf, but does not try to preserve
4134 * and fixup the path. It looks for and returns the next key in the
4135 * tree based on the current path and the cache_only and min_trans
4136 * parameters.
4137 *
4138 * 0 is returned if another key is found, < 0 if there are any errors
4139 * and 1 is returned if there are no higher keys in the tree
4140 *
4141 * path->keep_locks should be set to 1 on the search made before
4142 * calling this function.
4143 */
Chris Masone7a84562008-06-25 16:01:31 -04004144int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004145 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004146 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004147{
Chris Masone7a84562008-06-25 16:01:31 -04004148 int slot;
4149 struct extent_buffer *c;
4150
Chris Mason934d3752008-12-08 16:43:10 -05004151 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004152 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004153 if (!path->nodes[level])
4154 return 1;
4155
4156 slot = path->slots[level] + 1;
4157 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004158next:
Chris Masone7a84562008-06-25 16:01:31 -04004159 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004160 int ret;
4161 int orig_lowest;
4162 struct btrfs_key cur_key;
4163 if (level + 1 >= BTRFS_MAX_LEVEL ||
4164 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004165 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004166
4167 if (path->locks[level + 1]) {
4168 level++;
4169 continue;
4170 }
4171
4172 slot = btrfs_header_nritems(c) - 1;
4173 if (level == 0)
4174 btrfs_item_key_to_cpu(c, &cur_key, slot);
4175 else
4176 btrfs_node_key_to_cpu(c, &cur_key, slot);
4177
4178 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004179 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004180 path->lowest_level = level;
4181 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4182 0, 0);
4183 path->lowest_level = orig_lowest;
4184 if (ret < 0)
4185 return ret;
4186
4187 c = path->nodes[level];
4188 slot = path->slots[level];
4189 if (ret == 0)
4190 slot++;
4191 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004192 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004193
Chris Masone7a84562008-06-25 16:01:31 -04004194 if (level == 0)
4195 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004196 else {
4197 u64 blockptr = btrfs_node_blockptr(c, slot);
4198 u64 gen = btrfs_node_ptr_generation(c, slot);
4199
4200 if (cache_only) {
4201 struct extent_buffer *cur;
4202 cur = btrfs_find_tree_block(root, blockptr,
4203 btrfs_level_size(root, level - 1));
4204 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4205 slot++;
4206 if (cur)
4207 free_extent_buffer(cur);
4208 goto next;
4209 }
4210 free_extent_buffer(cur);
4211 }
4212 if (gen < min_trans) {
4213 slot++;
4214 goto next;
4215 }
Chris Masone7a84562008-06-25 16:01:31 -04004216 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004217 }
Chris Masone7a84562008-06-25 16:01:31 -04004218 return 0;
4219 }
4220 return 1;
4221}
4222
Chris Mason7bb86312007-12-11 09:25:06 -05004223/*
Chris Mason925baed2008-06-25 16:01:30 -04004224 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004225 * returns 0 if it found something or 1 if there are no greater leaves.
4226 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004227 */
Chris Mason234b63a2007-03-13 10:46:10 -04004228int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004229{
4230 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004231 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004232 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004233 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004234 struct btrfs_key key;
4235 u32 nritems;
4236 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004237 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004238 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004239
4240 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004241 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004242 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004243
Chris Mason8e73f272009-04-03 10:14:18 -04004244 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4245again:
4246 level = 1;
4247 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004248 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004249 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004250
Chris Masona2135012008-06-25 16:01:30 -04004251 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004252 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004253
Chris Mason925baed2008-06-25 16:01:30 -04004254 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4255 path->keep_locks = 0;
4256
4257 if (ret < 0)
4258 return ret;
4259
Chris Masona2135012008-06-25 16:01:30 -04004260 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004261 /*
4262 * by releasing the path above we dropped all our locks. A balance
4263 * could have added more items next to the key that used to be
4264 * at the very end of the block. So, check again here and
4265 * advance the path if there are now more items available.
4266 */
Chris Masona2135012008-06-25 16:01:30 -04004267 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004268 if (ret == 0)
4269 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004270 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004271 goto done;
4272 }
Chris Masond97e63b2007-02-20 16:40:44 -05004273
Chris Masond3977122009-01-05 21:25:51 -05004274 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004275 if (!path->nodes[level]) {
4276 ret = 1;
4277 goto done;
4278 }
Chris Mason5f39d392007-10-15 16:14:19 -04004279
Chris Masond97e63b2007-02-20 16:40:44 -05004280 slot = path->slots[level] + 1;
4281 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004282 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004283 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004284 if (level == BTRFS_MAX_LEVEL) {
4285 ret = 1;
4286 goto done;
4287 }
Chris Masond97e63b2007-02-20 16:40:44 -05004288 continue;
4289 }
Chris Mason5f39d392007-10-15 16:14:19 -04004290
Chris Mason925baed2008-06-25 16:01:30 -04004291 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004292 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004293 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004294 }
Chris Mason5f39d392007-10-15 16:14:19 -04004295
Chris Mason8e73f272009-04-03 10:14:18 -04004296 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004297 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004298 ret = read_block_for_search(NULL, root, path, &next, level,
4299 slot, &key);
4300 if (ret == -EAGAIN)
4301 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004302
Chris Mason76a05b32009-05-14 13:24:30 -04004303 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004304 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004305 goto done;
4306 }
4307
Chris Mason5cd57b22008-06-25 16:01:30 -04004308 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004309 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004310 if (!ret) {
4311 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004312 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004313 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004314 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004315 }
Chris Mason31533fb2011-07-26 16:01:59 -04004316 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004317 }
Chris Masond97e63b2007-02-20 16:40:44 -05004318 break;
4319 }
4320 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004321 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004322 level--;
4323 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004324 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004325 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004326
Chris Mason5f39d392007-10-15 16:14:19 -04004327 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004328 path->nodes[level] = next;
4329 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004330 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004331 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004332 if (!level)
4333 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004334
Chris Mason8e73f272009-04-03 10:14:18 -04004335 ret = read_block_for_search(NULL, root, path, &next, level,
4336 0, &key);
4337 if (ret == -EAGAIN)
4338 goto again;
4339
Chris Mason76a05b32009-05-14 13:24:30 -04004340 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004341 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004342 goto done;
4343 }
4344
Chris Mason5cd57b22008-06-25 16:01:30 -04004345 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004346 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004347 if (!ret) {
4348 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004349 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004350 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004351 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004352 }
Chris Mason31533fb2011-07-26 16:01:59 -04004353 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004354 }
Chris Masond97e63b2007-02-20 16:40:44 -05004355 }
Chris Mason8e73f272009-04-03 10:14:18 -04004356 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004357done:
4358 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004359 path->leave_spinning = old_spinning;
4360 if (!old_spinning)
4361 btrfs_set_path_blocking(path);
4362
4363 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004364}
Chris Mason0b86a832008-03-24 15:01:56 -04004365
Chris Mason3f157a22008-06-25 16:01:31 -04004366/*
4367 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4368 * searching until it gets past min_objectid or finds an item of 'type'
4369 *
4370 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4371 */
Chris Mason0b86a832008-03-24 15:01:56 -04004372int btrfs_previous_item(struct btrfs_root *root,
4373 struct btrfs_path *path, u64 min_objectid,
4374 int type)
4375{
4376 struct btrfs_key found_key;
4377 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004378 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004379 int ret;
4380
Chris Masond3977122009-01-05 21:25:51 -05004381 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004382 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004383 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004384 ret = btrfs_prev_leaf(root, path);
4385 if (ret != 0)
4386 return ret;
4387 } else {
4388 path->slots[0]--;
4389 }
4390 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004391 nritems = btrfs_header_nritems(leaf);
4392 if (nritems == 0)
4393 return 1;
4394 if (path->slots[0] == nritems)
4395 path->slots[0]--;
4396
Chris Mason0b86a832008-03-24 15:01:56 -04004397 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004398 if (found_key.objectid < min_objectid)
4399 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004400 if (found_key.type == type)
4401 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004402 if (found_key.objectid == min_objectid &&
4403 found_key.type < type)
4404 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004405 }
4406 return 1;
4407}