blob: 2543d11b7d8b421c7ae4dce1ff47b581ecaecb23 [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>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000040static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
41 int level, int slot);
Jan Schmidtf2304752012-05-26 11:43:17 +020042static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
43 struct extent_buffer *eb);
Eric Sandeen48a3b632013-04-25 20:41:01 +000044static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Masond97e63b2007-02-20 16:40:44 -050045
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050049 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040050 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040051}
52
Chris Masonb4ce94d2009-02-04 09:25:08 -050053/*
54 * set all locked nodes in the path to blocking locks. This should
55 * be done before scheduling
56 */
57noinline void btrfs_set_path_blocking(struct btrfs_path *p)
58{
59 int i;
60 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040061 if (!p->nodes[i] || !p->locks[i])
62 continue;
63 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
64 if (p->locks[i] == BTRFS_READ_LOCK)
65 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
66 else if (p->locks[i] == BTRFS_WRITE_LOCK)
67 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050068 }
69}
70
71/*
72 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050073 *
74 * held is used to keep lockdep happy, when lockdep is enabled
75 * we set held to a blocking lock before we go around and
76 * retake all the spinlocks in the path. You can safely use NULL
77 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050078 */
Chris Mason4008c042009-02-12 14:09:45 -050079noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040080 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050081{
82 int i;
Chris Mason4008c042009-02-12 14:09:45 -050083
84#ifdef CONFIG_DEBUG_LOCK_ALLOC
85 /* lockdep really cares that we take all of these spinlocks
86 * in the right order. If any of the locks in the path are not
87 * currently blocking, it is going to complain. So, make really
88 * really sure by forcing the path to blocking before we clear
89 * the path blocking.
90 */
Chris Masonbd681512011-07-16 15:23:14 -040091 if (held) {
92 btrfs_set_lock_blocking_rw(held, held_rw);
93 if (held_rw == BTRFS_WRITE_LOCK)
94 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
95 else if (held_rw == BTRFS_READ_LOCK)
96 held_rw = BTRFS_READ_LOCK_BLOCKING;
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098 btrfs_set_path_blocking(p);
99#endif
100
101 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400102 if (p->nodes[i] && p->locks[i]) {
103 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
104 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
105 p->locks[i] = BTRFS_WRITE_LOCK;
106 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
107 p->locks[i] = BTRFS_READ_LOCK;
108 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500109 }
Chris Mason4008c042009-02-12 14:09:45 -0500110
111#ifdef CONFIG_DEBUG_LOCK_ALLOC
112 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400113 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500114#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400118void btrfs_free_path(struct btrfs_path *p)
119{
Jesper Juhlff175d52010-12-25 21:22:30 +0000120 if (!p)
121 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200122 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400123 kmem_cache_free(btrfs_path_cachep, p);
124}
125
Chris Masond352ac62008-09-29 15:18:18 -0400126/*
127 * path release drops references on the extent buffers in the path
128 * and it drops any locks held by this path
129 *
130 * It is safe to call this on paths that no locks or extent buffers held.
131 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200132noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500133{
134 int i;
Chris Masona2135012008-06-25 16:01:30 -0400135
Chris Mason234b63a2007-03-13 10:46:10 -0400136 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400137 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400139 continue;
140 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400141 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400142 p->locks[i] = 0;
143 }
Chris Mason5f39d392007-10-15 16:14:19 -0400144 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400145 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500146 }
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/*
150 * safely gets a reference on the root node of a tree. A lock
151 * is not taken, so a concurrent writer may put a different node
152 * at the root of the tree. See btrfs_lock_root_node for the
153 * looping required.
154 *
155 * The extent buffer returned by this has a reference taken, so
156 * it won't disappear. It may stop being the root of the tree
157 * at any time because there are no locks held.
158 */
Chris Mason925baed2008-06-25 16:01:30 -0400159struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
160{
161 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400162
Josef Bacik3083ee22012-03-09 16:01:49 -0500163 while (1) {
164 rcu_read_lock();
165 eb = rcu_dereference(root->node);
166
167 /*
168 * RCU really hurts here, we could free up the root node because
169 * it was cow'ed but we may not get the new root node yet so do
170 * the inc_not_zero dance and if it doesn't work then
171 * synchronize_rcu and try again.
172 */
173 if (atomic_inc_not_zero(&eb->refs)) {
174 rcu_read_unlock();
175 break;
176 }
177 rcu_read_unlock();
178 synchronize_rcu();
179 }
Chris Mason925baed2008-06-25 16:01:30 -0400180 return eb;
181}
182
Chris Masond352ac62008-09-29 15:18:18 -0400183/* loop around taking references on and locking the root node of the
184 * tree until you end up with a lock on the root. A locked buffer
185 * is returned, with a reference held.
186 */
Chris Mason925baed2008-06-25 16:01:30 -0400187struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
188{
189 struct extent_buffer *eb;
190
Chris Masond3977122009-01-05 21:25:51 -0500191 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400192 eb = btrfs_root_node(root);
193 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400194 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400195 break;
Chris Mason925baed2008-06-25 16:01:30 -0400196 btrfs_tree_unlock(eb);
197 free_extent_buffer(eb);
198 }
199 return eb;
200}
201
Chris Masonbd681512011-07-16 15:23:14 -0400202/* loop around taking references on and locking the root node of the
203 * tree until you end up with a lock on the root. A locked buffer
204 * is returned, with a reference held.
205 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000206static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400207{
208 struct extent_buffer *eb;
209
210 while (1) {
211 eb = btrfs_root_node(root);
212 btrfs_tree_read_lock(eb);
213 if (eb == root->node)
214 break;
215 btrfs_tree_read_unlock(eb);
216 free_extent_buffer(eb);
217 }
218 return eb;
219}
220
Chris Masond352ac62008-09-29 15:18:18 -0400221/* cowonly root (everything not a reference counted cow subvolume), just get
222 * put onto a simple dirty list. transaction.c walks this to make sure they
223 * get properly updated on disk.
224 */
Chris Mason0b86a832008-03-24 15:01:56 -0400225static void add_root_to_dirty_list(struct btrfs_root *root)
226{
Chris Masone5846fc2012-05-03 12:08:48 -0400227 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400228 if (root->track_dirty && list_empty(&root->dirty_list)) {
229 list_add(&root->dirty_list,
230 &root->fs_info->dirty_cowonly_roots);
231 }
Chris Masone5846fc2012-05-03 12:08:48 -0400232 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400233}
234
Chris Masond352ac62008-09-29 15:18:18 -0400235/*
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
239 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500240int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
244{
245 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 int ret = 0;
247 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249
250 WARN_ON(root->ref_cows && trans->transid !=
251 root->fs_info->running_transaction->transid);
252 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
253
254 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 if (level == 0)
256 btrfs_item_key(buf, &disk_key, 0);
257 else
258 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400259
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
261 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200262 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 return PTR_ERR(cow);
265
266 copy_extent_buffer(cow, buf, 0, 0, cow->len);
267 btrfs_set_header_bytenr(cow, cow->start);
268 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
270 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
271 BTRFS_HEADER_FLAG_RELOC);
272 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
273 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
274 else
275 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500276
Yan Zheng2b820322008-11-17 21:11:30 -0500277 write_extent_buffer(cow, root->fs_info->fsid,
278 (unsigned long)btrfs_header_fsid(cow),
279 BTRFS_FSID_SIZE);
280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200283 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200285 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 if (ret)
288 return ret;
289
290 btrfs_mark_buffer_dirty(cow);
291 *cow_ret = cow;
292 return 0;
293}
294
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200295enum mod_log_op {
296 MOD_LOG_KEY_REPLACE,
297 MOD_LOG_KEY_ADD,
298 MOD_LOG_KEY_REMOVE,
299 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
300 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
301 MOD_LOG_MOVE_KEYS,
302 MOD_LOG_ROOT_REPLACE,
303};
304
305struct tree_mod_move {
306 int dst_slot;
307 int nr_items;
308};
309
310struct tree_mod_root {
311 u64 logical;
312 u8 level;
313};
314
315struct tree_mod_elem {
316 struct rb_node node;
317 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200318 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200319 enum mod_log_op op;
320
321 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
322 int slot;
323
324 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
325 u64 generation;
326
327 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
328 struct btrfs_disk_key key;
329 u64 blockptr;
330
331 /* this is used for op == MOD_LOG_MOVE_KEYS */
332 struct tree_mod_move move;
333
334 /* this is used for op == MOD_LOG_ROOT_REPLACE */
335 struct tree_mod_root old_root;
336};
337
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200339{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200340 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200341}
342
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200344{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200345 read_unlock(&fs_info->tree_mod_log_lock);
346}
347
348static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
349{
350 write_lock(&fs_info->tree_mod_log_lock);
351}
352
353static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
354{
355 write_unlock(&fs_info->tree_mod_log_lock);
356}
357
358/*
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000359 * Increment the upper half of tree_mod_seq, set lower half zero.
360 *
361 * Must be called with fs_info->tree_mod_seq_lock held.
362 */
363static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
364{
365 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
366 seq &= 0xffffffff00000000ull;
367 seq += 1ull << 32;
368 atomic64_set(&fs_info->tree_mod_seq, seq);
369 return seq;
370}
371
372/*
373 * Increment the lower half of tree_mod_seq.
374 *
375 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
376 * are generated should not technically require a spin lock here. (Rationale:
377 * incrementing the minor while incrementing the major seq number is between its
378 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
379 * just returns a unique sequence number as usual.) We have decided to leave
380 * that requirement in here and rethink it once we notice it really imposes a
381 * problem on some workload.
382 */
383static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
384{
385 return atomic64_inc_return(&fs_info->tree_mod_seq);
386}
387
388/*
389 * return the last minor in the previous major tree_mod_seq number
390 */
391u64 btrfs_tree_mod_seq_prev(u64 seq)
392{
393 return (seq & 0xffffffff00000000ull) - 1ull;
394}
395
396/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397 * This adds a new blocker to the tree mod log's blocker list if the @elem
398 * passed does not already have a sequence number set. So when a caller expects
399 * to record tree modifications, it should ensure to set elem->seq to zero
400 * before calling btrfs_get_tree_mod_seq.
401 * Returns a fresh, unused tree log modification sequence number, even if no new
402 * blocker was added.
403 */
404u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
405 struct seq_list *elem)
406{
407 u64 seq;
408
409 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200411 if (!elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000412 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200413 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
414 }
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000415 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200417 tree_mod_log_write_unlock(fs_info);
418
419 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420}
421
422void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
423 struct seq_list *elem)
424{
425 struct rb_root *tm_root;
426 struct rb_node *node;
427 struct rb_node *next;
428 struct seq_list *cur_elem;
429 struct tree_mod_elem *tm;
430 u64 min_seq = (u64)-1;
431 u64 seq_putting = elem->seq;
432
433 if (!seq_putting)
434 return;
435
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436 spin_lock(&fs_info->tree_mod_seq_lock);
437 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200438 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200439
440 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200441 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442 if (seq_putting > cur_elem->seq) {
443 /*
444 * blocker with lower sequence number exists, we
445 * cannot remove anything from the log
446 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200447 spin_unlock(&fs_info->tree_mod_seq_lock);
448 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200449 }
450 min_seq = cur_elem->seq;
451 }
452 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200453 spin_unlock(&fs_info->tree_mod_seq_lock);
454
455 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456 * anything that's lower than the lowest existing (read: blocked)
457 * sequence number can be removed from the tree.
458 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200459 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460 tm_root = &fs_info->tree_mod_log;
461 for (node = rb_first(tm_root); node; node = next) {
462 next = rb_next(node);
463 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200464 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 continue;
466 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200467 kfree(tm);
468 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200469 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200470}
471
472/*
473 * key order of the log:
474 * index -> sequence
475 *
476 * the index is the shifted logical of the *new* root node for root replace
477 * operations, or the shifted logical of the affected block for all other
478 * operations.
479 */
480static noinline int
481__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
482{
483 struct rb_root *tm_root;
484 struct rb_node **new;
485 struct rb_node *parent = NULL;
486 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200487
Jan Schmidt097b8a72012-06-21 11:08:04 +0200488 BUG_ON(!tm || !tm->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200489
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200490 tm_root = &fs_info->tree_mod_log;
491 new = &tm_root->rb_node;
492 while (*new) {
493 cur = container_of(*new, struct tree_mod_elem, node);
494 parent = *new;
495 if (cur->index < tm->index)
496 new = &((*new)->rb_left);
497 else if (cur->index > tm->index)
498 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200499 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200500 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200501 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200502 new = &((*new)->rb_right);
503 else {
504 kfree(tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200505 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200506 }
507 }
508
509 rb_link_node(&tm->node, parent, new);
510 rb_insert_color(&tm->node, tm_root);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200511 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200512}
513
Jan Schmidt097b8a72012-06-21 11:08:04 +0200514/*
515 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
516 * returns zero with the tree_mod_log_lock acquired. The caller must hold
517 * this until all tree mod log insertions are recorded in the rb tree and then
518 * call tree_mod_log_write_unlock() to release.
519 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200520static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
521 struct extent_buffer *eb) {
522 smp_mb();
523 if (list_empty(&(fs_info)->tree_mod_seq_list))
524 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200525 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200526 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200527
528 tree_mod_log_write_lock(fs_info);
529 if (list_empty(&fs_info->tree_mod_seq_list)) {
530 /*
531 * someone emptied the list while we were waiting for the lock.
532 * we must not add to the list when no blocker exists.
533 */
534 tree_mod_log_write_unlock(fs_info);
535 return 1;
536 }
537
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200538 return 0;
539}
540
Jan Schmidt3310c362012-06-11 10:52:38 +0200541/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200542 * This allocates memory and gets a tree modification sequence number.
Jan Schmidt3310c362012-06-11 10:52:38 +0200543 *
Jan Schmidt097b8a72012-06-21 11:08:04 +0200544 * Returns <0 on error.
545 * Returns >0 (the added sequence number) on success.
Jan Schmidt3310c362012-06-11 10:52:38 +0200546 */
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200547static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
548 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200549{
550 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200551
Jan Schmidt097b8a72012-06-21 11:08:04 +0200552 /*
553 * once we switch from spin locks to something different, we should
554 * honor the flags parameter here.
555 */
556 tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200557 if (!tm)
558 return -ENOMEM;
559
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000560 spin_lock(&fs_info->tree_mod_seq_lock);
561 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
562 spin_unlock(&fs_info->tree_mod_seq_lock);
563
Jan Schmidt097b8a72012-06-21 11:08:04 +0200564 return tm->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200565}
566
Jan Schmidt097b8a72012-06-21 11:08:04 +0200567static inline int
568__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
569 struct extent_buffer *eb, int slot,
570 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200571{
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200572 int ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200573 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200574
575 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200576 if (ret < 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200577 return ret;
578
579 tm->index = eb->start >> PAGE_CACHE_SHIFT;
580 if (op != MOD_LOG_KEY_ADD) {
581 btrfs_node_key(eb, &tm->key, slot);
582 tm->blockptr = btrfs_node_blockptr(eb, slot);
583 }
584 tm->op = op;
585 tm->slot = slot;
586 tm->generation = btrfs_node_ptr_generation(eb, slot);
587
Jan Schmidt097b8a72012-06-21 11:08:04 +0200588 return __tree_mod_log_insert(fs_info, tm);
589}
590
591static noinline int
592tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
593 struct extent_buffer *eb, int slot,
594 enum mod_log_op op, gfp_t flags)
595{
596 int ret;
597
598 if (tree_mod_dont_log(fs_info, eb))
599 return 0;
600
601 ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
602
603 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200604 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200605}
606
607static noinline int
608tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
609 int slot, enum mod_log_op op)
610{
611 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
612}
613
614static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200615tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info,
616 struct extent_buffer *eb, int slot,
617 enum mod_log_op op)
618{
619 return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS);
620}
621
622static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200623tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
624 struct extent_buffer *eb, int dst_slot, int src_slot,
625 int nr_items, gfp_t flags)
626{
627 struct tree_mod_elem *tm;
628 int ret;
629 int i;
630
Jan Schmidtf3956942012-05-31 15:02:32 +0200631 if (tree_mod_dont_log(fs_info, eb))
632 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200633
Jan Schmidt01763a22012-10-23 15:02:12 +0200634 /*
635 * When we override something during the move, we log these removals.
636 * This can only happen when we move towards the beginning of the
637 * buffer, i.e. dst_slot < src_slot.
638 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200639 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200640 ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200641 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
642 BUG_ON(ret < 0);
643 }
644
Jan Schmidtf3956942012-05-31 15:02:32 +0200645 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200646 if (ret < 0)
647 goto out;
Jan Schmidtf3956942012-05-31 15:02:32 +0200648
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200649 tm->index = eb->start >> PAGE_CACHE_SHIFT;
650 tm->slot = src_slot;
651 tm->move.dst_slot = dst_slot;
652 tm->move.nr_items = nr_items;
653 tm->op = MOD_LOG_MOVE_KEYS;
654
Jan Schmidt3310c362012-06-11 10:52:38 +0200655 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200656out:
657 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200658 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200659}
660
Jan Schmidt097b8a72012-06-21 11:08:04 +0200661static inline void
662__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
663{
664 int i;
665 u32 nritems;
666 int ret;
667
Chris Masonb12a3b12012-08-07 15:34:49 -0400668 if (btrfs_header_level(eb) == 0)
669 return;
670
Jan Schmidt097b8a72012-06-21 11:08:04 +0200671 nritems = btrfs_header_nritems(eb);
672 for (i = nritems - 1; i >= 0; i--) {
673 ret = tree_mod_log_insert_key_locked(fs_info, eb, i,
674 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
675 BUG_ON(ret < 0);
676 }
677}
678
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200679static noinline int
680tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
681 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000682 struct extent_buffer *new_root, gfp_t flags,
683 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200684{
685 struct tree_mod_elem *tm;
686 int ret;
687
Jan Schmidt097b8a72012-06-21 11:08:04 +0200688 if (tree_mod_dont_log(fs_info, NULL))
689 return 0;
690
Jan Schmidt90f8d622013-04-13 13:19:53 +0000691 if (log_removal)
692 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000693
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200695 if (ret < 0)
696 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697
698 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
699 tm->old_root.logical = old_root->start;
700 tm->old_root.level = btrfs_header_level(old_root);
701 tm->generation = btrfs_header_generation(old_root);
702 tm->op = MOD_LOG_ROOT_REPLACE;
703
Jan Schmidt3310c362012-06-11 10:52:38 +0200704 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200705out:
706 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200707 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708}
709
710static struct tree_mod_elem *
711__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
712 int smallest)
713{
714 struct rb_root *tm_root;
715 struct rb_node *node;
716 struct tree_mod_elem *cur = NULL;
717 struct tree_mod_elem *found = NULL;
718 u64 index = start >> PAGE_CACHE_SHIFT;
719
Jan Schmidt097b8a72012-06-21 11:08:04 +0200720 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200721 tm_root = &fs_info->tree_mod_log;
722 node = tm_root->rb_node;
723 while (node) {
724 cur = container_of(node, struct tree_mod_elem, node);
725 if (cur->index < index) {
726 node = node->rb_left;
727 } else if (cur->index > index) {
728 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200729 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200730 node = node->rb_left;
731 } else if (!smallest) {
732 /* we want the node with the highest seq */
733 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200734 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735 found = cur;
736 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200737 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200738 /* we want the node with the smallest seq */
739 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200740 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200741 found = cur;
742 node = node->rb_right;
743 } else {
744 found = cur;
745 break;
746 }
747 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200748 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200749
750 return found;
751}
752
753/*
754 * this returns the element from the log with the smallest time sequence
755 * value that's in the log (the oldest log item). any element with a time
756 * sequence lower than min_seq will be ignored.
757 */
758static struct tree_mod_elem *
759tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
760 u64 min_seq)
761{
762 return __tree_mod_log_search(fs_info, start, min_seq, 1);
763}
764
765/*
766 * this returns the element from the log with the largest time sequence
767 * value that's in the log (the most recent log item). any element with
768 * a time sequence lower than min_seq will be ignored.
769 */
770static struct tree_mod_elem *
771tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
772{
773 return __tree_mod_log_search(fs_info, start, min_seq, 0);
774}
775
Jan Schmidt097b8a72012-06-21 11:08:04 +0200776static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200777tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
778 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000779 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200780{
781 int ret;
782 int i;
783
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200784 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200785 return;
786
Jan Schmidt097b8a72012-06-21 11:08:04 +0200787 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
788 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200790 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200791
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792 for (i = 0; i < nr_items; i++) {
Jan Schmidt90f8d622013-04-13 13:19:53 +0000793 ret = tree_mod_log_insert_key_locked(fs_info, src,
794 i + src_offset,
795 MOD_LOG_KEY_REMOVE);
796 BUG_ON(ret < 0);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200797 ret = tree_mod_log_insert_key_locked(fs_info, dst,
798 i + dst_offset,
799 MOD_LOG_KEY_ADD);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 BUG_ON(ret < 0);
801 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200802
803 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200804}
805
806static inline void
807tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
808 int dst_offset, int src_offset, int nr_items)
809{
810 int ret;
811 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
812 nr_items, GFP_NOFS);
813 BUG_ON(ret < 0);
814}
815
Jan Schmidt097b8a72012-06-21 11:08:04 +0200816static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200817tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000818 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200819{
820 int ret;
821
822 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
823 MOD_LOG_KEY_REPLACE,
824 atomic ? GFP_ATOMIC : GFP_NOFS);
825 BUG_ON(ret < 0);
826}
827
Jan Schmidt097b8a72012-06-21 11:08:04 +0200828static noinline void
829tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200830{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200831 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200832 return;
833
Jan Schmidt097b8a72012-06-21 11:08:04 +0200834 __tree_mod_log_free_eb(fs_info, eb);
835
836 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200837}
838
Jan Schmidt097b8a72012-06-21 11:08:04 +0200839static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200840tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000841 struct extent_buffer *new_root_node,
842 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200843{
844 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200845 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000846 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200847 BUG_ON(ret < 0);
848}
849
Chris Masond352ac62008-09-29 15:18:18 -0400850/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851 * check if the tree block can be shared by multiple trees
852 */
853int btrfs_block_can_be_shared(struct btrfs_root *root,
854 struct extent_buffer *buf)
855{
856 /*
857 * Tree blocks not in refernece counted trees and tree roots
858 * are never shared. If a block was allocated after the last
859 * snapshot and the block was not allocated by tree relocation,
860 * we know the block is not shared.
861 */
862 if (root->ref_cows &&
863 buf != root->node && buf != root->commit_root &&
864 (btrfs_header_generation(buf) <=
865 btrfs_root_last_snapshot(&root->root_item) ||
866 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
867 return 1;
868#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
869 if (root->ref_cows &&
870 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
871 return 1;
872#endif
873 return 0;
874}
875
876static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
877 struct btrfs_root *root,
878 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400879 struct extent_buffer *cow,
880 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881{
882 u64 refs;
883 u64 owner;
884 u64 flags;
885 u64 new_flags = 0;
886 int ret;
887
888 /*
889 * Backrefs update rules:
890 *
891 * Always use full backrefs for extent pointers in tree block
892 * allocated by tree relocation.
893 *
894 * If a shared tree block is no longer referenced by its owner
895 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
896 * use full backrefs for extent pointers in tree block.
897 *
898 * If a tree block is been relocating
899 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
900 * use full backrefs for extent pointers in tree block.
901 * The reason for this is some operations (such as drop tree)
902 * are only allowed for blocks use full backrefs.
903 */
904
905 if (btrfs_block_can_be_shared(root, buf)) {
906 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500907 btrfs_header_level(buf), 1,
908 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700909 if (ret)
910 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700911 if (refs == 0) {
912 ret = -EROFS;
913 btrfs_std_error(root->fs_info, ret);
914 return ret;
915 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 } else {
917 refs = 1;
918 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
919 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
920 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
921 else
922 flags = 0;
923 }
924
925 owner = btrfs_header_owner(buf);
926 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
927 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
928
929 if (refs > 1) {
930 if ((owner == root->root_key.objectid ||
931 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
932 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200933 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935
936 if (root->root_key.objectid ==
937 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200938 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100939 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200940 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100941 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942 }
943 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
944 } else {
945
946 if (root->root_key.objectid ==
947 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200948 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200950 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100951 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 }
953 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400954 int level = btrfs_header_level(buf);
955
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 ret = btrfs_set_disk_extent_flags(trans, root,
957 buf->start,
958 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400959 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700960 if (ret)
961 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 }
963 } else {
964 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
965 if (root->root_key.objectid ==
966 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200967 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200969 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200971 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100972 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 }
974 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400975 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976 }
977 return 0;
978}
979
980/*
Chris Masond3977122009-01-05 21:25:51 -0500981 * does the dirty work in cow of a single block. The parent block (if
982 * supplied) is updated to point to the new cow copy. The new buffer is marked
983 * dirty and returned locked. If you modify the block it needs to be marked
984 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400985 *
986 * search_start -- an allocation hint for the new block
987 *
Chris Masond3977122009-01-05 21:25:51 -0500988 * empty_size -- a hint that you plan on doing more cow. This is the size in
989 * bytes the allocator should try to find free next to the block it returns.
990 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400991 */
Chris Masond3977122009-01-05 21:25:51 -0500992static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400993 struct btrfs_root *root,
994 struct extent_buffer *buf,
995 struct extent_buffer *parent, int parent_slot,
996 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400997 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400998{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001001 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001002 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001003 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001005
Chris Mason925baed2008-06-25 16:01:30 -04001006 if (*cow_ret == buf)
1007 unlock_orig = 1;
1008
Chris Masonb9447ef2009-03-09 11:45:38 -04001009 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001010
Chris Mason7bb86312007-12-11 09:25:06 -05001011 WARN_ON(root->ref_cows && trans->transid !=
1012 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001013 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001014
Chris Mason7bb86312007-12-11 09:25:06 -05001015 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 if (level == 0)
1018 btrfs_item_key(buf, &disk_key, 0);
1019 else
1020 btrfs_node_key(buf, &disk_key, 0);
1021
1022 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1023 if (parent)
1024 parent_start = parent->start;
1025 else
1026 parent_start = 0;
1027 } else
1028 parent_start = 0;
1029
1030 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1031 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001032 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001033 if (IS_ERR(cow))
1034 return PTR_ERR(cow);
1035
Chris Masonb4ce94d2009-02-04 09:25:08 -05001036 /* cow is set to blocking by btrfs_init_new_buffer */
1037
Chris Mason5f39d392007-10-15 16:14:19 -04001038 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001039 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001040 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001041 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1042 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1043 BTRFS_HEADER_FLAG_RELOC);
1044 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1045 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1046 else
1047 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001048
Yan Zheng2b820322008-11-17 21:11:30 -05001049 write_extent_buffer(cow, root->fs_info->fsid,
1050 (unsigned long)btrfs_header_fsid(cow),
1051 BTRFS_FSID_SIZE);
1052
Mark Fashehbe1a5562011-08-08 13:20:18 -07001053 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001054 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001055 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001056 return ret;
1057 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001058
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001059 if (root->ref_cows)
1060 btrfs_reloc_cow_block(trans, root, buf, cow);
1061
Chris Mason6702ed42007-08-07 16:15:09 -04001062 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001063 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1065 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1066 parent_start = buf->start;
1067 else
1068 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001069
Chris Mason5f39d392007-10-15 16:14:19 -04001070 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001071 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001072 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001073
Yan, Zhengf0486c62010-05-16 10:46:25 -04001074 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001075 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001076 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001077 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001078 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001079 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1080 parent_start = parent->start;
1081 else
1082 parent_start = 0;
1083
1084 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001085 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1086 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001087 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001088 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001089 btrfs_set_node_ptr_generation(parent, parent_slot,
1090 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001091 btrfs_mark_buffer_dirty(parent);
Jan Schmidtd9abbf12013-03-20 13:49:48 +00001092 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001093 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001094 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001095 }
Chris Mason925baed2008-06-25 16:01:30 -04001096 if (unlock_orig)
1097 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001098 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001099 btrfs_mark_buffer_dirty(cow);
1100 *cow_ret = cow;
1101 return 0;
1102}
1103
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001104/*
1105 * returns the logical address of the oldest predecessor of the given root.
1106 * entries older than time_seq are ignored.
1107 */
1108static struct tree_mod_elem *
1109__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001110 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001111{
1112 struct tree_mod_elem *tm;
1113 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001114 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001115 int looped = 0;
1116
1117 if (!time_seq)
1118 return 0;
1119
1120 /*
1121 * the very last operation that's logged for a root is the replacement
1122 * operation (if it is replaced at all). this has the index of the *new*
1123 * root, making it the very first operation that's logged for this root.
1124 */
1125 while (1) {
1126 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1127 time_seq);
1128 if (!looped && !tm)
1129 return 0;
1130 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001131 * if there are no tree operation for the oldest root, we simply
1132 * return it. this should only happen if that (old) root is at
1133 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001134 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001135 if (!tm)
1136 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001137
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001138 /*
1139 * if there's an operation that's not a root replacement, we
1140 * found the oldest version of our root. normally, we'll find a
1141 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1142 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001143 if (tm->op != MOD_LOG_ROOT_REPLACE)
1144 break;
1145
1146 found = tm;
1147 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001148 looped = 1;
1149 }
1150
Jan Schmidta95236d2012-06-05 16:41:24 +02001151 /* if there's no old root to return, return what we found instead */
1152 if (!found)
1153 found = tm;
1154
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 return found;
1156}
1157
1158/*
1159 * tm is a pointer to the first operation to rewind within eb. then, all
1160 * previous operations will be rewinded (until we reach something older than
1161 * time_seq).
1162 */
1163static void
Josef Bacikc7013432013-06-29 23:15:19 -04001164__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1165 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001166{
1167 u32 n;
1168 struct rb_node *next;
1169 struct tree_mod_elem *tm = first_tm;
1170 unsigned long o_dst;
1171 unsigned long o_src;
1172 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1173
1174 n = btrfs_header_nritems(eb);
Josef Bacikc7013432013-06-29 23:15:19 -04001175 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001176 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001177 /*
1178 * all the operations are recorded with the operator used for
1179 * the modification. as we're going backwards, we do the
1180 * opposite of each operation here.
1181 */
1182 switch (tm->op) {
1183 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1184 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001185 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001186 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001187 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001188 btrfs_set_node_key(eb, &tm->key, tm->slot);
1189 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1190 btrfs_set_node_ptr_generation(eb, tm->slot,
1191 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001192 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001193 break;
1194 case MOD_LOG_KEY_REPLACE:
1195 BUG_ON(tm->slot >= n);
1196 btrfs_set_node_key(eb, &tm->key, tm->slot);
1197 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1198 btrfs_set_node_ptr_generation(eb, tm->slot,
1199 tm->generation);
1200 break;
1201 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001202 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001203 n--;
1204 break;
1205 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001206 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1207 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1208 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001209 tm->move.nr_items * p_size);
1210 break;
1211 case MOD_LOG_ROOT_REPLACE:
1212 /*
1213 * this operation is special. for roots, this must be
1214 * handled explicitly before rewinding.
1215 * for non-roots, this operation may exist if the node
1216 * was a root: root A -> child B; then A gets empty and
1217 * B is promoted to the new root. in the mod log, we'll
1218 * have a root-replace operation for B, a tree block
1219 * that is no root. we simply ignore that operation.
1220 */
1221 break;
1222 }
1223 next = rb_next(&tm->node);
1224 if (!next)
1225 break;
1226 tm = container_of(next, struct tree_mod_elem, node);
1227 if (tm->index != first_tm->index)
1228 break;
1229 }
Josef Bacikc7013432013-06-29 23:15:19 -04001230 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001231 btrfs_set_header_nritems(eb, n);
1232}
1233
Jan Schmidt47fb0912013-04-13 13:19:55 +00001234/*
1235 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1236 * is returned. If rewind operations happen, a fresh buffer is returned. The
1237 * returned buffer is always read-locked. If the returned buffer is not the
1238 * input buffer, the lock on the input buffer is released and the input buffer
1239 * is freed (its refcount is decremented).
1240 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001241static struct extent_buffer *
1242tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1243 u64 time_seq)
1244{
1245 struct extent_buffer *eb_rewin;
1246 struct tree_mod_elem *tm;
1247
1248 if (!time_seq)
1249 return eb;
1250
1251 if (btrfs_header_level(eb) == 0)
1252 return eb;
1253
1254 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1255 if (!tm)
1256 return eb;
1257
1258 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1259 BUG_ON(tm->slot != 0);
1260 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1261 fs_info->tree_root->nodesize);
1262 BUG_ON(!eb_rewin);
1263 btrfs_set_header_bytenr(eb_rewin, eb->start);
1264 btrfs_set_header_backref_rev(eb_rewin,
1265 btrfs_header_backref_rev(eb));
1266 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001267 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268 } else {
1269 eb_rewin = btrfs_clone_extent_buffer(eb);
1270 BUG_ON(!eb_rewin);
1271 }
1272
1273 extent_buffer_get(eb_rewin);
Jan Schmidt47fb0912013-04-13 13:19:55 +00001274 btrfs_tree_read_unlock(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001275 free_extent_buffer(eb);
1276
Jan Schmidt47fb0912013-04-13 13:19:55 +00001277 extent_buffer_get(eb_rewin);
1278 btrfs_tree_read_lock(eb_rewin);
Josef Bacikc7013432013-06-29 23:15:19 -04001279 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001280 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001281 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001282
1283 return eb_rewin;
1284}
1285
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001286/*
1287 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1288 * value. If there are no changes, the current root->root_node is returned. If
1289 * anything changed in between, there's a fresh buffer allocated on which the
1290 * rewind operations are done. In any case, the returned buffer is read locked.
1291 * Returns NULL on error (with no locks held).
1292 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001293static inline struct extent_buffer *
1294get_old_root(struct btrfs_root *root, u64 time_seq)
1295{
1296 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001297 struct extent_buffer *eb = NULL;
1298 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001299 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001300 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001301 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001302 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001303 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001304
Jan Schmidt30b04632013-04-13 13:19:54 +00001305 eb_root = btrfs_read_lock_root_node(root);
1306 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001308 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001309
Jan Schmidta95236d2012-06-05 16:41:24 +02001310 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1311 old_root = &tm->old_root;
1312 old_generation = tm->generation;
1313 logical = old_root->logical;
1314 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001315 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001316 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001317
Jan Schmidta95236d2012-06-05 16:41:24 +02001318 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001319 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001320 btrfs_tree_read_unlock(eb_root);
1321 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001322 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001323 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001324 if (!old || !extent_buffer_uptodate(old)) {
1325 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001326 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1327 logical);
1328 WARN_ON(1);
1329 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001330 eb = btrfs_clone_extent_buffer(old);
1331 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001332 }
1333 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001334 btrfs_tree_read_unlock(eb_root);
1335 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001336 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001337 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001338 eb = btrfs_clone_extent_buffer(eb_root);
1339 btrfs_tree_read_unlock(eb_root);
1340 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001341 }
1342
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001343 if (!eb)
1344 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001345 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001346 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001347 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001348 btrfs_set_header_bytenr(eb, eb->start);
1349 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001350 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001351 btrfs_set_header_level(eb, old_root->level);
1352 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001353 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001354 if (tm)
Josef Bacikc7013432013-06-29 23:15:19 -04001355 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001356 else
1357 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001358 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001359
1360 return eb;
1361}
1362
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001363int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1364{
1365 struct tree_mod_elem *tm;
1366 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001367 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001368
Jan Schmidt30b04632013-04-13 13:19:54 +00001369 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001370 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1371 level = tm->old_root.level;
1372 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001373 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001374 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001375 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001376
1377 return level;
1378}
1379
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001380static inline int should_cow_block(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root,
1382 struct extent_buffer *buf)
1383{
Liu Bof1ebcc72011-11-14 20:48:06 -05001384 /* ensure we can see the force_cow */
1385 smp_rmb();
1386
1387 /*
1388 * We do not need to cow a block if
1389 * 1) this block is not created or changed in this transaction;
1390 * 2) this block does not belong to TREE_RELOC tree;
1391 * 3) the root is not forced COW.
1392 *
1393 * What is forced COW:
1394 * when we create snapshot during commiting the transaction,
1395 * after we've finished coping src root, we must COW the shared
1396 * block to ensure the metadata consistency.
1397 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001398 if (btrfs_header_generation(buf) == trans->transid &&
1399 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1400 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001401 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1402 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001403 return 0;
1404 return 1;
1405}
1406
Chris Masond352ac62008-09-29 15:18:18 -04001407/*
1408 * cows a single block, see __btrfs_cow_block for the real work.
1409 * This version of it has extra checks so that a block isn't cow'd more than
1410 * once per transaction, as long as it hasn't been written yet
1411 */
Chris Masond3977122009-01-05 21:25:51 -05001412noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001413 struct btrfs_root *root, struct extent_buffer *buf,
1414 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001415 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001416{
Chris Mason6702ed42007-08-07 16:15:09 -04001417 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001418 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001419
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001420 if (trans->transaction != root->fs_info->running_transaction)
1421 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001422 (unsigned long long)trans->transid,
1423 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001424 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001425
1426 if (trans->transid != root->fs_info->generation)
1427 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001428 (unsigned long long)trans->transid,
1429 (unsigned long long)root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001431 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001432 *cow_ret = buf;
1433 return 0;
1434 }
Chris Masonc4876852009-02-04 09:24:25 -05001435
Chris Mason0b86a832008-03-24 15:01:56 -04001436 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001437
1438 if (parent)
1439 btrfs_set_lock_blocking(parent);
1440 btrfs_set_lock_blocking(buf);
1441
Chris Masonf510cfe2007-10-15 16:14:48 -04001442 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001443 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001444
1445 trace_btrfs_cow_block(root, buf, *cow_ret);
1446
Chris Masonf510cfe2007-10-15 16:14:48 -04001447 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001448}
1449
Chris Masond352ac62008-09-29 15:18:18 -04001450/*
1451 * helper function for defrag to decide if two blocks pointed to by a
1452 * node are actually close by
1453 */
Chris Mason6b800532007-10-15 16:17:34 -04001454static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001455{
Chris Mason6b800532007-10-15 16:17:34 -04001456 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001457 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001458 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001459 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001460 return 0;
1461}
1462
Chris Mason081e9572007-11-06 10:26:24 -05001463/*
1464 * compare two keys in a memcmp fashion
1465 */
1466static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1467{
1468 struct btrfs_key k1;
1469
1470 btrfs_disk_key_to_cpu(&k1, disk);
1471
Diego Calleja20736ab2009-07-24 11:06:52 -04001472 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001473}
1474
Josef Bacikf3465ca2008-11-12 14:19:50 -05001475/*
1476 * same as comp_keys only with two btrfs_key's
1477 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001478int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001479{
1480 if (k1->objectid > k2->objectid)
1481 return 1;
1482 if (k1->objectid < k2->objectid)
1483 return -1;
1484 if (k1->type > k2->type)
1485 return 1;
1486 if (k1->type < k2->type)
1487 return -1;
1488 if (k1->offset > k2->offset)
1489 return 1;
1490 if (k1->offset < k2->offset)
1491 return -1;
1492 return 0;
1493}
Chris Mason081e9572007-11-06 10:26:24 -05001494
Chris Masond352ac62008-09-29 15:18:18 -04001495/*
1496 * this is used by the defrag code to go through all the
1497 * leaves pointed to by a node and reallocate them so that
1498 * disk order is close to key order
1499 */
Chris Mason6702ed42007-08-07 16:15:09 -04001500int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001501 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001502 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001503 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001504{
Chris Mason6b800532007-10-15 16:17:34 -04001505 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001506 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001507 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001508 u64 search_start = *last_ret;
1509 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001510 u64 other;
1511 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001512 int end_slot;
1513 int i;
1514 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001515 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001516 int uptodate;
1517 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001518 int progress_passed = 0;
1519 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001520
Chris Mason5708b952007-10-25 15:43:18 -04001521 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001522
Julia Lawall6c1500f2012-11-03 20:30:18 +00001523 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1524 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001525
Chris Mason6b800532007-10-15 16:17:34 -04001526 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001527 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001528 end_slot = parent_nritems;
1529
1530 if (parent_nritems == 1)
1531 return 0;
1532
Chris Masonb4ce94d2009-02-04 09:25:08 -05001533 btrfs_set_lock_blocking(parent);
1534
Chris Mason6702ed42007-08-07 16:15:09 -04001535 for (i = start_slot; i < end_slot; i++) {
1536 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001537
Chris Mason081e9572007-11-06 10:26:24 -05001538 btrfs_node_key(parent, &disk_key, i);
1539 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1540 continue;
1541
1542 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001543 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001544 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001545 if (last_block == 0)
1546 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001547
Chris Mason6702ed42007-08-07 16:15:09 -04001548 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001549 other = btrfs_node_blockptr(parent, i - 1);
1550 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001551 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001552 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001553 other = btrfs_node_blockptr(parent, i + 1);
1554 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001555 }
Chris Masone9d0b132007-08-10 14:06:19 -04001556 if (close) {
1557 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001558 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001559 }
Chris Mason6702ed42007-08-07 16:15:09 -04001560
Chris Mason6b800532007-10-15 16:17:34 -04001561 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1562 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001563 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001564 else
1565 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001566 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001567 if (!cur) {
1568 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001569 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001570 if (!cur || !extent_buffer_uptodate(cur)) {
1571 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001572 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001573 }
Chris Mason6b800532007-10-15 16:17:34 -04001574 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001575 err = btrfs_read_buffer(cur, gen);
1576 if (err) {
1577 free_extent_buffer(cur);
1578 return err;
1579 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001580 }
Chris Mason6702ed42007-08-07 16:15:09 -04001581 }
Chris Masone9d0b132007-08-10 14:06:19 -04001582 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001583 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001584
Chris Masone7a84562008-06-25 16:01:31 -04001585 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001586 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001587 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001588 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001589 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001590 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001591 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001592 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001593 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001594 break;
Yan252c38f2007-08-29 09:11:44 -04001595 }
Chris Masone7a84562008-06-25 16:01:31 -04001596 search_start = cur->start;
1597 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001598 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001599 btrfs_tree_unlock(cur);
1600 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001601 }
1602 return err;
1603}
1604
Chris Mason74123bd2007-02-02 11:05:29 -05001605/*
1606 * The leaf data grows from end-to-front in the node.
1607 * this returns the address of the start of the last item,
1608 * which is the stop of the leaf data stack
1609 */
Chris Mason123abc82007-03-14 14:14:43 -04001610static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001611 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612{
Chris Mason5f39d392007-10-15 16:14:19 -04001613 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001614 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001615 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001616 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001617}
1618
Chris Masonaa5d6be2007-02-28 16:35:06 -05001619
Chris Mason74123bd2007-02-02 11:05:29 -05001620/*
Chris Mason5f39d392007-10-15 16:14:19 -04001621 * search for key in the extent_buffer. The items start at offset p,
1622 * and they are item_size apart. There are 'max' items in p.
1623 *
Chris Mason74123bd2007-02-02 11:05:29 -05001624 * the slot in the array is returned via slot, and it points to
1625 * the place where you would insert key if it is not found in
1626 * the array.
1627 *
1628 * slot may point to max if the key is bigger than all of the keys
1629 */
Chris Masone02119d2008-09-05 16:13:11 -04001630static noinline int generic_bin_search(struct extent_buffer *eb,
1631 unsigned long p,
1632 int item_size, struct btrfs_key *key,
1633 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001634{
1635 int low = 0;
1636 int high = max;
1637 int mid;
1638 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001639 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001640 struct btrfs_disk_key unaligned;
1641 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001642 char *kaddr = NULL;
1643 unsigned long map_start = 0;
1644 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001645 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001646
Chris Masond3977122009-01-05 21:25:51 -05001647 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001648 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001649 offset = p + mid * item_size;
1650
Chris Masona6591712011-07-19 12:04:14 -04001651 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001652 (offset + sizeof(struct btrfs_disk_key)) >
1653 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001654
1655 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001656 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001657 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001658
Chris Mason479965d2007-10-15 16:14:27 -04001659 if (!err) {
1660 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1661 map_start);
1662 } else {
1663 read_extent_buffer(eb, &unaligned,
1664 offset, sizeof(unaligned));
1665 tmp = &unaligned;
1666 }
1667
Chris Mason5f39d392007-10-15 16:14:19 -04001668 } else {
1669 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1670 map_start);
1671 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001672 ret = comp_keys(tmp, key);
1673
1674 if (ret < 0)
1675 low = mid + 1;
1676 else if (ret > 0)
1677 high = mid;
1678 else {
1679 *slot = mid;
1680 return 0;
1681 }
1682 }
1683 *slot = low;
1684 return 1;
1685}
1686
Chris Mason97571fd2007-02-24 13:39:08 -05001687/*
1688 * simple bin_search frontend that does the right thing for
1689 * leaves vs nodes
1690 */
Chris Mason5f39d392007-10-15 16:14:19 -04001691static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1692 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001693{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001694 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001695 return generic_bin_search(eb,
1696 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001697 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001698 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001699 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001700 else
Chris Mason5f39d392007-10-15 16:14:19 -04001701 return generic_bin_search(eb,
1702 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001703 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001704 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001705 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001706}
1707
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001708int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1709 int level, int *slot)
1710{
1711 return bin_search(eb, key, level, slot);
1712}
1713
Yan, Zhengf0486c62010-05-16 10:46:25 -04001714static void root_add_used(struct btrfs_root *root, u32 size)
1715{
1716 spin_lock(&root->accounting_lock);
1717 btrfs_set_root_used(&root->root_item,
1718 btrfs_root_used(&root->root_item) + size);
1719 spin_unlock(&root->accounting_lock);
1720}
1721
1722static void root_sub_used(struct btrfs_root *root, u32 size)
1723{
1724 spin_lock(&root->accounting_lock);
1725 btrfs_set_root_used(&root->root_item,
1726 btrfs_root_used(&root->root_item) - size);
1727 spin_unlock(&root->accounting_lock);
1728}
1729
Chris Masond352ac62008-09-29 15:18:18 -04001730/* given a node and slot number, this reads the blocks it points to. The
1731 * extent buffer is returned with a reference taken (but unlocked).
1732 * NULL is returned on error.
1733 */
Chris Masone02119d2008-09-05 16:13:11 -04001734static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001735 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001736{
Chris Masonca7a79a2008-05-12 12:59:19 -04001737 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001738 struct extent_buffer *eb;
1739
Chris Masonbb803952007-03-01 12:04:21 -05001740 if (slot < 0)
1741 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001742 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001743 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001744
1745 BUG_ON(level == 0);
1746
Josef Bacik416bc652013-04-23 14:17:42 -04001747 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1748 btrfs_level_size(root, level - 1),
1749 btrfs_node_ptr_generation(parent, slot));
1750 if (eb && !extent_buffer_uptodate(eb)) {
1751 free_extent_buffer(eb);
1752 eb = NULL;
1753 }
1754
1755 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001756}
1757
Chris Masond352ac62008-09-29 15:18:18 -04001758/*
1759 * node level balancing, used to make sure nodes are in proper order for
1760 * item deletion. We balance from the top down, so we have to make sure
1761 * that a deletion won't leave an node completely empty later on.
1762 */
Chris Masone02119d2008-09-05 16:13:11 -04001763static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001764 struct btrfs_root *root,
1765 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001766{
Chris Mason5f39d392007-10-15 16:14:19 -04001767 struct extent_buffer *right = NULL;
1768 struct extent_buffer *mid;
1769 struct extent_buffer *left = NULL;
1770 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001771 int ret = 0;
1772 int wret;
1773 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001774 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001775 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001776
1777 if (level == 0)
1778 return 0;
1779
Chris Mason5f39d392007-10-15 16:14:19 -04001780 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001781
Chris Masonbd681512011-07-16 15:23:14 -04001782 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1783 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001784 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1785
Chris Mason1d4f8a02007-03-13 09:28:32 -04001786 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001787
Li Zefana05a9bb2011-09-06 16:55:34 +08001788 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001789 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001790 pslot = path->slots[level + 1];
1791 }
Chris Masonbb803952007-03-01 12:04:21 -05001792
Chris Mason40689472007-03-17 14:29:23 -04001793 /*
1794 * deal with the case where there is only one pointer in the root
1795 * by promoting the node below to a root
1796 */
Chris Mason5f39d392007-10-15 16:14:19 -04001797 if (!parent) {
1798 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001799
Chris Mason5f39d392007-10-15 16:14:19 -04001800 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001801 return 0;
1802
1803 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001804 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001805 if (!child) {
1806 ret = -EROFS;
1807 btrfs_std_error(root->fs_info, ret);
1808 goto enospc;
1809 }
1810
Chris Mason925baed2008-06-25 16:01:30 -04001811 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001812 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001813 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001814 if (ret) {
1815 btrfs_tree_unlock(child);
1816 free_extent_buffer(child);
1817 goto enospc;
1818 }
Yan2f375ab2008-02-01 14:58:07 -05001819
Jan Schmidt90f8d622013-04-13 13:19:53 +00001820 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001821 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001822
Chris Mason0b86a832008-03-24 15:01:56 -04001823 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001824 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001825
Chris Mason925baed2008-06-25 16:01:30 -04001826 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001827 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001828 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001829 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001830 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001831 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001832
1833 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001834 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001835 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001836 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001837 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001838 }
Chris Mason5f39d392007-10-15 16:14:19 -04001839 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001840 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001841 return 0;
1842
Chris Mason5f39d392007-10-15 16:14:19 -04001843 left = read_node_slot(root, parent, pslot - 1);
1844 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001845 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001846 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001847 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001848 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001849 if (wret) {
1850 ret = wret;
1851 goto enospc;
1852 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001853 }
Chris Mason5f39d392007-10-15 16:14:19 -04001854 right = read_node_slot(root, parent, pslot + 1);
1855 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001856 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001857 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001858 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001859 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001860 if (wret) {
1861 ret = wret;
1862 goto enospc;
1863 }
1864 }
1865
1866 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001867 if (left) {
1868 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001869 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001870 if (wret < 0)
1871 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001872 }
Chris Mason79f95c82007-03-01 15:16:26 -05001873
1874 /*
1875 * then try to empty the right most buffer into the middle
1876 */
Chris Mason5f39d392007-10-15 16:14:19 -04001877 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001878 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001879 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001880 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001881 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001882 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001883 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001884 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001885 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001886 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001887 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001888 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001889 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001890 struct btrfs_disk_key right_key;
1891 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001892 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001893 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001894 btrfs_set_node_key(parent, &right_key, pslot + 1);
1895 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001896 }
1897 }
Chris Mason5f39d392007-10-15 16:14:19 -04001898 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001899 /*
1900 * we're not allowed to leave a node with one item in the
1901 * tree during a delete. A deletion from lower in the tree
1902 * could try to delete the only pointer in this node.
1903 * So, pull some keys from the left.
1904 * There has to be a left pointer at this point because
1905 * otherwise we would have pulled some pointers from the
1906 * right
1907 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001908 if (!left) {
1909 ret = -EROFS;
1910 btrfs_std_error(root->fs_info, ret);
1911 goto enospc;
1912 }
Chris Mason5f39d392007-10-15 16:14:19 -04001913 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001914 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001915 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001916 goto enospc;
1917 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001918 if (wret == 1) {
1919 wret = push_node_left(trans, root, left, mid, 1);
1920 if (wret < 0)
1921 ret = wret;
1922 }
Chris Mason79f95c82007-03-01 15:16:26 -05001923 BUG_ON(wret == 1);
1924 }
Chris Mason5f39d392007-10-15 16:14:19 -04001925 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001926 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001927 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001928 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001929 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001930 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001931 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001932 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001933 } else {
1934 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001935 struct btrfs_disk_key mid_key;
1936 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001937 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001938 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001939 btrfs_set_node_key(parent, &mid_key, pslot);
1940 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001941 }
Chris Masonbb803952007-03-01 12:04:21 -05001942
Chris Mason79f95c82007-03-01 15:16:26 -05001943 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001944 if (left) {
1945 if (btrfs_header_nritems(left) > orig_slot) {
1946 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001947 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001948 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001949 path->slots[level + 1] -= 1;
1950 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001951 if (mid) {
1952 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001953 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001954 }
Chris Masonbb803952007-03-01 12:04:21 -05001955 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001956 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001957 path->slots[level] = orig_slot;
1958 }
1959 }
Chris Mason79f95c82007-03-01 15:16:26 -05001960 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001961 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001962 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001963 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001964enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001965 if (right) {
1966 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001967 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001968 }
1969 if (left) {
1970 if (path->nodes[level] != left)
1971 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001972 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001973 }
Chris Masonbb803952007-03-01 12:04:21 -05001974 return ret;
1975}
1976
Chris Masond352ac62008-09-29 15:18:18 -04001977/* Node balancing for insertion. Here we only split or push nodes around
1978 * when they are completely full. This is also done top down, so we
1979 * have to be pessimistic.
1980 */
Chris Masond3977122009-01-05 21:25:51 -05001981static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001982 struct btrfs_root *root,
1983 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001984{
Chris Mason5f39d392007-10-15 16:14:19 -04001985 struct extent_buffer *right = NULL;
1986 struct extent_buffer *mid;
1987 struct extent_buffer *left = NULL;
1988 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001989 int ret = 0;
1990 int wret;
1991 int pslot;
1992 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001993
1994 if (level == 0)
1995 return 1;
1996
Chris Mason5f39d392007-10-15 16:14:19 -04001997 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001998 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001999
Li Zefana05a9bb2011-09-06 16:55:34 +08002000 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002001 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002002 pslot = path->slots[level + 1];
2003 }
Chris Masone66f7092007-04-20 13:16:02 -04002004
Chris Mason5f39d392007-10-15 16:14:19 -04002005 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002006 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002007
Chris Mason5f39d392007-10-15 16:14:19 -04002008 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002009
2010 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002011 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002012 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002013
2014 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002015 btrfs_set_lock_blocking(left);
2016
Chris Mason5f39d392007-10-15 16:14:19 -04002017 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002018 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2019 wret = 1;
2020 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002021 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002022 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002023 if (ret)
2024 wret = 1;
2025 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002026 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002027 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002028 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002029 }
Chris Masone66f7092007-04-20 13:16:02 -04002030 if (wret < 0)
2031 ret = wret;
2032 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002034 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002035 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002036 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002037 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002038 btrfs_set_node_key(parent, &disk_key, pslot);
2039 btrfs_mark_buffer_dirty(parent);
2040 if (btrfs_header_nritems(left) > orig_slot) {
2041 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002042 path->slots[level + 1] -= 1;
2043 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002044 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002045 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002046 } else {
2047 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002048 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002049 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002050 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002052 }
Chris Masone66f7092007-04-20 13:16:02 -04002053 return 0;
2054 }
Chris Mason925baed2008-06-25 16:01:30 -04002055 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002056 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002057 }
Chris Mason925baed2008-06-25 16:01:30 -04002058 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002059
2060 /*
2061 * then try to empty the right most buffer into the middle
2062 */
Chris Mason5f39d392007-10-15 16:14:19 -04002063 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002064 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002065
Chris Mason925baed2008-06-25 16:01:30 -04002066 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002067 btrfs_set_lock_blocking(right);
2068
Chris Mason5f39d392007-10-15 16:14:19 -04002069 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002070 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2071 wret = 1;
2072 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002073 ret = btrfs_cow_block(trans, root, right,
2074 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002075 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002076 if (ret)
2077 wret = 1;
2078 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002079 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002080 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002081 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002082 }
Chris Masone66f7092007-04-20 13:16:02 -04002083 if (wret < 0)
2084 ret = wret;
2085 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002086 struct btrfs_disk_key disk_key;
2087
2088 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002089 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002090 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002091 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2092 btrfs_mark_buffer_dirty(parent);
2093
2094 if (btrfs_header_nritems(mid) <= orig_slot) {
2095 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002096 path->slots[level + 1] += 1;
2097 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002098 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002099 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002101 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002102 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002103 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002104 }
Chris Masone66f7092007-04-20 13:16:02 -04002105 return 0;
2106 }
Chris Mason925baed2008-06-25 16:01:30 -04002107 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002109 }
Chris Masone66f7092007-04-20 13:16:02 -04002110 return 1;
2111}
2112
Chris Mason74123bd2007-02-02 11:05:29 -05002113/*
Chris Masond352ac62008-09-29 15:18:18 -04002114 * readahead one full node of leaves, finding things that are close
2115 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002116 */
Chris Masonc8c42862009-04-03 10:14:18 -04002117static void reada_for_search(struct btrfs_root *root,
2118 struct btrfs_path *path,
2119 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002120{
Chris Mason5f39d392007-10-15 16:14:19 -04002121 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002122 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002123 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002124 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002125 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002126 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002127 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002128 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002129 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002130 u32 nr;
2131 u32 blocksize;
2132 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002133
Chris Masona6b6e752007-10-15 16:22:39 -04002134 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002135 return;
2136
Chris Mason6702ed42007-08-07 16:15:09 -04002137 if (!path->nodes[level])
2138 return;
2139
Chris Mason5f39d392007-10-15 16:14:19 -04002140 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002141
Chris Mason3c69fae2007-08-07 15:52:22 -04002142 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002143 blocksize = btrfs_level_size(root, level - 1);
2144 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002145 if (eb) {
2146 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002147 return;
2148 }
2149
Chris Masona7175312009-01-22 09:23:10 -05002150 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002151
Chris Mason5f39d392007-10-15 16:14:19 -04002152 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002153 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002154
Chris Masond3977122009-01-05 21:25:51 -05002155 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002156 if (direction < 0) {
2157 if (nr == 0)
2158 break;
2159 nr--;
2160 } else if (direction > 0) {
2161 nr++;
2162 if (nr >= nritems)
2163 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002164 }
Chris Mason01f46652007-12-21 16:24:26 -05002165 if (path->reada < 0 && objectid) {
2166 btrfs_node_key(node, &disk_key, nr);
2167 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2168 break;
2169 }
Chris Mason6b800532007-10-15 16:17:34 -04002170 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002171 if ((search <= target && target - search <= 65536) ||
2172 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002173 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002174 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002175 nread += blocksize;
2176 }
2177 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002178 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002179 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002180 }
2181}
Chris Mason925baed2008-06-25 16:01:30 -04002182
Chris Masond352ac62008-09-29 15:18:18 -04002183/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002184 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2185 * cache
2186 */
2187static noinline int reada_for_balance(struct btrfs_root *root,
2188 struct btrfs_path *path, int level)
2189{
2190 int slot;
2191 int nritems;
2192 struct extent_buffer *parent;
2193 struct extent_buffer *eb;
2194 u64 gen;
2195 u64 block1 = 0;
2196 u64 block2 = 0;
2197 int ret = 0;
2198 int blocksize;
2199
Chris Mason8c594ea2009-04-20 15:50:10 -04002200 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002201 if (!parent)
2202 return 0;
2203
2204 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002205 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002206 blocksize = btrfs_level_size(root, level);
2207
2208 if (slot > 0) {
2209 block1 = btrfs_node_blockptr(parent, slot - 1);
2210 gen = btrfs_node_ptr_generation(parent, slot - 1);
2211 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002212 /*
2213 * if we get -eagain from btrfs_buffer_uptodate, we
2214 * don't want to return eagain here. That will loop
2215 * forever
2216 */
2217 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002218 block1 = 0;
2219 free_extent_buffer(eb);
2220 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002221 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002222 block2 = btrfs_node_blockptr(parent, slot + 1);
2223 gen = btrfs_node_ptr_generation(parent, slot + 1);
2224 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002225 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002226 block2 = 0;
2227 free_extent_buffer(eb);
2228 }
2229 if (block1 || block2) {
2230 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002231
2232 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002233 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002234
2235 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002236 if (block1)
2237 readahead_tree_block(root, block1, blocksize, 0);
2238 if (block2)
2239 readahead_tree_block(root, block2, blocksize, 0);
2240
2241 if (block1) {
2242 eb = read_tree_block(root, block1, blocksize, 0);
2243 free_extent_buffer(eb);
2244 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002245 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002246 eb = read_tree_block(root, block2, blocksize, 0);
2247 free_extent_buffer(eb);
2248 }
2249 }
2250 return ret;
2251}
2252
2253
2254/*
Chris Masond3977122009-01-05 21:25:51 -05002255 * when we walk down the tree, it is usually safe to unlock the higher layers
2256 * in the tree. The exceptions are when our path goes through slot 0, because
2257 * operations on the tree might require changing key pointers higher up in the
2258 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002259 *
Chris Masond3977122009-01-05 21:25:51 -05002260 * callers might also have set path->keep_locks, which tells this code to keep
2261 * the lock if the path points to the last slot in the block. This is part of
2262 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002263 *
Chris Masond3977122009-01-05 21:25:51 -05002264 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2265 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002266 */
Chris Masone02119d2008-09-05 16:13:11 -04002267static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002268 int lowest_unlock, int min_write_lock_level,
2269 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002270{
2271 int i;
2272 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002273 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002274 struct extent_buffer *t;
2275
2276 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2277 if (!path->nodes[i])
2278 break;
2279 if (!path->locks[i])
2280 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002281 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002282 skip_level = i + 1;
2283 continue;
2284 }
Chris Mason051e1b92008-06-25 16:01:30 -04002285 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002286 u32 nritems;
2287 t = path->nodes[i];
2288 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002289 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002290 skip_level = i + 1;
2291 continue;
2292 }
2293 }
Chris Mason051e1b92008-06-25 16:01:30 -04002294 if (skip_level < i && i >= lowest_unlock)
2295 no_skips = 1;
2296
Chris Mason925baed2008-06-25 16:01:30 -04002297 t = path->nodes[i];
2298 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002299 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002300 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002301 if (write_lock_level &&
2302 i > min_write_lock_level &&
2303 i <= *write_lock_level) {
2304 *write_lock_level = i - 1;
2305 }
Chris Mason925baed2008-06-25 16:01:30 -04002306 }
2307 }
2308}
2309
Chris Mason3c69fae2007-08-07 15:52:22 -04002310/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002311 * This releases any locks held in the path starting at level and
2312 * going all the way up to the root.
2313 *
2314 * btrfs_search_slot will keep the lock held on higher nodes in a few
2315 * corner cases, such as COW of the block at slot zero in the node. This
2316 * ignores those rules, and it should only be called when there are no
2317 * more updates to be done higher up in the tree.
2318 */
2319noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2320{
2321 int i;
2322
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002323 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002324 return;
2325
2326 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2327 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002328 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002329 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002330 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002331 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002332 path->locks[i] = 0;
2333 }
2334}
2335
2336/*
Chris Masonc8c42862009-04-03 10:14:18 -04002337 * helper function for btrfs_search_slot. The goal is to find a block
2338 * in cache without setting the path to blocking. If we find the block
2339 * we return zero and the path is unchanged.
2340 *
2341 * If we can't find the block, we set the path blocking and do some
2342 * reada. -EAGAIN is returned and the search must be repeated.
2343 */
2344static int
2345read_block_for_search(struct btrfs_trans_handle *trans,
2346 struct btrfs_root *root, struct btrfs_path *p,
2347 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002348 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002349{
2350 u64 blocknr;
2351 u64 gen;
2352 u32 blocksize;
2353 struct extent_buffer *b = *eb_ret;
2354 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002355 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002356
2357 blocknr = btrfs_node_blockptr(b, slot);
2358 gen = btrfs_node_ptr_generation(b, slot);
2359 blocksize = btrfs_level_size(root, level - 1);
2360
2361 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002362 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002363 /* first we do an atomic uptodate check */
2364 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2365 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002366 /*
2367 * we found an up to date block without
2368 * sleeping, return
2369 * right away
2370 */
2371 *eb_ret = tmp;
2372 return 0;
2373 }
2374 /* the pages were up to date, but we failed
2375 * the generation number check. Do a full
2376 * read for the generation number that is correct.
2377 * We must do this without dropping locks so
2378 * we can trust our generation number
2379 */
2380 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002381 btrfs_set_path_blocking(p);
2382
Chris Masonb9fab912012-05-06 07:23:47 -04002383 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002384 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002385 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002386 *eb_ret = tmp;
2387 return 0;
2388 }
2389 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002390 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002391 return -EIO;
2392 }
Chris Masonc8c42862009-04-03 10:14:18 -04002393 }
2394
2395 /*
2396 * reduce lock contention at high levels
2397 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002398 * we read. Don't release the lock on the current
2399 * level because we need to walk this node to figure
2400 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002401 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002402 btrfs_unlock_up_safe(p, level + 1);
2403 btrfs_set_path_blocking(p);
2404
Chris Masoncb449212010-10-24 11:01:27 -04002405 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002406 if (p->reada)
2407 reada_for_search(root, p, level, slot, key->objectid);
2408
David Sterbab3b4aa72011-04-21 01:20:15 +02002409 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002410
2411 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002412 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002413 if (tmp) {
2414 /*
2415 * If the read above didn't mark this buffer up to date,
2416 * it will never end up being up to date. Set ret to EIO now
2417 * and give up so that our caller doesn't loop forever
2418 * on our EAGAINs.
2419 */
Chris Masonb9fab912012-05-06 07:23:47 -04002420 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002421 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002422 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002423 }
2424 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002425}
2426
2427/*
2428 * helper function for btrfs_search_slot. This does all of the checks
2429 * for node-level blocks and does any balancing required based on
2430 * the ins_len.
2431 *
2432 * If no extra work was required, zero is returned. If we had to
2433 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2434 * start over
2435 */
2436static int
2437setup_nodes_for_search(struct btrfs_trans_handle *trans,
2438 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002439 struct extent_buffer *b, int level, int ins_len,
2440 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002441{
2442 int ret;
2443 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2444 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2445 int sret;
2446
Chris Masonbd681512011-07-16 15:23:14 -04002447 if (*write_lock_level < level + 1) {
2448 *write_lock_level = level + 1;
2449 btrfs_release_path(p);
2450 goto again;
2451 }
2452
Chris Masonc8c42862009-04-03 10:14:18 -04002453 sret = reada_for_balance(root, p, level);
2454 if (sret)
2455 goto again;
2456
2457 btrfs_set_path_blocking(p);
2458 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002459 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002460
2461 BUG_ON(sret > 0);
2462 if (sret) {
2463 ret = sret;
2464 goto done;
2465 }
2466 b = p->nodes[level];
2467 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002468 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002469 int sret;
2470
Chris Masonbd681512011-07-16 15:23:14 -04002471 if (*write_lock_level < level + 1) {
2472 *write_lock_level = level + 1;
2473 btrfs_release_path(p);
2474 goto again;
2475 }
2476
Chris Masonc8c42862009-04-03 10:14:18 -04002477 sret = reada_for_balance(root, p, level);
2478 if (sret)
2479 goto again;
2480
2481 btrfs_set_path_blocking(p);
2482 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002483 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002484
2485 if (sret) {
2486 ret = sret;
2487 goto done;
2488 }
2489 b = p->nodes[level];
2490 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002491 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002492 goto again;
2493 }
2494 BUG_ON(btrfs_header_nritems(b) == 1);
2495 }
2496 return 0;
2497
2498again:
2499 ret = -EAGAIN;
2500done:
2501 return ret;
2502}
2503
2504/*
Chris Mason74123bd2007-02-02 11:05:29 -05002505 * look for key in the tree. path is filled in with nodes along the way
2506 * if key is found, we return zero and you can find the item in the leaf
2507 * level of the path (level 0)
2508 *
2509 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002510 * be inserted, and 1 is returned. If there are other errors during the
2511 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002512 *
2513 * if ins_len > 0, nodes and leaves will be split as we walk down the
2514 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2515 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002516 */
Chris Masone089f052007-03-16 16:20:31 -04002517int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2518 *root, struct btrfs_key *key, struct btrfs_path *p, int
2519 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002520{
Chris Mason5f39d392007-10-15 16:14:19 -04002521 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002522 int slot;
2523 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002524 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002525 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002526 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002527 int root_lock;
2528 /* everything at write_lock_level or lower must be write locked */
2529 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002530 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002531 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002532
Chris Mason6702ed42007-08-07 16:15:09 -04002533 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002534 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002535 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002536
Chris Masonbd681512011-07-16 15:23:14 -04002537 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002538 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002539
Chris Masonbd681512011-07-16 15:23:14 -04002540 /* when we are removing items, we might have to go up to level
2541 * two as we update tree pointers Make sure we keep write
2542 * for those levels as well
2543 */
2544 write_lock_level = 2;
2545 } else if (ins_len > 0) {
2546 /*
2547 * for inserting items, make sure we have a write lock on
2548 * level 1 so we can update keys
2549 */
2550 write_lock_level = 1;
2551 }
2552
2553 if (!cow)
2554 write_lock_level = -1;
2555
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002556 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002557 write_lock_level = BTRFS_MAX_LEVEL;
2558
Chris Masonf7c79f32012-03-19 15:54:38 -04002559 min_write_lock_level = write_lock_level;
2560
Chris Masonbb803952007-03-01 12:04:21 -05002561again:
Chris Masonbd681512011-07-16 15:23:14 -04002562 /*
2563 * we try very hard to do read locks on the root
2564 */
2565 root_lock = BTRFS_READ_LOCK;
2566 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002567 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002568 /*
2569 * the commit roots are read only
2570 * so we always do read locks
2571 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002572 b = root->commit_root;
2573 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002574 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002575 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002576 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002577 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002578 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002580 level = btrfs_header_level(b);
2581 } else {
2582 /* we don't know the level of the root node
2583 * until we actually have it read locked
2584 */
2585 b = btrfs_read_lock_root_node(root);
2586 level = btrfs_header_level(b);
2587 if (level <= write_lock_level) {
2588 /* whoops, must trade for write lock */
2589 btrfs_tree_read_unlock(b);
2590 free_extent_buffer(b);
2591 b = btrfs_lock_root_node(root);
2592 root_lock = BTRFS_WRITE_LOCK;
2593
2594 /* the level might have changed, check again */
2595 level = btrfs_header_level(b);
2596 }
2597 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002598 }
Chris Masonbd681512011-07-16 15:23:14 -04002599 p->nodes[level] = b;
2600 if (!p->skip_locking)
2601 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002602
Chris Masoneb60cea2007-02-02 09:18:22 -05002603 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002604 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002605
2606 /*
2607 * setup the path here so we can release it under lock
2608 * contention with the cow code
2609 */
Chris Mason02217ed2007-03-02 16:08:05 -05002610 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002611 /*
2612 * if we don't really need to cow this block
2613 * then we don't want to set the path blocking,
2614 * so we test it here
2615 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002616 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002617 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002618
Chris Masonb4ce94d2009-02-04 09:25:08 -05002619 btrfs_set_path_blocking(p);
2620
Chris Masonbd681512011-07-16 15:23:14 -04002621 /*
2622 * must have write locks on this node and the
2623 * parent
2624 */
Josef Bacik5124e002012-11-07 13:44:13 -05002625 if (level > write_lock_level ||
2626 (level + 1 > write_lock_level &&
2627 level + 1 < BTRFS_MAX_LEVEL &&
2628 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002629 write_lock_level = level + 1;
2630 btrfs_release_path(p);
2631 goto again;
2632 }
2633
Yan Zheng33c66f42009-07-22 09:59:00 -04002634 err = btrfs_cow_block(trans, root, b,
2635 p->nodes[level + 1],
2636 p->slots[level + 1], &b);
2637 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002638 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002639 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002640 }
Chris Mason02217ed2007-03-02 16:08:05 -05002641 }
Chris Mason65b51a02008-08-01 15:11:20 -04002642cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002643 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002644
Chris Masoneb60cea2007-02-02 09:18:22 -05002645 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002646 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002647
2648 /*
2649 * we have a lock on b and as long as we aren't changing
2650 * the tree, there is no way to for the items in b to change.
2651 * It is safe to drop the lock on our parent before we
2652 * go through the expensive btree search on b.
2653 *
2654 * If cow is true, then we might be changing slot zero,
2655 * which may require changing the parent. So, we can't
2656 * drop the lock until after we know which slot we're
2657 * operating on.
2658 */
2659 if (!cow)
2660 btrfs_unlock_up_safe(p, level + 1);
2661
Chris Mason5f39d392007-10-15 16:14:19 -04002662 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002663
Chris Mason5f39d392007-10-15 16:14:19 -04002664 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002665 int dec = 0;
2666 if (ret && slot > 0) {
2667 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002668 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002669 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002671 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002672 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002673 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002674 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002675 if (err) {
2676 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002677 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002678 }
Chris Masonc8c42862009-04-03 10:14:18 -04002679 b = p->nodes[level];
2680 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002681
Chris Masonbd681512011-07-16 15:23:14 -04002682 /*
2683 * slot 0 is special, if we change the key
2684 * we have to update the parent pointer
2685 * which means we must have a write lock
2686 * on the parent
2687 */
2688 if (slot == 0 && cow &&
2689 write_lock_level < level + 1) {
2690 write_lock_level = level + 1;
2691 btrfs_release_path(p);
2692 goto again;
2693 }
2694
Chris Masonf7c79f32012-03-19 15:54:38 -04002695 unlock_up(p, level, lowest_unlock,
2696 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002697
Chris Mason925baed2008-06-25 16:01:30 -04002698 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002699 if (dec)
2700 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002701 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002702 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002703
Yan Zheng33c66f42009-07-22 09:59:00 -04002704 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002705 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002706 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002707 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002708 if (err) {
2709 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002710 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002711 }
Chris Mason76a05b32009-05-14 13:24:30 -04002712
Chris Masonb4ce94d2009-02-04 09:25:08 -05002713 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002714 level = btrfs_header_level(b);
2715 if (level <= write_lock_level) {
2716 err = btrfs_try_tree_write_lock(b);
2717 if (!err) {
2718 btrfs_set_path_blocking(p);
2719 btrfs_tree_lock(b);
2720 btrfs_clear_path_blocking(p, b,
2721 BTRFS_WRITE_LOCK);
2722 }
2723 p->locks[level] = BTRFS_WRITE_LOCK;
2724 } else {
2725 err = btrfs_try_tree_read_lock(b);
2726 if (!err) {
2727 btrfs_set_path_blocking(p);
2728 btrfs_tree_read_lock(b);
2729 btrfs_clear_path_blocking(p, b,
2730 BTRFS_READ_LOCK);
2731 }
2732 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002733 }
Chris Masonbd681512011-07-16 15:23:14 -04002734 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002735 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002736 } else {
2737 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002738 if (ins_len > 0 &&
2739 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002740 if (write_lock_level < 1) {
2741 write_lock_level = 1;
2742 btrfs_release_path(p);
2743 goto again;
2744 }
2745
Chris Masonb4ce94d2009-02-04 09:25:08 -05002746 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002747 err = split_leaf(trans, root, key,
2748 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002749 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002750
Yan Zheng33c66f42009-07-22 09:59:00 -04002751 BUG_ON(err > 0);
2752 if (err) {
2753 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002754 goto done;
2755 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002756 }
Chris Mason459931e2008-12-10 09:10:46 -05002757 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002758 unlock_up(p, level, lowest_unlock,
2759 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002760 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761 }
2762 }
Chris Mason65b51a02008-08-01 15:11:20 -04002763 ret = 1;
2764done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002765 /*
2766 * we don't really know what they plan on doing with the path
2767 * from here on, so for now just mark it as blocking
2768 */
Chris Masonb9473432009-03-13 11:00:37 -04002769 if (!p->leave_spinning)
2770 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002771 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002772 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002773 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002774}
2775
Chris Mason74123bd2007-02-02 11:05:29 -05002776/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002777 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2778 * current state of the tree together with the operations recorded in the tree
2779 * modification log to search for the key in a previous version of this tree, as
2780 * denoted by the time_seq parameter.
2781 *
2782 * Naturally, there is no support for insert, delete or cow operations.
2783 *
2784 * The resulting path and return value will be set up as if we called
2785 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2786 */
2787int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2788 struct btrfs_path *p, u64 time_seq)
2789{
2790 struct extent_buffer *b;
2791 int slot;
2792 int ret;
2793 int err;
2794 int level;
2795 int lowest_unlock = 1;
2796 u8 lowest_level = 0;
2797
2798 lowest_level = p->lowest_level;
2799 WARN_ON(p->nodes[0] != NULL);
2800
2801 if (p->search_commit_root) {
2802 BUG_ON(time_seq);
2803 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2804 }
2805
2806again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002807 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002808 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002809 p->locks[level] = BTRFS_READ_LOCK;
2810
2811 while (b) {
2812 level = btrfs_header_level(b);
2813 p->nodes[level] = b;
2814 btrfs_clear_path_blocking(p, NULL, 0);
2815
2816 /*
2817 * we have a lock on b and as long as we aren't changing
2818 * the tree, there is no way to for the items in b to change.
2819 * It is safe to drop the lock on our parent before we
2820 * go through the expensive btree search on b.
2821 */
2822 btrfs_unlock_up_safe(p, level + 1);
2823
2824 ret = bin_search(b, key, level, &slot);
2825
2826 if (level != 0) {
2827 int dec = 0;
2828 if (ret && slot > 0) {
2829 dec = 1;
2830 slot -= 1;
2831 }
2832 p->slots[level] = slot;
2833 unlock_up(p, level, lowest_unlock, 0, NULL);
2834
2835 if (level == lowest_level) {
2836 if (dec)
2837 p->slots[level]++;
2838 goto done;
2839 }
2840
2841 err = read_block_for_search(NULL, root, p, &b, level,
2842 slot, key, time_seq);
2843 if (err == -EAGAIN)
2844 goto again;
2845 if (err) {
2846 ret = err;
2847 goto done;
2848 }
2849
2850 level = btrfs_header_level(b);
2851 err = btrfs_try_tree_read_lock(b);
2852 if (!err) {
2853 btrfs_set_path_blocking(p);
2854 btrfs_tree_read_lock(b);
2855 btrfs_clear_path_blocking(p, b,
2856 BTRFS_READ_LOCK);
2857 }
Jan Schmidt47fb0912013-04-13 13:19:55 +00002858 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002859 p->locks[level] = BTRFS_READ_LOCK;
2860 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002861 } else {
2862 p->slots[level] = slot;
2863 unlock_up(p, level, lowest_unlock, 0, NULL);
2864 goto done;
2865 }
2866 }
2867 ret = 1;
2868done:
2869 if (!p->leave_spinning)
2870 btrfs_set_path_blocking(p);
2871 if (ret < 0)
2872 btrfs_release_path(p);
2873
2874 return ret;
2875}
2876
2877/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002878 * helper to use instead of search slot if no exact match is needed but
2879 * instead the next or previous item should be returned.
2880 * When find_higher is true, the next higher item is returned, the next lower
2881 * otherwise.
2882 * When return_any and find_higher are both true, and no higher item is found,
2883 * return the next lower instead.
2884 * When return_any is true and find_higher is false, and no lower item is found,
2885 * return the next higher instead.
2886 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2887 * < 0 on error
2888 */
2889int btrfs_search_slot_for_read(struct btrfs_root *root,
2890 struct btrfs_key *key, struct btrfs_path *p,
2891 int find_higher, int return_any)
2892{
2893 int ret;
2894 struct extent_buffer *leaf;
2895
2896again:
2897 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2898 if (ret <= 0)
2899 return ret;
2900 /*
2901 * a return value of 1 means the path is at the position where the
2902 * item should be inserted. Normally this is the next bigger item,
2903 * but in case the previous item is the last in a leaf, path points
2904 * to the first free slot in the previous leaf, i.e. at an invalid
2905 * item.
2906 */
2907 leaf = p->nodes[0];
2908
2909 if (find_higher) {
2910 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2911 ret = btrfs_next_leaf(root, p);
2912 if (ret <= 0)
2913 return ret;
2914 if (!return_any)
2915 return 1;
2916 /*
2917 * no higher item found, return the next
2918 * lower instead
2919 */
2920 return_any = 0;
2921 find_higher = 0;
2922 btrfs_release_path(p);
2923 goto again;
2924 }
2925 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002926 if (p->slots[0] == 0) {
2927 ret = btrfs_prev_leaf(root, p);
2928 if (ret < 0)
2929 return ret;
2930 if (!ret) {
2931 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2932 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002933 }
Arne Jansene6793762011-09-13 11:18:10 +02002934 if (!return_any)
2935 return 1;
2936 /*
2937 * no lower item found, return the next
2938 * higher instead
2939 */
2940 return_any = 0;
2941 find_higher = 1;
2942 btrfs_release_path(p);
2943 goto again;
2944 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002945 --p->slots[0];
2946 }
2947 }
2948 return 0;
2949}
2950
2951/*
Chris Mason74123bd2007-02-02 11:05:29 -05002952 * adjust the pointers going up the tree, starting at level
2953 * making sure the right key of each node is points to 'key'.
2954 * This is used after shifting pointers to the left, so it stops
2955 * fixing up pointers when a given leaf/node is not in slot 0 of the
2956 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002957 *
Chris Mason74123bd2007-02-02 11:05:29 -05002958 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002959static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002960 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002961{
2962 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002963 struct extent_buffer *t;
2964
Chris Mason234b63a2007-03-13 10:46:10 -04002965 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002966 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002967 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002968 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002969 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002970 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002971 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002972 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002973 if (tslot != 0)
2974 break;
2975 }
2976}
2977
Chris Mason74123bd2007-02-02 11:05:29 -05002978/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002979 * update item key.
2980 *
2981 * This function isn't completely safe. It's the caller's responsibility
2982 * that the new key won't break the order
2983 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002984void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002985 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002986{
2987 struct btrfs_disk_key disk_key;
2988 struct extent_buffer *eb;
2989 int slot;
2990
2991 eb = path->nodes[0];
2992 slot = path->slots[0];
2993 if (slot > 0) {
2994 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002995 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002996 }
2997 if (slot < btrfs_header_nritems(eb) - 1) {
2998 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002999 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003000 }
3001
3002 btrfs_cpu_key_to_disk(&disk_key, new_key);
3003 btrfs_set_item_key(eb, &disk_key, slot);
3004 btrfs_mark_buffer_dirty(eb);
3005 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003006 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003007}
3008
3009/*
Chris Mason74123bd2007-02-02 11:05:29 -05003010 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003011 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003012 *
3013 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3014 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003015 */
Chris Mason98ed5172008-01-03 10:01:48 -05003016static int push_node_left(struct btrfs_trans_handle *trans,
3017 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003018 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003019{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003020 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003021 int src_nritems;
3022 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003023 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003024
Chris Mason5f39d392007-10-15 16:14:19 -04003025 src_nritems = btrfs_header_nritems(src);
3026 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003027 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003028 WARN_ON(btrfs_header_generation(src) != trans->transid);
3029 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003030
Chris Masonbce4eae2008-04-24 14:42:46 -04003031 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003032 return 1;
3033
Chris Masond3977122009-01-05 21:25:51 -05003034 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003035 return 1;
3036
Chris Masonbce4eae2008-04-24 14:42:46 -04003037 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003038 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003039 if (push_items < src_nritems) {
3040 /* leave at least 8 pointers in the node if
3041 * we aren't going to empty it
3042 */
3043 if (src_nritems - push_items < 8) {
3044 if (push_items <= 8)
3045 return 1;
3046 push_items -= 8;
3047 }
3048 }
3049 } else
3050 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003051
Jan Schmidtf2304752012-05-26 11:43:17 +02003052 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003053 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003054 copy_extent_buffer(dst, src,
3055 btrfs_node_key_ptr_offset(dst_nritems),
3056 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003057 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003058
Chris Masonbb803952007-03-01 12:04:21 -05003059 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003060 /*
3061 * don't call tree_mod_log_eb_move here, key removal was already
3062 * fully logged by tree_mod_log_eb_copy above.
3063 */
Chris Mason5f39d392007-10-15 16:14:19 -04003064 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3065 btrfs_node_key_ptr_offset(push_items),
3066 (src_nritems - push_items) *
3067 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003068 }
Chris Mason5f39d392007-10-15 16:14:19 -04003069 btrfs_set_header_nritems(src, src_nritems - push_items);
3070 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3071 btrfs_mark_buffer_dirty(src);
3072 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003073
Chris Masonbb803952007-03-01 12:04:21 -05003074 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003075}
3076
Chris Mason97571fd2007-02-24 13:39:08 -05003077/*
Chris Mason79f95c82007-03-01 15:16:26 -05003078 * try to push data from one node into the next node right in the
3079 * tree.
3080 *
3081 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3082 * error, and > 0 if there was no room in the right hand block.
3083 *
3084 * this will only push up to 1/2 the contents of the left node over
3085 */
Chris Mason5f39d392007-10-15 16:14:19 -04003086static int balance_node_right(struct btrfs_trans_handle *trans,
3087 struct btrfs_root *root,
3088 struct extent_buffer *dst,
3089 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003090{
Chris Mason79f95c82007-03-01 15:16:26 -05003091 int push_items = 0;
3092 int max_push;
3093 int src_nritems;
3094 int dst_nritems;
3095 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003096
Chris Mason7bb86312007-12-11 09:25:06 -05003097 WARN_ON(btrfs_header_generation(src) != trans->transid);
3098 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3099
Chris Mason5f39d392007-10-15 16:14:19 -04003100 src_nritems = btrfs_header_nritems(src);
3101 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003102 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003103 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003104 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003105
Chris Masond3977122009-01-05 21:25:51 -05003106 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003107 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003108
3109 max_push = src_nritems / 2 + 1;
3110 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003111 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003112 return 1;
Yan252c38f2007-08-29 09:11:44 -04003113
Chris Mason79f95c82007-03-01 15:16:26 -05003114 if (max_push < push_items)
3115 push_items = max_push;
3116
Jan Schmidtf2304752012-05-26 11:43:17 +02003117 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003118 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3119 btrfs_node_key_ptr_offset(0),
3120 (dst_nritems) *
3121 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003122
Jan Schmidtf2304752012-05-26 11:43:17 +02003123 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003124 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003125 copy_extent_buffer(dst, src,
3126 btrfs_node_key_ptr_offset(0),
3127 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003128 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003129
Chris Mason5f39d392007-10-15 16:14:19 -04003130 btrfs_set_header_nritems(src, src_nritems - push_items);
3131 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003132
Chris Mason5f39d392007-10-15 16:14:19 -04003133 btrfs_mark_buffer_dirty(src);
3134 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003135
Chris Mason79f95c82007-03-01 15:16:26 -05003136 return ret;
3137}
3138
3139/*
Chris Mason97571fd2007-02-24 13:39:08 -05003140 * helper function to insert a new root level in the tree.
3141 * A new node is allocated, and a single item is inserted to
3142 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003143 *
3144 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003145 */
Chris Masond3977122009-01-05 21:25:51 -05003146static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003147 struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003148 struct btrfs_path *path, int level, int log_removal)
Chris Mason5c680ed2007-02-22 11:39:13 -05003149{
Chris Mason7bb86312007-12-11 09:25:06 -05003150 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003151 struct extent_buffer *lower;
3152 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003153 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003154 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003155
3156 BUG_ON(path->nodes[level]);
3157 BUG_ON(path->nodes[level-1] != root->node);
3158
Chris Mason7bb86312007-12-11 09:25:06 -05003159 lower = path->nodes[level-1];
3160 if (level == 1)
3161 btrfs_item_key(lower, &lower_key, 0);
3162 else
3163 btrfs_node_key(lower, &lower_key, 0);
3164
Zheng Yan31840ae2008-09-23 13:14:14 -04003165 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003166 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003167 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003168 if (IS_ERR(c))
3169 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003170
Yan, Zhengf0486c62010-05-16 10:46:25 -04003171 root_add_used(root, root->nodesize);
3172
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003173 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003174 btrfs_set_header_nritems(c, 1);
3175 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003176 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003177 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003178 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003179 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003180
Chris Mason5f39d392007-10-15 16:14:19 -04003181 write_extent_buffer(c, root->fs_info->fsid,
3182 (unsigned long)btrfs_header_fsid(c),
3183 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003184
3185 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3186 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3187 BTRFS_UUID_SIZE);
3188
Chris Mason5f39d392007-10-15 16:14:19 -04003189 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003190 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003191 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003192 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003193
3194 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003195
3196 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003197
Chris Mason925baed2008-06-25 16:01:30 -04003198 old = root->node;
Jan Schmidt90f8d622013-04-13 13:19:53 +00003199 tree_mod_log_set_root_pointer(root, c, log_removal);
Chris Mason240f62c2011-03-23 14:54:42 -04003200 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003201
3202 /* the super has an extra ref to root->node */
3203 free_extent_buffer(old);
3204
Chris Mason0b86a832008-03-24 15:01:56 -04003205 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003206 extent_buffer_get(c);
3207 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003208 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003209 path->slots[level] = 0;
3210 return 0;
3211}
3212
Chris Mason74123bd2007-02-02 11:05:29 -05003213/*
3214 * worker function to insert a single pointer in a node.
3215 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003216 *
Chris Mason74123bd2007-02-02 11:05:29 -05003217 * slot and level indicate where you want the key to go, and
3218 * blocknr is the block the key points to.
3219 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003220static void insert_ptr(struct btrfs_trans_handle *trans,
3221 struct btrfs_root *root, struct btrfs_path *path,
3222 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003223 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003224{
Chris Mason5f39d392007-10-15 16:14:19 -04003225 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003226 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003227 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003228
3229 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003230 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003231 lower = path->nodes[level];
3232 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003233 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003234 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003235 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003236 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003237 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3238 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003239 memmove_extent_buffer(lower,
3240 btrfs_node_key_ptr_offset(slot + 1),
3241 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003242 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003243 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003244 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003245 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3246 MOD_LOG_KEY_ADD);
3247 BUG_ON(ret < 0);
3248 }
Chris Mason5f39d392007-10-15 16:14:19 -04003249 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003250 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003251 WARN_ON(trans->transid == 0);
3252 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003253 btrfs_set_header_nritems(lower, nritems + 1);
3254 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003255}
3256
Chris Mason97571fd2007-02-24 13:39:08 -05003257/*
3258 * split the node at the specified level in path in two.
3259 * The path is corrected to point to the appropriate node after the split
3260 *
3261 * Before splitting this tries to make some room in the node by pushing
3262 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003263 *
3264 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003265 */
Chris Masone02119d2008-09-05 16:13:11 -04003266static noinline int split_node(struct btrfs_trans_handle *trans,
3267 struct btrfs_root *root,
3268 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003269{
Chris Mason5f39d392007-10-15 16:14:19 -04003270 struct extent_buffer *c;
3271 struct extent_buffer *split;
3272 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003273 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003274 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003275 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003276
Chris Mason5f39d392007-10-15 16:14:19 -04003277 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003278 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003279 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003280 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003281 * trying to split the root, lets make a new one
3282 *
3283 * tree mod log: We pass 0 as log_removal parameter to
3284 * insert_new_root, because that root buffer will be kept as a
3285 * normal node. We are going to log removal of half of the
3286 * elements below with tree_mod_log_eb_copy. We're holding a
3287 * tree lock on the buffer, which is why we cannot race with
3288 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003289 */
Jan Schmidt90f8d622013-04-13 13:19:53 +00003290 ret = insert_new_root(trans, root, path, level + 1, 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05003291 if (ret)
3292 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003293 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003294 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003295 c = path->nodes[level];
3296 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003297 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003298 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003299 if (ret < 0)
3300 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003301 }
Chris Masone66f7092007-04-20 13:16:02 -04003302
Chris Mason5f39d392007-10-15 16:14:19 -04003303 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003304 mid = (c_nritems + 1) / 2;
3305 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003306
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003307 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003308 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003309 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003310 if (IS_ERR(split))
3311 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003312
Yan, Zhengf0486c62010-05-16 10:46:25 -04003313 root_add_used(root, root->nodesize);
3314
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003315 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003316 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003317 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003318 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003319 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003320 btrfs_set_header_owner(split, root->root_key.objectid);
3321 write_extent_buffer(split, root->fs_info->fsid,
3322 (unsigned long)btrfs_header_fsid(split),
3323 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003324 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3325 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3326 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003327
Jan Schmidt90f8d622013-04-13 13:19:53 +00003328 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003329 copy_extent_buffer(split, c,
3330 btrfs_node_key_ptr_offset(0),
3331 btrfs_node_key_ptr_offset(mid),
3332 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3333 btrfs_set_header_nritems(split, c_nritems - mid);
3334 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003335 ret = 0;
3336
Chris Mason5f39d392007-10-15 16:14:19 -04003337 btrfs_mark_buffer_dirty(c);
3338 btrfs_mark_buffer_dirty(split);
3339
Jeff Mahoney143bede2012-03-01 14:56:26 +01003340 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003341 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003342
Chris Mason5de08d72007-02-24 06:24:44 -05003343 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003344 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003345 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003346 free_extent_buffer(c);
3347 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003348 path->slots[level + 1] += 1;
3349 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003350 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003351 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003352 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003353 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003354}
3355
Chris Mason74123bd2007-02-02 11:05:29 -05003356/*
3357 * how many bytes are required to store the items in a leaf. start
3358 * and nr indicate which items in the leaf to check. This totals up the
3359 * space used both by the item structs and the item data
3360 */
Chris Mason5f39d392007-10-15 16:14:19 -04003361static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003362{
Josef Bacik41be1f32012-10-15 13:43:18 -04003363 struct btrfs_item *start_item;
3364 struct btrfs_item *end_item;
3365 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003366 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003367 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003368 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003369
3370 if (!nr)
3371 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003372 btrfs_init_map_token(&token);
3373 start_item = btrfs_item_nr(l, start);
3374 end_item = btrfs_item_nr(l, end);
3375 data_len = btrfs_token_item_offset(l, start_item, &token) +
3376 btrfs_token_item_size(l, start_item, &token);
3377 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003378 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003379 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003380 return data_len;
3381}
3382
Chris Mason74123bd2007-02-02 11:05:29 -05003383/*
Chris Masond4dbff92007-04-04 14:08:15 -04003384 * The space between the end of the leaf items and
3385 * the start of the leaf data. IOW, how much room
3386 * the leaf has left for both items and data
3387 */
Chris Masond3977122009-01-05 21:25:51 -05003388noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003389 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003390{
Chris Mason5f39d392007-10-15 16:14:19 -04003391 int nritems = btrfs_header_nritems(leaf);
3392 int ret;
3393 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3394 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003395 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3396 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003397 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003398 leaf_space_used(leaf, 0, nritems), nritems);
3399 }
3400 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003401}
3402
Chris Mason99d8f832010-07-07 10:51:48 -04003403/*
3404 * min slot controls the lowest index we're willing to push to the
3405 * right. We'll push up to and including min_slot, but no lower
3406 */
Chris Mason44871b12009-03-13 10:04:31 -04003407static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3408 struct btrfs_root *root,
3409 struct btrfs_path *path,
3410 int data_size, int empty,
3411 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003412 int free_space, u32 left_nritems,
3413 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003414{
Chris Mason5f39d392007-10-15 16:14:19 -04003415 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003416 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003417 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003418 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003419 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003420 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003421 int push_space = 0;
3422 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003423 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003424 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003425 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003426 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003427 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003428
Chris Masoncfed81a2012-03-03 07:40:03 -05003429 btrfs_init_map_token(&token);
3430
Chris Mason34a38212007-11-07 13:31:03 -05003431 if (empty)
3432 nr = 0;
3433 else
Chris Mason99d8f832010-07-07 10:51:48 -04003434 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003435
Zheng Yan31840ae2008-09-23 13:14:14 -04003436 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003437 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003438
Chris Mason44871b12009-03-13 10:04:31 -04003439 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003440 i = left_nritems - 1;
3441 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003442 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003443
Zheng Yan31840ae2008-09-23 13:14:14 -04003444 if (!empty && push_items > 0) {
3445 if (path->slots[0] > i)
3446 break;
3447 if (path->slots[0] == i) {
3448 int space = btrfs_leaf_free_space(root, left);
3449 if (space + push_space * 2 > free_space)
3450 break;
3451 }
3452 }
3453
Chris Mason00ec4c52007-02-24 12:47:20 -05003454 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003455 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003456
Chris Masondb945352007-10-15 16:15:53 -04003457 this_item_size = btrfs_item_size(left, item);
3458 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003459 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003460
Chris Mason00ec4c52007-02-24 12:47:20 -05003461 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003462 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003463 if (i == 0)
3464 break;
3465 i--;
Chris Masondb945352007-10-15 16:15:53 -04003466 }
Chris Mason5f39d392007-10-15 16:14:19 -04003467
Chris Mason925baed2008-06-25 16:01:30 -04003468 if (push_items == 0)
3469 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003470
Julia Lawall6c1500f2012-11-03 20:30:18 +00003471 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003472
Chris Mason00ec4c52007-02-24 12:47:20 -05003473 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003474 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003475
Chris Mason5f39d392007-10-15 16:14:19 -04003476 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003477 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003478
Chris Mason00ec4c52007-02-24 12:47:20 -05003479 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003480 data_end = leaf_data_end(root, right);
3481 memmove_extent_buffer(right,
3482 btrfs_leaf_data(right) + data_end - push_space,
3483 btrfs_leaf_data(right) + data_end,
3484 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3485
Chris Mason00ec4c52007-02-24 12:47:20 -05003486 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003487 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003488 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3489 btrfs_leaf_data(left) + leaf_data_end(root, left),
3490 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003491
3492 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3493 btrfs_item_nr_offset(0),
3494 right_nritems * sizeof(struct btrfs_item));
3495
Chris Mason00ec4c52007-02-24 12:47:20 -05003496 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003497 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3498 btrfs_item_nr_offset(left_nritems - push_items),
3499 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003500
3501 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003502 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003503 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003504 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003505 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003506 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003507 push_space -= btrfs_token_item_size(right, item, &token);
3508 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003509 }
3510
Chris Mason7518a232007-03-12 12:01:18 -04003511 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003512 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003513
Chris Mason34a38212007-11-07 13:31:03 -05003514 if (left_nritems)
3515 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003516 else
3517 clean_tree_block(trans, root, left);
3518
Chris Mason5f39d392007-10-15 16:14:19 -04003519 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003520
Chris Mason5f39d392007-10-15 16:14:19 -04003521 btrfs_item_key(right, &disk_key, 0);
3522 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003523 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003524
Chris Mason00ec4c52007-02-24 12:47:20 -05003525 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003526 if (path->slots[0] >= left_nritems) {
3527 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003528 if (btrfs_header_nritems(path->nodes[0]) == 0)
3529 clean_tree_block(trans, root, path->nodes[0]);
3530 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003531 free_extent_buffer(path->nodes[0]);
3532 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003533 path->slots[1] += 1;
3534 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003535 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003536 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003537 }
3538 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003539
3540out_unlock:
3541 btrfs_tree_unlock(right);
3542 free_extent_buffer(right);
3543 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003544}
Chris Mason925baed2008-06-25 16:01:30 -04003545
Chris Mason00ec4c52007-02-24 12:47:20 -05003546/*
Chris Mason44871b12009-03-13 10:04:31 -04003547 * push some data in the path leaf to the right, trying to free up at
3548 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3549 *
3550 * returns 1 if the push failed because the other node didn't have enough
3551 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003552 *
3553 * this will push starting from min_slot to the end of the leaf. It won't
3554 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003555 */
3556static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003557 *root, struct btrfs_path *path,
3558 int min_data_size, int data_size,
3559 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003560{
3561 struct extent_buffer *left = path->nodes[0];
3562 struct extent_buffer *right;
3563 struct extent_buffer *upper;
3564 int slot;
3565 int free_space;
3566 u32 left_nritems;
3567 int ret;
3568
3569 if (!path->nodes[1])
3570 return 1;
3571
3572 slot = path->slots[1];
3573 upper = path->nodes[1];
3574 if (slot >= btrfs_header_nritems(upper) - 1)
3575 return 1;
3576
3577 btrfs_assert_tree_locked(path->nodes[1]);
3578
3579 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003580 if (right == NULL)
3581 return 1;
3582
Chris Mason44871b12009-03-13 10:04:31 -04003583 btrfs_tree_lock(right);
3584 btrfs_set_lock_blocking(right);
3585
3586 free_space = btrfs_leaf_free_space(root, right);
3587 if (free_space < data_size)
3588 goto out_unlock;
3589
3590 /* cow and double check */
3591 ret = btrfs_cow_block(trans, root, right, upper,
3592 slot + 1, &right);
3593 if (ret)
3594 goto out_unlock;
3595
3596 free_space = btrfs_leaf_free_space(root, right);
3597 if (free_space < data_size)
3598 goto out_unlock;
3599
3600 left_nritems = btrfs_header_nritems(left);
3601 if (left_nritems == 0)
3602 goto out_unlock;
3603
Chris Mason99d8f832010-07-07 10:51:48 -04003604 return __push_leaf_right(trans, root, path, min_data_size, empty,
3605 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003606out_unlock:
3607 btrfs_tree_unlock(right);
3608 free_extent_buffer(right);
3609 return 1;
3610}
3611
3612/*
Chris Mason74123bd2007-02-02 11:05:29 -05003613 * push some data in the path leaf to the left, trying to free up at
3614 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003615 *
3616 * max_slot can put a limit on how far into the leaf we'll push items. The
3617 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3618 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003619 */
Chris Mason44871b12009-03-13 10:04:31 -04003620static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3621 struct btrfs_root *root,
3622 struct btrfs_path *path, int data_size,
3623 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003624 int free_space, u32 right_nritems,
3625 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003626{
Chris Mason5f39d392007-10-15 16:14:19 -04003627 struct btrfs_disk_key disk_key;
3628 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003629 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003630 int push_space = 0;
3631 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003632 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003633 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003634 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003635 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003636 u32 this_item_size;
3637 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003638 struct btrfs_map_token token;
3639
3640 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641
Chris Mason34a38212007-11-07 13:31:03 -05003642 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003643 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003644 else
Chris Mason99d8f832010-07-07 10:51:48 -04003645 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003646
3647 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003648 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003649
Zheng Yan31840ae2008-09-23 13:14:14 -04003650 if (!empty && push_items > 0) {
3651 if (path->slots[0] < i)
3652 break;
3653 if (path->slots[0] == i) {
3654 int space = btrfs_leaf_free_space(root, right);
3655 if (space + push_space * 2 > free_space)
3656 break;
3657 }
3658 }
3659
Chris Masonbe0e5c02007-01-26 15:51:26 -05003660 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003661 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003662
3663 this_item_size = btrfs_item_size(right, item);
3664 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003665 break;
Chris Masondb945352007-10-15 16:15:53 -04003666
Chris Masonbe0e5c02007-01-26 15:51:26 -05003667 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003668 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003669 }
Chris Masondb945352007-10-15 16:15:53 -04003670
Chris Masonbe0e5c02007-01-26 15:51:26 -05003671 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003672 ret = 1;
3673 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003674 }
Chris Mason34a38212007-11-07 13:31:03 -05003675 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003676 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003677
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003679 copy_extent_buffer(left, right,
3680 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3681 btrfs_item_nr_offset(0),
3682 push_items * sizeof(struct btrfs_item));
3683
Chris Mason123abc82007-03-14 14:14:43 -04003684 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003685 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003686
3687 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003688 leaf_data_end(root, left) - push_space,
3689 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003690 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003691 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003692 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003693 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003694
Chris Masondb945352007-10-15 16:15:53 -04003695 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003696 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003697 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003698
Chris Mason5f39d392007-10-15 16:14:19 -04003699 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003700
Chris Masoncfed81a2012-03-03 07:40:03 -05003701 ioff = btrfs_token_item_offset(left, item, &token);
3702 btrfs_set_token_item_offset(left, item,
3703 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3704 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003705 }
Chris Mason5f39d392007-10-15 16:14:19 -04003706 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003707
3708 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003709 if (push_items > right_nritems)
3710 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003711 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003712
Chris Mason34a38212007-11-07 13:31:03 -05003713 if (push_items < right_nritems) {
3714 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3715 leaf_data_end(root, right);
3716 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3717 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3718 btrfs_leaf_data(right) +
3719 leaf_data_end(root, right), push_space);
3720
3721 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003722 btrfs_item_nr_offset(push_items),
3723 (btrfs_header_nritems(right) - push_items) *
3724 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003725 }
Yaneef1c492007-11-26 10:58:13 -05003726 right_nritems -= push_items;
3727 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003728 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003729 for (i = 0; i < right_nritems; i++) {
3730 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003731
Chris Masoncfed81a2012-03-03 07:40:03 -05003732 push_space = push_space - btrfs_token_item_size(right,
3733 item, &token);
3734 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003735 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003736
Chris Mason5f39d392007-10-15 16:14:19 -04003737 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003738 if (right_nritems)
3739 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003740 else
3741 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003742
Chris Mason5f39d392007-10-15 16:14:19 -04003743 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003744 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003745
3746 /* then fixup the leaf pointer in the path */
3747 if (path->slots[0] < push_items) {
3748 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003749 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003750 free_extent_buffer(path->nodes[0]);
3751 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003752 path->slots[1] -= 1;
3753 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003754 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003755 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003756 path->slots[0] -= push_items;
3757 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003758 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003759 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003760out:
3761 btrfs_tree_unlock(left);
3762 free_extent_buffer(left);
3763 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003764}
3765
Chris Mason74123bd2007-02-02 11:05:29 -05003766/*
Chris Mason44871b12009-03-13 10:04:31 -04003767 * push some data in the path leaf to the left, trying to free up at
3768 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003769 *
3770 * max_slot can put a limit on how far into the leaf we'll push items. The
3771 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3772 * items
Chris Mason44871b12009-03-13 10:04:31 -04003773 */
3774static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003775 *root, struct btrfs_path *path, int min_data_size,
3776 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003777{
3778 struct extent_buffer *right = path->nodes[0];
3779 struct extent_buffer *left;
3780 int slot;
3781 int free_space;
3782 u32 right_nritems;
3783 int ret = 0;
3784
3785 slot = path->slots[1];
3786 if (slot == 0)
3787 return 1;
3788 if (!path->nodes[1])
3789 return 1;
3790
3791 right_nritems = btrfs_header_nritems(right);
3792 if (right_nritems == 0)
3793 return 1;
3794
3795 btrfs_assert_tree_locked(path->nodes[1]);
3796
3797 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003798 if (left == NULL)
3799 return 1;
3800
Chris Mason44871b12009-03-13 10:04:31 -04003801 btrfs_tree_lock(left);
3802 btrfs_set_lock_blocking(left);
3803
3804 free_space = btrfs_leaf_free_space(root, left);
3805 if (free_space < data_size) {
3806 ret = 1;
3807 goto out;
3808 }
3809
3810 /* cow and double check */
3811 ret = btrfs_cow_block(trans, root, left,
3812 path->nodes[1], slot - 1, &left);
3813 if (ret) {
3814 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003815 if (ret == -ENOSPC)
3816 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003817 goto out;
3818 }
3819
3820 free_space = btrfs_leaf_free_space(root, left);
3821 if (free_space < data_size) {
3822 ret = 1;
3823 goto out;
3824 }
3825
Chris Mason99d8f832010-07-07 10:51:48 -04003826 return __push_leaf_left(trans, root, path, min_data_size,
3827 empty, left, free_space, right_nritems,
3828 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003829out:
3830 btrfs_tree_unlock(left);
3831 free_extent_buffer(left);
3832 return ret;
3833}
3834
3835/*
Chris Mason74123bd2007-02-02 11:05:29 -05003836 * split the path's leaf in two, making sure there is at least data_size
3837 * available for the resulting leaf level of the path.
3838 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003839static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3840 struct btrfs_root *root,
3841 struct btrfs_path *path,
3842 struct extent_buffer *l,
3843 struct extent_buffer *right,
3844 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003845{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003846 int data_copy_size;
3847 int rt_data_off;
3848 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003849 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003850 struct btrfs_map_token token;
3851
3852 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853
Chris Mason5f39d392007-10-15 16:14:19 -04003854 nritems = nritems - mid;
3855 btrfs_set_header_nritems(right, nritems);
3856 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3857
3858 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3859 btrfs_item_nr_offset(mid),
3860 nritems * sizeof(struct btrfs_item));
3861
3862 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003863 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3864 data_copy_size, btrfs_leaf_data(l) +
3865 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003866
Chris Mason5f39d392007-10-15 16:14:19 -04003867 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3868 btrfs_item_end_nr(l, mid);
3869
3870 for (i = 0; i < nritems; i++) {
3871 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003872 u32 ioff;
3873
Chris Masoncfed81a2012-03-03 07:40:03 -05003874 ioff = btrfs_token_item_offset(right, item, &token);
3875 btrfs_set_token_item_offset(right, item,
3876 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003877 }
Chris Mason74123bd2007-02-02 11:05:29 -05003878
Chris Mason5f39d392007-10-15 16:14:19 -04003879 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003880 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003881 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003882 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003883
3884 btrfs_mark_buffer_dirty(right);
3885 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003886 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003887
Chris Masonbe0e5c02007-01-26 15:51:26 -05003888 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003889 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003890 free_extent_buffer(path->nodes[0]);
3891 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003892 path->slots[0] -= mid;
3893 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003894 } else {
3895 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003896 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003897 }
Chris Mason5f39d392007-10-15 16:14:19 -04003898
Chris Masoneb60cea2007-02-02 09:18:22 -05003899 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003900}
3901
3902/*
Chris Mason99d8f832010-07-07 10:51:48 -04003903 * double splits happen when we need to insert a big item in the middle
3904 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3905 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3906 * A B C
3907 *
3908 * We avoid this by trying to push the items on either side of our target
3909 * into the adjacent leaves. If all goes well we can avoid the double split
3910 * completely.
3911 */
3912static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3913 struct btrfs_root *root,
3914 struct btrfs_path *path,
3915 int data_size)
3916{
3917 int ret;
3918 int progress = 0;
3919 int slot;
3920 u32 nritems;
3921
3922 slot = path->slots[0];
3923
3924 /*
3925 * try to push all the items after our slot into the
3926 * right leaf
3927 */
3928 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3929 if (ret < 0)
3930 return ret;
3931
3932 if (ret == 0)
3933 progress++;
3934
3935 nritems = btrfs_header_nritems(path->nodes[0]);
3936 /*
3937 * our goal is to get our slot at the start or end of a leaf. If
3938 * we've done so we're done
3939 */
3940 if (path->slots[0] == 0 || path->slots[0] == nritems)
3941 return 0;
3942
3943 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3944 return 0;
3945
3946 /* try to push all the items before our slot into the next leaf */
3947 slot = path->slots[0];
3948 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3949 if (ret < 0)
3950 return ret;
3951
3952 if (ret == 0)
3953 progress++;
3954
3955 if (progress)
3956 return 0;
3957 return 1;
3958}
3959
3960/*
Chris Mason44871b12009-03-13 10:04:31 -04003961 * split the path's leaf in two, making sure there is at least data_size
3962 * available for the resulting leaf level of the path.
3963 *
3964 * returns 0 if all went well and < 0 on failure.
3965 */
3966static noinline int split_leaf(struct btrfs_trans_handle *trans,
3967 struct btrfs_root *root,
3968 struct btrfs_key *ins_key,
3969 struct btrfs_path *path, int data_size,
3970 int extend)
3971{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003972 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003973 struct extent_buffer *l;
3974 u32 nritems;
3975 int mid;
3976 int slot;
3977 struct extent_buffer *right;
3978 int ret = 0;
3979 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003980 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003981 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003982 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003983
Yan, Zhenga5719522009-09-24 09:17:31 -04003984 l = path->nodes[0];
3985 slot = path->slots[0];
3986 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3987 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3988 return -EOVERFLOW;
3989
Chris Mason44871b12009-03-13 10:04:31 -04003990 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003991 if (data_size) {
3992 wret = push_leaf_right(trans, root, path, data_size,
3993 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003994 if (wret < 0)
3995 return wret;
3996 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003997 wret = push_leaf_left(trans, root, path, data_size,
3998 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003999 if (wret < 0)
4000 return wret;
4001 }
4002 l = path->nodes[0];
4003
4004 /* did the pushes work? */
4005 if (btrfs_leaf_free_space(root, l) >= data_size)
4006 return 0;
4007 }
4008
4009 if (!path->nodes[1]) {
Jan Schmidt90f8d622013-04-13 13:19:53 +00004010 ret = insert_new_root(trans, root, path, 1, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004011 if (ret)
4012 return ret;
4013 }
4014again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004015 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004016 l = path->nodes[0];
4017 slot = path->slots[0];
4018 nritems = btrfs_header_nritems(l);
4019 mid = (nritems + 1) / 2;
4020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004021 if (mid <= slot) {
4022 if (nritems == 1 ||
4023 leaf_space_used(l, mid, nritems - mid) + data_size >
4024 BTRFS_LEAF_DATA_SIZE(root)) {
4025 if (slot >= nritems) {
4026 split = 0;
4027 } else {
4028 mid = slot;
4029 if (mid != nritems &&
4030 leaf_space_used(l, mid, nritems - mid) +
4031 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004032 if (data_size && !tried_avoid_double)
4033 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004034 split = 2;
4035 }
4036 }
4037 }
4038 } else {
4039 if (leaf_space_used(l, 0, mid) + data_size >
4040 BTRFS_LEAF_DATA_SIZE(root)) {
4041 if (!extend && data_size && slot == 0) {
4042 split = 0;
4043 } else if ((extend || !data_size) && slot == 0) {
4044 mid = 1;
4045 } else {
4046 mid = slot;
4047 if (mid != nritems &&
4048 leaf_space_used(l, mid, nritems - mid) +
4049 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004050 if (data_size && !tried_avoid_double)
4051 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052 split = 2 ;
4053 }
4054 }
4055 }
4056 }
4057
4058 if (split == 0)
4059 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4060 else
4061 btrfs_item_key(l, &disk_key, mid);
4062
4063 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004064 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004065 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004066 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004067 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004068
4069 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004070
4071 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4072 btrfs_set_header_bytenr(right, right->start);
4073 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004075 btrfs_set_header_owner(right, root->root_key.objectid);
4076 btrfs_set_header_level(right, 0);
4077 write_extent_buffer(right, root->fs_info->fsid,
4078 (unsigned long)btrfs_header_fsid(right),
4079 BTRFS_FSID_SIZE);
4080
4081 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
4082 (unsigned long)btrfs_header_chunk_tree_uuid(right),
4083 BTRFS_UUID_SIZE);
4084
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004085 if (split == 0) {
4086 if (mid <= slot) {
4087 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004088 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004089 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004090 btrfs_tree_unlock(path->nodes[0]);
4091 free_extent_buffer(path->nodes[0]);
4092 path->nodes[0] = right;
4093 path->slots[0] = 0;
4094 path->slots[1] += 1;
4095 } else {
4096 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004097 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004098 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004099 btrfs_tree_unlock(path->nodes[0]);
4100 free_extent_buffer(path->nodes[0]);
4101 path->nodes[0] = right;
4102 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004103 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004104 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004105 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004106 btrfs_mark_buffer_dirty(right);
4107 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004108 }
4109
Jeff Mahoney143bede2012-03-01 14:56:26 +01004110 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004111
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004112 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004113 BUG_ON(num_doubles != 0);
4114 num_doubles++;
4115 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004116 }
Chris Mason44871b12009-03-13 10:04:31 -04004117
Jeff Mahoney143bede2012-03-01 14:56:26 +01004118 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004119
4120push_for_double:
4121 push_for_double_split(trans, root, path, data_size);
4122 tried_avoid_double = 1;
4123 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4124 return 0;
4125 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004126}
4127
Yan, Zhengad48fd752009-11-12 09:33:58 +00004128static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4129 struct btrfs_root *root,
4130 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004131{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004132 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004133 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004134 struct btrfs_file_extent_item *fi;
4135 u64 extent_len = 0;
4136 u32 item_size;
4137 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004138
4139 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004140 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4141
4142 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4143 key.type != BTRFS_EXTENT_CSUM_KEY);
4144
4145 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4146 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004147
4148 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004149 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4150 fi = btrfs_item_ptr(leaf, path->slots[0],
4151 struct btrfs_file_extent_item);
4152 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4153 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004154 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004155
Chris Mason459931e2008-12-10 09:10:46 -05004156 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004157 path->search_for_split = 1;
4158 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004159 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004160 if (ret < 0)
4161 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004162
Yan, Zhengad48fd752009-11-12 09:33:58 +00004163 ret = -EAGAIN;
4164 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004165 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004166 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4167 goto err;
4168
Chris Mason109f6ae2010-04-02 09:20:18 -04004169 /* the leaf has changed, it now has room. return now */
4170 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4171 goto err;
4172
Yan, Zhengad48fd752009-11-12 09:33:58 +00004173 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4174 fi = btrfs_item_ptr(leaf, path->slots[0],
4175 struct btrfs_file_extent_item);
4176 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4177 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004178 }
4179
Chris Masonb9473432009-03-13 11:00:37 -04004180 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004181 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004182 if (ret)
4183 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004184
Yan, Zhengad48fd752009-11-12 09:33:58 +00004185 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004186 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004187 return 0;
4188err:
4189 path->keep_locks = 0;
4190 return ret;
4191}
4192
4193static noinline int split_item(struct btrfs_trans_handle *trans,
4194 struct btrfs_root *root,
4195 struct btrfs_path *path,
4196 struct btrfs_key *new_key,
4197 unsigned long split_offset)
4198{
4199 struct extent_buffer *leaf;
4200 struct btrfs_item *item;
4201 struct btrfs_item *new_item;
4202 int slot;
4203 char *buf;
4204 u32 nritems;
4205 u32 item_size;
4206 u32 orig_offset;
4207 struct btrfs_disk_key disk_key;
4208
Chris Masonb9473432009-03-13 11:00:37 -04004209 leaf = path->nodes[0];
4210 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4211
Chris Masonb4ce94d2009-02-04 09:25:08 -05004212 btrfs_set_path_blocking(path);
4213
Chris Mason459931e2008-12-10 09:10:46 -05004214 item = btrfs_item_nr(leaf, path->slots[0]);
4215 orig_offset = btrfs_item_offset(leaf, item);
4216 item_size = btrfs_item_size(leaf, item);
4217
Chris Mason459931e2008-12-10 09:10:46 -05004218 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004219 if (!buf)
4220 return -ENOMEM;
4221
Chris Mason459931e2008-12-10 09:10:46 -05004222 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4223 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004224
Chris Mason459931e2008-12-10 09:10:46 -05004225 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004226 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004227 if (slot != nritems) {
4228 /* shift the items */
4229 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004230 btrfs_item_nr_offset(slot),
4231 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004232 }
4233
4234 btrfs_cpu_key_to_disk(&disk_key, new_key);
4235 btrfs_set_item_key(leaf, &disk_key, slot);
4236
4237 new_item = btrfs_item_nr(leaf, slot);
4238
4239 btrfs_set_item_offset(leaf, new_item, orig_offset);
4240 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4241
4242 btrfs_set_item_offset(leaf, item,
4243 orig_offset + item_size - split_offset);
4244 btrfs_set_item_size(leaf, item, split_offset);
4245
4246 btrfs_set_header_nritems(leaf, nritems + 1);
4247
4248 /* write the data for the start of the original item */
4249 write_extent_buffer(leaf, buf,
4250 btrfs_item_ptr_offset(leaf, path->slots[0]),
4251 split_offset);
4252
4253 /* write the data for the new item */
4254 write_extent_buffer(leaf, buf + split_offset,
4255 btrfs_item_ptr_offset(leaf, slot),
4256 item_size - split_offset);
4257 btrfs_mark_buffer_dirty(leaf);
4258
Yan, Zhengad48fd752009-11-12 09:33:58 +00004259 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004260 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004261 return 0;
4262}
4263
4264/*
4265 * This function splits a single item into two items,
4266 * giving 'new_key' to the new item and splitting the
4267 * old one at split_offset (from the start of the item).
4268 *
4269 * The path may be released by this operation. After
4270 * the split, the path is pointing to the old item. The
4271 * new item is going to be in the same node as the old one.
4272 *
4273 * Note, the item being split must be smaller enough to live alone on
4274 * a tree block with room for one extra struct btrfs_item
4275 *
4276 * This allows us to split the item in place, keeping a lock on the
4277 * leaf the entire time.
4278 */
4279int btrfs_split_item(struct btrfs_trans_handle *trans,
4280 struct btrfs_root *root,
4281 struct btrfs_path *path,
4282 struct btrfs_key *new_key,
4283 unsigned long split_offset)
4284{
4285 int ret;
4286 ret = setup_leaf_for_split(trans, root, path,
4287 sizeof(struct btrfs_item));
4288 if (ret)
4289 return ret;
4290
4291 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004292 return ret;
4293}
4294
4295/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004296 * This function duplicate a item, giving 'new_key' to the new item.
4297 * It guarantees both items live in the same tree leaf and the new item
4298 * is contiguous with the original item.
4299 *
4300 * This allows us to split file extent in place, keeping a lock on the
4301 * leaf the entire time.
4302 */
4303int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4304 struct btrfs_root *root,
4305 struct btrfs_path *path,
4306 struct btrfs_key *new_key)
4307{
4308 struct extent_buffer *leaf;
4309 int ret;
4310 u32 item_size;
4311
4312 leaf = path->nodes[0];
4313 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4314 ret = setup_leaf_for_split(trans, root, path,
4315 item_size + sizeof(struct btrfs_item));
4316 if (ret)
4317 return ret;
4318
4319 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004320 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004321 item_size, item_size +
4322 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004323 leaf = path->nodes[0];
4324 memcpy_extent_buffer(leaf,
4325 btrfs_item_ptr_offset(leaf, path->slots[0]),
4326 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4327 item_size);
4328 return 0;
4329}
4330
4331/*
Chris Masond352ac62008-09-29 15:18:18 -04004332 * make the item pointed to by the path smaller. new_size indicates
4333 * how small to make it, and from_end tells us if we just chop bytes
4334 * off the end of the item or if we shift the item to chop bytes off
4335 * the front.
4336 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004337void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004338 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004339{
Chris Masonb18c6682007-04-17 13:26:50 -04004340 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004341 struct extent_buffer *leaf;
4342 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004343 u32 nritems;
4344 unsigned int data_end;
4345 unsigned int old_data_start;
4346 unsigned int old_size;
4347 unsigned int size_diff;
4348 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004349 struct btrfs_map_token token;
4350
4351 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004352
Chris Mason5f39d392007-10-15 16:14:19 -04004353 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004354 slot = path->slots[0];
4355
4356 old_size = btrfs_item_size_nr(leaf, slot);
4357 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004358 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004359
Chris Mason5f39d392007-10-15 16:14:19 -04004360 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004361 data_end = leaf_data_end(root, leaf);
4362
Chris Mason5f39d392007-10-15 16:14:19 -04004363 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004364
Chris Masonb18c6682007-04-17 13:26:50 -04004365 size_diff = old_size - new_size;
4366
4367 BUG_ON(slot < 0);
4368 BUG_ON(slot >= nritems);
4369
4370 /*
4371 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4372 */
4373 /* first correct the data pointers */
4374 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004375 u32 ioff;
4376 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004377
Chris Masoncfed81a2012-03-03 07:40:03 -05004378 ioff = btrfs_token_item_offset(leaf, item, &token);
4379 btrfs_set_token_item_offset(leaf, item,
4380 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004381 }
Chris Masondb945352007-10-15 16:15:53 -04004382
Chris Masonb18c6682007-04-17 13:26:50 -04004383 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004384 if (from_end) {
4385 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4386 data_end + size_diff, btrfs_leaf_data(leaf) +
4387 data_end, old_data_start + new_size - data_end);
4388 } else {
4389 struct btrfs_disk_key disk_key;
4390 u64 offset;
4391
4392 btrfs_item_key(leaf, &disk_key, slot);
4393
4394 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4395 unsigned long ptr;
4396 struct btrfs_file_extent_item *fi;
4397
4398 fi = btrfs_item_ptr(leaf, slot,
4399 struct btrfs_file_extent_item);
4400 fi = (struct btrfs_file_extent_item *)(
4401 (unsigned long)fi - size_diff);
4402
4403 if (btrfs_file_extent_type(leaf, fi) ==
4404 BTRFS_FILE_EXTENT_INLINE) {
4405 ptr = btrfs_item_ptr_offset(leaf, slot);
4406 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004407 (unsigned long)fi,
4408 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004409 disk_bytenr));
4410 }
4411 }
4412
4413 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4414 data_end + size_diff, btrfs_leaf_data(leaf) +
4415 data_end, old_data_start - data_end);
4416
4417 offset = btrfs_disk_key_offset(&disk_key);
4418 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4419 btrfs_set_item_key(leaf, &disk_key, slot);
4420 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004421 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004422 }
Chris Mason5f39d392007-10-15 16:14:19 -04004423
4424 item = btrfs_item_nr(leaf, slot);
4425 btrfs_set_item_size(leaf, item, new_size);
4426 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004427
Chris Mason5f39d392007-10-15 16:14:19 -04004428 if (btrfs_leaf_free_space(root, leaf) < 0) {
4429 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004430 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004431 }
Chris Masonb18c6682007-04-17 13:26:50 -04004432}
4433
Chris Masond352ac62008-09-29 15:18:18 -04004434/*
4435 * make the item pointed to by the path bigger, data_size is the new size.
4436 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004437void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004438 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004439{
Chris Mason6567e832007-04-16 09:22:45 -04004440 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004441 struct extent_buffer *leaf;
4442 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004443 u32 nritems;
4444 unsigned int data_end;
4445 unsigned int old_data;
4446 unsigned int old_size;
4447 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004448 struct btrfs_map_token token;
4449
4450 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004451
Chris Mason5f39d392007-10-15 16:14:19 -04004452 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004453
Chris Mason5f39d392007-10-15 16:14:19 -04004454 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004455 data_end = leaf_data_end(root, leaf);
4456
Chris Mason5f39d392007-10-15 16:14:19 -04004457 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4458 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004459 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004460 }
Chris Mason6567e832007-04-16 09:22:45 -04004461 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004462 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004463
4464 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004465 if (slot >= nritems) {
4466 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004467 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4468 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004469 BUG_ON(1);
4470 }
Chris Mason6567e832007-04-16 09:22:45 -04004471
4472 /*
4473 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4474 */
4475 /* first correct the data pointers */
4476 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004477 u32 ioff;
4478 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004479
Chris Masoncfed81a2012-03-03 07:40:03 -05004480 ioff = btrfs_token_item_offset(leaf, item, &token);
4481 btrfs_set_token_item_offset(leaf, item,
4482 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004483 }
Chris Mason5f39d392007-10-15 16:14:19 -04004484
Chris Mason6567e832007-04-16 09:22:45 -04004485 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004486 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004487 data_end - data_size, btrfs_leaf_data(leaf) +
4488 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004489
Chris Mason6567e832007-04-16 09:22:45 -04004490 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004491 old_size = btrfs_item_size_nr(leaf, slot);
4492 item = btrfs_item_nr(leaf, slot);
4493 btrfs_set_item_size(leaf, item, old_size + data_size);
4494 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004495
Chris Mason5f39d392007-10-15 16:14:19 -04004496 if (btrfs_leaf_free_space(root, leaf) < 0) {
4497 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004498 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004499 }
Chris Mason6567e832007-04-16 09:22:45 -04004500}
4501
Chris Mason74123bd2007-02-02 11:05:29 -05004502/*
Chris Mason44871b12009-03-13 10:04:31 -04004503 * this is a helper for btrfs_insert_empty_items, the main goal here is
4504 * to save stack depth by doing the bulk of the work in a function
4505 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004506 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004507void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004508 struct btrfs_key *cpu_key, u32 *data_size,
4509 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004510{
Chris Mason5f39d392007-10-15 16:14:19 -04004511 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004512 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004513 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004514 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004515 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004516 struct extent_buffer *leaf;
4517 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004518 struct btrfs_map_token token;
4519
4520 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004521
Chris Mason5f39d392007-10-15 16:14:19 -04004522 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004523 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004524
Chris Mason5f39d392007-10-15 16:14:19 -04004525 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004526 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004527
Chris Masonf25956c2008-09-12 15:32:53 -04004528 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004529 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004530 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004531 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004532 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004533 }
Chris Mason5f39d392007-10-15 16:14:19 -04004534
Chris Masonbe0e5c02007-01-26 15:51:26 -05004535 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004536 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004537
Chris Mason5f39d392007-10-15 16:14:19 -04004538 if (old_data < data_end) {
4539 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004540 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004541 slot, old_data, data_end);
4542 BUG_ON(1);
4543 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004544 /*
4545 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4546 */
4547 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004548 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004549 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004550
Chris Mason5f39d392007-10-15 16:14:19 -04004551 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004552 ioff = btrfs_token_item_offset(leaf, item, &token);
4553 btrfs_set_token_item_offset(leaf, item,
4554 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004555 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004556 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004557 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004558 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004559 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004560
4561 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004562 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004563 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004564 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004565 data_end = old_data;
4566 }
Chris Mason5f39d392007-10-15 16:14:19 -04004567
Chris Mason62e27492007-03-15 12:56:47 -04004568 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004569 for (i = 0; i < nr; i++) {
4570 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4571 btrfs_set_item_key(leaf, &disk_key, slot + i);
4572 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004573 btrfs_set_token_item_offset(leaf, item,
4574 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004575 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004576 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004577 }
Chris Mason44871b12009-03-13 10:04:31 -04004578
Chris Mason9c583092008-01-29 15:15:18 -05004579 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004580
Chris Mason5a01a2e2008-01-30 11:43:54 -05004581 if (slot == 0) {
4582 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004583 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004584 }
Chris Masonb9473432009-03-13 11:00:37 -04004585 btrfs_unlock_up_safe(path, 1);
4586 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004587
Chris Mason5f39d392007-10-15 16:14:19 -04004588 if (btrfs_leaf_free_space(root, leaf) < 0) {
4589 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004590 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004591 }
Chris Mason44871b12009-03-13 10:04:31 -04004592}
4593
4594/*
4595 * Given a key and some data, insert items into the tree.
4596 * This does all the path init required, making room in the tree if needed.
4597 */
4598int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4599 struct btrfs_root *root,
4600 struct btrfs_path *path,
4601 struct btrfs_key *cpu_key, u32 *data_size,
4602 int nr)
4603{
Chris Mason44871b12009-03-13 10:04:31 -04004604 int ret = 0;
4605 int slot;
4606 int i;
4607 u32 total_size = 0;
4608 u32 total_data = 0;
4609
4610 for (i = 0; i < nr; i++)
4611 total_data += data_size[i];
4612
4613 total_size = total_data + (nr * sizeof(struct btrfs_item));
4614 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4615 if (ret == 0)
4616 return -EEXIST;
4617 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004618 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004619
Chris Mason44871b12009-03-13 10:04:31 -04004620 slot = path->slots[0];
4621 BUG_ON(slot < 0);
4622
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004623 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004624 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004625 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004626}
4627
4628/*
4629 * Given a key and some data, insert an item into the tree.
4630 * This does all the path init required, making room in the tree if needed.
4631 */
Chris Masone089f052007-03-16 16:20:31 -04004632int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4633 *root, struct btrfs_key *cpu_key, void *data, u32
4634 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004635{
4636 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004637 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004638 struct extent_buffer *leaf;
4639 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004640
Chris Mason2c90e5d2007-04-02 10:50:19 -04004641 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004642 if (!path)
4643 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004644 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004645 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004646 leaf = path->nodes[0];
4647 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4648 write_extent_buffer(leaf, data, ptr, data_size);
4649 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004650 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004651 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004652 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004653}
4654
Chris Mason74123bd2007-02-02 11:05:29 -05004655/*
Chris Mason5de08d72007-02-24 06:24:44 -05004656 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004657 *
Chris Masond352ac62008-09-29 15:18:18 -04004658 * the tree should have been previously balanced so the deletion does not
4659 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004660 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004661static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4662 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004663{
Chris Mason5f39d392007-10-15 16:14:19 -04004664 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004665 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004666 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004667
Chris Mason5f39d392007-10-15 16:14:19 -04004668 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004669 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004670 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004671 tree_mod_log_eb_move(root->fs_info, parent, slot,
4672 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004673 memmove_extent_buffer(parent,
4674 btrfs_node_key_ptr_offset(slot),
4675 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004676 sizeof(struct btrfs_key_ptr) *
4677 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004678 } else if (level) {
4679 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4680 MOD_LOG_KEY_REMOVE);
4681 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004682 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004683
Chris Mason7518a232007-03-12 12:01:18 -04004684 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004685 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004686 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004687 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004688 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004689 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004690 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004691 struct btrfs_disk_key disk_key;
4692
4693 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004694 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004695 }
Chris Masond6025572007-03-30 14:27:56 -04004696 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004697}
4698
Chris Mason74123bd2007-02-02 11:05:29 -05004699/*
Chris Mason323ac952008-10-01 19:05:46 -04004700 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004701 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004702 *
4703 * This deletes the pointer in path->nodes[1] and frees the leaf
4704 * block extent. zero is returned if it all worked out, < 0 otherwise.
4705 *
4706 * The path must have already been setup for deleting the leaf, including
4707 * all the proper balancing. path->nodes[1] must be locked.
4708 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004709static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4710 struct btrfs_root *root,
4711 struct btrfs_path *path,
4712 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004713{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004714 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004715 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004716
Chris Mason4d081c42009-02-04 09:31:28 -05004717 /*
4718 * btrfs_free_extent is expensive, we want to make sure we
4719 * aren't holding any locks when we call it
4720 */
4721 btrfs_unlock_up_safe(path, 0);
4722
Yan, Zhengf0486c62010-05-16 10:46:25 -04004723 root_sub_used(root, leaf->len);
4724
Josef Bacik3083ee22012-03-09 16:01:49 -05004725 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004726 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004727 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004728}
4729/*
Chris Mason74123bd2007-02-02 11:05:29 -05004730 * delete the item at the leaf level in path. If that empties
4731 * the leaf, remove it from the tree
4732 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004733int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4734 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004735{
Chris Mason5f39d392007-10-15 16:14:19 -04004736 struct extent_buffer *leaf;
4737 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004738 int last_off;
4739 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004740 int ret = 0;
4741 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004742 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004743 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004744 struct btrfs_map_token token;
4745
4746 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004747
Chris Mason5f39d392007-10-15 16:14:19 -04004748 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004749 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4750
4751 for (i = 0; i < nr; i++)
4752 dsize += btrfs_item_size_nr(leaf, slot + i);
4753
Chris Mason5f39d392007-10-15 16:14:19 -04004754 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004755
Chris Mason85e21ba2008-01-29 15:11:36 -05004756 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004757 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004758
4759 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004760 data_end + dsize,
4761 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004762 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004763
Chris Mason85e21ba2008-01-29 15:11:36 -05004764 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004765 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004766
Chris Mason5f39d392007-10-15 16:14:19 -04004767 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004768 ioff = btrfs_token_item_offset(leaf, item, &token);
4769 btrfs_set_token_item_offset(leaf, item,
4770 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004771 }
Chris Masondb945352007-10-15 16:15:53 -04004772
Chris Mason5f39d392007-10-15 16:14:19 -04004773 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004774 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004775 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004776 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004777 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004778 btrfs_set_header_nritems(leaf, nritems - nr);
4779 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004780
Chris Mason74123bd2007-02-02 11:05:29 -05004781 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004782 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004783 if (leaf == root->node) {
4784 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004785 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004786 btrfs_set_path_blocking(path);
4787 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004788 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004789 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004790 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004791 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004792 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004793 struct btrfs_disk_key disk_key;
4794
4795 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004796 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004797 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004798
Chris Mason74123bd2007-02-02 11:05:29 -05004799 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004800 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004801 /* push_leaf_left fixes the path.
4802 * make sure the path still points to our leaf
4803 * for possible call to del_ptr below
4804 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004805 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004806 extent_buffer_get(leaf);
4807
Chris Masonb9473432009-03-13 11:00:37 -04004808 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004809 wret = push_leaf_left(trans, root, path, 1, 1,
4810 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004811 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004812 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004813
4814 if (path->nodes[0] == leaf &&
4815 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004816 wret = push_leaf_right(trans, root, path, 1,
4817 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004818 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004819 ret = wret;
4820 }
Chris Mason5f39d392007-10-15 16:14:19 -04004821
4822 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004823 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004824 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004825 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004826 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004827 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004828 /* if we're still in the path, make sure
4829 * we're dirty. Otherwise, one of the
4830 * push_leaf functions must have already
4831 * dirtied this buffer
4832 */
4833 if (path->nodes[0] == leaf)
4834 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004835 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004836 }
Chris Masond5719762007-03-23 10:01:08 -04004837 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004838 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004839 }
4840 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004841 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004842}
4843
Chris Mason97571fd2007-02-24 13:39:08 -05004844/*
Chris Mason925baed2008-06-25 16:01:30 -04004845 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004846 * returns 0 if it found something or 1 if there are no lesser leaves.
4847 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004848 *
4849 * This may release the path, and so you may lose any locks held at the
4850 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004851 */
4852int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4853{
Chris Mason925baed2008-06-25 16:01:30 -04004854 struct btrfs_key key;
4855 struct btrfs_disk_key found_key;
4856 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004857
Chris Mason925baed2008-06-25 16:01:30 -04004858 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004859
Chris Mason925baed2008-06-25 16:01:30 -04004860 if (key.offset > 0)
4861 key.offset--;
4862 else if (key.type > 0)
4863 key.type--;
4864 else if (key.objectid > 0)
4865 key.objectid--;
4866 else
4867 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004868
David Sterbab3b4aa72011-04-21 01:20:15 +02004869 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004870 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4871 if (ret < 0)
4872 return ret;
4873 btrfs_item_key(path->nodes[0], &found_key, 0);
4874 ret = comp_keys(&found_key, &key);
4875 if (ret < 0)
4876 return 0;
4877 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004878}
4879
Chris Mason3f157a22008-06-25 16:01:31 -04004880/*
4881 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004882 * for nodes or leaves that are have a minimum transaction id.
4883 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004884 *
4885 * This does not cow, but it does stuff the starting key it finds back
4886 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4887 * key and get a writable path.
4888 *
4889 * This does lock as it descends, and path->keep_locks should be set
4890 * to 1 by the caller.
4891 *
4892 * This honors path->lowest_level to prevent descent past a given level
4893 * of the tree.
4894 *
Chris Masond352ac62008-09-29 15:18:18 -04004895 * min_trans indicates the oldest transaction that you are interested
4896 * in walking through. Any nodes or leaves older than min_trans are
4897 * skipped over (without reading them).
4898 *
Chris Mason3f157a22008-06-25 16:01:31 -04004899 * returns zero if something useful was found, < 0 on error and 1 if there
4900 * was nothing in the tree that matched the search criteria.
4901 */
4902int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004903 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004904 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004905 u64 min_trans)
4906{
4907 struct extent_buffer *cur;
4908 struct btrfs_key found_key;
4909 int slot;
Yan96524802008-07-24 12:19:49 -04004910 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004911 u32 nritems;
4912 int level;
4913 int ret = 1;
4914
Chris Mason934d3752008-12-08 16:43:10 -05004915 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004916again:
Chris Masonbd681512011-07-16 15:23:14 -04004917 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004918 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004919 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004920 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004921 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004922
4923 if (btrfs_header_generation(cur) < min_trans) {
4924 ret = 1;
4925 goto out;
4926 }
Chris Masond3977122009-01-05 21:25:51 -05004927 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004928 nritems = btrfs_header_nritems(cur);
4929 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004930 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004931
Chris Mason323ac952008-10-01 19:05:46 -04004932 /* at the lowest level, we're done, setup the path and exit */
4933 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004934 if (slot >= nritems)
4935 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004936 ret = 0;
4937 path->slots[level] = slot;
4938 btrfs_item_key_to_cpu(cur, &found_key, slot);
4939 goto out;
4940 }
Yan96524802008-07-24 12:19:49 -04004941 if (sret && slot > 0)
4942 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004943 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004944 * check this node pointer against the min_trans parameters.
4945 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004946 */
Chris Masond3977122009-01-05 21:25:51 -05004947 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004948 u64 blockptr;
4949 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004950
Chris Mason3f157a22008-06-25 16:01:31 -04004951 blockptr = btrfs_node_blockptr(cur, slot);
4952 gen = btrfs_node_ptr_generation(cur, slot);
4953 if (gen < min_trans) {
4954 slot++;
4955 continue;
4956 }
Eric Sandeende78b512013-01-31 18:21:12 +00004957 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004958 }
Chris Masone02119d2008-09-05 16:13:11 -04004959find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004960 /*
4961 * we didn't find a candidate key in this node, walk forward
4962 * and find another one
4963 */
4964 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004965 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004966 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004967 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004968 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004969 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004970 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004971 goto again;
4972 } else {
4973 goto out;
4974 }
4975 }
4976 /* save our key for returning back */
4977 btrfs_node_key_to_cpu(cur, &found_key, slot);
4978 path->slots[level] = slot;
4979 if (level == path->lowest_level) {
4980 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004981 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004982 goto out;
4983 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004984 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004985 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004986 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004987
Chris Masonbd681512011-07-16 15:23:14 -04004988 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004989
Chris Masonbd681512011-07-16 15:23:14 -04004990 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004991 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004992 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004993 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004994 }
4995out:
4996 if (ret == 0)
4997 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004998 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004999 return ret;
5000}
5001
Alexander Block70698302012-06-05 21:07:48 +02005002static void tree_move_down(struct btrfs_root *root,
5003 struct btrfs_path *path,
5004 int *level, int root_level)
5005{
Chris Mason74dd17fb2012-08-07 16:25:13 -04005006 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005007 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5008 path->slots[*level]);
5009 path->slots[*level - 1] = 0;
5010 (*level)--;
5011}
5012
5013static int tree_move_next_or_upnext(struct btrfs_root *root,
5014 struct btrfs_path *path,
5015 int *level, int root_level)
5016{
5017 int ret = 0;
5018 int nritems;
5019 nritems = btrfs_header_nritems(path->nodes[*level]);
5020
5021 path->slots[*level]++;
5022
Chris Mason74dd17fb2012-08-07 16:25:13 -04005023 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005024 if (*level == root_level)
5025 return -1;
5026
5027 /* move upnext */
5028 path->slots[*level] = 0;
5029 free_extent_buffer(path->nodes[*level]);
5030 path->nodes[*level] = NULL;
5031 (*level)++;
5032 path->slots[*level]++;
5033
5034 nritems = btrfs_header_nritems(path->nodes[*level]);
5035 ret = 1;
5036 }
5037 return ret;
5038}
5039
5040/*
5041 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5042 * or down.
5043 */
5044static int tree_advance(struct btrfs_root *root,
5045 struct btrfs_path *path,
5046 int *level, int root_level,
5047 int allow_down,
5048 struct btrfs_key *key)
5049{
5050 int ret;
5051
5052 if (*level == 0 || !allow_down) {
5053 ret = tree_move_next_or_upnext(root, path, level, root_level);
5054 } else {
5055 tree_move_down(root, path, level, root_level);
5056 ret = 0;
5057 }
5058 if (ret >= 0) {
5059 if (*level == 0)
5060 btrfs_item_key_to_cpu(path->nodes[*level], key,
5061 path->slots[*level]);
5062 else
5063 btrfs_node_key_to_cpu(path->nodes[*level], key,
5064 path->slots[*level]);
5065 }
5066 return ret;
5067}
5068
5069static int tree_compare_item(struct btrfs_root *left_root,
5070 struct btrfs_path *left_path,
5071 struct btrfs_path *right_path,
5072 char *tmp_buf)
5073{
5074 int cmp;
5075 int len1, len2;
5076 unsigned long off1, off2;
5077
5078 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5079 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5080 if (len1 != len2)
5081 return 1;
5082
5083 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5084 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5085 right_path->slots[0]);
5086
5087 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5088
5089 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5090 if (cmp)
5091 return 1;
5092 return 0;
5093}
5094
5095#define ADVANCE 1
5096#define ADVANCE_ONLY_NEXT -1
5097
5098/*
5099 * This function compares two trees and calls the provided callback for
5100 * every changed/new/deleted item it finds.
5101 * If shared tree blocks are encountered, whole subtrees are skipped, making
5102 * the compare pretty fast on snapshotted subvolumes.
5103 *
5104 * This currently works on commit roots only. As commit roots are read only,
5105 * we don't do any locking. The commit roots are protected with transactions.
5106 * Transactions are ended and rejoined when a commit is tried in between.
5107 *
5108 * This function checks for modifications done to the trees while comparing.
5109 * If it detects a change, it aborts immediately.
5110 */
5111int btrfs_compare_trees(struct btrfs_root *left_root,
5112 struct btrfs_root *right_root,
5113 btrfs_changed_cb_t changed_cb, void *ctx)
5114{
5115 int ret;
5116 int cmp;
5117 struct btrfs_trans_handle *trans = NULL;
5118 struct btrfs_path *left_path = NULL;
5119 struct btrfs_path *right_path = NULL;
5120 struct btrfs_key left_key;
5121 struct btrfs_key right_key;
5122 char *tmp_buf = NULL;
5123 int left_root_level;
5124 int right_root_level;
5125 int left_level;
5126 int right_level;
5127 int left_end_reached;
5128 int right_end_reached;
5129 int advance_left;
5130 int advance_right;
5131 u64 left_blockptr;
5132 u64 right_blockptr;
5133 u64 left_start_ctransid;
5134 u64 right_start_ctransid;
5135 u64 ctransid;
5136
5137 left_path = btrfs_alloc_path();
5138 if (!left_path) {
5139 ret = -ENOMEM;
5140 goto out;
5141 }
5142 right_path = btrfs_alloc_path();
5143 if (!right_path) {
5144 ret = -ENOMEM;
5145 goto out;
5146 }
5147
5148 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5149 if (!tmp_buf) {
5150 ret = -ENOMEM;
5151 goto out;
5152 }
5153
5154 left_path->search_commit_root = 1;
5155 left_path->skip_locking = 1;
5156 right_path->search_commit_root = 1;
5157 right_path->skip_locking = 1;
5158
Anand Jain5f3ab902012-12-07 09:28:54 +00005159 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005160 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005161 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005162
Anand Jain5f3ab902012-12-07 09:28:54 +00005163 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005164 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005165 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005166
5167 trans = btrfs_join_transaction(left_root);
5168 if (IS_ERR(trans)) {
5169 ret = PTR_ERR(trans);
5170 trans = NULL;
5171 goto out;
5172 }
5173
5174 /*
5175 * Strategy: Go to the first items of both trees. Then do
5176 *
5177 * If both trees are at level 0
5178 * Compare keys of current items
5179 * If left < right treat left item as new, advance left tree
5180 * and repeat
5181 * If left > right treat right item as deleted, advance right tree
5182 * and repeat
5183 * If left == right do deep compare of items, treat as changed if
5184 * needed, advance both trees and repeat
5185 * If both trees are at the same level but not at level 0
5186 * Compare keys of current nodes/leafs
5187 * If left < right advance left tree and repeat
5188 * If left > right advance right tree and repeat
5189 * If left == right compare blockptrs of the next nodes/leafs
5190 * If they match advance both trees but stay at the same level
5191 * and repeat
5192 * If they don't match advance both trees while allowing to go
5193 * deeper and repeat
5194 * If tree levels are different
5195 * Advance the tree that needs it and repeat
5196 *
5197 * Advancing a tree means:
5198 * If we are at level 0, try to go to the next slot. If that's not
5199 * possible, go one level up and repeat. Stop when we found a level
5200 * where we could go to the next slot. We may at this point be on a
5201 * node or a leaf.
5202 *
5203 * If we are not at level 0 and not on shared tree blocks, go one
5204 * level deeper.
5205 *
5206 * If we are not at level 0 and on shared tree blocks, go one slot to
5207 * the right if possible or go up and right.
5208 */
5209
5210 left_level = btrfs_header_level(left_root->commit_root);
5211 left_root_level = left_level;
5212 left_path->nodes[left_level] = left_root->commit_root;
5213 extent_buffer_get(left_path->nodes[left_level]);
5214
5215 right_level = btrfs_header_level(right_root->commit_root);
5216 right_root_level = right_level;
5217 right_path->nodes[right_level] = right_root->commit_root;
5218 extent_buffer_get(right_path->nodes[right_level]);
5219
5220 if (left_level == 0)
5221 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5222 &left_key, left_path->slots[left_level]);
5223 else
5224 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5225 &left_key, left_path->slots[left_level]);
5226 if (right_level == 0)
5227 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5228 &right_key, right_path->slots[right_level]);
5229 else
5230 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5231 &right_key, right_path->slots[right_level]);
5232
5233 left_end_reached = right_end_reached = 0;
5234 advance_left = advance_right = 0;
5235
5236 while (1) {
5237 /*
5238 * We need to make sure the transaction does not get committed
5239 * while we do anything on commit roots. This means, we need to
5240 * join and leave transactions for every item that we process.
5241 */
5242 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5243 btrfs_release_path(left_path);
5244 btrfs_release_path(right_path);
5245
5246 ret = btrfs_end_transaction(trans, left_root);
5247 trans = NULL;
5248 if (ret < 0)
5249 goto out;
5250 }
5251 /* now rejoin the transaction */
5252 if (!trans) {
5253 trans = btrfs_join_transaction(left_root);
5254 if (IS_ERR(trans)) {
5255 ret = PTR_ERR(trans);
5256 trans = NULL;
5257 goto out;
5258 }
5259
Anand Jain5f3ab902012-12-07 09:28:54 +00005260 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005261 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005262 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005263 if (ctransid != left_start_ctransid)
5264 left_start_ctransid = 0;
5265
Anand Jain5f3ab902012-12-07 09:28:54 +00005266 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005267 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005268 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005269 if (ctransid != right_start_ctransid)
5270 right_start_ctransid = 0;
5271
5272 if (!left_start_ctransid || !right_start_ctransid) {
5273 WARN(1, KERN_WARNING
5274 "btrfs: btrfs_compare_tree detected "
5275 "a change in one of the trees while "
5276 "iterating. This is probably a "
5277 "bug.\n");
5278 ret = -EIO;
5279 goto out;
5280 }
5281
5282 /*
5283 * the commit root may have changed, so start again
5284 * where we stopped
5285 */
5286 left_path->lowest_level = left_level;
5287 right_path->lowest_level = right_level;
5288 ret = btrfs_search_slot(NULL, left_root,
5289 &left_key, left_path, 0, 0);
5290 if (ret < 0)
5291 goto out;
5292 ret = btrfs_search_slot(NULL, right_root,
5293 &right_key, right_path, 0, 0);
5294 if (ret < 0)
5295 goto out;
5296 }
5297
5298 if (advance_left && !left_end_reached) {
5299 ret = tree_advance(left_root, left_path, &left_level,
5300 left_root_level,
5301 advance_left != ADVANCE_ONLY_NEXT,
5302 &left_key);
5303 if (ret < 0)
5304 left_end_reached = ADVANCE;
5305 advance_left = 0;
5306 }
5307 if (advance_right && !right_end_reached) {
5308 ret = tree_advance(right_root, right_path, &right_level,
5309 right_root_level,
5310 advance_right != ADVANCE_ONLY_NEXT,
5311 &right_key);
5312 if (ret < 0)
5313 right_end_reached = ADVANCE;
5314 advance_right = 0;
5315 }
5316
5317 if (left_end_reached && right_end_reached) {
5318 ret = 0;
5319 goto out;
5320 } else if (left_end_reached) {
5321 if (right_level == 0) {
5322 ret = changed_cb(left_root, right_root,
5323 left_path, right_path,
5324 &right_key,
5325 BTRFS_COMPARE_TREE_DELETED,
5326 ctx);
5327 if (ret < 0)
5328 goto out;
5329 }
5330 advance_right = ADVANCE;
5331 continue;
5332 } else if (right_end_reached) {
5333 if (left_level == 0) {
5334 ret = changed_cb(left_root, right_root,
5335 left_path, right_path,
5336 &left_key,
5337 BTRFS_COMPARE_TREE_NEW,
5338 ctx);
5339 if (ret < 0)
5340 goto out;
5341 }
5342 advance_left = ADVANCE;
5343 continue;
5344 }
5345
5346 if (left_level == 0 && right_level == 0) {
5347 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5348 if (cmp < 0) {
5349 ret = changed_cb(left_root, right_root,
5350 left_path, right_path,
5351 &left_key,
5352 BTRFS_COMPARE_TREE_NEW,
5353 ctx);
5354 if (ret < 0)
5355 goto out;
5356 advance_left = ADVANCE;
5357 } else if (cmp > 0) {
5358 ret = changed_cb(left_root, right_root,
5359 left_path, right_path,
5360 &right_key,
5361 BTRFS_COMPARE_TREE_DELETED,
5362 ctx);
5363 if (ret < 0)
5364 goto out;
5365 advance_right = ADVANCE;
5366 } else {
Chris Mason74dd17fb2012-08-07 16:25:13 -04005367 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005368 ret = tree_compare_item(left_root, left_path,
5369 right_path, tmp_buf);
5370 if (ret) {
Chris Mason74dd17fb2012-08-07 16:25:13 -04005371 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005372 ret = changed_cb(left_root, right_root,
5373 left_path, right_path,
5374 &left_key,
5375 BTRFS_COMPARE_TREE_CHANGED,
5376 ctx);
5377 if (ret < 0)
5378 goto out;
5379 }
5380 advance_left = ADVANCE;
5381 advance_right = ADVANCE;
5382 }
5383 } else if (left_level == right_level) {
5384 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5385 if (cmp < 0) {
5386 advance_left = ADVANCE;
5387 } else if (cmp > 0) {
5388 advance_right = ADVANCE;
5389 } else {
5390 left_blockptr = btrfs_node_blockptr(
5391 left_path->nodes[left_level],
5392 left_path->slots[left_level]);
5393 right_blockptr = btrfs_node_blockptr(
5394 right_path->nodes[right_level],
5395 right_path->slots[right_level]);
5396 if (left_blockptr == right_blockptr) {
5397 /*
5398 * As we're on a shared block, don't
5399 * allow to go deeper.
5400 */
5401 advance_left = ADVANCE_ONLY_NEXT;
5402 advance_right = ADVANCE_ONLY_NEXT;
5403 } else {
5404 advance_left = ADVANCE;
5405 advance_right = ADVANCE;
5406 }
5407 }
5408 } else if (left_level < right_level) {
5409 advance_right = ADVANCE;
5410 } else {
5411 advance_left = ADVANCE;
5412 }
5413 }
5414
5415out:
5416 btrfs_free_path(left_path);
5417 btrfs_free_path(right_path);
5418 kfree(tmp_buf);
5419
5420 if (trans) {
5421 if (!ret)
5422 ret = btrfs_end_transaction(trans, left_root);
5423 else
5424 btrfs_end_transaction(trans, left_root);
5425 }
5426
5427 return ret;
5428}
5429
Chris Mason3f157a22008-06-25 16:01:31 -04005430/*
5431 * this is similar to btrfs_next_leaf, but does not try to preserve
5432 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005433 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005434 *
5435 * 0 is returned if another key is found, < 0 if there are any errors
5436 * and 1 is returned if there are no higher keys in the tree
5437 *
5438 * path->keep_locks should be set to 1 on the search made before
5439 * calling this function.
5440 */
Chris Masone7a84562008-06-25 16:01:31 -04005441int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005442 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005443{
Chris Masone7a84562008-06-25 16:01:31 -04005444 int slot;
5445 struct extent_buffer *c;
5446
Chris Mason934d3752008-12-08 16:43:10 -05005447 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005448 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005449 if (!path->nodes[level])
5450 return 1;
5451
5452 slot = path->slots[level] + 1;
5453 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005454next:
Chris Masone7a84562008-06-25 16:01:31 -04005455 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005456 int ret;
5457 int orig_lowest;
5458 struct btrfs_key cur_key;
5459 if (level + 1 >= BTRFS_MAX_LEVEL ||
5460 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005461 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005462
5463 if (path->locks[level + 1]) {
5464 level++;
5465 continue;
5466 }
5467
5468 slot = btrfs_header_nritems(c) - 1;
5469 if (level == 0)
5470 btrfs_item_key_to_cpu(c, &cur_key, slot);
5471 else
5472 btrfs_node_key_to_cpu(c, &cur_key, slot);
5473
5474 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005475 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005476 path->lowest_level = level;
5477 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5478 0, 0);
5479 path->lowest_level = orig_lowest;
5480 if (ret < 0)
5481 return ret;
5482
5483 c = path->nodes[level];
5484 slot = path->slots[level];
5485 if (ret == 0)
5486 slot++;
5487 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005488 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005489
Chris Masone7a84562008-06-25 16:01:31 -04005490 if (level == 0)
5491 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005492 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005493 u64 gen = btrfs_node_ptr_generation(c, slot);
5494
Chris Mason3f157a22008-06-25 16:01:31 -04005495 if (gen < min_trans) {
5496 slot++;
5497 goto next;
5498 }
Chris Masone7a84562008-06-25 16:01:31 -04005499 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005500 }
Chris Masone7a84562008-06-25 16:01:31 -04005501 return 0;
5502 }
5503 return 1;
5504}
5505
Chris Mason7bb86312007-12-11 09:25:06 -05005506/*
Chris Mason925baed2008-06-25 16:01:30 -04005507 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005508 * returns 0 if it found something or 1 if there are no greater leaves.
5509 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005510 */
Chris Mason234b63a2007-03-13 10:46:10 -04005511int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005512{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005513 return btrfs_next_old_leaf(root, path, 0);
5514}
5515
5516int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5517 u64 time_seq)
5518{
Chris Masond97e63b2007-02-20 16:40:44 -05005519 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005520 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005521 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005522 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005523 struct btrfs_key key;
5524 u32 nritems;
5525 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005526 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005527 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005528
5529 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005530 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005531 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005532
Chris Mason8e73f272009-04-03 10:14:18 -04005533 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5534again:
5535 level = 1;
5536 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005537 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005538 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005539
Chris Masona2135012008-06-25 16:01:30 -04005540 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005541 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005542
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005543 if (time_seq)
5544 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5545 else
5546 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005547 path->keep_locks = 0;
5548
5549 if (ret < 0)
5550 return ret;
5551
Chris Masona2135012008-06-25 16:01:30 -04005552 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005553 /*
5554 * by releasing the path above we dropped all our locks. A balance
5555 * could have added more items next to the key that used to be
5556 * at the very end of the block. So, check again here and
5557 * advance the path if there are now more items available.
5558 */
Chris Masona2135012008-06-25 16:01:30 -04005559 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005560 if (ret == 0)
5561 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005562 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005563 goto done;
5564 }
Chris Masond97e63b2007-02-20 16:40:44 -05005565
Chris Masond3977122009-01-05 21:25:51 -05005566 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005567 if (!path->nodes[level]) {
5568 ret = 1;
5569 goto done;
5570 }
Chris Mason5f39d392007-10-15 16:14:19 -04005571
Chris Masond97e63b2007-02-20 16:40:44 -05005572 slot = path->slots[level] + 1;
5573 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005574 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005575 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005576 if (level == BTRFS_MAX_LEVEL) {
5577 ret = 1;
5578 goto done;
5579 }
Chris Masond97e63b2007-02-20 16:40:44 -05005580 continue;
5581 }
Chris Mason5f39d392007-10-15 16:14:19 -04005582
Chris Mason925baed2008-06-25 16:01:30 -04005583 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005584 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005585 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005586 }
Chris Mason5f39d392007-10-15 16:14:19 -04005587
Chris Mason8e73f272009-04-03 10:14:18 -04005588 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005589 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005590 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005591 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005592 if (ret == -EAGAIN)
5593 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005594
Chris Mason76a05b32009-05-14 13:24:30 -04005595 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005596 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005597 goto done;
5598 }
5599
Chris Mason5cd57b22008-06-25 16:01:30 -04005600 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005601 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005602 if (!ret && time_seq) {
5603 /*
5604 * If we don't get the lock, we may be racing
5605 * with push_leaf_left, holding that lock while
5606 * itself waiting for the leaf we've currently
5607 * locked. To solve this situation, we give up
5608 * on our lock and cycle.
5609 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005610 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005611 btrfs_release_path(path);
5612 cond_resched();
5613 goto again;
5614 }
Chris Mason8e73f272009-04-03 10:14:18 -04005615 if (!ret) {
5616 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005617 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005618 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005619 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005620 }
Chris Mason31533fb2011-07-26 16:01:59 -04005621 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005622 }
Chris Masond97e63b2007-02-20 16:40:44 -05005623 break;
5624 }
5625 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005626 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005627 level--;
5628 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005629 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005630 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005631
Chris Mason5f39d392007-10-15 16:14:19 -04005632 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005633 path->nodes[level] = next;
5634 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005635 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005636 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005637 if (!level)
5638 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005639
Chris Mason8e73f272009-04-03 10:14:18 -04005640 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005641 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005642 if (ret == -EAGAIN)
5643 goto again;
5644
Chris Mason76a05b32009-05-14 13:24:30 -04005645 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005646 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005647 goto done;
5648 }
5649
Chris Mason5cd57b22008-06-25 16:01:30 -04005650 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005651 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005652 if (!ret) {
5653 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005654 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005655 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005656 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005657 }
Chris Mason31533fb2011-07-26 16:01:59 -04005658 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005659 }
Chris Masond97e63b2007-02-20 16:40:44 -05005660 }
Chris Mason8e73f272009-04-03 10:14:18 -04005661 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005662done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005663 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005664 path->leave_spinning = old_spinning;
5665 if (!old_spinning)
5666 btrfs_set_path_blocking(path);
5667
5668 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005669}
Chris Mason0b86a832008-03-24 15:01:56 -04005670
Chris Mason3f157a22008-06-25 16:01:31 -04005671/*
5672 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5673 * searching until it gets past min_objectid or finds an item of 'type'
5674 *
5675 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5676 */
Chris Mason0b86a832008-03-24 15:01:56 -04005677int btrfs_previous_item(struct btrfs_root *root,
5678 struct btrfs_path *path, u64 min_objectid,
5679 int type)
5680{
5681 struct btrfs_key found_key;
5682 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005683 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005684 int ret;
5685
Chris Masond3977122009-01-05 21:25:51 -05005686 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005687 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005688 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005689 ret = btrfs_prev_leaf(root, path);
5690 if (ret != 0)
5691 return ret;
5692 } else {
5693 path->slots[0]--;
5694 }
5695 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005696 nritems = btrfs_header_nritems(leaf);
5697 if (nritems == 0)
5698 return 1;
5699 if (path->slots[0] == nritems)
5700 path->slots[0]--;
5701
Chris Mason0b86a832008-03-24 15:01:56 -04005702 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005703 if (found_key.objectid < min_objectid)
5704 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005705 if (found_key.type == type)
5706 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005707 if (found_key.objectid == min_objectid &&
5708 found_key.type < type)
5709 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005710 }
5711 return 1;
5712}