blob: 0708ebed2df70f40cbff443d10dfeb4ffff568f0 [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;
Josef Bacikc8cc6342013-07-01 16:18:19 -0400487 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200488
Josef Bacikc8cc6342013-07-01 16:18:19 -0400489 BUG_ON(!tm);
490
491 tree_mod_log_write_lock(fs_info);
492 if (list_empty(&fs_info->tree_mod_seq_list)) {
493 tree_mod_log_write_unlock(fs_info);
494 /*
495 * Ok we no longer care about logging modifications, free up tm
496 * and return 0. Any callers shouldn't be using tm after
497 * calling tree_mod_log_insert, but if they do we can just
498 * change this to return a special error code to let the callers
499 * do their own thing.
500 */
501 kfree(tm);
502 return 0;
503 }
504
505 spin_lock(&fs_info->tree_mod_seq_lock);
506 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
507 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200508
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200509 tm_root = &fs_info->tree_mod_log;
510 new = &tm_root->rb_node;
511 while (*new) {
512 cur = container_of(*new, struct tree_mod_elem, node);
513 parent = *new;
514 if (cur->index < tm->index)
515 new = &((*new)->rb_left);
516 else if (cur->index > tm->index)
517 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200518 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200519 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200520 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200521 new = &((*new)->rb_right);
522 else {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400523 ret = -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200524 kfree(tm);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400525 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200526 }
527 }
528
529 rb_link_node(&tm->node, parent, new);
530 rb_insert_color(&tm->node, tm_root);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400531out:
532 tree_mod_log_write_unlock(fs_info);
533 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200534}
535
Jan Schmidt097b8a72012-06-21 11:08:04 +0200536/*
537 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
538 * returns zero with the tree_mod_log_lock acquired. The caller must hold
539 * this until all tree mod log insertions are recorded in the rb tree and then
540 * call tree_mod_log_write_unlock() to release.
541 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200542static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
543 struct extent_buffer *eb) {
544 smp_mb();
545 if (list_empty(&(fs_info)->tree_mod_seq_list))
546 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200547 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200548 return 1;
549 return 0;
550}
551
Jan Schmidt097b8a72012-06-21 11:08:04 +0200552static inline int
553__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
554 struct extent_buffer *eb, int slot,
555 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200557 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200558
Josef Bacikc8cc6342013-07-01 16:18:19 -0400559 tm = kzalloc(sizeof(*tm), flags);
560 if (!tm)
561 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200562
563 tm->index = eb->start >> PAGE_CACHE_SHIFT;
564 if (op != MOD_LOG_KEY_ADD) {
565 btrfs_node_key(eb, &tm->key, slot);
566 tm->blockptr = btrfs_node_blockptr(eb, slot);
567 }
568 tm->op = op;
569 tm->slot = slot;
570 tm->generation = btrfs_node_ptr_generation(eb, slot);
571
Jan Schmidt097b8a72012-06-21 11:08:04 +0200572 return __tree_mod_log_insert(fs_info, tm);
573}
574
575static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400576tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
577 struct extent_buffer *eb, int slot,
578 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200579{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200580 if (tree_mod_dont_log(fs_info, eb))
581 return 0;
582
Josef Bacikc8cc6342013-07-01 16:18:19 -0400583 return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200584}
585
586static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200587tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
588 struct extent_buffer *eb, int dst_slot, int src_slot,
589 int nr_items, gfp_t flags)
590{
591 struct tree_mod_elem *tm;
592 int ret;
593 int i;
594
Jan Schmidtf3956942012-05-31 15:02:32 +0200595 if (tree_mod_dont_log(fs_info, eb))
596 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200597
Jan Schmidt01763a22012-10-23 15:02:12 +0200598 /*
599 * When we override something during the move, we log these removals.
600 * This can only happen when we move towards the beginning of the
601 * buffer, i.e. dst_slot < src_slot.
602 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200603 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400604 ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
605 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200606 BUG_ON(ret < 0);
607 }
608
Josef Bacikc8cc6342013-07-01 16:18:19 -0400609 tm = kzalloc(sizeof(*tm), flags);
610 if (!tm)
611 return -ENOMEM;
Jan Schmidtf3956942012-05-31 15:02:32 +0200612
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200613 tm->index = eb->start >> PAGE_CACHE_SHIFT;
614 tm->slot = src_slot;
615 tm->move.dst_slot = dst_slot;
616 tm->move.nr_items = nr_items;
617 tm->op = MOD_LOG_MOVE_KEYS;
618
Josef Bacikc8cc6342013-07-01 16:18:19 -0400619 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200620}
621
Jan Schmidt097b8a72012-06-21 11:08:04 +0200622static inline void
623__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
624{
625 int i;
626 u32 nritems;
627 int ret;
628
Chris Masonb12a3b12012-08-07 15:34:49 -0400629 if (btrfs_header_level(eb) == 0)
630 return;
631
Jan Schmidt097b8a72012-06-21 11:08:04 +0200632 nritems = btrfs_header_nritems(eb);
633 for (i = nritems - 1; i >= 0; i--) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400634 ret = __tree_mod_log_insert_key(fs_info, eb, i,
635 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200636 BUG_ON(ret < 0);
637 }
638}
639
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200640static noinline int
641tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
642 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000643 struct extent_buffer *new_root, gfp_t flags,
644 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200645{
646 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200647
Jan Schmidt097b8a72012-06-21 11:08:04 +0200648 if (tree_mod_dont_log(fs_info, NULL))
649 return 0;
650
Jan Schmidt90f8d622013-04-13 13:19:53 +0000651 if (log_removal)
652 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000653
Josef Bacikc8cc6342013-07-01 16:18:19 -0400654 tm = kzalloc(sizeof(*tm), flags);
655 if (!tm)
656 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200657
658 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
659 tm->old_root.logical = old_root->start;
660 tm->old_root.level = btrfs_header_level(old_root);
661 tm->generation = btrfs_header_generation(old_root);
662 tm->op = MOD_LOG_ROOT_REPLACE;
663
Josef Bacikc8cc6342013-07-01 16:18:19 -0400664 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200665}
666
667static struct tree_mod_elem *
668__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
669 int smallest)
670{
671 struct rb_root *tm_root;
672 struct rb_node *node;
673 struct tree_mod_elem *cur = NULL;
674 struct tree_mod_elem *found = NULL;
675 u64 index = start >> PAGE_CACHE_SHIFT;
676
Jan Schmidt097b8a72012-06-21 11:08:04 +0200677 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200678 tm_root = &fs_info->tree_mod_log;
679 node = tm_root->rb_node;
680 while (node) {
681 cur = container_of(node, struct tree_mod_elem, node);
682 if (cur->index < index) {
683 node = node->rb_left;
684 } else if (cur->index > index) {
685 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200686 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200687 node = node->rb_left;
688 } else if (!smallest) {
689 /* we want the node with the highest seq */
690 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200691 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200692 found = cur;
693 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200694 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200695 /* we want the node with the smallest seq */
696 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200697 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200698 found = cur;
699 node = node->rb_right;
700 } else {
701 found = cur;
702 break;
703 }
704 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200705 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200706
707 return found;
708}
709
710/*
711 * this returns the element from the log with the smallest time sequence
712 * value that's in the log (the oldest log item). any element with a time
713 * sequence lower than min_seq will be ignored.
714 */
715static struct tree_mod_elem *
716tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
717 u64 min_seq)
718{
719 return __tree_mod_log_search(fs_info, start, min_seq, 1);
720}
721
722/*
723 * this returns the element from the log with the largest time sequence
724 * value that's in the log (the most recent log item). any element with
725 * a time sequence lower than min_seq will be ignored.
726 */
727static struct tree_mod_elem *
728tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
729{
730 return __tree_mod_log_search(fs_info, start, min_seq, 0);
731}
732
Jan Schmidt097b8a72012-06-21 11:08:04 +0200733static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200734tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
735 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000736 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200737{
738 int ret;
739 int i;
740
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200741 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200742 return;
743
Josef Bacikc8cc6342013-07-01 16:18:19 -0400744 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200745 return;
746
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200747 for (i = 0; i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400748 ret = __tree_mod_log_insert_key(fs_info, src,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000749 i + src_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400750 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Jan Schmidt90f8d622013-04-13 13:19:53 +0000751 BUG_ON(ret < 0);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400752 ret = __tree_mod_log_insert_key(fs_info, dst,
Jan Schmidt097b8a72012-06-21 11:08:04 +0200753 i + dst_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400754 MOD_LOG_KEY_ADD,
755 GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200756 BUG_ON(ret < 0);
757 }
758}
759
760static inline void
761tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
762 int dst_offset, int src_offset, int nr_items)
763{
764 int ret;
765 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
766 nr_items, GFP_NOFS);
767 BUG_ON(ret < 0);
768}
769
Jan Schmidt097b8a72012-06-21 11:08:04 +0200770static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200771tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000772 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200773{
774 int ret;
775
Josef Bacikc8cc6342013-07-01 16:18:19 -0400776 ret = __tree_mod_log_insert_key(fs_info, eb, slot,
777 MOD_LOG_KEY_REPLACE,
778 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200779 BUG_ON(ret < 0);
780}
781
Jan Schmidt097b8a72012-06-21 11:08:04 +0200782static noinline void
783tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200784{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200785 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200786 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200787 __tree_mod_log_free_eb(fs_info, eb);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200788}
789
Jan Schmidt097b8a72012-06-21 11:08:04 +0200790static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200791tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000792 struct extent_buffer *new_root_node,
793 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200794{
795 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200796 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000797 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200798 BUG_ON(ret < 0);
799}
800
Chris Masond352ac62008-09-29 15:18:18 -0400801/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400802 * check if the tree block can be shared by multiple trees
803 */
804int btrfs_block_can_be_shared(struct btrfs_root *root,
805 struct extent_buffer *buf)
806{
807 /*
808 * Tree blocks not in refernece counted trees and tree roots
809 * are never shared. If a block was allocated after the last
810 * snapshot and the block was not allocated by tree relocation,
811 * we know the block is not shared.
812 */
813 if (root->ref_cows &&
814 buf != root->node && buf != root->commit_root &&
815 (btrfs_header_generation(buf) <=
816 btrfs_root_last_snapshot(&root->root_item) ||
817 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
818 return 1;
819#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
820 if (root->ref_cows &&
821 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
822 return 1;
823#endif
824 return 0;
825}
826
827static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
828 struct btrfs_root *root,
829 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400830 struct extent_buffer *cow,
831 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400832{
833 u64 refs;
834 u64 owner;
835 u64 flags;
836 u64 new_flags = 0;
837 int ret;
838
839 /*
840 * Backrefs update rules:
841 *
842 * Always use full backrefs for extent pointers in tree block
843 * allocated by tree relocation.
844 *
845 * If a shared tree block is no longer referenced by its owner
846 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
847 * use full backrefs for extent pointers in tree block.
848 *
849 * If a tree block is been relocating
850 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
851 * use full backrefs for extent pointers in tree block.
852 * The reason for this is some operations (such as drop tree)
853 * are only allowed for blocks use full backrefs.
854 */
855
856 if (btrfs_block_can_be_shared(root, buf)) {
857 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500858 btrfs_header_level(buf), 1,
859 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700860 if (ret)
861 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700862 if (refs == 0) {
863 ret = -EROFS;
864 btrfs_std_error(root->fs_info, ret);
865 return ret;
866 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 } else {
868 refs = 1;
869 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
870 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
871 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
872 else
873 flags = 0;
874 }
875
876 owner = btrfs_header_owner(buf);
877 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
878 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
879
880 if (refs > 1) {
881 if ((owner == root->root_key.objectid ||
882 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
883 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200884 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886
887 if (root->root_key.objectid ==
888 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200889 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200891 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893 }
894 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
895 } else {
896
897 if (root->root_key.objectid ==
898 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200899 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200901 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400903 }
904 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400905 int level = btrfs_header_level(buf);
906
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 ret = btrfs_set_disk_extent_flags(trans, root,
908 buf->start,
909 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400910 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700911 if (ret)
912 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 }
914 } else {
915 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
916 if (root->root_key.objectid ==
917 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200918 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200920 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200922 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100923 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 }
925 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400926 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 }
928 return 0;
929}
930
931/*
Chris Masond3977122009-01-05 21:25:51 -0500932 * does the dirty work in cow of a single block. The parent block (if
933 * supplied) is updated to point to the new cow copy. The new buffer is marked
934 * dirty and returned locked. If you modify the block it needs to be marked
935 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400936 *
937 * search_start -- an allocation hint for the new block
938 *
Chris Masond3977122009-01-05 21:25:51 -0500939 * empty_size -- a hint that you plan on doing more cow. This is the size in
940 * bytes the allocator should try to find free next to the block it returns.
941 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400942 */
Chris Masond3977122009-01-05 21:25:51 -0500943static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400944 struct btrfs_root *root,
945 struct extent_buffer *buf,
946 struct extent_buffer *parent, int parent_slot,
947 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400948 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400949{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400951 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700952 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400953 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400954 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400956
Chris Mason925baed2008-06-25 16:01:30 -0400957 if (*cow_ret == buf)
958 unlock_orig = 1;
959
Chris Masonb9447ef2009-03-09 11:45:38 -0400960 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400961
Chris Mason7bb86312007-12-11 09:25:06 -0500962 WARN_ON(root->ref_cows && trans->transid !=
963 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400964 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400965
Chris Mason7bb86312007-12-11 09:25:06 -0500966 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400967
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 if (level == 0)
969 btrfs_item_key(buf, &disk_key, 0);
970 else
971 btrfs_node_key(buf, &disk_key, 0);
972
973 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
974 if (parent)
975 parent_start = parent->start;
976 else
977 parent_start = 0;
978 } else
979 parent_start = 0;
980
981 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
982 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200983 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400984 if (IS_ERR(cow))
985 return PTR_ERR(cow);
986
Chris Masonb4ce94d2009-02-04 09:25:08 -0500987 /* cow is set to blocking by btrfs_init_new_buffer */
988
Chris Mason5f39d392007-10-15 16:14:19 -0400989 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400990 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400991 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
993 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
994 BTRFS_HEADER_FLAG_RELOC);
995 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
996 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
997 else
998 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 write_extent_buffer(cow, root->fs_info->fsid,
1001 (unsigned long)btrfs_header_fsid(cow),
1002 BTRFS_FSID_SIZE);
1003
Mark Fashehbe1a5562011-08-08 13:20:18 -07001004 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001005 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001007 return ret;
1008 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001009
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001010 if (root->ref_cows)
1011 btrfs_reloc_cow_block(trans, root, buf, cow);
1012
Chris Mason6702ed42007-08-07 16:15:09 -04001013 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001014 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1016 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1017 parent_start = buf->start;
1018 else
1019 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001022 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001023 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001024
Yan, Zhengf0486c62010-05-16 10:46:25 -04001025 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001026 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001027 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001028 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001029 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1031 parent_start = parent->start;
1032 else
1033 parent_start = 0;
1034
1035 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001036 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001037 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001038 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001039 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001040 btrfs_set_node_ptr_generation(parent, parent_slot,
1041 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001042 btrfs_mark_buffer_dirty(parent);
Josef Bacik7fb7d76f2013-07-01 16:10:16 -04001043 if (last_ref)
1044 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001045 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001046 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001047 }
Chris Mason925baed2008-06-25 16:01:30 -04001048 if (unlock_orig)
1049 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001050 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001051 btrfs_mark_buffer_dirty(cow);
1052 *cow_ret = cow;
1053 return 0;
1054}
1055
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001056/*
1057 * returns the logical address of the oldest predecessor of the given root.
1058 * entries older than time_seq are ignored.
1059 */
1060static struct tree_mod_elem *
1061__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001062 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001063{
1064 struct tree_mod_elem *tm;
1065 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001066 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001067 int looped = 0;
1068
1069 if (!time_seq)
1070 return 0;
1071
1072 /*
1073 * the very last operation that's logged for a root is the replacement
1074 * operation (if it is replaced at all). this has the index of the *new*
1075 * root, making it the very first operation that's logged for this root.
1076 */
1077 while (1) {
1078 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1079 time_seq);
1080 if (!looped && !tm)
1081 return 0;
1082 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001083 * if there are no tree operation for the oldest root, we simply
1084 * return it. this should only happen if that (old) root is at
1085 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001086 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001087 if (!tm)
1088 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001089
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001090 /*
1091 * if there's an operation that's not a root replacement, we
1092 * found the oldest version of our root. normally, we'll find a
1093 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1094 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001095 if (tm->op != MOD_LOG_ROOT_REPLACE)
1096 break;
1097
1098 found = tm;
1099 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001100 looped = 1;
1101 }
1102
Jan Schmidta95236d2012-06-05 16:41:24 +02001103 /* if there's no old root to return, return what we found instead */
1104 if (!found)
1105 found = tm;
1106
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001107 return found;
1108}
1109
1110/*
1111 * tm is a pointer to the first operation to rewind within eb. then, all
1112 * previous operations will be rewinded (until we reach something older than
1113 * time_seq).
1114 */
1115static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001116__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1117 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001118{
1119 u32 n;
1120 struct rb_node *next;
1121 struct tree_mod_elem *tm = first_tm;
1122 unsigned long o_dst;
1123 unsigned long o_src;
1124 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1125
1126 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001127 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001128 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001129 /*
1130 * all the operations are recorded with the operator used for
1131 * the modification. as we're going backwards, we do the
1132 * opposite of each operation here.
1133 */
1134 switch (tm->op) {
1135 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1136 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001137 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001138 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001139 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001140 btrfs_set_node_key(eb, &tm->key, tm->slot);
1141 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1142 btrfs_set_node_ptr_generation(eb, tm->slot,
1143 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001144 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001145 break;
1146 case MOD_LOG_KEY_REPLACE:
1147 BUG_ON(tm->slot >= n);
1148 btrfs_set_node_key(eb, &tm->key, tm->slot);
1149 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1150 btrfs_set_node_ptr_generation(eb, tm->slot,
1151 tm->generation);
1152 break;
1153 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001154 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 n--;
1156 break;
1157 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001158 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1159 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1160 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001161 tm->move.nr_items * p_size);
1162 break;
1163 case MOD_LOG_ROOT_REPLACE:
1164 /*
1165 * this operation is special. for roots, this must be
1166 * handled explicitly before rewinding.
1167 * for non-roots, this operation may exist if the node
1168 * was a root: root A -> child B; then A gets empty and
1169 * B is promoted to the new root. in the mod log, we'll
1170 * have a root-replace operation for B, a tree block
1171 * that is no root. we simply ignore that operation.
1172 */
1173 break;
1174 }
1175 next = rb_next(&tm->node);
1176 if (!next)
1177 break;
1178 tm = container_of(next, struct tree_mod_elem, node);
1179 if (tm->index != first_tm->index)
1180 break;
1181 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001182 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001183 btrfs_set_header_nritems(eb, n);
1184}
1185
Jan Schmidt47fb0912013-04-13 13:19:55 +00001186/*
1187 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1188 * is returned. If rewind operations happen, a fresh buffer is returned. The
1189 * returned buffer is always read-locked. If the returned buffer is not the
1190 * input buffer, the lock on the input buffer is released and the input buffer
1191 * is freed (its refcount is decremented).
1192 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001193static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001194tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1195 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001196{
1197 struct extent_buffer *eb_rewin;
1198 struct tree_mod_elem *tm;
1199
1200 if (!time_seq)
1201 return eb;
1202
1203 if (btrfs_header_level(eb) == 0)
1204 return eb;
1205
1206 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1207 if (!tm)
1208 return eb;
1209
Josef Bacik9ec72672013-08-07 16:57:23 -04001210 btrfs_set_path_blocking(path);
1211 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1212
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001213 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1214 BUG_ON(tm->slot != 0);
1215 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1216 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001217 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001218 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001219 free_extent_buffer(eb);
1220 return NULL;
1221 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001222 btrfs_set_header_bytenr(eb_rewin, eb->start);
1223 btrfs_set_header_backref_rev(eb_rewin,
1224 btrfs_header_backref_rev(eb));
1225 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001226 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001227 } else {
1228 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001229 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001230 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001231 free_extent_buffer(eb);
1232 return NULL;
1233 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001234 }
1235
Josef Bacik9ec72672013-08-07 16:57:23 -04001236 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1237 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001238 free_extent_buffer(eb);
1239
Jan Schmidt47fb0912013-04-13 13:19:55 +00001240 extent_buffer_get(eb_rewin);
1241 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001242 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001243 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001244 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001245
1246 return eb_rewin;
1247}
1248
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001249/*
1250 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1251 * value. If there are no changes, the current root->root_node is returned. If
1252 * anything changed in between, there's a fresh buffer allocated on which the
1253 * rewind operations are done. In any case, the returned buffer is read locked.
1254 * Returns NULL on error (with no locks held).
1255 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001256static inline struct extent_buffer *
1257get_old_root(struct btrfs_root *root, u64 time_seq)
1258{
1259 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001260 struct extent_buffer *eb = NULL;
1261 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001262 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001263 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001264 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001265 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001266 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001267
Jan Schmidt30b04632013-04-13 13:19:54 +00001268 eb_root = btrfs_read_lock_root_node(root);
1269 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001270 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001271 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001272
Jan Schmidta95236d2012-06-05 16:41:24 +02001273 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1274 old_root = &tm->old_root;
1275 old_generation = tm->generation;
1276 logical = old_root->logical;
1277 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001278 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001279 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280
Jan Schmidta95236d2012-06-05 16:41:24 +02001281 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001282 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001283 btrfs_tree_read_unlock(eb_root);
1284 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001285 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001286 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001287 if (!old || !extent_buffer_uptodate(old)) {
1288 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001289 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1290 logical);
1291 WARN_ON(1);
1292 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001293 eb = btrfs_clone_extent_buffer(old);
1294 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001295 }
1296 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001297 btrfs_tree_read_unlock(eb_root);
1298 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001299 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001300 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001301 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001302 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001303 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001304 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001305 }
1306
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001307 if (!eb)
1308 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001309 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001310 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001311 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001312 btrfs_set_header_bytenr(eb, eb->start);
1313 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001314 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001315 btrfs_set_header_level(eb, old_root->level);
1316 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001317 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001318 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001319 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001320 else
1321 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001322 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001323
1324 return eb;
1325}
1326
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001327int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1328{
1329 struct tree_mod_elem *tm;
1330 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001331 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001332
Jan Schmidt30b04632013-04-13 13:19:54 +00001333 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001334 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1335 level = tm->old_root.level;
1336 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001337 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001338 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001339 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001340
1341 return level;
1342}
1343
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001344static inline int should_cow_block(struct btrfs_trans_handle *trans,
1345 struct btrfs_root *root,
1346 struct extent_buffer *buf)
1347{
Liu Bof1ebcc72011-11-14 20:48:06 -05001348 /* ensure we can see the force_cow */
1349 smp_rmb();
1350
1351 /*
1352 * We do not need to cow a block if
1353 * 1) this block is not created or changed in this transaction;
1354 * 2) this block does not belong to TREE_RELOC tree;
1355 * 3) the root is not forced COW.
1356 *
1357 * What is forced COW:
1358 * when we create snapshot during commiting the transaction,
1359 * after we've finished coping src root, we must COW the shared
1360 * block to ensure the metadata consistency.
1361 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 if (btrfs_header_generation(buf) == trans->transid &&
1363 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1364 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001365 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1366 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001367 return 0;
1368 return 1;
1369}
1370
Chris Masond352ac62008-09-29 15:18:18 -04001371/*
1372 * cows a single block, see __btrfs_cow_block for the real work.
1373 * This version of it has extra checks so that a block isn't cow'd more than
1374 * once per transaction, as long as it hasn't been written yet
1375 */
Chris Masond3977122009-01-05 21:25:51 -05001376noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001377 struct btrfs_root *root, struct extent_buffer *buf,
1378 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001379 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001380{
Chris Mason6702ed42007-08-07 16:15:09 -04001381 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001382 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001383
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001384 if (trans->transaction != root->fs_info->running_transaction)
1385 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001386 (unsigned long long)trans->transid,
1387 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001388 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001389
1390 if (trans->transid != root->fs_info->generation)
1391 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001392 (unsigned long long)trans->transid,
1393 (unsigned long long)root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001394
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001395 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001396 *cow_ret = buf;
1397 return 0;
1398 }
Chris Masonc4876852009-02-04 09:24:25 -05001399
Chris Mason0b86a832008-03-24 15:01:56 -04001400 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001401
1402 if (parent)
1403 btrfs_set_lock_blocking(parent);
1404 btrfs_set_lock_blocking(buf);
1405
Chris Masonf510cfe2007-10-15 16:14:48 -04001406 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001407 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001408
1409 trace_btrfs_cow_block(root, buf, *cow_ret);
1410
Chris Masonf510cfe2007-10-15 16:14:48 -04001411 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001412}
1413
Chris Masond352ac62008-09-29 15:18:18 -04001414/*
1415 * helper function for defrag to decide if two blocks pointed to by a
1416 * node are actually close by
1417 */
Chris Mason6b800532007-10-15 16:17:34 -04001418static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001419{
Chris Mason6b800532007-10-15 16:17:34 -04001420 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001421 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001422 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001423 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001424 return 0;
1425}
1426
Chris Mason081e9572007-11-06 10:26:24 -05001427/*
1428 * compare two keys in a memcmp fashion
1429 */
1430static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1431{
1432 struct btrfs_key k1;
1433
1434 btrfs_disk_key_to_cpu(&k1, disk);
1435
Diego Calleja20736ab2009-07-24 11:06:52 -04001436 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001437}
1438
Josef Bacikf3465ca2008-11-12 14:19:50 -05001439/*
1440 * same as comp_keys only with two btrfs_key's
1441 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001442int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001443{
1444 if (k1->objectid > k2->objectid)
1445 return 1;
1446 if (k1->objectid < k2->objectid)
1447 return -1;
1448 if (k1->type > k2->type)
1449 return 1;
1450 if (k1->type < k2->type)
1451 return -1;
1452 if (k1->offset > k2->offset)
1453 return 1;
1454 if (k1->offset < k2->offset)
1455 return -1;
1456 return 0;
1457}
Chris Mason081e9572007-11-06 10:26:24 -05001458
Chris Masond352ac62008-09-29 15:18:18 -04001459/*
1460 * this is used by the defrag code to go through all the
1461 * leaves pointed to by a node and reallocate them so that
1462 * disk order is close to key order
1463 */
Chris Mason6702ed42007-08-07 16:15:09 -04001464int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001465 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001466 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001467 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001468{
Chris Mason6b800532007-10-15 16:17:34 -04001469 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001470 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001471 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001472 u64 search_start = *last_ret;
1473 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001474 u64 other;
1475 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001476 int end_slot;
1477 int i;
1478 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001479 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001480 int uptodate;
1481 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001482 int progress_passed = 0;
1483 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001484
Chris Mason5708b952007-10-25 15:43:18 -04001485 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001486
Julia Lawall6c1500f2012-11-03 20:30:18 +00001487 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1488 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001489
Chris Mason6b800532007-10-15 16:17:34 -04001490 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001491 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001492 end_slot = parent_nritems;
1493
1494 if (parent_nritems == 1)
1495 return 0;
1496
Chris Masonb4ce94d2009-02-04 09:25:08 -05001497 btrfs_set_lock_blocking(parent);
1498
Chris Mason6702ed42007-08-07 16:15:09 -04001499 for (i = start_slot; i < end_slot; i++) {
1500 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001501
Chris Mason081e9572007-11-06 10:26:24 -05001502 btrfs_node_key(parent, &disk_key, i);
1503 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1504 continue;
1505
1506 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001507 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001508 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001509 if (last_block == 0)
1510 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001511
Chris Mason6702ed42007-08-07 16:15:09 -04001512 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001513 other = btrfs_node_blockptr(parent, i - 1);
1514 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001515 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001516 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001517 other = btrfs_node_blockptr(parent, i + 1);
1518 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001519 }
Chris Masone9d0b132007-08-10 14:06:19 -04001520 if (close) {
1521 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001522 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001523 }
Chris Mason6702ed42007-08-07 16:15:09 -04001524
Chris Mason6b800532007-10-15 16:17:34 -04001525 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1526 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001527 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001528 else
1529 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001530 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001531 if (!cur) {
1532 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001533 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001534 if (!cur || !extent_buffer_uptodate(cur)) {
1535 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001536 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001537 }
Chris Mason6b800532007-10-15 16:17:34 -04001538 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001539 err = btrfs_read_buffer(cur, gen);
1540 if (err) {
1541 free_extent_buffer(cur);
1542 return err;
1543 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001544 }
Chris Mason6702ed42007-08-07 16:15:09 -04001545 }
Chris Masone9d0b132007-08-10 14:06:19 -04001546 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001547 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001548
Chris Masone7a84562008-06-25 16:01:31 -04001549 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001550 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001551 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001552 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001553 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001554 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001555 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001556 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001557 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001558 break;
Yan252c38f2007-08-29 09:11:44 -04001559 }
Chris Masone7a84562008-06-25 16:01:31 -04001560 search_start = cur->start;
1561 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001562 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001563 btrfs_tree_unlock(cur);
1564 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001565 }
1566 return err;
1567}
1568
Chris Mason74123bd2007-02-02 11:05:29 -05001569/*
1570 * The leaf data grows from end-to-front in the node.
1571 * this returns the address of the start of the last item,
1572 * which is the stop of the leaf data stack
1573 */
Chris Mason123abc82007-03-14 14:14:43 -04001574static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001575 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001576{
Chris Mason5f39d392007-10-15 16:14:19 -04001577 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001578 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001579 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001580 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001581}
1582
Chris Masonaa5d6be2007-02-28 16:35:06 -05001583
Chris Mason74123bd2007-02-02 11:05:29 -05001584/*
Chris Mason5f39d392007-10-15 16:14:19 -04001585 * search for key in the extent_buffer. The items start at offset p,
1586 * and they are item_size apart. There are 'max' items in p.
1587 *
Chris Mason74123bd2007-02-02 11:05:29 -05001588 * the slot in the array is returned via slot, and it points to
1589 * the place where you would insert key if it is not found in
1590 * the array.
1591 *
1592 * slot may point to max if the key is bigger than all of the keys
1593 */
Chris Masone02119d2008-09-05 16:13:11 -04001594static noinline int generic_bin_search(struct extent_buffer *eb,
1595 unsigned long p,
1596 int item_size, struct btrfs_key *key,
1597 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001598{
1599 int low = 0;
1600 int high = max;
1601 int mid;
1602 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001603 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001604 struct btrfs_disk_key unaligned;
1605 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001606 char *kaddr = NULL;
1607 unsigned long map_start = 0;
1608 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001609 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001610
Chris Masond3977122009-01-05 21:25:51 -05001611 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001613 offset = p + mid * item_size;
1614
Chris Masona6591712011-07-19 12:04:14 -04001615 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001616 (offset + sizeof(struct btrfs_disk_key)) >
1617 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001618
1619 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001620 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001621 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001622
Chris Mason479965d2007-10-15 16:14:27 -04001623 if (!err) {
1624 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1625 map_start);
1626 } else {
1627 read_extent_buffer(eb, &unaligned,
1628 offset, sizeof(unaligned));
1629 tmp = &unaligned;
1630 }
1631
Chris Mason5f39d392007-10-15 16:14:19 -04001632 } else {
1633 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1634 map_start);
1635 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001636 ret = comp_keys(tmp, key);
1637
1638 if (ret < 0)
1639 low = mid + 1;
1640 else if (ret > 0)
1641 high = mid;
1642 else {
1643 *slot = mid;
1644 return 0;
1645 }
1646 }
1647 *slot = low;
1648 return 1;
1649}
1650
Chris Mason97571fd2007-02-24 13:39:08 -05001651/*
1652 * simple bin_search frontend that does the right thing for
1653 * leaves vs nodes
1654 */
Chris Mason5f39d392007-10-15 16:14:19 -04001655static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1656 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001657{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001658 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001659 return generic_bin_search(eb,
1660 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001661 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001662 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001663 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001664 else
Chris Mason5f39d392007-10-15 16:14:19 -04001665 return generic_bin_search(eb,
1666 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001667 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001668 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001669 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001670}
1671
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001672int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1673 int level, int *slot)
1674{
1675 return bin_search(eb, key, level, slot);
1676}
1677
Yan, Zhengf0486c62010-05-16 10:46:25 -04001678static void root_add_used(struct btrfs_root *root, u32 size)
1679{
1680 spin_lock(&root->accounting_lock);
1681 btrfs_set_root_used(&root->root_item,
1682 btrfs_root_used(&root->root_item) + size);
1683 spin_unlock(&root->accounting_lock);
1684}
1685
1686static void root_sub_used(struct btrfs_root *root, u32 size)
1687{
1688 spin_lock(&root->accounting_lock);
1689 btrfs_set_root_used(&root->root_item,
1690 btrfs_root_used(&root->root_item) - size);
1691 spin_unlock(&root->accounting_lock);
1692}
1693
Chris Masond352ac62008-09-29 15:18:18 -04001694/* given a node and slot number, this reads the blocks it points to. The
1695 * extent buffer is returned with a reference taken (but unlocked).
1696 * NULL is returned on error.
1697 */
Chris Masone02119d2008-09-05 16:13:11 -04001698static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001699 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001700{
Chris Masonca7a79a2008-05-12 12:59:19 -04001701 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001702 struct extent_buffer *eb;
1703
Chris Masonbb803952007-03-01 12:04:21 -05001704 if (slot < 0)
1705 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001706 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001707 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001708
1709 BUG_ON(level == 0);
1710
Josef Bacik416bc652013-04-23 14:17:42 -04001711 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1712 btrfs_level_size(root, level - 1),
1713 btrfs_node_ptr_generation(parent, slot));
1714 if (eb && !extent_buffer_uptodate(eb)) {
1715 free_extent_buffer(eb);
1716 eb = NULL;
1717 }
1718
1719 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * node level balancing, used to make sure nodes are in proper order for
1724 * item deletion. We balance from the top down, so we have to make sure
1725 * that a deletion won't leave an node completely empty later on.
1726 */
Chris Masone02119d2008-09-05 16:13:11 -04001727static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001728 struct btrfs_root *root,
1729 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001730{
Chris Mason5f39d392007-10-15 16:14:19 -04001731 struct extent_buffer *right = NULL;
1732 struct extent_buffer *mid;
1733 struct extent_buffer *left = NULL;
1734 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001735 int ret = 0;
1736 int wret;
1737 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001738 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001739 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001740
1741 if (level == 0)
1742 return 0;
1743
Chris Mason5f39d392007-10-15 16:14:19 -04001744 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001745
Chris Masonbd681512011-07-16 15:23:14 -04001746 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1747 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001748 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1749
Chris Mason1d4f8a02007-03-13 09:28:32 -04001750 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001751
Li Zefana05a9bb2011-09-06 16:55:34 +08001752 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001753 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001754 pslot = path->slots[level + 1];
1755 }
Chris Masonbb803952007-03-01 12:04:21 -05001756
Chris Mason40689472007-03-17 14:29:23 -04001757 /*
1758 * deal with the case where there is only one pointer in the root
1759 * by promoting the node below to a root
1760 */
Chris Mason5f39d392007-10-15 16:14:19 -04001761 if (!parent) {
1762 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001763
Chris Mason5f39d392007-10-15 16:14:19 -04001764 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001765 return 0;
1766
1767 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001768 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001769 if (!child) {
1770 ret = -EROFS;
1771 btrfs_std_error(root->fs_info, ret);
1772 goto enospc;
1773 }
1774
Chris Mason925baed2008-06-25 16:01:30 -04001775 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001776 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001777 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001778 if (ret) {
1779 btrfs_tree_unlock(child);
1780 free_extent_buffer(child);
1781 goto enospc;
1782 }
Yan2f375ab2008-02-01 14:58:07 -05001783
Jan Schmidt90f8d622013-04-13 13:19:53 +00001784 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001785 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001786
Chris Mason0b86a832008-03-24 15:01:56 -04001787 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001788 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001789
Chris Mason925baed2008-06-25 16:01:30 -04001790 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001791 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001792 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001793 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001794 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001795 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001796
1797 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001798 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001799 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001800 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001801 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001802 }
Chris Mason5f39d392007-10-15 16:14:19 -04001803 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001804 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001805 return 0;
1806
Chris Mason5f39d392007-10-15 16:14:19 -04001807 left = read_node_slot(root, parent, pslot - 1);
1808 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001809 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001810 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001811 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001812 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001813 if (wret) {
1814 ret = wret;
1815 goto enospc;
1816 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001817 }
Chris Mason5f39d392007-10-15 16:14:19 -04001818 right = read_node_slot(root, parent, pslot + 1);
1819 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001820 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001821 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001822 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001823 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001824 if (wret) {
1825 ret = wret;
1826 goto enospc;
1827 }
1828 }
1829
1830 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001831 if (left) {
1832 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001833 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001834 if (wret < 0)
1835 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001836 }
Chris Mason79f95c82007-03-01 15:16:26 -05001837
1838 /*
1839 * then try to empty the right most buffer into the middle
1840 */
Chris Mason5f39d392007-10-15 16:14:19 -04001841 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001842 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001843 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001844 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001845 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001846 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001847 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001848 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001849 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001850 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001851 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001852 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001853 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001854 struct btrfs_disk_key right_key;
1855 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001856 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001857 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001858 btrfs_set_node_key(parent, &right_key, pslot + 1);
1859 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001860 }
1861 }
Chris Mason5f39d392007-10-15 16:14:19 -04001862 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001863 /*
1864 * we're not allowed to leave a node with one item in the
1865 * tree during a delete. A deletion from lower in the tree
1866 * could try to delete the only pointer in this node.
1867 * So, pull some keys from the left.
1868 * There has to be a left pointer at this point because
1869 * otherwise we would have pulled some pointers from the
1870 * right
1871 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001872 if (!left) {
1873 ret = -EROFS;
1874 btrfs_std_error(root->fs_info, ret);
1875 goto enospc;
1876 }
Chris Mason5f39d392007-10-15 16:14:19 -04001877 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001878 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001879 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001880 goto enospc;
1881 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001882 if (wret == 1) {
1883 wret = push_node_left(trans, root, left, mid, 1);
1884 if (wret < 0)
1885 ret = wret;
1886 }
Chris Mason79f95c82007-03-01 15:16:26 -05001887 BUG_ON(wret == 1);
1888 }
Chris Mason5f39d392007-10-15 16:14:19 -04001889 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001890 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001891 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001892 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001893 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001894 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001895 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001896 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001897 } else {
1898 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001899 struct btrfs_disk_key mid_key;
1900 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001901 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001902 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001903 btrfs_set_node_key(parent, &mid_key, pslot);
1904 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001905 }
Chris Masonbb803952007-03-01 12:04:21 -05001906
Chris Mason79f95c82007-03-01 15:16:26 -05001907 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001908 if (left) {
1909 if (btrfs_header_nritems(left) > orig_slot) {
1910 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001911 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001912 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001913 path->slots[level + 1] -= 1;
1914 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001915 if (mid) {
1916 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001917 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001918 }
Chris Masonbb803952007-03-01 12:04:21 -05001919 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001920 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001921 path->slots[level] = orig_slot;
1922 }
1923 }
Chris Mason79f95c82007-03-01 15:16:26 -05001924 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001925 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001926 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001927 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001928enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001929 if (right) {
1930 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001931 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001932 }
1933 if (left) {
1934 if (path->nodes[level] != left)
1935 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001936 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001937 }
Chris Masonbb803952007-03-01 12:04:21 -05001938 return ret;
1939}
1940
Chris Masond352ac62008-09-29 15:18:18 -04001941/* Node balancing for insertion. Here we only split or push nodes around
1942 * when they are completely full. This is also done top down, so we
1943 * have to be pessimistic.
1944 */
Chris Masond3977122009-01-05 21:25:51 -05001945static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001946 struct btrfs_root *root,
1947 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001948{
Chris Mason5f39d392007-10-15 16:14:19 -04001949 struct extent_buffer *right = NULL;
1950 struct extent_buffer *mid;
1951 struct extent_buffer *left = NULL;
1952 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001953 int ret = 0;
1954 int wret;
1955 int pslot;
1956 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001957
1958 if (level == 0)
1959 return 1;
1960
Chris Mason5f39d392007-10-15 16:14:19 -04001961 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001962 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001963
Li Zefana05a9bb2011-09-06 16:55:34 +08001964 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001965 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001966 pslot = path->slots[level + 1];
1967 }
Chris Masone66f7092007-04-20 13:16:02 -04001968
Chris Mason5f39d392007-10-15 16:14:19 -04001969 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001970 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001973
1974 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001975 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001976 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001977
1978 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001979 btrfs_set_lock_blocking(left);
1980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001982 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1983 wret = 1;
1984 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001985 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001986 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001987 if (ret)
1988 wret = 1;
1989 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001990 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001991 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001992 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001993 }
Chris Masone66f7092007-04-20 13:16:02 -04001994 if (wret < 0)
1995 ret = wret;
1996 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001997 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001998 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001999 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002000 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002001 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002002 btrfs_set_node_key(parent, &disk_key, pslot);
2003 btrfs_mark_buffer_dirty(parent);
2004 if (btrfs_header_nritems(left) > orig_slot) {
2005 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002006 path->slots[level + 1] -= 1;
2007 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002008 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002009 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002010 } else {
2011 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002012 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002013 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002014 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002015 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002016 }
Chris Masone66f7092007-04-20 13:16:02 -04002017 return 0;
2018 }
Chris Mason925baed2008-06-25 16:01:30 -04002019 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002020 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002021 }
Chris Mason925baed2008-06-25 16:01:30 -04002022 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002023
2024 /*
2025 * then try to empty the right most buffer into the middle
2026 */
Chris Mason5f39d392007-10-15 16:14:19 -04002027 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002028 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002029
Chris Mason925baed2008-06-25 16:01:30 -04002030 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002031 btrfs_set_lock_blocking(right);
2032
Chris Mason5f39d392007-10-15 16:14:19 -04002033 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002034 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2035 wret = 1;
2036 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002037 ret = btrfs_cow_block(trans, root, right,
2038 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002039 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002040 if (ret)
2041 wret = 1;
2042 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002043 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002044 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002045 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002046 }
Chris Masone66f7092007-04-20 13:16:02 -04002047 if (wret < 0)
2048 ret = wret;
2049 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002050 struct btrfs_disk_key disk_key;
2051
2052 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002053 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002054 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002055 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2056 btrfs_mark_buffer_dirty(parent);
2057
2058 if (btrfs_header_nritems(mid) <= orig_slot) {
2059 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002060 path->slots[level + 1] += 1;
2061 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002062 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002063 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002064 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002065 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002066 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002067 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002068 }
Chris Masone66f7092007-04-20 13:16:02 -04002069 return 0;
2070 }
Chris Mason925baed2008-06-25 16:01:30 -04002071 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002072 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002073 }
Chris Masone66f7092007-04-20 13:16:02 -04002074 return 1;
2075}
2076
Chris Mason74123bd2007-02-02 11:05:29 -05002077/*
Chris Masond352ac62008-09-29 15:18:18 -04002078 * readahead one full node of leaves, finding things that are close
2079 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002080 */
Chris Masonc8c42862009-04-03 10:14:18 -04002081static void reada_for_search(struct btrfs_root *root,
2082 struct btrfs_path *path,
2083 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002084{
Chris Mason5f39d392007-10-15 16:14:19 -04002085 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002086 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002087 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002088 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002089 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002090 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002091 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002092 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002093 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002094 u32 nr;
2095 u32 blocksize;
2096 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002097
Chris Masona6b6e752007-10-15 16:22:39 -04002098 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002099 return;
2100
Chris Mason6702ed42007-08-07 16:15:09 -04002101 if (!path->nodes[level])
2102 return;
2103
Chris Mason5f39d392007-10-15 16:14:19 -04002104 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002105
Chris Mason3c69fae2007-08-07 15:52:22 -04002106 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002107 blocksize = btrfs_level_size(root, level - 1);
2108 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002109 if (eb) {
2110 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002111 return;
2112 }
2113
Chris Masona7175312009-01-22 09:23:10 -05002114 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002115
Chris Mason5f39d392007-10-15 16:14:19 -04002116 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002117 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002118
Chris Masond3977122009-01-05 21:25:51 -05002119 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002120 if (direction < 0) {
2121 if (nr == 0)
2122 break;
2123 nr--;
2124 } else if (direction > 0) {
2125 nr++;
2126 if (nr >= nritems)
2127 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002128 }
Chris Mason01f46652007-12-21 16:24:26 -05002129 if (path->reada < 0 && objectid) {
2130 btrfs_node_key(node, &disk_key, nr);
2131 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2132 break;
2133 }
Chris Mason6b800532007-10-15 16:17:34 -04002134 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002135 if ((search <= target && target - search <= 65536) ||
2136 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002137 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002138 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002139 nread += blocksize;
2140 }
2141 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002142 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002143 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002144 }
2145}
Chris Mason925baed2008-06-25 16:01:30 -04002146
Josef Bacik0b088512013-06-17 14:23:02 -04002147static noinline void reada_for_balance(struct btrfs_root *root,
2148 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002149{
2150 int slot;
2151 int nritems;
2152 struct extent_buffer *parent;
2153 struct extent_buffer *eb;
2154 u64 gen;
2155 u64 block1 = 0;
2156 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002157 int blocksize;
2158
Chris Mason8c594ea2009-04-20 15:50:10 -04002159 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002160 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002161 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002162
2163 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002164 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002165 blocksize = btrfs_level_size(root, level);
2166
2167 if (slot > 0) {
2168 block1 = btrfs_node_blockptr(parent, slot - 1);
2169 gen = btrfs_node_ptr_generation(parent, slot - 1);
2170 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002171 /*
2172 * if we get -eagain from btrfs_buffer_uptodate, we
2173 * don't want to return eagain here. That will loop
2174 * forever
2175 */
2176 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002177 block1 = 0;
2178 free_extent_buffer(eb);
2179 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002180 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002181 block2 = btrfs_node_blockptr(parent, slot + 1);
2182 gen = btrfs_node_ptr_generation(parent, slot + 1);
2183 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002184 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002185 block2 = 0;
2186 free_extent_buffer(eb);
2187 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002188
Josef Bacik0b088512013-06-17 14:23:02 -04002189 if (block1)
2190 readahead_tree_block(root, block1, blocksize, 0);
2191 if (block2)
2192 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002193}
2194
2195
2196/*
Chris Masond3977122009-01-05 21:25:51 -05002197 * when we walk down the tree, it is usually safe to unlock the higher layers
2198 * in the tree. The exceptions are when our path goes through slot 0, because
2199 * operations on the tree might require changing key pointers higher up in the
2200 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002201 *
Chris Masond3977122009-01-05 21:25:51 -05002202 * callers might also have set path->keep_locks, which tells this code to keep
2203 * the lock if the path points to the last slot in the block. This is part of
2204 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002205 *
Chris Masond3977122009-01-05 21:25:51 -05002206 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2207 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002208 */
Chris Masone02119d2008-09-05 16:13:11 -04002209static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002210 int lowest_unlock, int min_write_lock_level,
2211 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002212{
2213 int i;
2214 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002215 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002216 struct extent_buffer *t;
2217
2218 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2219 if (!path->nodes[i])
2220 break;
2221 if (!path->locks[i])
2222 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002223 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002224 skip_level = i + 1;
2225 continue;
2226 }
Chris Mason051e1b92008-06-25 16:01:30 -04002227 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002228 u32 nritems;
2229 t = path->nodes[i];
2230 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002231 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002232 skip_level = i + 1;
2233 continue;
2234 }
2235 }
Chris Mason051e1b92008-06-25 16:01:30 -04002236 if (skip_level < i && i >= lowest_unlock)
2237 no_skips = 1;
2238
Chris Mason925baed2008-06-25 16:01:30 -04002239 t = path->nodes[i];
2240 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002241 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002242 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002243 if (write_lock_level &&
2244 i > min_write_lock_level &&
2245 i <= *write_lock_level) {
2246 *write_lock_level = i - 1;
2247 }
Chris Mason925baed2008-06-25 16:01:30 -04002248 }
2249 }
2250}
2251
Chris Mason3c69fae2007-08-07 15:52:22 -04002252/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002253 * This releases any locks held in the path starting at level and
2254 * going all the way up to the root.
2255 *
2256 * btrfs_search_slot will keep the lock held on higher nodes in a few
2257 * corner cases, such as COW of the block at slot zero in the node. This
2258 * ignores those rules, and it should only be called when there are no
2259 * more updates to be done higher up in the tree.
2260 */
2261noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2262{
2263 int i;
2264
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002265 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002266 return;
2267
2268 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2269 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002270 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002271 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002272 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002273 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002274 path->locks[i] = 0;
2275 }
2276}
2277
2278/*
Chris Masonc8c42862009-04-03 10:14:18 -04002279 * helper function for btrfs_search_slot. The goal is to find a block
2280 * in cache without setting the path to blocking. If we find the block
2281 * we return zero and the path is unchanged.
2282 *
2283 * If we can't find the block, we set the path blocking and do some
2284 * reada. -EAGAIN is returned and the search must be repeated.
2285 */
2286static int
2287read_block_for_search(struct btrfs_trans_handle *trans,
2288 struct btrfs_root *root, struct btrfs_path *p,
2289 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002290 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002291{
2292 u64 blocknr;
2293 u64 gen;
2294 u32 blocksize;
2295 struct extent_buffer *b = *eb_ret;
2296 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002297 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002298
2299 blocknr = btrfs_node_blockptr(b, slot);
2300 gen = btrfs_node_ptr_generation(b, slot);
2301 blocksize = btrfs_level_size(root, level - 1);
2302
2303 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002304 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002305 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002306 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2307 *eb_ret = tmp;
2308 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002309 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002310
2311 /* the pages were up to date, but we failed
2312 * the generation number check. Do a full
2313 * read for the generation number that is correct.
2314 * We must do this without dropping locks so
2315 * we can trust our generation number
2316 */
2317 btrfs_set_path_blocking(p);
2318
2319 /* now we're allowed to do a blocking uptodate check */
2320 ret = btrfs_read_buffer(tmp, gen);
2321 if (!ret) {
2322 *eb_ret = tmp;
2323 return 0;
2324 }
2325 free_extent_buffer(tmp);
2326 btrfs_release_path(p);
2327 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002328 }
2329
2330 /*
2331 * reduce lock contention at high levels
2332 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002333 * we read. Don't release the lock on the current
2334 * level because we need to walk this node to figure
2335 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002336 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002337 btrfs_unlock_up_safe(p, level + 1);
2338 btrfs_set_path_blocking(p);
2339
Chris Masoncb449212010-10-24 11:01:27 -04002340 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002341 if (p->reada)
2342 reada_for_search(root, p, level, slot, key->objectid);
2343
David Sterbab3b4aa72011-04-21 01:20:15 +02002344 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002345
2346 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002347 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002348 if (tmp) {
2349 /*
2350 * If the read above didn't mark this buffer up to date,
2351 * it will never end up being up to date. Set ret to EIO now
2352 * and give up so that our caller doesn't loop forever
2353 * on our EAGAINs.
2354 */
Chris Masonb9fab912012-05-06 07:23:47 -04002355 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002356 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002357 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002358 }
2359 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002360}
2361
2362/*
2363 * helper function for btrfs_search_slot. This does all of the checks
2364 * for node-level blocks and does any balancing required based on
2365 * the ins_len.
2366 *
2367 * If no extra work was required, zero is returned. If we had to
2368 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2369 * start over
2370 */
2371static int
2372setup_nodes_for_search(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002374 struct extent_buffer *b, int level, int ins_len,
2375 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002376{
2377 int ret;
2378 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2379 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2380 int sret;
2381
Chris Masonbd681512011-07-16 15:23:14 -04002382 if (*write_lock_level < level + 1) {
2383 *write_lock_level = level + 1;
2384 btrfs_release_path(p);
2385 goto again;
2386 }
2387
Chris Masonc8c42862009-04-03 10:14:18 -04002388 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002389 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002390 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002391 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002392
2393 BUG_ON(sret > 0);
2394 if (sret) {
2395 ret = sret;
2396 goto done;
2397 }
2398 b = p->nodes[level];
2399 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002400 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002401 int sret;
2402
Chris Masonbd681512011-07-16 15:23:14 -04002403 if (*write_lock_level < level + 1) {
2404 *write_lock_level = level + 1;
2405 btrfs_release_path(p);
2406 goto again;
2407 }
2408
Chris Masonc8c42862009-04-03 10:14:18 -04002409 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002410 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002411 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002412 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002413
2414 if (sret) {
2415 ret = sret;
2416 goto done;
2417 }
2418 b = p->nodes[level];
2419 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002420 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002421 goto again;
2422 }
2423 BUG_ON(btrfs_header_nritems(b) == 1);
2424 }
2425 return 0;
2426
2427again:
2428 ret = -EAGAIN;
2429done:
2430 return ret;
2431}
2432
2433/*
Chris Mason74123bd2007-02-02 11:05:29 -05002434 * look for key in the tree. path is filled in with nodes along the way
2435 * if key is found, we return zero and you can find the item in the leaf
2436 * level of the path (level 0)
2437 *
2438 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002439 * be inserted, and 1 is returned. If there are other errors during the
2440 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002441 *
2442 * if ins_len > 0, nodes and leaves will be split as we walk down the
2443 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2444 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002445 */
Chris Masone089f052007-03-16 16:20:31 -04002446int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2447 *root, struct btrfs_key *key, struct btrfs_path *p, int
2448 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002449{
Chris Mason5f39d392007-10-15 16:14:19 -04002450 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002451 int slot;
2452 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002453 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002454 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002455 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002456 int root_lock;
2457 /* everything at write_lock_level or lower must be write locked */
2458 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002459 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002460 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002461
Chris Mason6702ed42007-08-07 16:15:09 -04002462 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002463 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002464 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002465
Chris Masonbd681512011-07-16 15:23:14 -04002466 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002467 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002468
Chris Masonbd681512011-07-16 15:23:14 -04002469 /* when we are removing items, we might have to go up to level
2470 * two as we update tree pointers Make sure we keep write
2471 * for those levels as well
2472 */
2473 write_lock_level = 2;
2474 } else if (ins_len > 0) {
2475 /*
2476 * for inserting items, make sure we have a write lock on
2477 * level 1 so we can update keys
2478 */
2479 write_lock_level = 1;
2480 }
2481
2482 if (!cow)
2483 write_lock_level = -1;
2484
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002485 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002486 write_lock_level = BTRFS_MAX_LEVEL;
2487
Chris Masonf7c79f32012-03-19 15:54:38 -04002488 min_write_lock_level = write_lock_level;
2489
Chris Masonbb803952007-03-01 12:04:21 -05002490again:
Chris Masonbd681512011-07-16 15:23:14 -04002491 /*
2492 * we try very hard to do read locks on the root
2493 */
2494 root_lock = BTRFS_READ_LOCK;
2495 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002496 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002497 /*
2498 * the commit roots are read only
2499 * so we always do read locks
2500 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002501 b = root->commit_root;
2502 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002503 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002504 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002505 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002506 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002507 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002508 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002509 level = btrfs_header_level(b);
2510 } else {
2511 /* we don't know the level of the root node
2512 * until we actually have it read locked
2513 */
2514 b = btrfs_read_lock_root_node(root);
2515 level = btrfs_header_level(b);
2516 if (level <= write_lock_level) {
2517 /* whoops, must trade for write lock */
2518 btrfs_tree_read_unlock(b);
2519 free_extent_buffer(b);
2520 b = btrfs_lock_root_node(root);
2521 root_lock = BTRFS_WRITE_LOCK;
2522
2523 /* the level might have changed, check again */
2524 level = btrfs_header_level(b);
2525 }
2526 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002527 }
Chris Masonbd681512011-07-16 15:23:14 -04002528 p->nodes[level] = b;
2529 if (!p->skip_locking)
2530 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002531
Chris Masoneb60cea2007-02-02 09:18:22 -05002532 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002533 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002534
2535 /*
2536 * setup the path here so we can release it under lock
2537 * contention with the cow code
2538 */
Chris Mason02217ed2007-03-02 16:08:05 -05002539 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002540 /*
2541 * if we don't really need to cow this block
2542 * then we don't want to set the path blocking,
2543 * so we test it here
2544 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002545 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002546 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002547
Chris Masonb4ce94d2009-02-04 09:25:08 -05002548 btrfs_set_path_blocking(p);
2549
Chris Masonbd681512011-07-16 15:23:14 -04002550 /*
2551 * must have write locks on this node and the
2552 * parent
2553 */
Josef Bacik5124e002012-11-07 13:44:13 -05002554 if (level > write_lock_level ||
2555 (level + 1 > write_lock_level &&
2556 level + 1 < BTRFS_MAX_LEVEL &&
2557 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002558 write_lock_level = level + 1;
2559 btrfs_release_path(p);
2560 goto again;
2561 }
2562
Yan Zheng33c66f42009-07-22 09:59:00 -04002563 err = btrfs_cow_block(trans, root, b,
2564 p->nodes[level + 1],
2565 p->slots[level + 1], &b);
2566 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002567 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002568 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002569 }
Chris Mason02217ed2007-03-02 16:08:05 -05002570 }
Chris Mason65b51a02008-08-01 15:11:20 -04002571cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002572 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002573
Chris Masoneb60cea2007-02-02 09:18:22 -05002574 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002575 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002576
2577 /*
2578 * we have a lock on b and as long as we aren't changing
2579 * the tree, there is no way to for the items in b to change.
2580 * It is safe to drop the lock on our parent before we
2581 * go through the expensive btree search on b.
2582 *
2583 * If cow is true, then we might be changing slot zero,
2584 * which may require changing the parent. So, we can't
2585 * drop the lock until after we know which slot we're
2586 * operating on.
2587 */
2588 if (!cow)
2589 btrfs_unlock_up_safe(p, level + 1);
2590
Chris Mason5f39d392007-10-15 16:14:19 -04002591 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002592
Chris Mason5f39d392007-10-15 16:14:19 -04002593 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002594 int dec = 0;
2595 if (ret && slot > 0) {
2596 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002597 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002598 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002599 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002600 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002601 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002602 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002603 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002604 if (err) {
2605 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002606 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002607 }
Chris Masonc8c42862009-04-03 10:14:18 -04002608 b = p->nodes[level];
2609 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002610
Chris Masonbd681512011-07-16 15:23:14 -04002611 /*
2612 * slot 0 is special, if we change the key
2613 * we have to update the parent pointer
2614 * which means we must have a write lock
2615 * on the parent
2616 */
2617 if (slot == 0 && cow &&
2618 write_lock_level < level + 1) {
2619 write_lock_level = level + 1;
2620 btrfs_release_path(p);
2621 goto again;
2622 }
2623
Chris Masonf7c79f32012-03-19 15:54:38 -04002624 unlock_up(p, level, lowest_unlock,
2625 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002626
Chris Mason925baed2008-06-25 16:01:30 -04002627 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002628 if (dec)
2629 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002630 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002631 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002632
Yan Zheng33c66f42009-07-22 09:59:00 -04002633 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002634 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002635 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002636 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002637 if (err) {
2638 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002639 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002640 }
Chris Mason76a05b32009-05-14 13:24:30 -04002641
Chris Masonb4ce94d2009-02-04 09:25:08 -05002642 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002643 level = btrfs_header_level(b);
2644 if (level <= write_lock_level) {
2645 err = btrfs_try_tree_write_lock(b);
2646 if (!err) {
2647 btrfs_set_path_blocking(p);
2648 btrfs_tree_lock(b);
2649 btrfs_clear_path_blocking(p, b,
2650 BTRFS_WRITE_LOCK);
2651 }
2652 p->locks[level] = BTRFS_WRITE_LOCK;
2653 } else {
2654 err = btrfs_try_tree_read_lock(b);
2655 if (!err) {
2656 btrfs_set_path_blocking(p);
2657 btrfs_tree_read_lock(b);
2658 btrfs_clear_path_blocking(p, b,
2659 BTRFS_READ_LOCK);
2660 }
2661 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002662 }
Chris Masonbd681512011-07-16 15:23:14 -04002663 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002664 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002665 } else {
2666 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002667 if (ins_len > 0 &&
2668 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002669 if (write_lock_level < 1) {
2670 write_lock_level = 1;
2671 btrfs_release_path(p);
2672 goto again;
2673 }
2674
Chris Masonb4ce94d2009-02-04 09:25:08 -05002675 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002676 err = split_leaf(trans, root, key,
2677 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002678 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002679
Yan Zheng33c66f42009-07-22 09:59:00 -04002680 BUG_ON(err > 0);
2681 if (err) {
2682 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002683 goto done;
2684 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002685 }
Chris Mason459931e2008-12-10 09:10:46 -05002686 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002687 unlock_up(p, level, lowest_unlock,
2688 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002689 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002690 }
2691 }
Chris Mason65b51a02008-08-01 15:11:20 -04002692 ret = 1;
2693done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002694 /*
2695 * we don't really know what they plan on doing with the path
2696 * from here on, so for now just mark it as blocking
2697 */
Chris Masonb9473432009-03-13 11:00:37 -04002698 if (!p->leave_spinning)
2699 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002700 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002701 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002702 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002703}
2704
Chris Mason74123bd2007-02-02 11:05:29 -05002705/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002706 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2707 * current state of the tree together with the operations recorded in the tree
2708 * modification log to search for the key in a previous version of this tree, as
2709 * denoted by the time_seq parameter.
2710 *
2711 * Naturally, there is no support for insert, delete or cow operations.
2712 *
2713 * The resulting path and return value will be set up as if we called
2714 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2715 */
2716int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2717 struct btrfs_path *p, u64 time_seq)
2718{
2719 struct extent_buffer *b;
2720 int slot;
2721 int ret;
2722 int err;
2723 int level;
2724 int lowest_unlock = 1;
2725 u8 lowest_level = 0;
2726
2727 lowest_level = p->lowest_level;
2728 WARN_ON(p->nodes[0] != NULL);
2729
2730 if (p->search_commit_root) {
2731 BUG_ON(time_seq);
2732 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2733 }
2734
2735again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002736 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002737 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002738 p->locks[level] = BTRFS_READ_LOCK;
2739
2740 while (b) {
2741 level = btrfs_header_level(b);
2742 p->nodes[level] = b;
2743 btrfs_clear_path_blocking(p, NULL, 0);
2744
2745 /*
2746 * we have a lock on b and as long as we aren't changing
2747 * the tree, there is no way to for the items in b to change.
2748 * It is safe to drop the lock on our parent before we
2749 * go through the expensive btree search on b.
2750 */
2751 btrfs_unlock_up_safe(p, level + 1);
2752
2753 ret = bin_search(b, key, level, &slot);
2754
2755 if (level != 0) {
2756 int dec = 0;
2757 if (ret && slot > 0) {
2758 dec = 1;
2759 slot -= 1;
2760 }
2761 p->slots[level] = slot;
2762 unlock_up(p, level, lowest_unlock, 0, NULL);
2763
2764 if (level == lowest_level) {
2765 if (dec)
2766 p->slots[level]++;
2767 goto done;
2768 }
2769
2770 err = read_block_for_search(NULL, root, p, &b, level,
2771 slot, key, time_seq);
2772 if (err == -EAGAIN)
2773 goto again;
2774 if (err) {
2775 ret = err;
2776 goto done;
2777 }
2778
2779 level = btrfs_header_level(b);
2780 err = btrfs_try_tree_read_lock(b);
2781 if (!err) {
2782 btrfs_set_path_blocking(p);
2783 btrfs_tree_read_lock(b);
2784 btrfs_clear_path_blocking(p, b,
2785 BTRFS_READ_LOCK);
2786 }
Josef Bacik9ec72672013-08-07 16:57:23 -04002787 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04002788 if (!b) {
2789 ret = -ENOMEM;
2790 goto done;
2791 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002792 p->locks[level] = BTRFS_READ_LOCK;
2793 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002794 } else {
2795 p->slots[level] = slot;
2796 unlock_up(p, level, lowest_unlock, 0, NULL);
2797 goto done;
2798 }
2799 }
2800 ret = 1;
2801done:
2802 if (!p->leave_spinning)
2803 btrfs_set_path_blocking(p);
2804 if (ret < 0)
2805 btrfs_release_path(p);
2806
2807 return ret;
2808}
2809
2810/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002811 * helper to use instead of search slot if no exact match is needed but
2812 * instead the next or previous item should be returned.
2813 * When find_higher is true, the next higher item is returned, the next lower
2814 * otherwise.
2815 * When return_any and find_higher are both true, and no higher item is found,
2816 * return the next lower instead.
2817 * When return_any is true and find_higher is false, and no lower item is found,
2818 * return the next higher instead.
2819 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2820 * < 0 on error
2821 */
2822int btrfs_search_slot_for_read(struct btrfs_root *root,
2823 struct btrfs_key *key, struct btrfs_path *p,
2824 int find_higher, int return_any)
2825{
2826 int ret;
2827 struct extent_buffer *leaf;
2828
2829again:
2830 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2831 if (ret <= 0)
2832 return ret;
2833 /*
2834 * a return value of 1 means the path is at the position where the
2835 * item should be inserted. Normally this is the next bigger item,
2836 * but in case the previous item is the last in a leaf, path points
2837 * to the first free slot in the previous leaf, i.e. at an invalid
2838 * item.
2839 */
2840 leaf = p->nodes[0];
2841
2842 if (find_higher) {
2843 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2844 ret = btrfs_next_leaf(root, p);
2845 if (ret <= 0)
2846 return ret;
2847 if (!return_any)
2848 return 1;
2849 /*
2850 * no higher item found, return the next
2851 * lower instead
2852 */
2853 return_any = 0;
2854 find_higher = 0;
2855 btrfs_release_path(p);
2856 goto again;
2857 }
2858 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002859 if (p->slots[0] == 0) {
2860 ret = btrfs_prev_leaf(root, p);
2861 if (ret < 0)
2862 return ret;
2863 if (!ret) {
2864 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2865 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002866 }
Arne Jansene6793762011-09-13 11:18:10 +02002867 if (!return_any)
2868 return 1;
2869 /*
2870 * no lower item found, return the next
2871 * higher instead
2872 */
2873 return_any = 0;
2874 find_higher = 1;
2875 btrfs_release_path(p);
2876 goto again;
2877 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002878 --p->slots[0];
2879 }
2880 }
2881 return 0;
2882}
2883
2884/*
Chris Mason74123bd2007-02-02 11:05:29 -05002885 * adjust the pointers going up the tree, starting at level
2886 * making sure the right key of each node is points to 'key'.
2887 * This is used after shifting pointers to the left, so it stops
2888 * fixing up pointers when a given leaf/node is not in slot 0 of the
2889 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002890 *
Chris Mason74123bd2007-02-02 11:05:29 -05002891 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002892static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002893 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002894{
2895 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002896 struct extent_buffer *t;
2897
Chris Mason234b63a2007-03-13 10:46:10 -04002898 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002899 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002900 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002901 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002902 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002903 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002904 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002905 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002906 if (tslot != 0)
2907 break;
2908 }
2909}
2910
Chris Mason74123bd2007-02-02 11:05:29 -05002911/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002912 * update item key.
2913 *
2914 * This function isn't completely safe. It's the caller's responsibility
2915 * that the new key won't break the order
2916 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002917void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002918 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002919{
2920 struct btrfs_disk_key disk_key;
2921 struct extent_buffer *eb;
2922 int slot;
2923
2924 eb = path->nodes[0];
2925 slot = path->slots[0];
2926 if (slot > 0) {
2927 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002928 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002929 }
2930 if (slot < btrfs_header_nritems(eb) - 1) {
2931 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002932 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002933 }
2934
2935 btrfs_cpu_key_to_disk(&disk_key, new_key);
2936 btrfs_set_item_key(eb, &disk_key, slot);
2937 btrfs_mark_buffer_dirty(eb);
2938 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002939 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002940}
2941
2942/*
Chris Mason74123bd2007-02-02 11:05:29 -05002943 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002944 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002945 *
2946 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2947 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002948 */
Chris Mason98ed5172008-01-03 10:01:48 -05002949static int push_node_left(struct btrfs_trans_handle *trans,
2950 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002951 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002952{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002953 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002954 int src_nritems;
2955 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002956 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002957
Chris Mason5f39d392007-10-15 16:14:19 -04002958 src_nritems = btrfs_header_nritems(src);
2959 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002960 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002961 WARN_ON(btrfs_header_generation(src) != trans->transid);
2962 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002963
Chris Masonbce4eae2008-04-24 14:42:46 -04002964 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002965 return 1;
2966
Chris Masond3977122009-01-05 21:25:51 -05002967 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002968 return 1;
2969
Chris Masonbce4eae2008-04-24 14:42:46 -04002970 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002971 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002972 if (push_items < src_nritems) {
2973 /* leave at least 8 pointers in the node if
2974 * we aren't going to empty it
2975 */
2976 if (src_nritems - push_items < 8) {
2977 if (push_items <= 8)
2978 return 1;
2979 push_items -= 8;
2980 }
2981 }
2982 } else
2983 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002984
Jan Schmidtf2304752012-05-26 11:43:17 +02002985 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00002986 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002987 copy_extent_buffer(dst, src,
2988 btrfs_node_key_ptr_offset(dst_nritems),
2989 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002990 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002991
Chris Masonbb803952007-03-01 12:04:21 -05002992 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02002993 /*
2994 * don't call tree_mod_log_eb_move here, key removal was already
2995 * fully logged by tree_mod_log_eb_copy above.
2996 */
Chris Mason5f39d392007-10-15 16:14:19 -04002997 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2998 btrfs_node_key_ptr_offset(push_items),
2999 (src_nritems - push_items) *
3000 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003001 }
Chris Mason5f39d392007-10-15 16:14:19 -04003002 btrfs_set_header_nritems(src, src_nritems - push_items);
3003 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3004 btrfs_mark_buffer_dirty(src);
3005 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003006
Chris Masonbb803952007-03-01 12:04:21 -05003007 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003008}
3009
Chris Mason97571fd2007-02-24 13:39:08 -05003010/*
Chris Mason79f95c82007-03-01 15:16:26 -05003011 * try to push data from one node into the next node right in the
3012 * tree.
3013 *
3014 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3015 * error, and > 0 if there was no room in the right hand block.
3016 *
3017 * this will only push up to 1/2 the contents of the left node over
3018 */
Chris Mason5f39d392007-10-15 16:14:19 -04003019static int balance_node_right(struct btrfs_trans_handle *trans,
3020 struct btrfs_root *root,
3021 struct extent_buffer *dst,
3022 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003023{
Chris Mason79f95c82007-03-01 15:16:26 -05003024 int push_items = 0;
3025 int max_push;
3026 int src_nritems;
3027 int dst_nritems;
3028 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003029
Chris Mason7bb86312007-12-11 09:25:06 -05003030 WARN_ON(btrfs_header_generation(src) != trans->transid);
3031 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3032
Chris Mason5f39d392007-10-15 16:14:19 -04003033 src_nritems = btrfs_header_nritems(src);
3034 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003035 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003036 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003037 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003038
Chris Masond3977122009-01-05 21:25:51 -05003039 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003040 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003041
3042 max_push = src_nritems / 2 + 1;
3043 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003044 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003045 return 1;
Yan252c38f2007-08-29 09:11:44 -04003046
Chris Mason79f95c82007-03-01 15:16:26 -05003047 if (max_push < push_items)
3048 push_items = max_push;
3049
Jan Schmidtf2304752012-05-26 11:43:17 +02003050 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003051 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3052 btrfs_node_key_ptr_offset(0),
3053 (dst_nritems) *
3054 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003055
Jan Schmidtf2304752012-05-26 11:43:17 +02003056 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003057 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003058 copy_extent_buffer(dst, src,
3059 btrfs_node_key_ptr_offset(0),
3060 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003061 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003062
Chris Mason5f39d392007-10-15 16:14:19 -04003063 btrfs_set_header_nritems(src, src_nritems - push_items);
3064 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003065
Chris Mason5f39d392007-10-15 16:14:19 -04003066 btrfs_mark_buffer_dirty(src);
3067 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003068
Chris Mason79f95c82007-03-01 15:16:26 -05003069 return ret;
3070}
3071
3072/*
Chris Mason97571fd2007-02-24 13:39:08 -05003073 * helper function to insert a new root level in the tree.
3074 * A new node is allocated, and a single item is inserted to
3075 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003076 *
3077 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003078 */
Chris Masond3977122009-01-05 21:25:51 -05003079static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003080 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003081 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003082{
Chris Mason7bb86312007-12-11 09:25:06 -05003083 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003084 struct extent_buffer *lower;
3085 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003086 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003087 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003088
3089 BUG_ON(path->nodes[level]);
3090 BUG_ON(path->nodes[level-1] != root->node);
3091
Chris Mason7bb86312007-12-11 09:25:06 -05003092 lower = path->nodes[level-1];
3093 if (level == 1)
3094 btrfs_item_key(lower, &lower_key, 0);
3095 else
3096 btrfs_node_key(lower, &lower_key, 0);
3097
Zheng Yan31840ae2008-09-23 13:14:14 -04003098 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003099 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003100 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003101 if (IS_ERR(c))
3102 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003103
Yan, Zhengf0486c62010-05-16 10:46:25 -04003104 root_add_used(root, root->nodesize);
3105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003106 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003107 btrfs_set_header_nritems(c, 1);
3108 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003109 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003110 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003112 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003113
Chris Mason5f39d392007-10-15 16:14:19 -04003114 write_extent_buffer(c, root->fs_info->fsid,
3115 (unsigned long)btrfs_header_fsid(c),
3116 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003117
3118 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3119 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3120 BTRFS_UUID_SIZE);
3121
Chris Mason5f39d392007-10-15 16:14:19 -04003122 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003123 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003124 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003125 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003126
3127 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003128
3129 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003130
Chris Mason925baed2008-06-25 16:01:30 -04003131 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003132 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003133 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003134
3135 /* the super has an extra ref to root->node */
3136 free_extent_buffer(old);
3137
Chris Mason0b86a832008-03-24 15:01:56 -04003138 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003139 extent_buffer_get(c);
3140 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003141 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003142 path->slots[level] = 0;
3143 return 0;
3144}
3145
Chris Mason74123bd2007-02-02 11:05:29 -05003146/*
3147 * worker function to insert a single pointer in a node.
3148 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003149 *
Chris Mason74123bd2007-02-02 11:05:29 -05003150 * slot and level indicate where you want the key to go, and
3151 * blocknr is the block the key points to.
3152 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003153static void insert_ptr(struct btrfs_trans_handle *trans,
3154 struct btrfs_root *root, struct btrfs_path *path,
3155 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003156 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003157{
Chris Mason5f39d392007-10-15 16:14:19 -04003158 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003159 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003160 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003161
3162 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003163 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003164 lower = path->nodes[level];
3165 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003166 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003167 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003168 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003169 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003170 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3171 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003172 memmove_extent_buffer(lower,
3173 btrfs_node_key_ptr_offset(slot + 1),
3174 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003175 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003176 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003177 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003178 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003179 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003180 BUG_ON(ret < 0);
3181 }
Chris Mason5f39d392007-10-15 16:14:19 -04003182 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003183 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003184 WARN_ON(trans->transid == 0);
3185 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003186 btrfs_set_header_nritems(lower, nritems + 1);
3187 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003188}
3189
Chris Mason97571fd2007-02-24 13:39:08 -05003190/*
3191 * split the node at the specified level in path in two.
3192 * The path is corrected to point to the appropriate node after the split
3193 *
3194 * Before splitting this tries to make some room in the node by pushing
3195 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003196 *
3197 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003198 */
Chris Masone02119d2008-09-05 16:13:11 -04003199static noinline int split_node(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root,
3201 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003202{
Chris Mason5f39d392007-10-15 16:14:19 -04003203 struct extent_buffer *c;
3204 struct extent_buffer *split;
3205 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003206 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003207 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003208 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003209
Chris Mason5f39d392007-10-15 16:14:19 -04003210 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003211 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003212 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003213 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003214 * trying to split the root, lets make a new one
3215 *
Liu Bofdd99c72013-05-22 12:06:51 +00003216 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003217 * insert_new_root, because that root buffer will be kept as a
3218 * normal node. We are going to log removal of half of the
3219 * elements below with tree_mod_log_eb_copy. We're holding a
3220 * tree lock on the buffer, which is why we cannot race with
3221 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003222 */
Liu Bofdd99c72013-05-22 12:06:51 +00003223 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003224 if (ret)
3225 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003226 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003227 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003228 c = path->nodes[level];
3229 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003230 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003231 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003232 if (ret < 0)
3233 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003234 }
Chris Masone66f7092007-04-20 13:16:02 -04003235
Chris Mason5f39d392007-10-15 16:14:19 -04003236 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003237 mid = (c_nritems + 1) / 2;
3238 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003239
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003240 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003241 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003242 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003243 if (IS_ERR(split))
3244 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003245
Yan, Zhengf0486c62010-05-16 10:46:25 -04003246 root_add_used(root, root->nodesize);
3247
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003248 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003249 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003250 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003251 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003252 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003253 btrfs_set_header_owner(split, root->root_key.objectid);
3254 write_extent_buffer(split, root->fs_info->fsid,
3255 (unsigned long)btrfs_header_fsid(split),
3256 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003257 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3258 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3259 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003260
Jan Schmidt90f8d622013-04-13 13:19:53 +00003261 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003262 copy_extent_buffer(split, c,
3263 btrfs_node_key_ptr_offset(0),
3264 btrfs_node_key_ptr_offset(mid),
3265 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3266 btrfs_set_header_nritems(split, c_nritems - mid);
3267 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003268 ret = 0;
3269
Chris Mason5f39d392007-10-15 16:14:19 -04003270 btrfs_mark_buffer_dirty(c);
3271 btrfs_mark_buffer_dirty(split);
3272
Jeff Mahoney143bede2012-03-01 14:56:26 +01003273 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003274 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003275
Chris Mason5de08d72007-02-24 06:24:44 -05003276 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003277 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003278 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003279 free_extent_buffer(c);
3280 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003281 path->slots[level + 1] += 1;
3282 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003283 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003284 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003285 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003286 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003287}
3288
Chris Mason74123bd2007-02-02 11:05:29 -05003289/*
3290 * how many bytes are required to store the items in a leaf. start
3291 * and nr indicate which items in the leaf to check. This totals up the
3292 * space used both by the item structs and the item data
3293 */
Chris Mason5f39d392007-10-15 16:14:19 -04003294static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003295{
Josef Bacik41be1f32012-10-15 13:43:18 -04003296 struct btrfs_item *start_item;
3297 struct btrfs_item *end_item;
3298 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003299 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003300 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003301 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003302
3303 if (!nr)
3304 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003305 btrfs_init_map_token(&token);
3306 start_item = btrfs_item_nr(l, start);
3307 end_item = btrfs_item_nr(l, end);
3308 data_len = btrfs_token_item_offset(l, start_item, &token) +
3309 btrfs_token_item_size(l, start_item, &token);
3310 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003311 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003312 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003313 return data_len;
3314}
3315
Chris Mason74123bd2007-02-02 11:05:29 -05003316/*
Chris Masond4dbff92007-04-04 14:08:15 -04003317 * The space between the end of the leaf items and
3318 * the start of the leaf data. IOW, how much room
3319 * the leaf has left for both items and data
3320 */
Chris Masond3977122009-01-05 21:25:51 -05003321noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003322 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003323{
Chris Mason5f39d392007-10-15 16:14:19 -04003324 int nritems = btrfs_header_nritems(leaf);
3325 int ret;
3326 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3327 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003328 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3329 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003330 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003331 leaf_space_used(leaf, 0, nritems), nritems);
3332 }
3333 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003334}
3335
Chris Mason99d8f832010-07-07 10:51:48 -04003336/*
3337 * min slot controls the lowest index we're willing to push to the
3338 * right. We'll push up to and including min_slot, but no lower
3339 */
Chris Mason44871b12009-03-13 10:04:31 -04003340static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3341 struct btrfs_root *root,
3342 struct btrfs_path *path,
3343 int data_size, int empty,
3344 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003345 int free_space, u32 left_nritems,
3346 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003347{
Chris Mason5f39d392007-10-15 16:14:19 -04003348 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003349 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003350 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003351 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003352 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003353 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003354 int push_space = 0;
3355 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003356 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003357 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003358 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003359 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003360 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003361
Chris Masoncfed81a2012-03-03 07:40:03 -05003362 btrfs_init_map_token(&token);
3363
Chris Mason34a38212007-11-07 13:31:03 -05003364 if (empty)
3365 nr = 0;
3366 else
Chris Mason99d8f832010-07-07 10:51:48 -04003367 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003368
Zheng Yan31840ae2008-09-23 13:14:14 -04003369 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003370 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003371
Chris Mason44871b12009-03-13 10:04:31 -04003372 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003373 i = left_nritems - 1;
3374 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003375 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003376
Zheng Yan31840ae2008-09-23 13:14:14 -04003377 if (!empty && push_items > 0) {
3378 if (path->slots[0] > i)
3379 break;
3380 if (path->slots[0] == i) {
3381 int space = btrfs_leaf_free_space(root, left);
3382 if (space + push_space * 2 > free_space)
3383 break;
3384 }
3385 }
3386
Chris Mason00ec4c52007-02-24 12:47:20 -05003387 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003388 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003389
Chris Masondb945352007-10-15 16:15:53 -04003390 this_item_size = btrfs_item_size(left, item);
3391 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003392 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003393
Chris Mason00ec4c52007-02-24 12:47:20 -05003394 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003395 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003396 if (i == 0)
3397 break;
3398 i--;
Chris Masondb945352007-10-15 16:15:53 -04003399 }
Chris Mason5f39d392007-10-15 16:14:19 -04003400
Chris Mason925baed2008-06-25 16:01:30 -04003401 if (push_items == 0)
3402 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003403
Julia Lawall6c1500f2012-11-03 20:30:18 +00003404 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003405
Chris Mason00ec4c52007-02-24 12:47:20 -05003406 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003407 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003408
Chris Mason5f39d392007-10-15 16:14:19 -04003409 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003410 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003411
Chris Mason00ec4c52007-02-24 12:47:20 -05003412 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003413 data_end = leaf_data_end(root, right);
3414 memmove_extent_buffer(right,
3415 btrfs_leaf_data(right) + data_end - push_space,
3416 btrfs_leaf_data(right) + data_end,
3417 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3418
Chris Mason00ec4c52007-02-24 12:47:20 -05003419 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003420 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003421 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3422 btrfs_leaf_data(left) + leaf_data_end(root, left),
3423 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003424
3425 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3426 btrfs_item_nr_offset(0),
3427 right_nritems * sizeof(struct btrfs_item));
3428
Chris Mason00ec4c52007-02-24 12:47:20 -05003429 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003430 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3431 btrfs_item_nr_offset(left_nritems - push_items),
3432 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003433
3434 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003435 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003436 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003437 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003438 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003439 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003440 push_space -= btrfs_token_item_size(right, item, &token);
3441 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003442 }
3443
Chris Mason7518a232007-03-12 12:01:18 -04003444 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003445 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003446
Chris Mason34a38212007-11-07 13:31:03 -05003447 if (left_nritems)
3448 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003449 else
3450 clean_tree_block(trans, root, left);
3451
Chris Mason5f39d392007-10-15 16:14:19 -04003452 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003453
Chris Mason5f39d392007-10-15 16:14:19 -04003454 btrfs_item_key(right, &disk_key, 0);
3455 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003456 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003457
Chris Mason00ec4c52007-02-24 12:47:20 -05003458 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003459 if (path->slots[0] >= left_nritems) {
3460 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003461 if (btrfs_header_nritems(path->nodes[0]) == 0)
3462 clean_tree_block(trans, root, path->nodes[0]);
3463 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003464 free_extent_buffer(path->nodes[0]);
3465 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003466 path->slots[1] += 1;
3467 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003468 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003469 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003470 }
3471 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003472
3473out_unlock:
3474 btrfs_tree_unlock(right);
3475 free_extent_buffer(right);
3476 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003477}
Chris Mason925baed2008-06-25 16:01:30 -04003478
Chris Mason00ec4c52007-02-24 12:47:20 -05003479/*
Chris Mason44871b12009-03-13 10:04:31 -04003480 * push some data in the path leaf to the right, trying to free up at
3481 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3482 *
3483 * returns 1 if the push failed because the other node didn't have enough
3484 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003485 *
3486 * this will push starting from min_slot to the end of the leaf. It won't
3487 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003488 */
3489static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003490 *root, struct btrfs_path *path,
3491 int min_data_size, int data_size,
3492 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003493{
3494 struct extent_buffer *left = path->nodes[0];
3495 struct extent_buffer *right;
3496 struct extent_buffer *upper;
3497 int slot;
3498 int free_space;
3499 u32 left_nritems;
3500 int ret;
3501
3502 if (!path->nodes[1])
3503 return 1;
3504
3505 slot = path->slots[1];
3506 upper = path->nodes[1];
3507 if (slot >= btrfs_header_nritems(upper) - 1)
3508 return 1;
3509
3510 btrfs_assert_tree_locked(path->nodes[1]);
3511
3512 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003513 if (right == NULL)
3514 return 1;
3515
Chris Mason44871b12009-03-13 10:04:31 -04003516 btrfs_tree_lock(right);
3517 btrfs_set_lock_blocking(right);
3518
3519 free_space = btrfs_leaf_free_space(root, right);
3520 if (free_space < data_size)
3521 goto out_unlock;
3522
3523 /* cow and double check */
3524 ret = btrfs_cow_block(trans, root, right, upper,
3525 slot + 1, &right);
3526 if (ret)
3527 goto out_unlock;
3528
3529 free_space = btrfs_leaf_free_space(root, right);
3530 if (free_space < data_size)
3531 goto out_unlock;
3532
3533 left_nritems = btrfs_header_nritems(left);
3534 if (left_nritems == 0)
3535 goto out_unlock;
3536
Chris Mason99d8f832010-07-07 10:51:48 -04003537 return __push_leaf_right(trans, root, path, min_data_size, empty,
3538 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003539out_unlock:
3540 btrfs_tree_unlock(right);
3541 free_extent_buffer(right);
3542 return 1;
3543}
3544
3545/*
Chris Mason74123bd2007-02-02 11:05:29 -05003546 * push some data in the path leaf to the left, trying to free up at
3547 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003548 *
3549 * max_slot can put a limit on how far into the leaf we'll push items. The
3550 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3551 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003552 */
Chris Mason44871b12009-03-13 10:04:31 -04003553static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3554 struct btrfs_root *root,
3555 struct btrfs_path *path, int data_size,
3556 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003557 int free_space, u32 right_nritems,
3558 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003559{
Chris Mason5f39d392007-10-15 16:14:19 -04003560 struct btrfs_disk_key disk_key;
3561 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003562 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003563 int push_space = 0;
3564 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003565 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003566 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003567 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003568 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003569 u32 this_item_size;
3570 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003571 struct btrfs_map_token token;
3572
3573 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574
Chris Mason34a38212007-11-07 13:31:03 -05003575 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003576 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003577 else
Chris Mason99d8f832010-07-07 10:51:48 -04003578 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003579
3580 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003581 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003582
Zheng Yan31840ae2008-09-23 13:14:14 -04003583 if (!empty && push_items > 0) {
3584 if (path->slots[0] < i)
3585 break;
3586 if (path->slots[0] == i) {
3587 int space = btrfs_leaf_free_space(root, right);
3588 if (space + push_space * 2 > free_space)
3589 break;
3590 }
3591 }
3592
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003594 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003595
3596 this_item_size = btrfs_item_size(right, item);
3597 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003598 break;
Chris Masondb945352007-10-15 16:15:53 -04003599
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003601 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003602 }
Chris Masondb945352007-10-15 16:15:53 -04003603
Chris Masonbe0e5c02007-01-26 15:51:26 -05003604 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003605 ret = 1;
3606 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003607 }
Chris Mason34a38212007-11-07 13:31:03 -05003608 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003609 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003610
Chris Masonbe0e5c02007-01-26 15:51:26 -05003611 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003612 copy_extent_buffer(left, right,
3613 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3614 btrfs_item_nr_offset(0),
3615 push_items * sizeof(struct btrfs_item));
3616
Chris Mason123abc82007-03-14 14:14:43 -04003617 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003618 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
3620 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003621 leaf_data_end(root, left) - push_space,
3622 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003623 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003624 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003625 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003626 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003627
Chris Masondb945352007-10-15 16:15:53 -04003628 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003629 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003630 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003631
Chris Mason5f39d392007-10-15 16:14:19 -04003632 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003633
Chris Masoncfed81a2012-03-03 07:40:03 -05003634 ioff = btrfs_token_item_offset(left, item, &token);
3635 btrfs_set_token_item_offset(left, item,
3636 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3637 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003638 }
Chris Mason5f39d392007-10-15 16:14:19 -04003639 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003640
3641 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003642 if (push_items > right_nritems)
3643 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003644 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003645
Chris Mason34a38212007-11-07 13:31:03 -05003646 if (push_items < right_nritems) {
3647 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3648 leaf_data_end(root, right);
3649 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3650 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3651 btrfs_leaf_data(right) +
3652 leaf_data_end(root, right), push_space);
3653
3654 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003655 btrfs_item_nr_offset(push_items),
3656 (btrfs_header_nritems(right) - push_items) *
3657 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003658 }
Yaneef1c492007-11-26 10:58:13 -05003659 right_nritems -= push_items;
3660 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003661 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003662 for (i = 0; i < right_nritems; i++) {
3663 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003664
Chris Masoncfed81a2012-03-03 07:40:03 -05003665 push_space = push_space - btrfs_token_item_size(right,
3666 item, &token);
3667 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003668 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003669
Chris Mason5f39d392007-10-15 16:14:19 -04003670 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003671 if (right_nritems)
3672 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003673 else
3674 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003675
Chris Mason5f39d392007-10-15 16:14:19 -04003676 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003677 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678
3679 /* then fixup the leaf pointer in the path */
3680 if (path->slots[0] < push_items) {
3681 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003682 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003683 free_extent_buffer(path->nodes[0]);
3684 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003685 path->slots[1] -= 1;
3686 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003687 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003688 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003689 path->slots[0] -= push_items;
3690 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003691 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003692 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003693out:
3694 btrfs_tree_unlock(left);
3695 free_extent_buffer(left);
3696 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003697}
3698
Chris Mason74123bd2007-02-02 11:05:29 -05003699/*
Chris Mason44871b12009-03-13 10:04:31 -04003700 * push some data in the path leaf to the left, trying to free up at
3701 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003702 *
3703 * max_slot can put a limit on how far into the leaf we'll push items. The
3704 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3705 * items
Chris Mason44871b12009-03-13 10:04:31 -04003706 */
3707static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003708 *root, struct btrfs_path *path, int min_data_size,
3709 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003710{
3711 struct extent_buffer *right = path->nodes[0];
3712 struct extent_buffer *left;
3713 int slot;
3714 int free_space;
3715 u32 right_nritems;
3716 int ret = 0;
3717
3718 slot = path->slots[1];
3719 if (slot == 0)
3720 return 1;
3721 if (!path->nodes[1])
3722 return 1;
3723
3724 right_nritems = btrfs_header_nritems(right);
3725 if (right_nritems == 0)
3726 return 1;
3727
3728 btrfs_assert_tree_locked(path->nodes[1]);
3729
3730 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003731 if (left == NULL)
3732 return 1;
3733
Chris Mason44871b12009-03-13 10:04:31 -04003734 btrfs_tree_lock(left);
3735 btrfs_set_lock_blocking(left);
3736
3737 free_space = btrfs_leaf_free_space(root, left);
3738 if (free_space < data_size) {
3739 ret = 1;
3740 goto out;
3741 }
3742
3743 /* cow and double check */
3744 ret = btrfs_cow_block(trans, root, left,
3745 path->nodes[1], slot - 1, &left);
3746 if (ret) {
3747 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003748 if (ret == -ENOSPC)
3749 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003750 goto out;
3751 }
3752
3753 free_space = btrfs_leaf_free_space(root, left);
3754 if (free_space < data_size) {
3755 ret = 1;
3756 goto out;
3757 }
3758
Chris Mason99d8f832010-07-07 10:51:48 -04003759 return __push_leaf_left(trans, root, path, min_data_size,
3760 empty, left, free_space, right_nritems,
3761 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003762out:
3763 btrfs_tree_unlock(left);
3764 free_extent_buffer(left);
3765 return ret;
3766}
3767
3768/*
Chris Mason74123bd2007-02-02 11:05:29 -05003769 * split the path's leaf in two, making sure there is at least data_size
3770 * available for the resulting leaf level of the path.
3771 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003772static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3773 struct btrfs_root *root,
3774 struct btrfs_path *path,
3775 struct extent_buffer *l,
3776 struct extent_buffer *right,
3777 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003778{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003779 int data_copy_size;
3780 int rt_data_off;
3781 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003782 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003783 struct btrfs_map_token token;
3784
3785 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003786
Chris Mason5f39d392007-10-15 16:14:19 -04003787 nritems = nritems - mid;
3788 btrfs_set_header_nritems(right, nritems);
3789 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3790
3791 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3792 btrfs_item_nr_offset(mid),
3793 nritems * sizeof(struct btrfs_item));
3794
3795 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003796 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3797 data_copy_size, btrfs_leaf_data(l) +
3798 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003799
Chris Mason5f39d392007-10-15 16:14:19 -04003800 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3801 btrfs_item_end_nr(l, mid);
3802
3803 for (i = 0; i < nritems; i++) {
3804 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003805 u32 ioff;
3806
Chris Masoncfed81a2012-03-03 07:40:03 -05003807 ioff = btrfs_token_item_offset(right, item, &token);
3808 btrfs_set_token_item_offset(right, item,
3809 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003810 }
Chris Mason74123bd2007-02-02 11:05:29 -05003811
Chris Mason5f39d392007-10-15 16:14:19 -04003812 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003813 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003814 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003815 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003816
3817 btrfs_mark_buffer_dirty(right);
3818 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003819 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003820
Chris Masonbe0e5c02007-01-26 15:51:26 -05003821 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003822 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003823 free_extent_buffer(path->nodes[0]);
3824 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003825 path->slots[0] -= mid;
3826 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003827 } else {
3828 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003829 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003830 }
Chris Mason5f39d392007-10-15 16:14:19 -04003831
Chris Masoneb60cea2007-02-02 09:18:22 -05003832 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003833}
3834
3835/*
Chris Mason99d8f832010-07-07 10:51:48 -04003836 * double splits happen when we need to insert a big item in the middle
3837 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3838 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3839 * A B C
3840 *
3841 * We avoid this by trying to push the items on either side of our target
3842 * into the adjacent leaves. If all goes well we can avoid the double split
3843 * completely.
3844 */
3845static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3846 struct btrfs_root *root,
3847 struct btrfs_path *path,
3848 int data_size)
3849{
3850 int ret;
3851 int progress = 0;
3852 int slot;
3853 u32 nritems;
3854
3855 slot = path->slots[0];
3856
3857 /*
3858 * try to push all the items after our slot into the
3859 * right leaf
3860 */
3861 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3862 if (ret < 0)
3863 return ret;
3864
3865 if (ret == 0)
3866 progress++;
3867
3868 nritems = btrfs_header_nritems(path->nodes[0]);
3869 /*
3870 * our goal is to get our slot at the start or end of a leaf. If
3871 * we've done so we're done
3872 */
3873 if (path->slots[0] == 0 || path->slots[0] == nritems)
3874 return 0;
3875
3876 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3877 return 0;
3878
3879 /* try to push all the items before our slot into the next leaf */
3880 slot = path->slots[0];
3881 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3882 if (ret < 0)
3883 return ret;
3884
3885 if (ret == 0)
3886 progress++;
3887
3888 if (progress)
3889 return 0;
3890 return 1;
3891}
3892
3893/*
Chris Mason44871b12009-03-13 10:04:31 -04003894 * split the path's leaf in two, making sure there is at least data_size
3895 * available for the resulting leaf level of the path.
3896 *
3897 * returns 0 if all went well and < 0 on failure.
3898 */
3899static noinline int split_leaf(struct btrfs_trans_handle *trans,
3900 struct btrfs_root *root,
3901 struct btrfs_key *ins_key,
3902 struct btrfs_path *path, int data_size,
3903 int extend)
3904{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003905 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003906 struct extent_buffer *l;
3907 u32 nritems;
3908 int mid;
3909 int slot;
3910 struct extent_buffer *right;
3911 int ret = 0;
3912 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003914 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003915 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003916
Yan, Zhenga5719522009-09-24 09:17:31 -04003917 l = path->nodes[0];
3918 slot = path->slots[0];
3919 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3920 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3921 return -EOVERFLOW;
3922
Chris Mason44871b12009-03-13 10:04:31 -04003923 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003924 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003925 wret = push_leaf_right(trans, root, path, data_size,
3926 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003927 if (wret < 0)
3928 return wret;
3929 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003930 wret = push_leaf_left(trans, root, path, data_size,
3931 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003932 if (wret < 0)
3933 return wret;
3934 }
3935 l = path->nodes[0];
3936
3937 /* did the pushes work? */
3938 if (btrfs_leaf_free_space(root, l) >= data_size)
3939 return 0;
3940 }
3941
3942 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003943 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003944 if (ret)
3945 return ret;
3946 }
3947again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003948 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003949 l = path->nodes[0];
3950 slot = path->slots[0];
3951 nritems = btrfs_header_nritems(l);
3952 mid = (nritems + 1) / 2;
3953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003954 if (mid <= slot) {
3955 if (nritems == 1 ||
3956 leaf_space_used(l, mid, nritems - mid) + data_size >
3957 BTRFS_LEAF_DATA_SIZE(root)) {
3958 if (slot >= nritems) {
3959 split = 0;
3960 } else {
3961 mid = slot;
3962 if (mid != nritems &&
3963 leaf_space_used(l, mid, nritems - mid) +
3964 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003965 if (data_size && !tried_avoid_double)
3966 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003967 split = 2;
3968 }
3969 }
3970 }
3971 } else {
3972 if (leaf_space_used(l, 0, mid) + data_size >
3973 BTRFS_LEAF_DATA_SIZE(root)) {
3974 if (!extend && data_size && slot == 0) {
3975 split = 0;
3976 } else if ((extend || !data_size) && slot == 0) {
3977 mid = 1;
3978 } else {
3979 mid = slot;
3980 if (mid != nritems &&
3981 leaf_space_used(l, mid, nritems - mid) +
3982 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003983 if (data_size && !tried_avoid_double)
3984 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003985 split = 2 ;
3986 }
3987 }
3988 }
3989 }
3990
3991 if (split == 0)
3992 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3993 else
3994 btrfs_item_key(l, &disk_key, mid);
3995
3996 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003997 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003998 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003999 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004000 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004001
4002 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004003
4004 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4005 btrfs_set_header_bytenr(right, right->start);
4006 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004007 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004008 btrfs_set_header_owner(right, root->root_key.objectid);
4009 btrfs_set_header_level(right, 0);
4010 write_extent_buffer(right, root->fs_info->fsid,
4011 (unsigned long)btrfs_header_fsid(right),
4012 BTRFS_FSID_SIZE);
4013
4014 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
4015 (unsigned long)btrfs_header_chunk_tree_uuid(right),
4016 BTRFS_UUID_SIZE);
4017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004018 if (split == 0) {
4019 if (mid <= slot) {
4020 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004021 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004022 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004023 btrfs_tree_unlock(path->nodes[0]);
4024 free_extent_buffer(path->nodes[0]);
4025 path->nodes[0] = right;
4026 path->slots[0] = 0;
4027 path->slots[1] += 1;
4028 } else {
4029 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004030 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004031 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004032 btrfs_tree_unlock(path->nodes[0]);
4033 free_extent_buffer(path->nodes[0]);
4034 path->nodes[0] = right;
4035 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004036 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004037 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004038 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004039 btrfs_mark_buffer_dirty(right);
4040 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004041 }
4042
Jeff Mahoney143bede2012-03-01 14:56:26 +01004043 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004046 BUG_ON(num_doubles != 0);
4047 num_doubles++;
4048 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004049 }
Chris Mason44871b12009-03-13 10:04:31 -04004050
Jeff Mahoney143bede2012-03-01 14:56:26 +01004051 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004052
4053push_for_double:
4054 push_for_double_split(trans, root, path, data_size);
4055 tried_avoid_double = 1;
4056 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4057 return 0;
4058 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004059}
4060
Yan, Zhengad48fd752009-11-12 09:33:58 +00004061static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
4063 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004064{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004065 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004066 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004067 struct btrfs_file_extent_item *fi;
4068 u64 extent_len = 0;
4069 u32 item_size;
4070 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004071
4072 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004073 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4074
4075 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4076 key.type != BTRFS_EXTENT_CSUM_KEY);
4077
4078 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4079 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004080
4081 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004082 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4083 fi = btrfs_item_ptr(leaf, path->slots[0],
4084 struct btrfs_file_extent_item);
4085 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4086 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004087 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004088
Chris Mason459931e2008-12-10 09:10:46 -05004089 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004090 path->search_for_split = 1;
4091 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004092 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004093 if (ret < 0)
4094 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004095
Yan, Zhengad48fd752009-11-12 09:33:58 +00004096 ret = -EAGAIN;
4097 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004098 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004099 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4100 goto err;
4101
Chris Mason109f6ae2010-04-02 09:20:18 -04004102 /* the leaf has changed, it now has room. return now */
4103 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4104 goto err;
4105
Yan, Zhengad48fd752009-11-12 09:33:58 +00004106 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4107 fi = btrfs_item_ptr(leaf, path->slots[0],
4108 struct btrfs_file_extent_item);
4109 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4110 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004111 }
4112
Chris Masonb9473432009-03-13 11:00:37 -04004113 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004114 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004115 if (ret)
4116 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004117
Yan, Zhengad48fd752009-11-12 09:33:58 +00004118 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004119 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004120 return 0;
4121err:
4122 path->keep_locks = 0;
4123 return ret;
4124}
4125
4126static noinline int split_item(struct btrfs_trans_handle *trans,
4127 struct btrfs_root *root,
4128 struct btrfs_path *path,
4129 struct btrfs_key *new_key,
4130 unsigned long split_offset)
4131{
4132 struct extent_buffer *leaf;
4133 struct btrfs_item *item;
4134 struct btrfs_item *new_item;
4135 int slot;
4136 char *buf;
4137 u32 nritems;
4138 u32 item_size;
4139 u32 orig_offset;
4140 struct btrfs_disk_key disk_key;
4141
Chris Masonb9473432009-03-13 11:00:37 -04004142 leaf = path->nodes[0];
4143 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4144
Chris Masonb4ce94d2009-02-04 09:25:08 -05004145 btrfs_set_path_blocking(path);
4146
Chris Mason459931e2008-12-10 09:10:46 -05004147 item = btrfs_item_nr(leaf, path->slots[0]);
4148 orig_offset = btrfs_item_offset(leaf, item);
4149 item_size = btrfs_item_size(leaf, item);
4150
Chris Mason459931e2008-12-10 09:10:46 -05004151 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004152 if (!buf)
4153 return -ENOMEM;
4154
Chris Mason459931e2008-12-10 09:10:46 -05004155 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4156 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004157
Chris Mason459931e2008-12-10 09:10:46 -05004158 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004159 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004160 if (slot != nritems) {
4161 /* shift the items */
4162 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004163 btrfs_item_nr_offset(slot),
4164 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004165 }
4166
4167 btrfs_cpu_key_to_disk(&disk_key, new_key);
4168 btrfs_set_item_key(leaf, &disk_key, slot);
4169
4170 new_item = btrfs_item_nr(leaf, slot);
4171
4172 btrfs_set_item_offset(leaf, new_item, orig_offset);
4173 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4174
4175 btrfs_set_item_offset(leaf, item,
4176 orig_offset + item_size - split_offset);
4177 btrfs_set_item_size(leaf, item, split_offset);
4178
4179 btrfs_set_header_nritems(leaf, nritems + 1);
4180
4181 /* write the data for the start of the original item */
4182 write_extent_buffer(leaf, buf,
4183 btrfs_item_ptr_offset(leaf, path->slots[0]),
4184 split_offset);
4185
4186 /* write the data for the new item */
4187 write_extent_buffer(leaf, buf + split_offset,
4188 btrfs_item_ptr_offset(leaf, slot),
4189 item_size - split_offset);
4190 btrfs_mark_buffer_dirty(leaf);
4191
Yan, Zhengad48fd752009-11-12 09:33:58 +00004192 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004193 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004194 return 0;
4195}
4196
4197/*
4198 * This function splits a single item into two items,
4199 * giving 'new_key' to the new item and splitting the
4200 * old one at split_offset (from the start of the item).
4201 *
4202 * The path may be released by this operation. After
4203 * the split, the path is pointing to the old item. The
4204 * new item is going to be in the same node as the old one.
4205 *
4206 * Note, the item being split must be smaller enough to live alone on
4207 * a tree block with room for one extra struct btrfs_item
4208 *
4209 * This allows us to split the item in place, keeping a lock on the
4210 * leaf the entire time.
4211 */
4212int btrfs_split_item(struct btrfs_trans_handle *trans,
4213 struct btrfs_root *root,
4214 struct btrfs_path *path,
4215 struct btrfs_key *new_key,
4216 unsigned long split_offset)
4217{
4218 int ret;
4219 ret = setup_leaf_for_split(trans, root, path,
4220 sizeof(struct btrfs_item));
4221 if (ret)
4222 return ret;
4223
4224 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004225 return ret;
4226}
4227
4228/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004229 * This function duplicate a item, giving 'new_key' to the new item.
4230 * It guarantees both items live in the same tree leaf and the new item
4231 * is contiguous with the original item.
4232 *
4233 * This allows us to split file extent in place, keeping a lock on the
4234 * leaf the entire time.
4235 */
4236int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4237 struct btrfs_root *root,
4238 struct btrfs_path *path,
4239 struct btrfs_key *new_key)
4240{
4241 struct extent_buffer *leaf;
4242 int ret;
4243 u32 item_size;
4244
4245 leaf = path->nodes[0];
4246 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4247 ret = setup_leaf_for_split(trans, root, path,
4248 item_size + sizeof(struct btrfs_item));
4249 if (ret)
4250 return ret;
4251
4252 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004253 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004254 item_size, item_size +
4255 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004256 leaf = path->nodes[0];
4257 memcpy_extent_buffer(leaf,
4258 btrfs_item_ptr_offset(leaf, path->slots[0]),
4259 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4260 item_size);
4261 return 0;
4262}
4263
4264/*
Chris Masond352ac62008-09-29 15:18:18 -04004265 * make the item pointed to by the path smaller. new_size indicates
4266 * how small to make it, and from_end tells us if we just chop bytes
4267 * off the end of the item or if we shift the item to chop bytes off
4268 * the front.
4269 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004270void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004271 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004272{
Chris Masonb18c6682007-04-17 13:26:50 -04004273 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004274 struct extent_buffer *leaf;
4275 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004276 u32 nritems;
4277 unsigned int data_end;
4278 unsigned int old_data_start;
4279 unsigned int old_size;
4280 unsigned int size_diff;
4281 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004282 struct btrfs_map_token token;
4283
4284 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004285
Chris Mason5f39d392007-10-15 16:14:19 -04004286 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004287 slot = path->slots[0];
4288
4289 old_size = btrfs_item_size_nr(leaf, slot);
4290 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004291 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004292
Chris Mason5f39d392007-10-15 16:14:19 -04004293 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004294 data_end = leaf_data_end(root, leaf);
4295
Chris Mason5f39d392007-10-15 16:14:19 -04004296 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004297
Chris Masonb18c6682007-04-17 13:26:50 -04004298 size_diff = old_size - new_size;
4299
4300 BUG_ON(slot < 0);
4301 BUG_ON(slot >= nritems);
4302
4303 /*
4304 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4305 */
4306 /* first correct the data pointers */
4307 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004308 u32 ioff;
4309 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004310
Chris Masoncfed81a2012-03-03 07:40:03 -05004311 ioff = btrfs_token_item_offset(leaf, item, &token);
4312 btrfs_set_token_item_offset(leaf, item,
4313 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004314 }
Chris Masondb945352007-10-15 16:15:53 -04004315
Chris Masonb18c6682007-04-17 13:26:50 -04004316 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004317 if (from_end) {
4318 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4319 data_end + size_diff, btrfs_leaf_data(leaf) +
4320 data_end, old_data_start + new_size - data_end);
4321 } else {
4322 struct btrfs_disk_key disk_key;
4323 u64 offset;
4324
4325 btrfs_item_key(leaf, &disk_key, slot);
4326
4327 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4328 unsigned long ptr;
4329 struct btrfs_file_extent_item *fi;
4330
4331 fi = btrfs_item_ptr(leaf, slot,
4332 struct btrfs_file_extent_item);
4333 fi = (struct btrfs_file_extent_item *)(
4334 (unsigned long)fi - size_diff);
4335
4336 if (btrfs_file_extent_type(leaf, fi) ==
4337 BTRFS_FILE_EXTENT_INLINE) {
4338 ptr = btrfs_item_ptr_offset(leaf, slot);
4339 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004340 (unsigned long)fi,
4341 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004342 disk_bytenr));
4343 }
4344 }
4345
4346 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4347 data_end + size_diff, btrfs_leaf_data(leaf) +
4348 data_end, old_data_start - data_end);
4349
4350 offset = btrfs_disk_key_offset(&disk_key);
4351 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4352 btrfs_set_item_key(leaf, &disk_key, slot);
4353 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004354 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004355 }
Chris Mason5f39d392007-10-15 16:14:19 -04004356
4357 item = btrfs_item_nr(leaf, slot);
4358 btrfs_set_item_size(leaf, item, new_size);
4359 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004360
Chris Mason5f39d392007-10-15 16:14:19 -04004361 if (btrfs_leaf_free_space(root, leaf) < 0) {
4362 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004363 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004364 }
Chris Masonb18c6682007-04-17 13:26:50 -04004365}
4366
Chris Masond352ac62008-09-29 15:18:18 -04004367/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004368 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004369 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004370void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004371 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004372{
Chris Mason6567e832007-04-16 09:22:45 -04004373 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004374 struct extent_buffer *leaf;
4375 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004376 u32 nritems;
4377 unsigned int data_end;
4378 unsigned int old_data;
4379 unsigned int old_size;
4380 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004381 struct btrfs_map_token token;
4382
4383 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004384
Chris Mason5f39d392007-10-15 16:14:19 -04004385 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004386
Chris Mason5f39d392007-10-15 16:14:19 -04004387 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004388 data_end = leaf_data_end(root, leaf);
4389
Chris Mason5f39d392007-10-15 16:14:19 -04004390 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4391 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004392 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004393 }
Chris Mason6567e832007-04-16 09:22:45 -04004394 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004395 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004396
4397 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004398 if (slot >= nritems) {
4399 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004400 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4401 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004402 BUG_ON(1);
4403 }
Chris Mason6567e832007-04-16 09:22:45 -04004404
4405 /*
4406 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4407 */
4408 /* first correct the data pointers */
4409 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004410 u32 ioff;
4411 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004412
Chris Masoncfed81a2012-03-03 07:40:03 -05004413 ioff = btrfs_token_item_offset(leaf, item, &token);
4414 btrfs_set_token_item_offset(leaf, item,
4415 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004416 }
Chris Mason5f39d392007-10-15 16:14:19 -04004417
Chris Mason6567e832007-04-16 09:22:45 -04004418 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004419 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004420 data_end - data_size, btrfs_leaf_data(leaf) +
4421 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004422
Chris Mason6567e832007-04-16 09:22:45 -04004423 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004424 old_size = btrfs_item_size_nr(leaf, slot);
4425 item = btrfs_item_nr(leaf, slot);
4426 btrfs_set_item_size(leaf, item, old_size + data_size);
4427 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004428
Chris Mason5f39d392007-10-15 16:14:19 -04004429 if (btrfs_leaf_free_space(root, leaf) < 0) {
4430 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004431 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004432 }
Chris Mason6567e832007-04-16 09:22:45 -04004433}
4434
Chris Mason74123bd2007-02-02 11:05:29 -05004435/*
Chris Mason44871b12009-03-13 10:04:31 -04004436 * this is a helper for btrfs_insert_empty_items, the main goal here is
4437 * to save stack depth by doing the bulk of the work in a function
4438 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004439 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004440void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004441 struct btrfs_key *cpu_key, u32 *data_size,
4442 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004443{
Chris Mason5f39d392007-10-15 16:14:19 -04004444 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004445 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004446 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004447 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004448 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004449 struct extent_buffer *leaf;
4450 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004451 struct btrfs_map_token token;
4452
4453 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004454
Chris Mason5f39d392007-10-15 16:14:19 -04004455 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004456 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004457
Chris Mason5f39d392007-10-15 16:14:19 -04004458 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004459 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004460
Chris Masonf25956c2008-09-12 15:32:53 -04004461 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004462 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004463 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004464 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004465 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004466 }
Chris Mason5f39d392007-10-15 16:14:19 -04004467
Chris Masonbe0e5c02007-01-26 15:51:26 -05004468 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004469 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004470
Chris Mason5f39d392007-10-15 16:14:19 -04004471 if (old_data < data_end) {
4472 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004473 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004474 slot, old_data, data_end);
4475 BUG_ON(1);
4476 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004477 /*
4478 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4479 */
4480 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004481 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004482 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004483
Chris Mason5f39d392007-10-15 16:14:19 -04004484 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004485 ioff = btrfs_token_item_offset(leaf, item, &token);
4486 btrfs_set_token_item_offset(leaf, item,
4487 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004488 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004489 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004490 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004491 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004492 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004493
4494 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004495 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004496 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004497 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004498 data_end = old_data;
4499 }
Chris Mason5f39d392007-10-15 16:14:19 -04004500
Chris Mason62e27492007-03-15 12:56:47 -04004501 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004502 for (i = 0; i < nr; i++) {
4503 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4504 btrfs_set_item_key(leaf, &disk_key, slot + i);
4505 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004506 btrfs_set_token_item_offset(leaf, item,
4507 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004508 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004509 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004510 }
Chris Mason44871b12009-03-13 10:04:31 -04004511
Chris Mason9c583092008-01-29 15:15:18 -05004512 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004513
Chris Mason5a01a2e2008-01-30 11:43:54 -05004514 if (slot == 0) {
4515 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004516 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004517 }
Chris Masonb9473432009-03-13 11:00:37 -04004518 btrfs_unlock_up_safe(path, 1);
4519 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004520
Chris Mason5f39d392007-10-15 16:14:19 -04004521 if (btrfs_leaf_free_space(root, leaf) < 0) {
4522 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004523 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004524 }
Chris Mason44871b12009-03-13 10:04:31 -04004525}
4526
4527/*
4528 * Given a key and some data, insert items into the tree.
4529 * This does all the path init required, making room in the tree if needed.
4530 */
4531int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4532 struct btrfs_root *root,
4533 struct btrfs_path *path,
4534 struct btrfs_key *cpu_key, u32 *data_size,
4535 int nr)
4536{
Chris Mason44871b12009-03-13 10:04:31 -04004537 int ret = 0;
4538 int slot;
4539 int i;
4540 u32 total_size = 0;
4541 u32 total_data = 0;
4542
4543 for (i = 0; i < nr; i++)
4544 total_data += data_size[i];
4545
4546 total_size = total_data + (nr * sizeof(struct btrfs_item));
4547 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4548 if (ret == 0)
4549 return -EEXIST;
4550 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004551 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004552
Chris Mason44871b12009-03-13 10:04:31 -04004553 slot = path->slots[0];
4554 BUG_ON(slot < 0);
4555
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004556 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004557 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004558 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004559}
4560
4561/*
4562 * Given a key and some data, insert an item into the tree.
4563 * This does all the path init required, making room in the tree if needed.
4564 */
Chris Masone089f052007-03-16 16:20:31 -04004565int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4566 *root, struct btrfs_key *cpu_key, void *data, u32
4567 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004568{
4569 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004570 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004571 struct extent_buffer *leaf;
4572 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004573
Chris Mason2c90e5d2007-04-02 10:50:19 -04004574 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004575 if (!path)
4576 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004577 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004578 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004579 leaf = path->nodes[0];
4580 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4581 write_extent_buffer(leaf, data, ptr, data_size);
4582 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004583 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004584 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004585 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004586}
4587
Chris Mason74123bd2007-02-02 11:05:29 -05004588/*
Chris Mason5de08d72007-02-24 06:24:44 -05004589 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004590 *
Chris Masond352ac62008-09-29 15:18:18 -04004591 * the tree should have been previously balanced so the deletion does not
4592 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004593 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004594static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4595 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004596{
Chris Mason5f39d392007-10-15 16:14:19 -04004597 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004598 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004599 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004600
Chris Mason5f39d392007-10-15 16:14:19 -04004601 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004602 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004603 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004604 tree_mod_log_eb_move(root->fs_info, parent, slot,
4605 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004606 memmove_extent_buffer(parent,
4607 btrfs_node_key_ptr_offset(slot),
4608 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004609 sizeof(struct btrfs_key_ptr) *
4610 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004611 } else if (level) {
4612 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004613 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004614 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004615 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004616
Chris Mason7518a232007-03-12 12:01:18 -04004617 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004618 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004619 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004620 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004621 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004622 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004623 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004624 struct btrfs_disk_key disk_key;
4625
4626 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004627 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004628 }
Chris Masond6025572007-03-30 14:27:56 -04004629 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004630}
4631
Chris Mason74123bd2007-02-02 11:05:29 -05004632/*
Chris Mason323ac952008-10-01 19:05:46 -04004633 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004634 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004635 *
4636 * This deletes the pointer in path->nodes[1] and frees the leaf
4637 * block extent. zero is returned if it all worked out, < 0 otherwise.
4638 *
4639 * The path must have already been setup for deleting the leaf, including
4640 * all the proper balancing. path->nodes[1] must be locked.
4641 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004642static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4643 struct btrfs_root *root,
4644 struct btrfs_path *path,
4645 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004646{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004647 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004648 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004649
Chris Mason4d081c42009-02-04 09:31:28 -05004650 /*
4651 * btrfs_free_extent is expensive, we want to make sure we
4652 * aren't holding any locks when we call it
4653 */
4654 btrfs_unlock_up_safe(path, 0);
4655
Yan, Zhengf0486c62010-05-16 10:46:25 -04004656 root_sub_used(root, leaf->len);
4657
Josef Bacik3083ee22012-03-09 16:01:49 -05004658 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004659 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004660 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004661}
4662/*
Chris Mason74123bd2007-02-02 11:05:29 -05004663 * delete the item at the leaf level in path. If that empties
4664 * the leaf, remove it from the tree
4665 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004666int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4667 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004668{
Chris Mason5f39d392007-10-15 16:14:19 -04004669 struct extent_buffer *leaf;
4670 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004671 int last_off;
4672 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004673 int ret = 0;
4674 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004675 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004676 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004677 struct btrfs_map_token token;
4678
4679 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004680
Chris Mason5f39d392007-10-15 16:14:19 -04004681 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004682 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4683
4684 for (i = 0; i < nr; i++)
4685 dsize += btrfs_item_size_nr(leaf, slot + i);
4686
Chris Mason5f39d392007-10-15 16:14:19 -04004687 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004688
Chris Mason85e21ba2008-01-29 15:11:36 -05004689 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004690 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004691
4692 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004693 data_end + dsize,
4694 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004695 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004696
Chris Mason85e21ba2008-01-29 15:11:36 -05004697 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004698 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004699
Chris Mason5f39d392007-10-15 16:14:19 -04004700 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004701 ioff = btrfs_token_item_offset(leaf, item, &token);
4702 btrfs_set_token_item_offset(leaf, item,
4703 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004704 }
Chris Masondb945352007-10-15 16:15:53 -04004705
Chris Mason5f39d392007-10-15 16:14:19 -04004706 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004707 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004708 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004709 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004710 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004711 btrfs_set_header_nritems(leaf, nritems - nr);
4712 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004713
Chris Mason74123bd2007-02-02 11:05:29 -05004714 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004715 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004716 if (leaf == root->node) {
4717 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004718 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004719 btrfs_set_path_blocking(path);
4720 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004721 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004722 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004723 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004724 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004725 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004726 struct btrfs_disk_key disk_key;
4727
4728 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004729 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004730 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004731
Chris Mason74123bd2007-02-02 11:05:29 -05004732 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004733 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004734 /* push_leaf_left fixes the path.
4735 * make sure the path still points to our leaf
4736 * for possible call to del_ptr below
4737 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004738 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004739 extent_buffer_get(leaf);
4740
Chris Masonb9473432009-03-13 11:00:37 -04004741 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004742 wret = push_leaf_left(trans, root, path, 1, 1,
4743 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004744 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004745 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004746
4747 if (path->nodes[0] == leaf &&
4748 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004749 wret = push_leaf_right(trans, root, path, 1,
4750 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004751 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004752 ret = wret;
4753 }
Chris Mason5f39d392007-10-15 16:14:19 -04004754
4755 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004756 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004757 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004758 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004759 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004760 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004761 /* if we're still in the path, make sure
4762 * we're dirty. Otherwise, one of the
4763 * push_leaf functions must have already
4764 * dirtied this buffer
4765 */
4766 if (path->nodes[0] == leaf)
4767 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004768 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004769 }
Chris Masond5719762007-03-23 10:01:08 -04004770 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004771 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004772 }
4773 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004774 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004775}
4776
Chris Mason97571fd2007-02-24 13:39:08 -05004777/*
Chris Mason925baed2008-06-25 16:01:30 -04004778 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004779 * returns 0 if it found something or 1 if there are no lesser leaves.
4780 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004781 *
4782 * This may release the path, and so you may lose any locks held at the
4783 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004784 */
4785int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4786{
Chris Mason925baed2008-06-25 16:01:30 -04004787 struct btrfs_key key;
4788 struct btrfs_disk_key found_key;
4789 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004790
Chris Mason925baed2008-06-25 16:01:30 -04004791 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004792
Chris Mason925baed2008-06-25 16:01:30 -04004793 if (key.offset > 0)
4794 key.offset--;
4795 else if (key.type > 0)
4796 key.type--;
4797 else if (key.objectid > 0)
4798 key.objectid--;
4799 else
4800 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004801
David Sterbab3b4aa72011-04-21 01:20:15 +02004802 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004803 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4804 if (ret < 0)
4805 return ret;
4806 btrfs_item_key(path->nodes[0], &found_key, 0);
4807 ret = comp_keys(&found_key, &key);
4808 if (ret < 0)
4809 return 0;
4810 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004811}
4812
Chris Mason3f157a22008-06-25 16:01:31 -04004813/*
4814 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004815 * for nodes or leaves that are have a minimum transaction id.
4816 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004817 *
4818 * This does not cow, but it does stuff the starting key it finds back
4819 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4820 * key and get a writable path.
4821 *
4822 * This does lock as it descends, and path->keep_locks should be set
4823 * to 1 by the caller.
4824 *
4825 * This honors path->lowest_level to prevent descent past a given level
4826 * of the tree.
4827 *
Chris Masond352ac62008-09-29 15:18:18 -04004828 * min_trans indicates the oldest transaction that you are interested
4829 * in walking through. Any nodes or leaves older than min_trans are
4830 * skipped over (without reading them).
4831 *
Chris Mason3f157a22008-06-25 16:01:31 -04004832 * returns zero if something useful was found, < 0 on error and 1 if there
4833 * was nothing in the tree that matched the search criteria.
4834 */
4835int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004836 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004837 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004838 u64 min_trans)
4839{
4840 struct extent_buffer *cur;
4841 struct btrfs_key found_key;
4842 int slot;
Yan96524802008-07-24 12:19:49 -04004843 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004844 u32 nritems;
4845 int level;
4846 int ret = 1;
4847
Chris Mason934d3752008-12-08 16:43:10 -05004848 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004849again:
Chris Masonbd681512011-07-16 15:23:14 -04004850 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004851 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004852 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004853 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004854 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004855
4856 if (btrfs_header_generation(cur) < min_trans) {
4857 ret = 1;
4858 goto out;
4859 }
Chris Masond3977122009-01-05 21:25:51 -05004860 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004861 nritems = btrfs_header_nritems(cur);
4862 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004863 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004864
Chris Mason323ac952008-10-01 19:05:46 -04004865 /* at the lowest level, we're done, setup the path and exit */
4866 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004867 if (slot >= nritems)
4868 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004869 ret = 0;
4870 path->slots[level] = slot;
4871 btrfs_item_key_to_cpu(cur, &found_key, slot);
4872 goto out;
4873 }
Yan96524802008-07-24 12:19:49 -04004874 if (sret && slot > 0)
4875 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004876 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004877 * check this node pointer against the min_trans parameters.
4878 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004879 */
Chris Masond3977122009-01-05 21:25:51 -05004880 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004881 u64 blockptr;
4882 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004883
Chris Mason3f157a22008-06-25 16:01:31 -04004884 blockptr = btrfs_node_blockptr(cur, slot);
4885 gen = btrfs_node_ptr_generation(cur, slot);
4886 if (gen < min_trans) {
4887 slot++;
4888 continue;
4889 }
Eric Sandeende78b512013-01-31 18:21:12 +00004890 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004891 }
Chris Masone02119d2008-09-05 16:13:11 -04004892find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004893 /*
4894 * we didn't find a candidate key in this node, walk forward
4895 * and find another one
4896 */
4897 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004898 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004899 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004900 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004901 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004902 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004903 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004904 goto again;
4905 } else {
4906 goto out;
4907 }
4908 }
4909 /* save our key for returning back */
4910 btrfs_node_key_to_cpu(cur, &found_key, slot);
4911 path->slots[level] = slot;
4912 if (level == path->lowest_level) {
4913 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004914 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004915 goto out;
4916 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004917 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004918 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004919 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004920
Chris Masonbd681512011-07-16 15:23:14 -04004921 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004922
Chris Masonbd681512011-07-16 15:23:14 -04004923 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004924 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004925 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004926 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004927 }
4928out:
4929 if (ret == 0)
4930 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004931 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004932 return ret;
4933}
4934
Alexander Block70698302012-06-05 21:07:48 +02004935static void tree_move_down(struct btrfs_root *root,
4936 struct btrfs_path *path,
4937 int *level, int root_level)
4938{
Chris Mason74dd17f2012-08-07 16:25:13 -04004939 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004940 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4941 path->slots[*level]);
4942 path->slots[*level - 1] = 0;
4943 (*level)--;
4944}
4945
4946static int tree_move_next_or_upnext(struct btrfs_root *root,
4947 struct btrfs_path *path,
4948 int *level, int root_level)
4949{
4950 int ret = 0;
4951 int nritems;
4952 nritems = btrfs_header_nritems(path->nodes[*level]);
4953
4954 path->slots[*level]++;
4955
Chris Mason74dd17f2012-08-07 16:25:13 -04004956 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004957 if (*level == root_level)
4958 return -1;
4959
4960 /* move upnext */
4961 path->slots[*level] = 0;
4962 free_extent_buffer(path->nodes[*level]);
4963 path->nodes[*level] = NULL;
4964 (*level)++;
4965 path->slots[*level]++;
4966
4967 nritems = btrfs_header_nritems(path->nodes[*level]);
4968 ret = 1;
4969 }
4970 return ret;
4971}
4972
4973/*
4974 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
4975 * or down.
4976 */
4977static int tree_advance(struct btrfs_root *root,
4978 struct btrfs_path *path,
4979 int *level, int root_level,
4980 int allow_down,
4981 struct btrfs_key *key)
4982{
4983 int ret;
4984
4985 if (*level == 0 || !allow_down) {
4986 ret = tree_move_next_or_upnext(root, path, level, root_level);
4987 } else {
4988 tree_move_down(root, path, level, root_level);
4989 ret = 0;
4990 }
4991 if (ret >= 0) {
4992 if (*level == 0)
4993 btrfs_item_key_to_cpu(path->nodes[*level], key,
4994 path->slots[*level]);
4995 else
4996 btrfs_node_key_to_cpu(path->nodes[*level], key,
4997 path->slots[*level]);
4998 }
4999 return ret;
5000}
5001
5002static int tree_compare_item(struct btrfs_root *left_root,
5003 struct btrfs_path *left_path,
5004 struct btrfs_path *right_path,
5005 char *tmp_buf)
5006{
5007 int cmp;
5008 int len1, len2;
5009 unsigned long off1, off2;
5010
5011 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5012 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5013 if (len1 != len2)
5014 return 1;
5015
5016 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5017 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5018 right_path->slots[0]);
5019
5020 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5021
5022 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5023 if (cmp)
5024 return 1;
5025 return 0;
5026}
5027
5028#define ADVANCE 1
5029#define ADVANCE_ONLY_NEXT -1
5030
5031/*
5032 * This function compares two trees and calls the provided callback for
5033 * every changed/new/deleted item it finds.
5034 * If shared tree blocks are encountered, whole subtrees are skipped, making
5035 * the compare pretty fast on snapshotted subvolumes.
5036 *
5037 * This currently works on commit roots only. As commit roots are read only,
5038 * we don't do any locking. The commit roots are protected with transactions.
5039 * Transactions are ended and rejoined when a commit is tried in between.
5040 *
5041 * This function checks for modifications done to the trees while comparing.
5042 * If it detects a change, it aborts immediately.
5043 */
5044int btrfs_compare_trees(struct btrfs_root *left_root,
5045 struct btrfs_root *right_root,
5046 btrfs_changed_cb_t changed_cb, void *ctx)
5047{
5048 int ret;
5049 int cmp;
5050 struct btrfs_trans_handle *trans = NULL;
5051 struct btrfs_path *left_path = NULL;
5052 struct btrfs_path *right_path = NULL;
5053 struct btrfs_key left_key;
5054 struct btrfs_key right_key;
5055 char *tmp_buf = NULL;
5056 int left_root_level;
5057 int right_root_level;
5058 int left_level;
5059 int right_level;
5060 int left_end_reached;
5061 int right_end_reached;
5062 int advance_left;
5063 int advance_right;
5064 u64 left_blockptr;
5065 u64 right_blockptr;
5066 u64 left_start_ctransid;
5067 u64 right_start_ctransid;
5068 u64 ctransid;
5069
5070 left_path = btrfs_alloc_path();
5071 if (!left_path) {
5072 ret = -ENOMEM;
5073 goto out;
5074 }
5075 right_path = btrfs_alloc_path();
5076 if (!right_path) {
5077 ret = -ENOMEM;
5078 goto out;
5079 }
5080
5081 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5082 if (!tmp_buf) {
5083 ret = -ENOMEM;
5084 goto out;
5085 }
5086
5087 left_path->search_commit_root = 1;
5088 left_path->skip_locking = 1;
5089 right_path->search_commit_root = 1;
5090 right_path->skip_locking = 1;
5091
Anand Jain5f3ab902012-12-07 09:28:54 +00005092 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005093 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005094 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005095
Anand Jain5f3ab902012-12-07 09:28:54 +00005096 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005097 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005098 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005099
5100 trans = btrfs_join_transaction(left_root);
5101 if (IS_ERR(trans)) {
5102 ret = PTR_ERR(trans);
5103 trans = NULL;
5104 goto out;
5105 }
5106
5107 /*
5108 * Strategy: Go to the first items of both trees. Then do
5109 *
5110 * If both trees are at level 0
5111 * Compare keys of current items
5112 * If left < right treat left item as new, advance left tree
5113 * and repeat
5114 * If left > right treat right item as deleted, advance right tree
5115 * and repeat
5116 * If left == right do deep compare of items, treat as changed if
5117 * needed, advance both trees and repeat
5118 * If both trees are at the same level but not at level 0
5119 * Compare keys of current nodes/leafs
5120 * If left < right advance left tree and repeat
5121 * If left > right advance right tree and repeat
5122 * If left == right compare blockptrs of the next nodes/leafs
5123 * If they match advance both trees but stay at the same level
5124 * and repeat
5125 * If they don't match advance both trees while allowing to go
5126 * deeper and repeat
5127 * If tree levels are different
5128 * Advance the tree that needs it and repeat
5129 *
5130 * Advancing a tree means:
5131 * If we are at level 0, try to go to the next slot. If that's not
5132 * possible, go one level up and repeat. Stop when we found a level
5133 * where we could go to the next slot. We may at this point be on a
5134 * node or a leaf.
5135 *
5136 * If we are not at level 0 and not on shared tree blocks, go one
5137 * level deeper.
5138 *
5139 * If we are not at level 0 and on shared tree blocks, go one slot to
5140 * the right if possible or go up and right.
5141 */
5142
5143 left_level = btrfs_header_level(left_root->commit_root);
5144 left_root_level = left_level;
5145 left_path->nodes[left_level] = left_root->commit_root;
5146 extent_buffer_get(left_path->nodes[left_level]);
5147
5148 right_level = btrfs_header_level(right_root->commit_root);
5149 right_root_level = right_level;
5150 right_path->nodes[right_level] = right_root->commit_root;
5151 extent_buffer_get(right_path->nodes[right_level]);
5152
5153 if (left_level == 0)
5154 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5155 &left_key, left_path->slots[left_level]);
5156 else
5157 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5158 &left_key, left_path->slots[left_level]);
5159 if (right_level == 0)
5160 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5161 &right_key, right_path->slots[right_level]);
5162 else
5163 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5164 &right_key, right_path->slots[right_level]);
5165
5166 left_end_reached = right_end_reached = 0;
5167 advance_left = advance_right = 0;
5168
5169 while (1) {
5170 /*
5171 * We need to make sure the transaction does not get committed
5172 * while we do anything on commit roots. This means, we need to
5173 * join and leave transactions for every item that we process.
5174 */
5175 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5176 btrfs_release_path(left_path);
5177 btrfs_release_path(right_path);
5178
5179 ret = btrfs_end_transaction(trans, left_root);
5180 trans = NULL;
5181 if (ret < 0)
5182 goto out;
5183 }
5184 /* now rejoin the transaction */
5185 if (!trans) {
5186 trans = btrfs_join_transaction(left_root);
5187 if (IS_ERR(trans)) {
5188 ret = PTR_ERR(trans);
5189 trans = NULL;
5190 goto out;
5191 }
5192
Anand Jain5f3ab902012-12-07 09:28:54 +00005193 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005194 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005195 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005196 if (ctransid != left_start_ctransid)
5197 left_start_ctransid = 0;
5198
Anand Jain5f3ab902012-12-07 09:28:54 +00005199 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005200 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005201 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005202 if (ctransid != right_start_ctransid)
5203 right_start_ctransid = 0;
5204
5205 if (!left_start_ctransid || !right_start_ctransid) {
5206 WARN(1, KERN_WARNING
5207 "btrfs: btrfs_compare_tree detected "
5208 "a change in one of the trees while "
5209 "iterating. This is probably a "
5210 "bug.\n");
5211 ret = -EIO;
5212 goto out;
5213 }
5214
5215 /*
5216 * the commit root may have changed, so start again
5217 * where we stopped
5218 */
5219 left_path->lowest_level = left_level;
5220 right_path->lowest_level = right_level;
5221 ret = btrfs_search_slot(NULL, left_root,
5222 &left_key, left_path, 0, 0);
5223 if (ret < 0)
5224 goto out;
5225 ret = btrfs_search_slot(NULL, right_root,
5226 &right_key, right_path, 0, 0);
5227 if (ret < 0)
5228 goto out;
5229 }
5230
5231 if (advance_left && !left_end_reached) {
5232 ret = tree_advance(left_root, left_path, &left_level,
5233 left_root_level,
5234 advance_left != ADVANCE_ONLY_NEXT,
5235 &left_key);
5236 if (ret < 0)
5237 left_end_reached = ADVANCE;
5238 advance_left = 0;
5239 }
5240 if (advance_right && !right_end_reached) {
5241 ret = tree_advance(right_root, right_path, &right_level,
5242 right_root_level,
5243 advance_right != ADVANCE_ONLY_NEXT,
5244 &right_key);
5245 if (ret < 0)
5246 right_end_reached = ADVANCE;
5247 advance_right = 0;
5248 }
5249
5250 if (left_end_reached && right_end_reached) {
5251 ret = 0;
5252 goto out;
5253 } else if (left_end_reached) {
5254 if (right_level == 0) {
5255 ret = changed_cb(left_root, right_root,
5256 left_path, right_path,
5257 &right_key,
5258 BTRFS_COMPARE_TREE_DELETED,
5259 ctx);
5260 if (ret < 0)
5261 goto out;
5262 }
5263 advance_right = ADVANCE;
5264 continue;
5265 } else if (right_end_reached) {
5266 if (left_level == 0) {
5267 ret = changed_cb(left_root, right_root,
5268 left_path, right_path,
5269 &left_key,
5270 BTRFS_COMPARE_TREE_NEW,
5271 ctx);
5272 if (ret < 0)
5273 goto out;
5274 }
5275 advance_left = ADVANCE;
5276 continue;
5277 }
5278
5279 if (left_level == 0 && right_level == 0) {
5280 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5281 if (cmp < 0) {
5282 ret = changed_cb(left_root, right_root,
5283 left_path, right_path,
5284 &left_key,
5285 BTRFS_COMPARE_TREE_NEW,
5286 ctx);
5287 if (ret < 0)
5288 goto out;
5289 advance_left = ADVANCE;
5290 } else if (cmp > 0) {
5291 ret = changed_cb(left_root, right_root,
5292 left_path, right_path,
5293 &right_key,
5294 BTRFS_COMPARE_TREE_DELETED,
5295 ctx);
5296 if (ret < 0)
5297 goto out;
5298 advance_right = ADVANCE;
5299 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005300 enum btrfs_compare_tree_result cmp;
5301
Chris Mason74dd17f2012-08-07 16:25:13 -04005302 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005303 ret = tree_compare_item(left_root, left_path,
5304 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005305 if (ret)
5306 cmp = BTRFS_COMPARE_TREE_CHANGED;
5307 else
5308 cmp = BTRFS_COMPARE_TREE_SAME;
5309 ret = changed_cb(left_root, right_root,
5310 left_path, right_path,
5311 &left_key, cmp, ctx);
5312 if (ret < 0)
5313 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005314 advance_left = ADVANCE;
5315 advance_right = ADVANCE;
5316 }
5317 } else if (left_level == right_level) {
5318 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5319 if (cmp < 0) {
5320 advance_left = ADVANCE;
5321 } else if (cmp > 0) {
5322 advance_right = ADVANCE;
5323 } else {
5324 left_blockptr = btrfs_node_blockptr(
5325 left_path->nodes[left_level],
5326 left_path->slots[left_level]);
5327 right_blockptr = btrfs_node_blockptr(
5328 right_path->nodes[right_level],
5329 right_path->slots[right_level]);
5330 if (left_blockptr == right_blockptr) {
5331 /*
5332 * As we're on a shared block, don't
5333 * allow to go deeper.
5334 */
5335 advance_left = ADVANCE_ONLY_NEXT;
5336 advance_right = ADVANCE_ONLY_NEXT;
5337 } else {
5338 advance_left = ADVANCE;
5339 advance_right = ADVANCE;
5340 }
5341 }
5342 } else if (left_level < right_level) {
5343 advance_right = ADVANCE;
5344 } else {
5345 advance_left = ADVANCE;
5346 }
5347 }
5348
5349out:
5350 btrfs_free_path(left_path);
5351 btrfs_free_path(right_path);
5352 kfree(tmp_buf);
5353
5354 if (trans) {
5355 if (!ret)
5356 ret = btrfs_end_transaction(trans, left_root);
5357 else
5358 btrfs_end_transaction(trans, left_root);
5359 }
5360
5361 return ret;
5362}
5363
Chris Mason3f157a22008-06-25 16:01:31 -04005364/*
5365 * this is similar to btrfs_next_leaf, but does not try to preserve
5366 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005367 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005368 *
5369 * 0 is returned if another key is found, < 0 if there are any errors
5370 * and 1 is returned if there are no higher keys in the tree
5371 *
5372 * path->keep_locks should be set to 1 on the search made before
5373 * calling this function.
5374 */
Chris Masone7a84562008-06-25 16:01:31 -04005375int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005376 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005377{
Chris Masone7a84562008-06-25 16:01:31 -04005378 int slot;
5379 struct extent_buffer *c;
5380
Chris Mason934d3752008-12-08 16:43:10 -05005381 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005382 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005383 if (!path->nodes[level])
5384 return 1;
5385
5386 slot = path->slots[level] + 1;
5387 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005388next:
Chris Masone7a84562008-06-25 16:01:31 -04005389 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005390 int ret;
5391 int orig_lowest;
5392 struct btrfs_key cur_key;
5393 if (level + 1 >= BTRFS_MAX_LEVEL ||
5394 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005395 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005396
5397 if (path->locks[level + 1]) {
5398 level++;
5399 continue;
5400 }
5401
5402 slot = btrfs_header_nritems(c) - 1;
5403 if (level == 0)
5404 btrfs_item_key_to_cpu(c, &cur_key, slot);
5405 else
5406 btrfs_node_key_to_cpu(c, &cur_key, slot);
5407
5408 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005409 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005410 path->lowest_level = level;
5411 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5412 0, 0);
5413 path->lowest_level = orig_lowest;
5414 if (ret < 0)
5415 return ret;
5416
5417 c = path->nodes[level];
5418 slot = path->slots[level];
5419 if (ret == 0)
5420 slot++;
5421 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005422 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005423
Chris Masone7a84562008-06-25 16:01:31 -04005424 if (level == 0)
5425 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005426 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005427 u64 gen = btrfs_node_ptr_generation(c, slot);
5428
Chris Mason3f157a22008-06-25 16:01:31 -04005429 if (gen < min_trans) {
5430 slot++;
5431 goto next;
5432 }
Chris Masone7a84562008-06-25 16:01:31 -04005433 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005434 }
Chris Masone7a84562008-06-25 16:01:31 -04005435 return 0;
5436 }
5437 return 1;
5438}
5439
Chris Mason7bb86312007-12-11 09:25:06 -05005440/*
Chris Mason925baed2008-06-25 16:01:30 -04005441 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005442 * returns 0 if it found something or 1 if there are no greater leaves.
5443 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005444 */
Chris Mason234b63a2007-03-13 10:46:10 -04005445int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005446{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005447 return btrfs_next_old_leaf(root, path, 0);
5448}
5449
5450int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5451 u64 time_seq)
5452{
Chris Masond97e63b2007-02-20 16:40:44 -05005453 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005454 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005455 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005456 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005457 struct btrfs_key key;
5458 u32 nritems;
5459 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005460 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005461 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005462
5463 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005464 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005465 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005466
Chris Mason8e73f272009-04-03 10:14:18 -04005467 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5468again:
5469 level = 1;
5470 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005471 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005472 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005473
Chris Masona2135012008-06-25 16:01:30 -04005474 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005475 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005476
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005477 if (time_seq)
5478 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5479 else
5480 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005481 path->keep_locks = 0;
5482
5483 if (ret < 0)
5484 return ret;
5485
Chris Masona2135012008-06-25 16:01:30 -04005486 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005487 /*
5488 * by releasing the path above we dropped all our locks. A balance
5489 * could have added more items next to the key that used to be
5490 * at the very end of the block. So, check again here and
5491 * advance the path if there are now more items available.
5492 */
Chris Masona2135012008-06-25 16:01:30 -04005493 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005494 if (ret == 0)
5495 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005496 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005497 goto done;
5498 }
Chris Masond97e63b2007-02-20 16:40:44 -05005499
Chris Masond3977122009-01-05 21:25:51 -05005500 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005501 if (!path->nodes[level]) {
5502 ret = 1;
5503 goto done;
5504 }
Chris Mason5f39d392007-10-15 16:14:19 -04005505
Chris Masond97e63b2007-02-20 16:40:44 -05005506 slot = path->slots[level] + 1;
5507 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005508 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005509 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005510 if (level == BTRFS_MAX_LEVEL) {
5511 ret = 1;
5512 goto done;
5513 }
Chris Masond97e63b2007-02-20 16:40:44 -05005514 continue;
5515 }
Chris Mason5f39d392007-10-15 16:14:19 -04005516
Chris Mason925baed2008-06-25 16:01:30 -04005517 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005518 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005519 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005520 }
Chris Mason5f39d392007-10-15 16:14:19 -04005521
Chris Mason8e73f272009-04-03 10:14:18 -04005522 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005523 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005524 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005525 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005526 if (ret == -EAGAIN)
5527 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005528
Chris Mason76a05b32009-05-14 13:24:30 -04005529 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005530 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005531 goto done;
5532 }
5533
Chris Mason5cd57b22008-06-25 16:01:30 -04005534 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005535 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005536 if (!ret && time_seq) {
5537 /*
5538 * If we don't get the lock, we may be racing
5539 * with push_leaf_left, holding that lock while
5540 * itself waiting for the leaf we've currently
5541 * locked. To solve this situation, we give up
5542 * on our lock and cycle.
5543 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005544 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005545 btrfs_release_path(path);
5546 cond_resched();
5547 goto again;
5548 }
Chris Mason8e73f272009-04-03 10:14:18 -04005549 if (!ret) {
5550 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005551 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005552 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005553 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005554 }
Chris Mason31533fb2011-07-26 16:01:59 -04005555 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005556 }
Chris Masond97e63b2007-02-20 16:40:44 -05005557 break;
5558 }
5559 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005560 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005561 level--;
5562 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005563 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005564 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005565
Chris Mason5f39d392007-10-15 16:14:19 -04005566 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005567 path->nodes[level] = next;
5568 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005569 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005570 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005571 if (!level)
5572 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005573
Chris Mason8e73f272009-04-03 10:14:18 -04005574 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005575 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005576 if (ret == -EAGAIN)
5577 goto again;
5578
Chris Mason76a05b32009-05-14 13:24:30 -04005579 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005580 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005581 goto done;
5582 }
5583
Chris Mason5cd57b22008-06-25 16:01:30 -04005584 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005585 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005586 if (!ret) {
5587 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005588 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005589 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005590 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005591 }
Chris Mason31533fb2011-07-26 16:01:59 -04005592 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005593 }
Chris Masond97e63b2007-02-20 16:40:44 -05005594 }
Chris Mason8e73f272009-04-03 10:14:18 -04005595 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005596done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005597 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005598 path->leave_spinning = old_spinning;
5599 if (!old_spinning)
5600 btrfs_set_path_blocking(path);
5601
5602 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005603}
Chris Mason0b86a832008-03-24 15:01:56 -04005604
Chris Mason3f157a22008-06-25 16:01:31 -04005605/*
5606 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5607 * searching until it gets past min_objectid or finds an item of 'type'
5608 *
5609 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5610 */
Chris Mason0b86a832008-03-24 15:01:56 -04005611int btrfs_previous_item(struct btrfs_root *root,
5612 struct btrfs_path *path, u64 min_objectid,
5613 int type)
5614{
5615 struct btrfs_key found_key;
5616 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005617 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005618 int ret;
5619
Chris Masond3977122009-01-05 21:25:51 -05005620 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005621 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005622 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005623 ret = btrfs_prev_leaf(root, path);
5624 if (ret != 0)
5625 return ret;
5626 } else {
5627 path->slots[0]--;
5628 }
5629 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005630 nritems = btrfs_header_nritems(leaf);
5631 if (nritems == 0)
5632 return 1;
5633 if (path->slots[0] == nritems)
5634 path->slots[0]--;
5635
Chris Mason0b86a832008-03-24 15:01:56 -04005636 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005637 if (found_key.objectid < min_objectid)
5638 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005639 if (found_key.type == type)
5640 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005641 if (found_key.objectid == min_objectid &&
5642 found_key.type < type)
5643 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005644 }
5645 return 1;
5646}