blob: 316136bd6dd7eb3899bcf060c3e0bd749f674310 [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
Ross Kirk0a4e5582013-09-24 10:12:38 +0100277 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500278 BTRFS_FSID_SIZE);
279
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400281 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200282 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400283 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200284 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500285
Chris Masonbe20aa92007-12-17 20:14:01 -0500286 if (ret)
287 return ret;
288
289 btrfs_mark_buffer_dirty(cow);
290 *cow_ret = cow;
291 return 0;
292}
293
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200294enum mod_log_op {
295 MOD_LOG_KEY_REPLACE,
296 MOD_LOG_KEY_ADD,
297 MOD_LOG_KEY_REMOVE,
298 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
299 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
300 MOD_LOG_MOVE_KEYS,
301 MOD_LOG_ROOT_REPLACE,
302};
303
304struct tree_mod_move {
305 int dst_slot;
306 int nr_items;
307};
308
309struct tree_mod_root {
310 u64 logical;
311 u8 level;
312};
313
314struct tree_mod_elem {
315 struct rb_node node;
316 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200317 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200318 enum mod_log_op op;
319
320 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
321 int slot;
322
323 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
324 u64 generation;
325
326 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
327 struct btrfs_disk_key key;
328 u64 blockptr;
329
330 /* this is used for op == MOD_LOG_MOVE_KEYS */
331 struct tree_mod_move move;
332
333 /* this is used for op == MOD_LOG_ROOT_REPLACE */
334 struct tree_mod_root old_root;
335};
336
Jan Schmidt097b8a72012-06-21 11:08:04 +0200337static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200338{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200339 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200340}
341
Jan Schmidt097b8a72012-06-21 11:08:04 +0200342static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200343{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200344 read_unlock(&fs_info->tree_mod_log_lock);
345}
346
347static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
348{
349 write_lock(&fs_info->tree_mod_log_lock);
350}
351
352static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
353{
354 write_unlock(&fs_info->tree_mod_log_lock);
355}
356
357/*
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000358 * Increment the upper half of tree_mod_seq, set lower half zero.
359 *
360 * Must be called with fs_info->tree_mod_seq_lock held.
361 */
362static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
363{
364 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
365 seq &= 0xffffffff00000000ull;
366 seq += 1ull << 32;
367 atomic64_set(&fs_info->tree_mod_seq, seq);
368 return seq;
369}
370
371/*
372 * Increment the lower half of tree_mod_seq.
373 *
374 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
375 * are generated should not technically require a spin lock here. (Rationale:
376 * incrementing the minor while incrementing the major seq number is between its
377 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
378 * just returns a unique sequence number as usual.) We have decided to leave
379 * that requirement in here and rethink it once we notice it really imposes a
380 * problem on some workload.
381 */
382static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
383{
384 return atomic64_inc_return(&fs_info->tree_mod_seq);
385}
386
387/*
388 * return the last minor in the previous major tree_mod_seq number
389 */
390u64 btrfs_tree_mod_seq_prev(u64 seq)
391{
392 return (seq & 0xffffffff00000000ull) - 1ull;
393}
394
395/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200396 * This adds a new blocker to the tree mod log's blocker list if the @elem
397 * passed does not already have a sequence number set. So when a caller expects
398 * to record tree modifications, it should ensure to set elem->seq to zero
399 * before calling btrfs_get_tree_mod_seq.
400 * Returns a fresh, unused tree log modification sequence number, even if no new
401 * blocker was added.
402 */
403u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
404 struct seq_list *elem)
405{
406 u64 seq;
407
408 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200410 if (!elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000411 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200412 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
413 }
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000414 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200415 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200416 tree_mod_log_write_unlock(fs_info);
417
418 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200419}
420
421void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
422 struct seq_list *elem)
423{
424 struct rb_root *tm_root;
425 struct rb_node *node;
426 struct rb_node *next;
427 struct seq_list *cur_elem;
428 struct tree_mod_elem *tm;
429 u64 min_seq = (u64)-1;
430 u64 seq_putting = elem->seq;
431
432 if (!seq_putting)
433 return;
434
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200435 spin_lock(&fs_info->tree_mod_seq_lock);
436 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200437 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200438
439 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200440 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200441 if (seq_putting > cur_elem->seq) {
442 /*
443 * blocker with lower sequence number exists, we
444 * cannot remove anything from the log
445 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200446 spin_unlock(&fs_info->tree_mod_seq_lock);
447 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200448 }
449 min_seq = cur_elem->seq;
450 }
451 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200452 spin_unlock(&fs_info->tree_mod_seq_lock);
453
454 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200455 * anything that's lower than the lowest existing (read: blocked)
456 * sequence number can be removed from the tree.
457 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200458 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200459 tm_root = &fs_info->tree_mod_log;
460 for (node = rb_first(tm_root); node; node = next) {
461 next = rb_next(node);
462 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200463 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200464 continue;
465 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200466 kfree(tm);
467 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200468 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200469}
470
471/*
472 * key order of the log:
473 * index -> sequence
474 *
475 * the index is the shifted logical of the *new* root node for root replace
476 * operations, or the shifted logical of the affected block for all other
477 * operations.
478 */
479static noinline int
480__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
481{
482 struct rb_root *tm_root;
483 struct rb_node **new;
484 struct rb_node *parent = NULL;
485 struct tree_mod_elem *cur;
Josef Bacikc8cc6342013-07-01 16:18:19 -0400486 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200487
Josef Bacikc8cc6342013-07-01 16:18:19 -0400488 BUG_ON(!tm);
489
490 tree_mod_log_write_lock(fs_info);
491 if (list_empty(&fs_info->tree_mod_seq_list)) {
492 tree_mod_log_write_unlock(fs_info);
493 /*
494 * Ok we no longer care about logging modifications, free up tm
495 * and return 0. Any callers shouldn't be using tm after
496 * calling tree_mod_log_insert, but if they do we can just
497 * change this to return a special error code to let the callers
498 * do their own thing.
499 */
500 kfree(tm);
501 return 0;
502 }
503
504 spin_lock(&fs_info->tree_mod_seq_lock);
505 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
506 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200507
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200508 tm_root = &fs_info->tree_mod_log;
509 new = &tm_root->rb_node;
510 while (*new) {
511 cur = container_of(*new, struct tree_mod_elem, node);
512 parent = *new;
513 if (cur->index < tm->index)
514 new = &((*new)->rb_left);
515 else if (cur->index > tm->index)
516 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200517 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200518 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200519 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200520 new = &((*new)->rb_right);
521 else {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400522 ret = -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200523 kfree(tm);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400524 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200525 }
526 }
527
528 rb_link_node(&tm->node, parent, new);
529 rb_insert_color(&tm->node, tm_root);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400530out:
531 tree_mod_log_write_unlock(fs_info);
532 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200533}
534
Jan Schmidt097b8a72012-06-21 11:08:04 +0200535/*
536 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
537 * returns zero with the tree_mod_log_lock acquired. The caller must hold
538 * this until all tree mod log insertions are recorded in the rb tree and then
539 * call tree_mod_log_write_unlock() to release.
540 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200541static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
542 struct extent_buffer *eb) {
543 smp_mb();
544 if (list_empty(&(fs_info)->tree_mod_seq_list))
545 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200546 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200547 return 1;
548 return 0;
549}
550
Jan Schmidt097b8a72012-06-21 11:08:04 +0200551static inline int
552__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
553 struct extent_buffer *eb, int slot,
554 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200555{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200556 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200557
Josef Bacikc8cc6342013-07-01 16:18:19 -0400558 tm = kzalloc(sizeof(*tm), flags);
559 if (!tm)
560 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200561
562 tm->index = eb->start >> PAGE_CACHE_SHIFT;
563 if (op != MOD_LOG_KEY_ADD) {
564 btrfs_node_key(eb, &tm->key, slot);
565 tm->blockptr = btrfs_node_blockptr(eb, slot);
566 }
567 tm->op = op;
568 tm->slot = slot;
569 tm->generation = btrfs_node_ptr_generation(eb, slot);
570
Jan Schmidt097b8a72012-06-21 11:08:04 +0200571 return __tree_mod_log_insert(fs_info, tm);
572}
573
574static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400575tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
576 struct extent_buffer *eb, int slot,
577 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200578{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200579 if (tree_mod_dont_log(fs_info, eb))
580 return 0;
581
Josef Bacikc8cc6342013-07-01 16:18:19 -0400582 return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200583}
584
585static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200586tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
587 struct extent_buffer *eb, int dst_slot, int src_slot,
588 int nr_items, gfp_t flags)
589{
590 struct tree_mod_elem *tm;
591 int ret;
592 int i;
593
Jan Schmidtf3956942012-05-31 15:02:32 +0200594 if (tree_mod_dont_log(fs_info, eb))
595 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200596
Jan Schmidt01763a22012-10-23 15:02:12 +0200597 /*
598 * When we override something during the move, we log these removals.
599 * This can only happen when we move towards the beginning of the
600 * buffer, i.e. dst_slot < src_slot.
601 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200602 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400603 ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
604 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200605 BUG_ON(ret < 0);
606 }
607
Josef Bacikc8cc6342013-07-01 16:18:19 -0400608 tm = kzalloc(sizeof(*tm), flags);
609 if (!tm)
610 return -ENOMEM;
Jan Schmidtf3956942012-05-31 15:02:32 +0200611
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200612 tm->index = eb->start >> PAGE_CACHE_SHIFT;
613 tm->slot = src_slot;
614 tm->move.dst_slot = dst_slot;
615 tm->move.nr_items = nr_items;
616 tm->op = MOD_LOG_MOVE_KEYS;
617
Josef Bacikc8cc6342013-07-01 16:18:19 -0400618 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200619}
620
Jan Schmidt097b8a72012-06-21 11:08:04 +0200621static inline void
622__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
623{
624 int i;
625 u32 nritems;
626 int ret;
627
Chris Masonb12a3b12012-08-07 15:34:49 -0400628 if (btrfs_header_level(eb) == 0)
629 return;
630
Jan Schmidt097b8a72012-06-21 11:08:04 +0200631 nritems = btrfs_header_nritems(eb);
632 for (i = nritems - 1; i >= 0; i--) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400633 ret = __tree_mod_log_insert_key(fs_info, eb, i,
634 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200635 BUG_ON(ret < 0);
636 }
637}
638
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200639static noinline int
640tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
641 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000642 struct extent_buffer *new_root, gfp_t flags,
643 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200644{
645 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200646
Jan Schmidt097b8a72012-06-21 11:08:04 +0200647 if (tree_mod_dont_log(fs_info, NULL))
648 return 0;
649
Jan Schmidt90f8d622013-04-13 13:19:53 +0000650 if (log_removal)
651 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000652
Josef Bacikc8cc6342013-07-01 16:18:19 -0400653 tm = kzalloc(sizeof(*tm), flags);
654 if (!tm)
655 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200656
657 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
658 tm->old_root.logical = old_root->start;
659 tm->old_root.level = btrfs_header_level(old_root);
660 tm->generation = btrfs_header_generation(old_root);
661 tm->op = MOD_LOG_ROOT_REPLACE;
662
Josef Bacikc8cc6342013-07-01 16:18:19 -0400663 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200664}
665
666static struct tree_mod_elem *
667__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
668 int smallest)
669{
670 struct rb_root *tm_root;
671 struct rb_node *node;
672 struct tree_mod_elem *cur = NULL;
673 struct tree_mod_elem *found = NULL;
674 u64 index = start >> PAGE_CACHE_SHIFT;
675
Jan Schmidt097b8a72012-06-21 11:08:04 +0200676 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200677 tm_root = &fs_info->tree_mod_log;
678 node = tm_root->rb_node;
679 while (node) {
680 cur = container_of(node, struct tree_mod_elem, node);
681 if (cur->index < index) {
682 node = node->rb_left;
683 } else if (cur->index > index) {
684 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200685 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200686 node = node->rb_left;
687 } else if (!smallest) {
688 /* we want the node with the highest seq */
689 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200690 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200691 found = cur;
692 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200693 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 /* we want the node with the smallest seq */
695 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200696 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697 found = cur;
698 node = node->rb_right;
699 } else {
700 found = cur;
701 break;
702 }
703 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200704 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200705
706 return found;
707}
708
709/*
710 * this returns the element from the log with the smallest time sequence
711 * value that's in the log (the oldest log item). any element with a time
712 * sequence lower than min_seq will be ignored.
713 */
714static struct tree_mod_elem *
715tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
716 u64 min_seq)
717{
718 return __tree_mod_log_search(fs_info, start, min_seq, 1);
719}
720
721/*
722 * this returns the element from the log with the largest time sequence
723 * value that's in the log (the most recent log item). any element with
724 * a time sequence lower than min_seq will be ignored.
725 */
726static struct tree_mod_elem *
727tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
728{
729 return __tree_mod_log_search(fs_info, start, min_seq, 0);
730}
731
Jan Schmidt097b8a72012-06-21 11:08:04 +0200732static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200733tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
734 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000735 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200736{
737 int ret;
738 int i;
739
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200740 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200741 return;
742
Josef Bacikc8cc6342013-07-01 16:18:19 -0400743 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200744 return;
745
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200746 for (i = 0; i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400747 ret = __tree_mod_log_insert_key(fs_info, src,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000748 i + src_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400749 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Jan Schmidt90f8d622013-04-13 13:19:53 +0000750 BUG_ON(ret < 0);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400751 ret = __tree_mod_log_insert_key(fs_info, dst,
Jan Schmidt097b8a72012-06-21 11:08:04 +0200752 i + dst_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400753 MOD_LOG_KEY_ADD,
754 GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200755 BUG_ON(ret < 0);
756 }
757}
758
759static inline void
760tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
761 int dst_offset, int src_offset, int nr_items)
762{
763 int ret;
764 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
765 nr_items, GFP_NOFS);
766 BUG_ON(ret < 0);
767}
768
Jan Schmidt097b8a72012-06-21 11:08:04 +0200769static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200770tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000771 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200772{
773 int ret;
774
Josef Bacikc8cc6342013-07-01 16:18:19 -0400775 ret = __tree_mod_log_insert_key(fs_info, eb, slot,
776 MOD_LOG_KEY_REPLACE,
777 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200778 BUG_ON(ret < 0);
779}
780
Jan Schmidt097b8a72012-06-21 11:08:04 +0200781static noinline void
782tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200783{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200784 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200785 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200786 __tree_mod_log_free_eb(fs_info, eb);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200787}
788
Jan Schmidt097b8a72012-06-21 11:08:04 +0200789static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200790tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000791 struct extent_buffer *new_root_node,
792 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200793{
794 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200795 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000796 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200797 BUG_ON(ret < 0);
798}
799
Chris Masond352ac62008-09-29 15:18:18 -0400800/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 * check if the tree block can be shared by multiple trees
802 */
803int btrfs_block_can_be_shared(struct btrfs_root *root,
804 struct extent_buffer *buf)
805{
806 /*
807 * Tree blocks not in refernece counted trees and tree roots
808 * are never shared. If a block was allocated after the last
809 * snapshot and the block was not allocated by tree relocation,
810 * we know the block is not shared.
811 */
812 if (root->ref_cows &&
813 buf != root->node && buf != root->commit_root &&
814 (btrfs_header_generation(buf) <=
815 btrfs_root_last_snapshot(&root->root_item) ||
816 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
817 return 1;
818#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
819 if (root->ref_cows &&
820 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
821 return 1;
822#endif
823 return 0;
824}
825
826static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400829 struct extent_buffer *cow,
830 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400831{
832 u64 refs;
833 u64 owner;
834 u64 flags;
835 u64 new_flags = 0;
836 int ret;
837
838 /*
839 * Backrefs update rules:
840 *
841 * Always use full backrefs for extent pointers in tree block
842 * allocated by tree relocation.
843 *
844 * If a shared tree block is no longer referenced by its owner
845 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
846 * use full backrefs for extent pointers in tree block.
847 *
848 * If a tree block is been relocating
849 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
850 * use full backrefs for extent pointers in tree block.
851 * The reason for this is some operations (such as drop tree)
852 * are only allowed for blocks use full backrefs.
853 */
854
855 if (btrfs_block_can_be_shared(root, buf)) {
856 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500857 btrfs_header_level(buf), 1,
858 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700859 if (ret)
860 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700861 if (refs == 0) {
862 ret = -EROFS;
863 btrfs_std_error(root->fs_info, ret);
864 return ret;
865 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 } else {
867 refs = 1;
868 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
869 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
870 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
871 else
872 flags = 0;
873 }
874
875 owner = btrfs_header_owner(buf);
876 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
877 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
878
879 if (refs > 1) {
880 if ((owner == root->root_key.objectid ||
881 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
882 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200883 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885
886 if (root->root_key.objectid ==
887 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200888 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200890 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892 }
893 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
894 } else {
895
896 if (root->root_key.objectid ==
897 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200898 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200900 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 }
903 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400904 int level = btrfs_header_level(buf);
905
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 ret = btrfs_set_disk_extent_flags(trans, root,
907 buf->start,
908 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400909 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700910 if (ret)
911 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 }
913 } else {
914 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
915 if (root->root_key.objectid ==
916 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200917 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200919 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100920 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200921 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 }
924 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400925 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 }
927 return 0;
928}
929
930/*
Chris Masond3977122009-01-05 21:25:51 -0500931 * does the dirty work in cow of a single block. The parent block (if
932 * supplied) is updated to point to the new cow copy. The new buffer is marked
933 * dirty and returned locked. If you modify the block it needs to be marked
934 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400935 *
936 * search_start -- an allocation hint for the new block
937 *
Chris Masond3977122009-01-05 21:25:51 -0500938 * empty_size -- a hint that you plan on doing more cow. This is the size in
939 * bytes the allocator should try to find free next to the block it returns.
940 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400941 */
Chris Masond3977122009-01-05 21:25:51 -0500942static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400943 struct btrfs_root *root,
944 struct extent_buffer *buf,
945 struct extent_buffer *parent, int parent_slot,
946 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400947 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400948{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400950 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700951 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400952 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400953 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400955
Chris Mason925baed2008-06-25 16:01:30 -0400956 if (*cow_ret == buf)
957 unlock_orig = 1;
958
Chris Masonb9447ef2009-03-09 11:45:38 -0400959 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400960
Chris Mason7bb86312007-12-11 09:25:06 -0500961 WARN_ON(root->ref_cows && trans->transid !=
962 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400963 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400964
Chris Mason7bb86312007-12-11 09:25:06 -0500965 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400966
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 if (level == 0)
968 btrfs_item_key(buf, &disk_key, 0);
969 else
970 btrfs_node_key(buf, &disk_key, 0);
971
972 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
973 if (parent)
974 parent_start = parent->start;
975 else
976 parent_start = 0;
977 } else
978 parent_start = 0;
979
980 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
981 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200982 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400983 if (IS_ERR(cow))
984 return PTR_ERR(cow);
985
Chris Masonb4ce94d2009-02-04 09:25:08 -0500986 /* cow is set to blocking by btrfs_init_new_buffer */
987
Chris Mason5f39d392007-10-15 16:14:19 -0400988 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400989 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400990 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
992 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
993 BTRFS_HEADER_FLAG_RELOC);
994 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
995 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
996 else
997 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400998
Ross Kirk0a4e5582013-09-24 10:12:38 +0100999 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001000 BTRFS_FSID_SIZE);
1001
Mark Fashehbe1a5562011-08-08 13:20:18 -07001002 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001003 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001005 return ret;
1006 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001007
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001008 if (root->ref_cows) {
1009 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1010 if (ret)
1011 return ret;
1012 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001013
Chris Mason6702ed42007-08-07 16:15:09 -04001014 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001015 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1017 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1018 parent_start = buf->start;
1019 else
1020 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001021
Chris Mason5f39d392007-10-15 16:14:19 -04001022 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001023 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001024 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001025
Yan, Zhengf0486c62010-05-16 10:46:25 -04001026 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001027 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001028 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001029 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001030 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1032 parent_start = parent->start;
1033 else
1034 parent_start = 0;
1035
1036 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001037 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001038 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001039 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001040 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001041 btrfs_set_node_ptr_generation(parent, parent_slot,
1042 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001043 btrfs_mark_buffer_dirty(parent);
Josef Bacik7fb7d76f2013-07-01 16:10:16 -04001044 if (last_ref)
1045 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001046 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001047 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001048 }
Chris Mason925baed2008-06-25 16:01:30 -04001049 if (unlock_orig)
1050 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001051 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001052 btrfs_mark_buffer_dirty(cow);
1053 *cow_ret = cow;
1054 return 0;
1055}
1056
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001057/*
1058 * returns the logical address of the oldest predecessor of the given root.
1059 * entries older than time_seq are ignored.
1060 */
1061static struct tree_mod_elem *
1062__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001063 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001064{
1065 struct tree_mod_elem *tm;
1066 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001067 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001068 int looped = 0;
1069
1070 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001071 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001072
1073 /*
1074 * the very last operation that's logged for a root is the replacement
1075 * operation (if it is replaced at all). this has the index of the *new*
1076 * root, making it the very first operation that's logged for this root.
1077 */
1078 while (1) {
1079 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1080 time_seq);
1081 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001082 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001083 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001084 * if there are no tree operation for the oldest root, we simply
1085 * return it. this should only happen if that (old) root is at
1086 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001087 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001088 if (!tm)
1089 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001090
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001091 /*
1092 * if there's an operation that's not a root replacement, we
1093 * found the oldest version of our root. normally, we'll find a
1094 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1095 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001096 if (tm->op != MOD_LOG_ROOT_REPLACE)
1097 break;
1098
1099 found = tm;
1100 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001101 looped = 1;
1102 }
1103
Jan Schmidta95236d2012-06-05 16:41:24 +02001104 /* if there's no old root to return, return what we found instead */
1105 if (!found)
1106 found = tm;
1107
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001108 return found;
1109}
1110
1111/*
1112 * tm is a pointer to the first operation to rewind within eb. then, all
1113 * previous operations will be rewinded (until we reach something older than
1114 * time_seq).
1115 */
1116static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001117__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1118 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001119{
1120 u32 n;
1121 struct rb_node *next;
1122 struct tree_mod_elem *tm = first_tm;
1123 unsigned long o_dst;
1124 unsigned long o_src;
1125 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1126
1127 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001128 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001129 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001130 /*
1131 * all the operations are recorded with the operator used for
1132 * the modification. as we're going backwards, we do the
1133 * opposite of each operation here.
1134 */
1135 switch (tm->op) {
1136 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1137 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001138 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001139 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001140 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001141 btrfs_set_node_key(eb, &tm->key, tm->slot);
1142 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1143 btrfs_set_node_ptr_generation(eb, tm->slot,
1144 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001145 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001146 break;
1147 case MOD_LOG_KEY_REPLACE:
1148 BUG_ON(tm->slot >= n);
1149 btrfs_set_node_key(eb, &tm->key, tm->slot);
1150 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1151 btrfs_set_node_ptr_generation(eb, tm->slot,
1152 tm->generation);
1153 break;
1154 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001155 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001156 n--;
1157 break;
1158 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001159 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1160 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1161 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001162 tm->move.nr_items * p_size);
1163 break;
1164 case MOD_LOG_ROOT_REPLACE:
1165 /*
1166 * this operation is special. for roots, this must be
1167 * handled explicitly before rewinding.
1168 * for non-roots, this operation may exist if the node
1169 * was a root: root A -> child B; then A gets empty and
1170 * B is promoted to the new root. in the mod log, we'll
1171 * have a root-replace operation for B, a tree block
1172 * that is no root. we simply ignore that operation.
1173 */
1174 break;
1175 }
1176 next = rb_next(&tm->node);
1177 if (!next)
1178 break;
1179 tm = container_of(next, struct tree_mod_elem, node);
1180 if (tm->index != first_tm->index)
1181 break;
1182 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001183 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001184 btrfs_set_header_nritems(eb, n);
1185}
1186
Jan Schmidt47fb0912013-04-13 13:19:55 +00001187/*
1188 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1189 * is returned. If rewind operations happen, a fresh buffer is returned. The
1190 * returned buffer is always read-locked. If the returned buffer is not the
1191 * input buffer, the lock on the input buffer is released and the input buffer
1192 * is freed (its refcount is decremented).
1193 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001194static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001195tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1196 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001197{
1198 struct extent_buffer *eb_rewin;
1199 struct tree_mod_elem *tm;
1200
1201 if (!time_seq)
1202 return eb;
1203
1204 if (btrfs_header_level(eb) == 0)
1205 return eb;
1206
1207 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1208 if (!tm)
1209 return eb;
1210
Josef Bacik9ec72672013-08-07 16:57:23 -04001211 btrfs_set_path_blocking(path);
1212 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1213
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001214 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1215 BUG_ON(tm->slot != 0);
1216 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1217 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001218 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001219 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001220 free_extent_buffer(eb);
1221 return NULL;
1222 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001223 btrfs_set_header_bytenr(eb_rewin, eb->start);
1224 btrfs_set_header_backref_rev(eb_rewin,
1225 btrfs_header_backref_rev(eb));
1226 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001227 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001228 } else {
1229 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001230 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001231 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001232 free_extent_buffer(eb);
1233 return NULL;
1234 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001235 }
1236
Josef Bacik9ec72672013-08-07 16:57:23 -04001237 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1238 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239 free_extent_buffer(eb);
1240
Jan Schmidt47fb0912013-04-13 13:19:55 +00001241 extent_buffer_get(eb_rewin);
1242 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001243 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001244 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001245 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001246
1247 return eb_rewin;
1248}
1249
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001250/*
1251 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1252 * value. If there are no changes, the current root->root_node is returned. If
1253 * anything changed in between, there's a fresh buffer allocated on which the
1254 * rewind operations are done. In any case, the returned buffer is read locked.
1255 * Returns NULL on error (with no locks held).
1256 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001257static inline struct extent_buffer *
1258get_old_root(struct btrfs_root *root, u64 time_seq)
1259{
1260 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001261 struct extent_buffer *eb = NULL;
1262 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001263 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001264 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001265 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001266 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001267 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268
Jan Schmidt30b04632013-04-13 13:19:54 +00001269 eb_root = btrfs_read_lock_root_node(root);
1270 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001271 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001272 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001273
Jan Schmidta95236d2012-06-05 16:41:24 +02001274 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1275 old_root = &tm->old_root;
1276 old_generation = tm->generation;
1277 logical = old_root->logical;
1278 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001279 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001280 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001281
Jan Schmidta95236d2012-06-05 16:41:24 +02001282 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001283 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001284 btrfs_tree_read_unlock(eb_root);
1285 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001286 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001287 old = read_tree_block(root, logical, blocksize, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301288 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001289 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001290 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1291 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001292 } 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",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001386 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001387 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001388
1389 if (trans->transid != root->fs_info->generation)
1390 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001391 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001392
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001394 *cow_ret = buf;
1395 return 0;
1396 }
Chris Masonc4876852009-02-04 09:24:25 -05001397
Chris Mason0b86a832008-03-24 15:01:56 -04001398 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001399
1400 if (parent)
1401 btrfs_set_lock_blocking(parent);
1402 btrfs_set_lock_blocking(buf);
1403
Chris Masonf510cfe2007-10-15 16:14:48 -04001404 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001405 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001406
1407 trace_btrfs_cow_block(root, buf, *cow_ret);
1408
Chris Masonf510cfe2007-10-15 16:14:48 -04001409 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001410}
1411
Chris Masond352ac62008-09-29 15:18:18 -04001412/*
1413 * helper function for defrag to decide if two blocks pointed to by a
1414 * node are actually close by
1415 */
Chris Mason6b800532007-10-15 16:17:34 -04001416static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001417{
Chris Mason6b800532007-10-15 16:17:34 -04001418 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001419 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001420 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001421 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001422 return 0;
1423}
1424
Chris Mason081e9572007-11-06 10:26:24 -05001425/*
1426 * compare two keys in a memcmp fashion
1427 */
1428static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1429{
1430 struct btrfs_key k1;
1431
1432 btrfs_disk_key_to_cpu(&k1, disk);
1433
Diego Calleja20736ab2009-07-24 11:06:52 -04001434 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001435}
1436
Josef Bacikf3465ca2008-11-12 14:19:50 -05001437/*
1438 * same as comp_keys only with two btrfs_key's
1439 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001440int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001441{
1442 if (k1->objectid > k2->objectid)
1443 return 1;
1444 if (k1->objectid < k2->objectid)
1445 return -1;
1446 if (k1->type > k2->type)
1447 return 1;
1448 if (k1->type < k2->type)
1449 return -1;
1450 if (k1->offset > k2->offset)
1451 return 1;
1452 if (k1->offset < k2->offset)
1453 return -1;
1454 return 0;
1455}
Chris Mason081e9572007-11-06 10:26:24 -05001456
Chris Masond352ac62008-09-29 15:18:18 -04001457/*
1458 * this is used by the defrag code to go through all the
1459 * leaves pointed to by a node and reallocate them so that
1460 * disk order is close to key order
1461 */
Chris Mason6702ed42007-08-07 16:15:09 -04001462int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001463 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001464 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001465 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001466{
Chris Mason6b800532007-10-15 16:17:34 -04001467 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001468 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001469 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001470 u64 search_start = *last_ret;
1471 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001472 u64 other;
1473 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001474 int end_slot;
1475 int i;
1476 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001477 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001478 int uptodate;
1479 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001480 int progress_passed = 0;
1481 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001482
Chris Mason5708b952007-10-25 15:43:18 -04001483 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001484
Julia Lawall6c1500f2012-11-03 20:30:18 +00001485 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1486 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001487
Chris Mason6b800532007-10-15 16:17:34 -04001488 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001489 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001490 end_slot = parent_nritems;
1491
1492 if (parent_nritems == 1)
1493 return 0;
1494
Chris Masonb4ce94d2009-02-04 09:25:08 -05001495 btrfs_set_lock_blocking(parent);
1496
Chris Mason6702ed42007-08-07 16:15:09 -04001497 for (i = start_slot; i < end_slot; i++) {
1498 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001499
Chris Mason081e9572007-11-06 10:26:24 -05001500 btrfs_node_key(parent, &disk_key, i);
1501 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1502 continue;
1503
1504 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001505 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001506 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001507 if (last_block == 0)
1508 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001509
Chris Mason6702ed42007-08-07 16:15:09 -04001510 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001511 other = btrfs_node_blockptr(parent, i - 1);
1512 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001513 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001514 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001515 other = btrfs_node_blockptr(parent, i + 1);
1516 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001517 }
Chris Masone9d0b132007-08-10 14:06:19 -04001518 if (close) {
1519 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001520 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001521 }
Chris Mason6702ed42007-08-07 16:15:09 -04001522
Chris Mason6b800532007-10-15 16:17:34 -04001523 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1524 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001525 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001526 else
1527 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001528 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001529 if (!cur) {
1530 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001531 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001532 if (!cur || !extent_buffer_uptodate(cur)) {
1533 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001534 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001535 }
Chris Mason6b800532007-10-15 16:17:34 -04001536 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001537 err = btrfs_read_buffer(cur, gen);
1538 if (err) {
1539 free_extent_buffer(cur);
1540 return err;
1541 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001542 }
Chris Mason6702ed42007-08-07 16:15:09 -04001543 }
Chris Masone9d0b132007-08-10 14:06:19 -04001544 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001545 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001546
Chris Masone7a84562008-06-25 16:01:31 -04001547 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001548 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001549 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001550 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001551 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001552 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001553 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001554 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001555 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001556 break;
Yan252c38f2007-08-29 09:11:44 -04001557 }
Chris Masone7a84562008-06-25 16:01:31 -04001558 search_start = cur->start;
1559 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001560 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001561 btrfs_tree_unlock(cur);
1562 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001563 }
1564 return err;
1565}
1566
Chris Mason74123bd2007-02-02 11:05:29 -05001567/*
1568 * The leaf data grows from end-to-front in the node.
1569 * this returns the address of the start of the last item,
1570 * which is the stop of the leaf data stack
1571 */
Chris Mason123abc82007-03-14 14:14:43 -04001572static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001573 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001574{
Chris Mason5f39d392007-10-15 16:14:19 -04001575 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001576 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001577 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001578 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001579}
1580
Chris Masonaa5d6be2007-02-28 16:35:06 -05001581
Chris Mason74123bd2007-02-02 11:05:29 -05001582/*
Chris Mason5f39d392007-10-15 16:14:19 -04001583 * search for key in the extent_buffer. The items start at offset p,
1584 * and they are item_size apart. There are 'max' items in p.
1585 *
Chris Mason74123bd2007-02-02 11:05:29 -05001586 * the slot in the array is returned via slot, and it points to
1587 * the place where you would insert key if it is not found in
1588 * the array.
1589 *
1590 * slot may point to max if the key is bigger than all of the keys
1591 */
Chris Masone02119d2008-09-05 16:13:11 -04001592static noinline int generic_bin_search(struct extent_buffer *eb,
1593 unsigned long p,
1594 int item_size, struct btrfs_key *key,
1595 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001596{
1597 int low = 0;
1598 int high = max;
1599 int mid;
1600 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001601 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001602 struct btrfs_disk_key unaligned;
1603 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001604 char *kaddr = NULL;
1605 unsigned long map_start = 0;
1606 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001607 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001608
Chris Masond3977122009-01-05 21:25:51 -05001609 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001610 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001611 offset = p + mid * item_size;
1612
Chris Masona6591712011-07-19 12:04:14 -04001613 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001614 (offset + sizeof(struct btrfs_disk_key)) >
1615 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001616
1617 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001618 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001619 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001620
Chris Mason479965d2007-10-15 16:14:27 -04001621 if (!err) {
1622 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1623 map_start);
1624 } else {
1625 read_extent_buffer(eb, &unaligned,
1626 offset, sizeof(unaligned));
1627 tmp = &unaligned;
1628 }
1629
Chris Mason5f39d392007-10-15 16:14:19 -04001630 } else {
1631 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1632 map_start);
1633 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001634 ret = comp_keys(tmp, key);
1635
1636 if (ret < 0)
1637 low = mid + 1;
1638 else if (ret > 0)
1639 high = mid;
1640 else {
1641 *slot = mid;
1642 return 0;
1643 }
1644 }
1645 *slot = low;
1646 return 1;
1647}
1648
Chris Mason97571fd2007-02-24 13:39:08 -05001649/*
1650 * simple bin_search frontend that does the right thing for
1651 * leaves vs nodes
1652 */
Chris Mason5f39d392007-10-15 16:14:19 -04001653static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1654 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001655{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001656 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001657 return generic_bin_search(eb,
1658 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001659 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001660 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001661 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001662 else
Chris Mason5f39d392007-10-15 16:14:19 -04001663 return generic_bin_search(eb,
1664 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001665 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001666 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001667 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001668}
1669
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001670int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1671 int level, int *slot)
1672{
1673 return bin_search(eb, key, level, slot);
1674}
1675
Yan, Zhengf0486c62010-05-16 10:46:25 -04001676static void root_add_used(struct btrfs_root *root, u32 size)
1677{
1678 spin_lock(&root->accounting_lock);
1679 btrfs_set_root_used(&root->root_item,
1680 btrfs_root_used(&root->root_item) + size);
1681 spin_unlock(&root->accounting_lock);
1682}
1683
1684static void root_sub_used(struct btrfs_root *root, u32 size)
1685{
1686 spin_lock(&root->accounting_lock);
1687 btrfs_set_root_used(&root->root_item,
1688 btrfs_root_used(&root->root_item) - size);
1689 spin_unlock(&root->accounting_lock);
1690}
1691
Chris Masond352ac62008-09-29 15:18:18 -04001692/* given a node and slot number, this reads the blocks it points to. The
1693 * extent buffer is returned with a reference taken (but unlocked).
1694 * NULL is returned on error.
1695 */
Chris Masone02119d2008-09-05 16:13:11 -04001696static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001697 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001698{
Chris Masonca7a79a2008-05-12 12:59:19 -04001699 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001700 struct extent_buffer *eb;
1701
Chris Masonbb803952007-03-01 12:04:21 -05001702 if (slot < 0)
1703 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001704 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001705 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001706
1707 BUG_ON(level == 0);
1708
Josef Bacik416bc652013-04-23 14:17:42 -04001709 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1710 btrfs_level_size(root, level - 1),
1711 btrfs_node_ptr_generation(parent, slot));
1712 if (eb && !extent_buffer_uptodate(eb)) {
1713 free_extent_buffer(eb);
1714 eb = NULL;
1715 }
1716
1717 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001718}
1719
Chris Masond352ac62008-09-29 15:18:18 -04001720/*
1721 * node level balancing, used to make sure nodes are in proper order for
1722 * item deletion. We balance from the top down, so we have to make sure
1723 * that a deletion won't leave an node completely empty later on.
1724 */
Chris Masone02119d2008-09-05 16:13:11 -04001725static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001726 struct btrfs_root *root,
1727 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001728{
Chris Mason5f39d392007-10-15 16:14:19 -04001729 struct extent_buffer *right = NULL;
1730 struct extent_buffer *mid;
1731 struct extent_buffer *left = NULL;
1732 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001733 int ret = 0;
1734 int wret;
1735 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001736 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001737 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001738
1739 if (level == 0)
1740 return 0;
1741
Chris Mason5f39d392007-10-15 16:14:19 -04001742 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001743
Chris Masonbd681512011-07-16 15:23:14 -04001744 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1745 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001746 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1747
Chris Mason1d4f8a02007-03-13 09:28:32 -04001748 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001749
Li Zefana05a9bb2011-09-06 16:55:34 +08001750 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001751 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001752 pslot = path->slots[level + 1];
1753 }
Chris Masonbb803952007-03-01 12:04:21 -05001754
Chris Mason40689472007-03-17 14:29:23 -04001755 /*
1756 * deal with the case where there is only one pointer in the root
1757 * by promoting the node below to a root
1758 */
Chris Mason5f39d392007-10-15 16:14:19 -04001759 if (!parent) {
1760 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001761
Chris Mason5f39d392007-10-15 16:14:19 -04001762 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001763 return 0;
1764
1765 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001766 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001767 if (!child) {
1768 ret = -EROFS;
1769 btrfs_std_error(root->fs_info, ret);
1770 goto enospc;
1771 }
1772
Chris Mason925baed2008-06-25 16:01:30 -04001773 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001774 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001775 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001776 if (ret) {
1777 btrfs_tree_unlock(child);
1778 free_extent_buffer(child);
1779 goto enospc;
1780 }
Yan2f375ab2008-02-01 14:58:07 -05001781
Jan Schmidt90f8d622013-04-13 13:19:53 +00001782 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001783 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001784
Chris Mason0b86a832008-03-24 15:01:56 -04001785 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001786 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001787
Chris Mason925baed2008-06-25 16:01:30 -04001788 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001789 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001790 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001791 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001792 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001793 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001794
1795 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001796 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001797 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001798 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001799 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001800 }
Chris Mason5f39d392007-10-15 16:14:19 -04001801 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001802 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001803 return 0;
1804
Chris Mason5f39d392007-10-15 16:14:19 -04001805 left = read_node_slot(root, parent, pslot - 1);
1806 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001807 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001808 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001809 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001810 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001811 if (wret) {
1812 ret = wret;
1813 goto enospc;
1814 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001815 }
Chris Mason5f39d392007-10-15 16:14:19 -04001816 right = read_node_slot(root, parent, pslot + 1);
1817 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001818 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001819 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001820 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001821 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001822 if (wret) {
1823 ret = wret;
1824 goto enospc;
1825 }
1826 }
1827
1828 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001829 if (left) {
1830 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001831 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001832 if (wret < 0)
1833 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001834 }
Chris Mason79f95c82007-03-01 15:16:26 -05001835
1836 /*
1837 * then try to empty the right most buffer into the middle
1838 */
Chris Mason5f39d392007-10-15 16:14:19 -04001839 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001840 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001841 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001842 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001843 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001844 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001845 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001846 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001847 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001848 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001849 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001850 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001851 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001852 struct btrfs_disk_key right_key;
1853 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001854 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001855 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001856 btrfs_set_node_key(parent, &right_key, pslot + 1);
1857 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001858 }
1859 }
Chris Mason5f39d392007-10-15 16:14:19 -04001860 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001861 /*
1862 * we're not allowed to leave a node with one item in the
1863 * tree during a delete. A deletion from lower in the tree
1864 * could try to delete the only pointer in this node.
1865 * So, pull some keys from the left.
1866 * There has to be a left pointer at this point because
1867 * otherwise we would have pulled some pointers from the
1868 * right
1869 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001870 if (!left) {
1871 ret = -EROFS;
1872 btrfs_std_error(root->fs_info, ret);
1873 goto enospc;
1874 }
Chris Mason5f39d392007-10-15 16:14:19 -04001875 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001876 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001877 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001878 goto enospc;
1879 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001880 if (wret == 1) {
1881 wret = push_node_left(trans, root, left, mid, 1);
1882 if (wret < 0)
1883 ret = wret;
1884 }
Chris Mason79f95c82007-03-01 15:16:26 -05001885 BUG_ON(wret == 1);
1886 }
Chris Mason5f39d392007-10-15 16:14:19 -04001887 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001888 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001889 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001890 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001891 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001892 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001893 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001894 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001895 } else {
1896 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001897 struct btrfs_disk_key mid_key;
1898 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001899 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001900 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001901 btrfs_set_node_key(parent, &mid_key, pslot);
1902 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001903 }
Chris Masonbb803952007-03-01 12:04:21 -05001904
Chris Mason79f95c82007-03-01 15:16:26 -05001905 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001906 if (left) {
1907 if (btrfs_header_nritems(left) > orig_slot) {
1908 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001909 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001910 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001911 path->slots[level + 1] -= 1;
1912 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001913 if (mid) {
1914 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001915 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001916 }
Chris Masonbb803952007-03-01 12:04:21 -05001917 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001918 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001919 path->slots[level] = orig_slot;
1920 }
1921 }
Chris Mason79f95c82007-03-01 15:16:26 -05001922 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001923 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001924 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001925 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001926enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001927 if (right) {
1928 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001929 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001930 }
1931 if (left) {
1932 if (path->nodes[level] != left)
1933 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001934 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001935 }
Chris Masonbb803952007-03-01 12:04:21 -05001936 return ret;
1937}
1938
Chris Masond352ac62008-09-29 15:18:18 -04001939/* Node balancing for insertion. Here we only split or push nodes around
1940 * when they are completely full. This is also done top down, so we
1941 * have to be pessimistic.
1942 */
Chris Masond3977122009-01-05 21:25:51 -05001943static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001944 struct btrfs_root *root,
1945 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001946{
Chris Mason5f39d392007-10-15 16:14:19 -04001947 struct extent_buffer *right = NULL;
1948 struct extent_buffer *mid;
1949 struct extent_buffer *left = NULL;
1950 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001951 int ret = 0;
1952 int wret;
1953 int pslot;
1954 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001955
1956 if (level == 0)
1957 return 1;
1958
Chris Mason5f39d392007-10-15 16:14:19 -04001959 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001960 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001961
Li Zefana05a9bb2011-09-06 16:55:34 +08001962 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001963 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001964 pslot = path->slots[level + 1];
1965 }
Chris Masone66f7092007-04-20 13:16:02 -04001966
Chris Mason5f39d392007-10-15 16:14:19 -04001967 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001968 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001969
Chris Mason5f39d392007-10-15 16:14:19 -04001970 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001971
1972 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001973 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001974 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001975
1976 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001977 btrfs_set_lock_blocking(left);
1978
Chris Mason5f39d392007-10-15 16:14:19 -04001979 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001980 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1981 wret = 1;
1982 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001983 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001984 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001985 if (ret)
1986 wret = 1;
1987 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001988 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001989 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001990 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001991 }
Chris Masone66f7092007-04-20 13:16:02 -04001992 if (wret < 0)
1993 ret = wret;
1994 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001995 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001996 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001997 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001998 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001999 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002000 btrfs_set_node_key(parent, &disk_key, pslot);
2001 btrfs_mark_buffer_dirty(parent);
2002 if (btrfs_header_nritems(left) > orig_slot) {
2003 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002004 path->slots[level + 1] -= 1;
2005 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002006 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002007 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002008 } else {
2009 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002010 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002011 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002012 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002013 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002014 }
Chris Masone66f7092007-04-20 13:16:02 -04002015 return 0;
2016 }
Chris Mason925baed2008-06-25 16:01:30 -04002017 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002018 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002019 }
Chris Mason925baed2008-06-25 16:01:30 -04002020 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002021
2022 /*
2023 * then try to empty the right most buffer into the middle
2024 */
Chris Mason5f39d392007-10-15 16:14:19 -04002025 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002026 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002027
Chris Mason925baed2008-06-25 16:01:30 -04002028 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002029 btrfs_set_lock_blocking(right);
2030
Chris Mason5f39d392007-10-15 16:14:19 -04002031 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002032 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2033 wret = 1;
2034 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002035 ret = btrfs_cow_block(trans, root, right,
2036 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002037 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002038 if (ret)
2039 wret = 1;
2040 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002041 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002042 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002043 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002044 }
Chris Masone66f7092007-04-20 13:16:02 -04002045 if (wret < 0)
2046 ret = wret;
2047 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002048 struct btrfs_disk_key disk_key;
2049
2050 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002051 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002052 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2054 btrfs_mark_buffer_dirty(parent);
2055
2056 if (btrfs_header_nritems(mid) <= orig_slot) {
2057 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002058 path->slots[level + 1] += 1;
2059 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002060 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002061 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002062 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002063 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002064 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002065 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002066 }
Chris Masone66f7092007-04-20 13:16:02 -04002067 return 0;
2068 }
Chris Mason925baed2008-06-25 16:01:30 -04002069 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002070 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002071 }
Chris Masone66f7092007-04-20 13:16:02 -04002072 return 1;
2073}
2074
Chris Mason74123bd2007-02-02 11:05:29 -05002075/*
Chris Masond352ac62008-09-29 15:18:18 -04002076 * readahead one full node of leaves, finding things that are close
2077 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002078 */
Chris Masonc8c42862009-04-03 10:14:18 -04002079static void reada_for_search(struct btrfs_root *root,
2080 struct btrfs_path *path,
2081 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002082{
Chris Mason5f39d392007-10-15 16:14:19 -04002083 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002084 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002085 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002086 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002087 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002088 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002089 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002090 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002091 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002092 u32 nr;
2093 u32 blocksize;
2094 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002095
Chris Masona6b6e752007-10-15 16:22:39 -04002096 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002097 return;
2098
Chris Mason6702ed42007-08-07 16:15:09 -04002099 if (!path->nodes[level])
2100 return;
2101
Chris Mason5f39d392007-10-15 16:14:19 -04002102 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002103
Chris Mason3c69fae2007-08-07 15:52:22 -04002104 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002105 blocksize = btrfs_level_size(root, level - 1);
2106 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002107 if (eb) {
2108 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002109 return;
2110 }
2111
Chris Masona7175312009-01-22 09:23:10 -05002112 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002113
Chris Mason5f39d392007-10-15 16:14:19 -04002114 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002115 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002116
Chris Masond3977122009-01-05 21:25:51 -05002117 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002118 if (direction < 0) {
2119 if (nr == 0)
2120 break;
2121 nr--;
2122 } else if (direction > 0) {
2123 nr++;
2124 if (nr >= nritems)
2125 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002126 }
Chris Mason01f46652007-12-21 16:24:26 -05002127 if (path->reada < 0 && objectid) {
2128 btrfs_node_key(node, &disk_key, nr);
2129 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2130 break;
2131 }
Chris Mason6b800532007-10-15 16:17:34 -04002132 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002133 if ((search <= target && target - search <= 65536) ||
2134 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002135 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002136 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002137 nread += blocksize;
2138 }
2139 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002140 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002141 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002142 }
2143}
Chris Mason925baed2008-06-25 16:01:30 -04002144
Josef Bacik0b088512013-06-17 14:23:02 -04002145static noinline void reada_for_balance(struct btrfs_root *root,
2146 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002147{
2148 int slot;
2149 int nritems;
2150 struct extent_buffer *parent;
2151 struct extent_buffer *eb;
2152 u64 gen;
2153 u64 block1 = 0;
2154 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002155 int blocksize;
2156
Chris Mason8c594ea2009-04-20 15:50:10 -04002157 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002158 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002159 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002160
2161 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002162 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002163 blocksize = btrfs_level_size(root, level);
2164
2165 if (slot > 0) {
2166 block1 = btrfs_node_blockptr(parent, slot - 1);
2167 gen = btrfs_node_ptr_generation(parent, slot - 1);
2168 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002169 /*
2170 * if we get -eagain from btrfs_buffer_uptodate, we
2171 * don't want to return eagain here. That will loop
2172 * forever
2173 */
2174 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002175 block1 = 0;
2176 free_extent_buffer(eb);
2177 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002178 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002179 block2 = btrfs_node_blockptr(parent, slot + 1);
2180 gen = btrfs_node_ptr_generation(parent, slot + 1);
2181 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002182 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002183 block2 = 0;
2184 free_extent_buffer(eb);
2185 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002186
Josef Bacik0b088512013-06-17 14:23:02 -04002187 if (block1)
2188 readahead_tree_block(root, block1, blocksize, 0);
2189 if (block2)
2190 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002191}
2192
2193
2194/*
Chris Masond3977122009-01-05 21:25:51 -05002195 * when we walk down the tree, it is usually safe to unlock the higher layers
2196 * in the tree. The exceptions are when our path goes through slot 0, because
2197 * operations on the tree might require changing key pointers higher up in the
2198 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002199 *
Chris Masond3977122009-01-05 21:25:51 -05002200 * callers might also have set path->keep_locks, which tells this code to keep
2201 * the lock if the path points to the last slot in the block. This is part of
2202 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002203 *
Chris Masond3977122009-01-05 21:25:51 -05002204 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2205 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002206 */
Chris Masone02119d2008-09-05 16:13:11 -04002207static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002208 int lowest_unlock, int min_write_lock_level,
2209 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002210{
2211 int i;
2212 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002213 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002214 struct extent_buffer *t;
2215
2216 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2217 if (!path->nodes[i])
2218 break;
2219 if (!path->locks[i])
2220 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002221 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002222 skip_level = i + 1;
2223 continue;
2224 }
Chris Mason051e1b92008-06-25 16:01:30 -04002225 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002226 u32 nritems;
2227 t = path->nodes[i];
2228 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002229 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002230 skip_level = i + 1;
2231 continue;
2232 }
2233 }
Chris Mason051e1b92008-06-25 16:01:30 -04002234 if (skip_level < i && i >= lowest_unlock)
2235 no_skips = 1;
2236
Chris Mason925baed2008-06-25 16:01:30 -04002237 t = path->nodes[i];
2238 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002239 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002240 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002241 if (write_lock_level &&
2242 i > min_write_lock_level &&
2243 i <= *write_lock_level) {
2244 *write_lock_level = i - 1;
2245 }
Chris Mason925baed2008-06-25 16:01:30 -04002246 }
2247 }
2248}
2249
Chris Mason3c69fae2007-08-07 15:52:22 -04002250/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002251 * This releases any locks held in the path starting at level and
2252 * going all the way up to the root.
2253 *
2254 * btrfs_search_slot will keep the lock held on higher nodes in a few
2255 * corner cases, such as COW of the block at slot zero in the node. This
2256 * ignores those rules, and it should only be called when there are no
2257 * more updates to be done higher up in the tree.
2258 */
2259noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2260{
2261 int i;
2262
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002263 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002264 return;
2265
2266 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2267 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002268 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002269 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002270 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002271 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002272 path->locks[i] = 0;
2273 }
2274}
2275
2276/*
Chris Masonc8c42862009-04-03 10:14:18 -04002277 * helper function for btrfs_search_slot. The goal is to find a block
2278 * in cache without setting the path to blocking. If we find the block
2279 * we return zero and the path is unchanged.
2280 *
2281 * If we can't find the block, we set the path blocking and do some
2282 * reada. -EAGAIN is returned and the search must be repeated.
2283 */
2284static int
2285read_block_for_search(struct btrfs_trans_handle *trans,
2286 struct btrfs_root *root, struct btrfs_path *p,
2287 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002288 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002289{
2290 u64 blocknr;
2291 u64 gen;
2292 u32 blocksize;
2293 struct extent_buffer *b = *eb_ret;
2294 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002295 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002296
2297 blocknr = btrfs_node_blockptr(b, slot);
2298 gen = btrfs_node_ptr_generation(b, slot);
2299 blocksize = btrfs_level_size(root, level - 1);
2300
2301 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002302 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002303 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002304 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2305 *eb_ret = tmp;
2306 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002307 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002308
2309 /* the pages were up to date, but we failed
2310 * the generation number check. Do a full
2311 * read for the generation number that is correct.
2312 * We must do this without dropping locks so
2313 * we can trust our generation number
2314 */
2315 btrfs_set_path_blocking(p);
2316
2317 /* now we're allowed to do a blocking uptodate check */
2318 ret = btrfs_read_buffer(tmp, gen);
2319 if (!ret) {
2320 *eb_ret = tmp;
2321 return 0;
2322 }
2323 free_extent_buffer(tmp);
2324 btrfs_release_path(p);
2325 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002326 }
2327
2328 /*
2329 * reduce lock contention at high levels
2330 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002331 * we read. Don't release the lock on the current
2332 * level because we need to walk this node to figure
2333 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002334 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002335 btrfs_unlock_up_safe(p, level + 1);
2336 btrfs_set_path_blocking(p);
2337
Chris Masoncb449212010-10-24 11:01:27 -04002338 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002339 if (p->reada)
2340 reada_for_search(root, p, level, slot, key->objectid);
2341
David Sterbab3b4aa72011-04-21 01:20:15 +02002342 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002343
2344 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002345 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002346 if (tmp) {
2347 /*
2348 * If the read above didn't mark this buffer up to date,
2349 * it will never end up being up to date. Set ret to EIO now
2350 * and give up so that our caller doesn't loop forever
2351 * on our EAGAINs.
2352 */
Chris Masonb9fab912012-05-06 07:23:47 -04002353 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002354 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002355 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002356 }
2357 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002358}
2359
2360/*
2361 * helper function for btrfs_search_slot. This does all of the checks
2362 * for node-level blocks and does any balancing required based on
2363 * the ins_len.
2364 *
2365 * If no extra work was required, zero is returned. If we had to
2366 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2367 * start over
2368 */
2369static int
2370setup_nodes_for_search(struct btrfs_trans_handle *trans,
2371 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002372 struct extent_buffer *b, int level, int ins_len,
2373 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002374{
2375 int ret;
2376 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2377 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2378 int sret;
2379
Chris Masonbd681512011-07-16 15:23:14 -04002380 if (*write_lock_level < level + 1) {
2381 *write_lock_level = level + 1;
2382 btrfs_release_path(p);
2383 goto again;
2384 }
2385
Chris Masonc8c42862009-04-03 10:14:18 -04002386 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002387 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002388 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002389 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002390
2391 BUG_ON(sret > 0);
2392 if (sret) {
2393 ret = sret;
2394 goto done;
2395 }
2396 b = p->nodes[level];
2397 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002398 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002399 int sret;
2400
Chris Masonbd681512011-07-16 15:23:14 -04002401 if (*write_lock_level < level + 1) {
2402 *write_lock_level = level + 1;
2403 btrfs_release_path(p);
2404 goto again;
2405 }
2406
Chris Masonc8c42862009-04-03 10:14:18 -04002407 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002408 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002409 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002410 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002411
2412 if (sret) {
2413 ret = sret;
2414 goto done;
2415 }
2416 b = p->nodes[level];
2417 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002418 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002419 goto again;
2420 }
2421 BUG_ON(btrfs_header_nritems(b) == 1);
2422 }
2423 return 0;
2424
2425again:
2426 ret = -EAGAIN;
2427done:
2428 return ret;
2429}
2430
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002431static void key_search_validate(struct extent_buffer *b,
2432 struct btrfs_key *key,
2433 int level)
2434{
2435#ifdef CONFIG_BTRFS_ASSERT
2436 struct btrfs_disk_key disk_key;
2437
2438 btrfs_cpu_key_to_disk(&disk_key, key);
2439
2440 if (level == 0)
2441 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2442 offsetof(struct btrfs_leaf, items[0].key),
2443 sizeof(disk_key)));
2444 else
2445 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2446 offsetof(struct btrfs_node, ptrs[0].key),
2447 sizeof(disk_key)));
2448#endif
2449}
2450
2451static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2452 int level, int *prev_cmp, int *slot)
2453{
2454 if (*prev_cmp != 0) {
2455 *prev_cmp = bin_search(b, key, level, slot);
2456 return *prev_cmp;
2457 }
2458
2459 key_search_validate(b, key, level);
2460 *slot = 0;
2461
2462 return 0;
2463}
2464
Chris Masonc8c42862009-04-03 10:14:18 -04002465/*
Chris Mason74123bd2007-02-02 11:05:29 -05002466 * look for key in the tree. path is filled in with nodes along the way
2467 * if key is found, we return zero and you can find the item in the leaf
2468 * level of the path (level 0)
2469 *
2470 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002471 * be inserted, and 1 is returned. If there are other errors during the
2472 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002473 *
2474 * if ins_len > 0, nodes and leaves will be split as we walk down the
2475 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2476 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002477 */
Chris Masone089f052007-03-16 16:20:31 -04002478int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2479 *root, struct btrfs_key *key, struct btrfs_path *p, int
2480 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002481{
Chris Mason5f39d392007-10-15 16:14:19 -04002482 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002483 int slot;
2484 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002485 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002486 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002487 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002488 int root_lock;
2489 /* everything at write_lock_level or lower must be write locked */
2490 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002491 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002492 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002493 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002494
Chris Mason6702ed42007-08-07 16:15:09 -04002495 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002496 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002497 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002498
Chris Masonbd681512011-07-16 15:23:14 -04002499 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002500 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002501
Chris Masonbd681512011-07-16 15:23:14 -04002502 /* when we are removing items, we might have to go up to level
2503 * two as we update tree pointers Make sure we keep write
2504 * for those levels as well
2505 */
2506 write_lock_level = 2;
2507 } else if (ins_len > 0) {
2508 /*
2509 * for inserting items, make sure we have a write lock on
2510 * level 1 so we can update keys
2511 */
2512 write_lock_level = 1;
2513 }
2514
2515 if (!cow)
2516 write_lock_level = -1;
2517
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002518 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002519 write_lock_level = BTRFS_MAX_LEVEL;
2520
Chris Masonf7c79f32012-03-19 15:54:38 -04002521 min_write_lock_level = write_lock_level;
2522
Chris Masonbb803952007-03-01 12:04:21 -05002523again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002524 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002525 /*
2526 * we try very hard to do read locks on the root
2527 */
2528 root_lock = BTRFS_READ_LOCK;
2529 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002531 /*
2532 * the commit roots are read only
2533 * so we always do read locks
2534 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002535 b = root->commit_root;
2536 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002537 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002538 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002539 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002541 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002542 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002543 level = btrfs_header_level(b);
2544 } else {
2545 /* we don't know the level of the root node
2546 * until we actually have it read locked
2547 */
2548 b = btrfs_read_lock_root_node(root);
2549 level = btrfs_header_level(b);
2550 if (level <= write_lock_level) {
2551 /* whoops, must trade for write lock */
2552 btrfs_tree_read_unlock(b);
2553 free_extent_buffer(b);
2554 b = btrfs_lock_root_node(root);
2555 root_lock = BTRFS_WRITE_LOCK;
2556
2557 /* the level might have changed, check again */
2558 level = btrfs_header_level(b);
2559 }
2560 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002561 }
Chris Masonbd681512011-07-16 15:23:14 -04002562 p->nodes[level] = b;
2563 if (!p->skip_locking)
2564 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002565
Chris Masoneb60cea2007-02-02 09:18:22 -05002566 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002567 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002568
2569 /*
2570 * setup the path here so we can release it under lock
2571 * contention with the cow code
2572 */
Chris Mason02217ed2007-03-02 16:08:05 -05002573 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002574 /*
2575 * if we don't really need to cow this block
2576 * then we don't want to set the path blocking,
2577 * so we test it here
2578 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002580 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002581
Chris Masonb4ce94d2009-02-04 09:25:08 -05002582 btrfs_set_path_blocking(p);
2583
Chris Masonbd681512011-07-16 15:23:14 -04002584 /*
2585 * must have write locks on this node and the
2586 * parent
2587 */
Josef Bacik5124e002012-11-07 13:44:13 -05002588 if (level > write_lock_level ||
2589 (level + 1 > write_lock_level &&
2590 level + 1 < BTRFS_MAX_LEVEL &&
2591 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002592 write_lock_level = level + 1;
2593 btrfs_release_path(p);
2594 goto again;
2595 }
2596
Yan Zheng33c66f42009-07-22 09:59:00 -04002597 err = btrfs_cow_block(trans, root, b,
2598 p->nodes[level + 1],
2599 p->slots[level + 1], &b);
2600 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002601 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002602 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002603 }
Chris Mason02217ed2007-03-02 16:08:05 -05002604 }
Chris Mason65b51a02008-08-01 15:11:20 -04002605cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002606 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002607
Chris Masoneb60cea2007-02-02 09:18:22 -05002608 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002609 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002610
2611 /*
2612 * we have a lock on b and as long as we aren't changing
2613 * the tree, there is no way to for the items in b to change.
2614 * It is safe to drop the lock on our parent before we
2615 * go through the expensive btree search on b.
2616 *
2617 * If cow is true, then we might be changing slot zero,
2618 * which may require changing the parent. So, we can't
2619 * drop the lock until after we know which slot we're
2620 * operating on.
2621 */
2622 if (!cow)
2623 btrfs_unlock_up_safe(p, level + 1);
2624
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002625 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002626
Chris Mason5f39d392007-10-15 16:14:19 -04002627 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002628 int dec = 0;
2629 if (ret && slot > 0) {
2630 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002631 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002632 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002633 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002634 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002635 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002636 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002637 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002638 if (err) {
2639 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002640 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002641 }
Chris Masonc8c42862009-04-03 10:14:18 -04002642 b = p->nodes[level];
2643 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002644
Chris Masonbd681512011-07-16 15:23:14 -04002645 /*
2646 * slot 0 is special, if we change the key
2647 * we have to update the parent pointer
2648 * which means we must have a write lock
2649 * on the parent
2650 */
2651 if (slot == 0 && cow &&
2652 write_lock_level < level + 1) {
2653 write_lock_level = level + 1;
2654 btrfs_release_path(p);
2655 goto again;
2656 }
2657
Chris Masonf7c79f32012-03-19 15:54:38 -04002658 unlock_up(p, level, lowest_unlock,
2659 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002660
Chris Mason925baed2008-06-25 16:01:30 -04002661 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002662 if (dec)
2663 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002664 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002665 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002666
Yan Zheng33c66f42009-07-22 09:59:00 -04002667 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002668 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002669 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002670 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002671 if (err) {
2672 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002673 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002674 }
Chris Mason76a05b32009-05-14 13:24:30 -04002675
Chris Masonb4ce94d2009-02-04 09:25:08 -05002676 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002677 level = btrfs_header_level(b);
2678 if (level <= write_lock_level) {
2679 err = btrfs_try_tree_write_lock(b);
2680 if (!err) {
2681 btrfs_set_path_blocking(p);
2682 btrfs_tree_lock(b);
2683 btrfs_clear_path_blocking(p, b,
2684 BTRFS_WRITE_LOCK);
2685 }
2686 p->locks[level] = BTRFS_WRITE_LOCK;
2687 } else {
2688 err = btrfs_try_tree_read_lock(b);
2689 if (!err) {
2690 btrfs_set_path_blocking(p);
2691 btrfs_tree_read_lock(b);
2692 btrfs_clear_path_blocking(p, b,
2693 BTRFS_READ_LOCK);
2694 }
2695 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002696 }
Chris Masonbd681512011-07-16 15:23:14 -04002697 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002698 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699 } else {
2700 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002701 if (ins_len > 0 &&
2702 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002703 if (write_lock_level < 1) {
2704 write_lock_level = 1;
2705 btrfs_release_path(p);
2706 goto again;
2707 }
2708
Chris Masonb4ce94d2009-02-04 09:25:08 -05002709 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002710 err = split_leaf(trans, root, key,
2711 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002712 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002713
Yan Zheng33c66f42009-07-22 09:59:00 -04002714 BUG_ON(err > 0);
2715 if (err) {
2716 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002717 goto done;
2718 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002719 }
Chris Mason459931e2008-12-10 09:10:46 -05002720 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002721 unlock_up(p, level, lowest_unlock,
2722 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002723 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002724 }
2725 }
Chris Mason65b51a02008-08-01 15:11:20 -04002726 ret = 1;
2727done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002728 /*
2729 * we don't really know what they plan on doing with the path
2730 * from here on, so for now just mark it as blocking
2731 */
Chris Masonb9473432009-03-13 11:00:37 -04002732 if (!p->leave_spinning)
2733 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002734 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002735 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002736 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002737}
2738
Chris Mason74123bd2007-02-02 11:05:29 -05002739/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002740 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2741 * current state of the tree together with the operations recorded in the tree
2742 * modification log to search for the key in a previous version of this tree, as
2743 * denoted by the time_seq parameter.
2744 *
2745 * Naturally, there is no support for insert, delete or cow operations.
2746 *
2747 * The resulting path and return value will be set up as if we called
2748 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2749 */
2750int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2751 struct btrfs_path *p, u64 time_seq)
2752{
2753 struct extent_buffer *b;
2754 int slot;
2755 int ret;
2756 int err;
2757 int level;
2758 int lowest_unlock = 1;
2759 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002760 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002761
2762 lowest_level = p->lowest_level;
2763 WARN_ON(p->nodes[0] != NULL);
2764
2765 if (p->search_commit_root) {
2766 BUG_ON(time_seq);
2767 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2768 }
2769
2770again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002771 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002772 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002773 p->locks[level] = BTRFS_READ_LOCK;
2774
2775 while (b) {
2776 level = btrfs_header_level(b);
2777 p->nodes[level] = b;
2778 btrfs_clear_path_blocking(p, NULL, 0);
2779
2780 /*
2781 * we have a lock on b and as long as we aren't changing
2782 * the tree, there is no way to for the items in b to change.
2783 * It is safe to drop the lock on our parent before we
2784 * go through the expensive btree search on b.
2785 */
2786 btrfs_unlock_up_safe(p, level + 1);
2787
Josef Bacikd4b40872013-09-24 14:09:34 -04002788 /*
2789 * Since we can unwind eb's we want to do a real search every
2790 * time.
2791 */
2792 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002793 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002794
2795 if (level != 0) {
2796 int dec = 0;
2797 if (ret && slot > 0) {
2798 dec = 1;
2799 slot -= 1;
2800 }
2801 p->slots[level] = slot;
2802 unlock_up(p, level, lowest_unlock, 0, NULL);
2803
2804 if (level == lowest_level) {
2805 if (dec)
2806 p->slots[level]++;
2807 goto done;
2808 }
2809
2810 err = read_block_for_search(NULL, root, p, &b, level,
2811 slot, key, time_seq);
2812 if (err == -EAGAIN)
2813 goto again;
2814 if (err) {
2815 ret = err;
2816 goto done;
2817 }
2818
2819 level = btrfs_header_level(b);
2820 err = btrfs_try_tree_read_lock(b);
2821 if (!err) {
2822 btrfs_set_path_blocking(p);
2823 btrfs_tree_read_lock(b);
2824 btrfs_clear_path_blocking(p, b,
2825 BTRFS_READ_LOCK);
2826 }
Josef Bacik9ec72672013-08-07 16:57:23 -04002827 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04002828 if (!b) {
2829 ret = -ENOMEM;
2830 goto done;
2831 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002832 p->locks[level] = BTRFS_READ_LOCK;
2833 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002834 } else {
2835 p->slots[level] = slot;
2836 unlock_up(p, level, lowest_unlock, 0, NULL);
2837 goto done;
2838 }
2839 }
2840 ret = 1;
2841done:
2842 if (!p->leave_spinning)
2843 btrfs_set_path_blocking(p);
2844 if (ret < 0)
2845 btrfs_release_path(p);
2846
2847 return ret;
2848}
2849
2850/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002851 * helper to use instead of search slot if no exact match is needed but
2852 * instead the next or previous item should be returned.
2853 * When find_higher is true, the next higher item is returned, the next lower
2854 * otherwise.
2855 * When return_any and find_higher are both true, and no higher item is found,
2856 * return the next lower instead.
2857 * When return_any is true and find_higher is false, and no lower item is found,
2858 * return the next higher instead.
2859 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2860 * < 0 on error
2861 */
2862int btrfs_search_slot_for_read(struct btrfs_root *root,
2863 struct btrfs_key *key, struct btrfs_path *p,
2864 int find_higher, int return_any)
2865{
2866 int ret;
2867 struct extent_buffer *leaf;
2868
2869again:
2870 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2871 if (ret <= 0)
2872 return ret;
2873 /*
2874 * a return value of 1 means the path is at the position where the
2875 * item should be inserted. Normally this is the next bigger item,
2876 * but in case the previous item is the last in a leaf, path points
2877 * to the first free slot in the previous leaf, i.e. at an invalid
2878 * item.
2879 */
2880 leaf = p->nodes[0];
2881
2882 if (find_higher) {
2883 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2884 ret = btrfs_next_leaf(root, p);
2885 if (ret <= 0)
2886 return ret;
2887 if (!return_any)
2888 return 1;
2889 /*
2890 * no higher item found, return the next
2891 * lower instead
2892 */
2893 return_any = 0;
2894 find_higher = 0;
2895 btrfs_release_path(p);
2896 goto again;
2897 }
2898 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002899 if (p->slots[0] == 0) {
2900 ret = btrfs_prev_leaf(root, p);
2901 if (ret < 0)
2902 return ret;
2903 if (!ret) {
2904 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2905 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002906 }
Arne Jansene6793762011-09-13 11:18:10 +02002907 if (!return_any)
2908 return 1;
2909 /*
2910 * no lower item found, return the next
2911 * higher instead
2912 */
2913 return_any = 0;
2914 find_higher = 1;
2915 btrfs_release_path(p);
2916 goto again;
2917 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002918 --p->slots[0];
2919 }
2920 }
2921 return 0;
2922}
2923
2924/*
Chris Mason74123bd2007-02-02 11:05:29 -05002925 * adjust the pointers going up the tree, starting at level
2926 * making sure the right key of each node is points to 'key'.
2927 * This is used after shifting pointers to the left, so it stops
2928 * fixing up pointers when a given leaf/node is not in slot 0 of the
2929 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002930 *
Chris Mason74123bd2007-02-02 11:05:29 -05002931 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002932static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002933 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002934{
2935 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002936 struct extent_buffer *t;
2937
Chris Mason234b63a2007-03-13 10:46:10 -04002938 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002939 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002940 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002941 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002942 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002943 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002944 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002945 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002946 if (tslot != 0)
2947 break;
2948 }
2949}
2950
Chris Mason74123bd2007-02-02 11:05:29 -05002951/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002952 * update item key.
2953 *
2954 * This function isn't completely safe. It's the caller's responsibility
2955 * that the new key won't break the order
2956 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002957void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002958 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002959{
2960 struct btrfs_disk_key disk_key;
2961 struct extent_buffer *eb;
2962 int slot;
2963
2964 eb = path->nodes[0];
2965 slot = path->slots[0];
2966 if (slot > 0) {
2967 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002968 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002969 }
2970 if (slot < btrfs_header_nritems(eb) - 1) {
2971 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002972 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002973 }
2974
2975 btrfs_cpu_key_to_disk(&disk_key, new_key);
2976 btrfs_set_item_key(eb, &disk_key, slot);
2977 btrfs_mark_buffer_dirty(eb);
2978 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002979 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002980}
2981
2982/*
Chris Mason74123bd2007-02-02 11:05:29 -05002983 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002984 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002985 *
2986 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2987 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002988 */
Chris Mason98ed5172008-01-03 10:01:48 -05002989static int push_node_left(struct btrfs_trans_handle *trans,
2990 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002991 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002992{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002993 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002994 int src_nritems;
2995 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002996 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002997
Chris Mason5f39d392007-10-15 16:14:19 -04002998 src_nritems = btrfs_header_nritems(src);
2999 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003000 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003001 WARN_ON(btrfs_header_generation(src) != trans->transid);
3002 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003003
Chris Masonbce4eae2008-04-24 14:42:46 -04003004 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003005 return 1;
3006
Chris Masond3977122009-01-05 21:25:51 -05003007 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003008 return 1;
3009
Chris Masonbce4eae2008-04-24 14:42:46 -04003010 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003011 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003012 if (push_items < src_nritems) {
3013 /* leave at least 8 pointers in the node if
3014 * we aren't going to empty it
3015 */
3016 if (src_nritems - push_items < 8) {
3017 if (push_items <= 8)
3018 return 1;
3019 push_items -= 8;
3020 }
3021 }
3022 } else
3023 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003024
Jan Schmidtf2304752012-05-26 11:43:17 +02003025 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003026 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003027 copy_extent_buffer(dst, src,
3028 btrfs_node_key_ptr_offset(dst_nritems),
3029 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003030 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003031
Chris Masonbb803952007-03-01 12:04:21 -05003032 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003033 /*
3034 * don't call tree_mod_log_eb_move here, key removal was already
3035 * fully logged by tree_mod_log_eb_copy above.
3036 */
Chris Mason5f39d392007-10-15 16:14:19 -04003037 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3038 btrfs_node_key_ptr_offset(push_items),
3039 (src_nritems - push_items) *
3040 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003041 }
Chris Mason5f39d392007-10-15 16:14:19 -04003042 btrfs_set_header_nritems(src, src_nritems - push_items);
3043 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3044 btrfs_mark_buffer_dirty(src);
3045 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003046
Chris Masonbb803952007-03-01 12:04:21 -05003047 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003048}
3049
Chris Mason97571fd2007-02-24 13:39:08 -05003050/*
Chris Mason79f95c82007-03-01 15:16:26 -05003051 * try to push data from one node into the next node right in the
3052 * tree.
3053 *
3054 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3055 * error, and > 0 if there was no room in the right hand block.
3056 *
3057 * this will only push up to 1/2 the contents of the left node over
3058 */
Chris Mason5f39d392007-10-15 16:14:19 -04003059static int balance_node_right(struct btrfs_trans_handle *trans,
3060 struct btrfs_root *root,
3061 struct extent_buffer *dst,
3062 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003063{
Chris Mason79f95c82007-03-01 15:16:26 -05003064 int push_items = 0;
3065 int max_push;
3066 int src_nritems;
3067 int dst_nritems;
3068 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003069
Chris Mason7bb86312007-12-11 09:25:06 -05003070 WARN_ON(btrfs_header_generation(src) != trans->transid);
3071 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3072
Chris Mason5f39d392007-10-15 16:14:19 -04003073 src_nritems = btrfs_header_nritems(src);
3074 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003075 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003076 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003077 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003078
Chris Masond3977122009-01-05 21:25:51 -05003079 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003080 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003081
3082 max_push = src_nritems / 2 + 1;
3083 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003084 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003085 return 1;
Yan252c38f2007-08-29 09:11:44 -04003086
Chris Mason79f95c82007-03-01 15:16:26 -05003087 if (max_push < push_items)
3088 push_items = max_push;
3089
Jan Schmidtf2304752012-05-26 11:43:17 +02003090 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003091 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3092 btrfs_node_key_ptr_offset(0),
3093 (dst_nritems) *
3094 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003095
Jan Schmidtf2304752012-05-26 11:43:17 +02003096 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003097 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003098 copy_extent_buffer(dst, src,
3099 btrfs_node_key_ptr_offset(0),
3100 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003101 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003102
Chris Mason5f39d392007-10-15 16:14:19 -04003103 btrfs_set_header_nritems(src, src_nritems - push_items);
3104 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003105
Chris Mason5f39d392007-10-15 16:14:19 -04003106 btrfs_mark_buffer_dirty(src);
3107 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003108
Chris Mason79f95c82007-03-01 15:16:26 -05003109 return ret;
3110}
3111
3112/*
Chris Mason97571fd2007-02-24 13:39:08 -05003113 * helper function to insert a new root level in the tree.
3114 * A new node is allocated, and a single item is inserted to
3115 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003116 *
3117 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003118 */
Chris Masond3977122009-01-05 21:25:51 -05003119static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003120 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003121 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003122{
Chris Mason7bb86312007-12-11 09:25:06 -05003123 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003124 struct extent_buffer *lower;
3125 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003126 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003127 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003128
3129 BUG_ON(path->nodes[level]);
3130 BUG_ON(path->nodes[level-1] != root->node);
3131
Chris Mason7bb86312007-12-11 09:25:06 -05003132 lower = path->nodes[level-1];
3133 if (level == 1)
3134 btrfs_item_key(lower, &lower_key, 0);
3135 else
3136 btrfs_node_key(lower, &lower_key, 0);
3137
Zheng Yan31840ae2008-09-23 13:14:14 -04003138 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003139 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003140 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003141 if (IS_ERR(c))
3142 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003143
Yan, Zhengf0486c62010-05-16 10:46:25 -04003144 root_add_used(root, root->nodesize);
3145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003146 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003147 btrfs_set_header_nritems(c, 1);
3148 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003149 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003150 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003151 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003152 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003153
Ross Kirk0a4e5582013-09-24 10:12:38 +01003154 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003155 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003156
3157 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003158 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003159
Chris Mason5f39d392007-10-15 16:14:19 -04003160 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003161 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003162 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003163 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003164
3165 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003166
3167 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003168
Chris Mason925baed2008-06-25 16:01:30 -04003169 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003170 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003171 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003172
3173 /* the super has an extra ref to root->node */
3174 free_extent_buffer(old);
3175
Chris Mason0b86a832008-03-24 15:01:56 -04003176 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003177 extent_buffer_get(c);
3178 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003179 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003180 path->slots[level] = 0;
3181 return 0;
3182}
3183
Chris Mason74123bd2007-02-02 11:05:29 -05003184/*
3185 * worker function to insert a single pointer in a node.
3186 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003187 *
Chris Mason74123bd2007-02-02 11:05:29 -05003188 * slot and level indicate where you want the key to go, and
3189 * blocknr is the block the key points to.
3190 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003191static void insert_ptr(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root, struct btrfs_path *path,
3193 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003194 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003195{
Chris Mason5f39d392007-10-15 16:14:19 -04003196 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003197 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003198 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003199
3200 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003201 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003202 lower = path->nodes[level];
3203 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003204 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003205 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003206 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003207 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003208 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3209 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003210 memmove_extent_buffer(lower,
3211 btrfs_node_key_ptr_offset(slot + 1),
3212 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003213 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003214 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003215 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003216 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003217 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003218 BUG_ON(ret < 0);
3219 }
Chris Mason5f39d392007-10-15 16:14:19 -04003220 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003221 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003222 WARN_ON(trans->transid == 0);
3223 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003224 btrfs_set_header_nritems(lower, nritems + 1);
3225 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003226}
3227
Chris Mason97571fd2007-02-24 13:39:08 -05003228/*
3229 * split the node at the specified level in path in two.
3230 * The path is corrected to point to the appropriate node after the split
3231 *
3232 * Before splitting this tries to make some room in the node by pushing
3233 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003234 *
3235 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003236 */
Chris Masone02119d2008-09-05 16:13:11 -04003237static noinline int split_node(struct btrfs_trans_handle *trans,
3238 struct btrfs_root *root,
3239 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003240{
Chris Mason5f39d392007-10-15 16:14:19 -04003241 struct extent_buffer *c;
3242 struct extent_buffer *split;
3243 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003244 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003245 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003246 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003247
Chris Mason5f39d392007-10-15 16:14:19 -04003248 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003249 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003250 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003251 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003252 * trying to split the root, lets make a new one
3253 *
Liu Bofdd99c72013-05-22 12:06:51 +00003254 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003255 * insert_new_root, because that root buffer will be kept as a
3256 * normal node. We are going to log removal of half of the
3257 * elements below with tree_mod_log_eb_copy. We're holding a
3258 * tree lock on the buffer, which is why we cannot race with
3259 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003260 */
Liu Bofdd99c72013-05-22 12:06:51 +00003261 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003262 if (ret)
3263 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003264 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003265 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003266 c = path->nodes[level];
3267 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003268 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003269 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003270 if (ret < 0)
3271 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003272 }
Chris Masone66f7092007-04-20 13:16:02 -04003273
Chris Mason5f39d392007-10-15 16:14:19 -04003274 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003275 mid = (c_nritems + 1) / 2;
3276 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003278 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003279 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003280 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003281 if (IS_ERR(split))
3282 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003283
Yan, Zhengf0486c62010-05-16 10:46:25 -04003284 root_add_used(root, root->nodesize);
3285
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003286 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003287 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003288 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003289 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003290 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003291 btrfs_set_header_owner(split, root->root_key.objectid);
3292 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003293 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003294 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003295 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003296 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003297
Jan Schmidt90f8d622013-04-13 13:19:53 +00003298 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003299 copy_extent_buffer(split, c,
3300 btrfs_node_key_ptr_offset(0),
3301 btrfs_node_key_ptr_offset(mid),
3302 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3303 btrfs_set_header_nritems(split, c_nritems - mid);
3304 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003305 ret = 0;
3306
Chris Mason5f39d392007-10-15 16:14:19 -04003307 btrfs_mark_buffer_dirty(c);
3308 btrfs_mark_buffer_dirty(split);
3309
Jeff Mahoney143bede2012-03-01 14:56:26 +01003310 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003311 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003312
Chris Mason5de08d72007-02-24 06:24:44 -05003313 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003314 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003315 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003316 free_extent_buffer(c);
3317 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003318 path->slots[level + 1] += 1;
3319 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003320 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003321 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003322 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003323 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003324}
3325
Chris Mason74123bd2007-02-02 11:05:29 -05003326/*
3327 * how many bytes are required to store the items in a leaf. start
3328 * and nr indicate which items in the leaf to check. This totals up the
3329 * space used both by the item structs and the item data
3330 */
Chris Mason5f39d392007-10-15 16:14:19 -04003331static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003332{
Josef Bacik41be1f32012-10-15 13:43:18 -04003333 struct btrfs_item *start_item;
3334 struct btrfs_item *end_item;
3335 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003336 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003337 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003338 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003339
3340 if (!nr)
3341 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003342 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003343 start_item = btrfs_item_nr(start);
3344 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003345 data_len = btrfs_token_item_offset(l, start_item, &token) +
3346 btrfs_token_item_size(l, start_item, &token);
3347 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003348 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003349 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003350 return data_len;
3351}
3352
Chris Mason74123bd2007-02-02 11:05:29 -05003353/*
Chris Masond4dbff92007-04-04 14:08:15 -04003354 * The space between the end of the leaf items and
3355 * the start of the leaf data. IOW, how much room
3356 * the leaf has left for both items and data
3357 */
Chris Masond3977122009-01-05 21:25:51 -05003358noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003359 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003360{
Chris Mason5f39d392007-10-15 16:14:19 -04003361 int nritems = btrfs_header_nritems(leaf);
3362 int ret;
3363 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3364 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003365 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3366 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003367 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003368 leaf_space_used(leaf, 0, nritems), nritems);
3369 }
3370 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003371}
3372
Chris Mason99d8f832010-07-07 10:51:48 -04003373/*
3374 * min slot controls the lowest index we're willing to push to the
3375 * right. We'll push up to and including min_slot, but no lower
3376 */
Chris Mason44871b12009-03-13 10:04:31 -04003377static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3378 struct btrfs_root *root,
3379 struct btrfs_path *path,
3380 int data_size, int empty,
3381 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003382 int free_space, u32 left_nritems,
3383 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003384{
Chris Mason5f39d392007-10-15 16:14:19 -04003385 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003386 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003387 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003388 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003389 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003390 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003391 int push_space = 0;
3392 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003393 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003394 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003395 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003396 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003397 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003398
Chris Masoncfed81a2012-03-03 07:40:03 -05003399 btrfs_init_map_token(&token);
3400
Chris Mason34a38212007-11-07 13:31:03 -05003401 if (empty)
3402 nr = 0;
3403 else
Chris Mason99d8f832010-07-07 10:51:48 -04003404 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003405
Zheng Yan31840ae2008-09-23 13:14:14 -04003406 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003407 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003408
Chris Mason44871b12009-03-13 10:04:31 -04003409 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003410 i = left_nritems - 1;
3411 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003412 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003413
Zheng Yan31840ae2008-09-23 13:14:14 -04003414 if (!empty && push_items > 0) {
3415 if (path->slots[0] > i)
3416 break;
3417 if (path->slots[0] == i) {
3418 int space = btrfs_leaf_free_space(root, left);
3419 if (space + push_space * 2 > free_space)
3420 break;
3421 }
3422 }
3423
Chris Mason00ec4c52007-02-24 12:47:20 -05003424 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003425 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003426
Chris Masondb945352007-10-15 16:15:53 -04003427 this_item_size = btrfs_item_size(left, item);
3428 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003429 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003430
Chris Mason00ec4c52007-02-24 12:47:20 -05003431 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003432 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003433 if (i == 0)
3434 break;
3435 i--;
Chris Masondb945352007-10-15 16:15:53 -04003436 }
Chris Mason5f39d392007-10-15 16:14:19 -04003437
Chris Mason925baed2008-06-25 16:01:30 -04003438 if (push_items == 0)
3439 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003440
Julia Lawall6c1500f2012-11-03 20:30:18 +00003441 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003442
Chris Mason00ec4c52007-02-24 12:47:20 -05003443 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003444 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003445
Chris Mason5f39d392007-10-15 16:14:19 -04003446 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003447 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003448
Chris Mason00ec4c52007-02-24 12:47:20 -05003449 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003450 data_end = leaf_data_end(root, right);
3451 memmove_extent_buffer(right,
3452 btrfs_leaf_data(right) + data_end - push_space,
3453 btrfs_leaf_data(right) + data_end,
3454 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3455
Chris Mason00ec4c52007-02-24 12:47:20 -05003456 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003457 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003458 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3459 btrfs_leaf_data(left) + leaf_data_end(root, left),
3460 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003461
3462 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3463 btrfs_item_nr_offset(0),
3464 right_nritems * sizeof(struct btrfs_item));
3465
Chris Mason00ec4c52007-02-24 12:47:20 -05003466 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003467 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3468 btrfs_item_nr_offset(left_nritems - push_items),
3469 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003470
3471 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003472 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003473 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003474 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003475 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003476 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003477 push_space -= btrfs_token_item_size(right, item, &token);
3478 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003479 }
3480
Chris Mason7518a232007-03-12 12:01:18 -04003481 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003483
Chris Mason34a38212007-11-07 13:31:03 -05003484 if (left_nritems)
3485 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003486 else
3487 clean_tree_block(trans, root, left);
3488
Chris Mason5f39d392007-10-15 16:14:19 -04003489 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003490
Chris Mason5f39d392007-10-15 16:14:19 -04003491 btrfs_item_key(right, &disk_key, 0);
3492 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003493 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003494
Chris Mason00ec4c52007-02-24 12:47:20 -05003495 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003496 if (path->slots[0] >= left_nritems) {
3497 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003498 if (btrfs_header_nritems(path->nodes[0]) == 0)
3499 clean_tree_block(trans, root, path->nodes[0]);
3500 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 free_extent_buffer(path->nodes[0]);
3502 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003503 path->slots[1] += 1;
3504 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003505 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003506 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003507 }
3508 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003509
3510out_unlock:
3511 btrfs_tree_unlock(right);
3512 free_extent_buffer(right);
3513 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003514}
Chris Mason925baed2008-06-25 16:01:30 -04003515
Chris Mason00ec4c52007-02-24 12:47:20 -05003516/*
Chris Mason44871b12009-03-13 10:04:31 -04003517 * push some data in the path leaf to the right, trying to free up at
3518 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3519 *
3520 * returns 1 if the push failed because the other node didn't have enough
3521 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003522 *
3523 * this will push starting from min_slot to the end of the leaf. It won't
3524 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003525 */
3526static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003527 *root, struct btrfs_path *path,
3528 int min_data_size, int data_size,
3529 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003530{
3531 struct extent_buffer *left = path->nodes[0];
3532 struct extent_buffer *right;
3533 struct extent_buffer *upper;
3534 int slot;
3535 int free_space;
3536 u32 left_nritems;
3537 int ret;
3538
3539 if (!path->nodes[1])
3540 return 1;
3541
3542 slot = path->slots[1];
3543 upper = path->nodes[1];
3544 if (slot >= btrfs_header_nritems(upper) - 1)
3545 return 1;
3546
3547 btrfs_assert_tree_locked(path->nodes[1]);
3548
3549 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003550 if (right == NULL)
3551 return 1;
3552
Chris Mason44871b12009-03-13 10:04:31 -04003553 btrfs_tree_lock(right);
3554 btrfs_set_lock_blocking(right);
3555
3556 free_space = btrfs_leaf_free_space(root, right);
3557 if (free_space < data_size)
3558 goto out_unlock;
3559
3560 /* cow and double check */
3561 ret = btrfs_cow_block(trans, root, right, upper,
3562 slot + 1, &right);
3563 if (ret)
3564 goto out_unlock;
3565
3566 free_space = btrfs_leaf_free_space(root, right);
3567 if (free_space < data_size)
3568 goto out_unlock;
3569
3570 left_nritems = btrfs_header_nritems(left);
3571 if (left_nritems == 0)
3572 goto out_unlock;
3573
Chris Mason99d8f832010-07-07 10:51:48 -04003574 return __push_leaf_right(trans, root, path, min_data_size, empty,
3575 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003576out_unlock:
3577 btrfs_tree_unlock(right);
3578 free_extent_buffer(right);
3579 return 1;
3580}
3581
3582/*
Chris Mason74123bd2007-02-02 11:05:29 -05003583 * push some data in the path leaf to the left, trying to free up at
3584 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003585 *
3586 * max_slot can put a limit on how far into the leaf we'll push items. The
3587 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3588 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003589 */
Chris Mason44871b12009-03-13 10:04:31 -04003590static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3591 struct btrfs_root *root,
3592 struct btrfs_path *path, int data_size,
3593 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003594 int free_space, u32 right_nritems,
3595 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003596{
Chris Mason5f39d392007-10-15 16:14:19 -04003597 struct btrfs_disk_key disk_key;
3598 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003599 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600 int push_space = 0;
3601 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003602 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003603 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003604 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003605 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003606 u32 this_item_size;
3607 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003608 struct btrfs_map_token token;
3609
3610 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003611
Chris Mason34a38212007-11-07 13:31:03 -05003612 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003613 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003614 else
Chris Mason99d8f832010-07-07 10:51:48 -04003615 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003616
3617 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003618 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003619
Zheng Yan31840ae2008-09-23 13:14:14 -04003620 if (!empty && push_items > 0) {
3621 if (path->slots[0] < i)
3622 break;
3623 if (path->slots[0] == i) {
3624 int space = btrfs_leaf_free_space(root, right);
3625 if (space + push_space * 2 > free_space)
3626 break;
3627 }
3628 }
3629
Chris Masonbe0e5c02007-01-26 15:51:26 -05003630 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003631 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003632
3633 this_item_size = btrfs_item_size(right, item);
3634 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003635 break;
Chris Masondb945352007-10-15 16:15:53 -04003636
Chris Masonbe0e5c02007-01-26 15:51:26 -05003637 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003638 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003639 }
Chris Masondb945352007-10-15 16:15:53 -04003640
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003642 ret = 1;
3643 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003644 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303645 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003646
Chris Masonbe0e5c02007-01-26 15:51:26 -05003647 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003648 copy_extent_buffer(left, right,
3649 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3650 btrfs_item_nr_offset(0),
3651 push_items * sizeof(struct btrfs_item));
3652
Chris Mason123abc82007-03-14 14:14:43 -04003653 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003654 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003655
3656 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003657 leaf_data_end(root, left) - push_space,
3658 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003659 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003660 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003661 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003662 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003663
Chris Masondb945352007-10-15 16:15:53 -04003664 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003665 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003666 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003667
Ross Kirkdd3cc162013-09-16 15:58:09 +01003668 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003669
Chris Masoncfed81a2012-03-03 07:40:03 -05003670 ioff = btrfs_token_item_offset(left, item, &token);
3671 btrfs_set_token_item_offset(left, item,
3672 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3673 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003674 }
Chris Mason5f39d392007-10-15 16:14:19 -04003675 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003676
3677 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003678 if (push_items > right_nritems)
3679 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003680 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003681
Chris Mason34a38212007-11-07 13:31:03 -05003682 if (push_items < right_nritems) {
3683 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3684 leaf_data_end(root, right);
3685 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3686 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3687 btrfs_leaf_data(right) +
3688 leaf_data_end(root, right), push_space);
3689
3690 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003691 btrfs_item_nr_offset(push_items),
3692 (btrfs_header_nritems(right) - push_items) *
3693 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003694 }
Yaneef1c492007-11-26 10:58:13 -05003695 right_nritems -= push_items;
3696 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003697 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003698 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003699 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003700
Chris Masoncfed81a2012-03-03 07:40:03 -05003701 push_space = push_space - btrfs_token_item_size(right,
3702 item, &token);
3703 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003704 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003705
Chris Mason5f39d392007-10-15 16:14:19 -04003706 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003707 if (right_nritems)
3708 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003709 else
3710 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003711
Chris Mason5f39d392007-10-15 16:14:19 -04003712 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003713 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003714
3715 /* then fixup the leaf pointer in the path */
3716 if (path->slots[0] < push_items) {
3717 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003718 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003719 free_extent_buffer(path->nodes[0]);
3720 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003721 path->slots[1] -= 1;
3722 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003723 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003724 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003725 path->slots[0] -= push_items;
3726 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003727 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003728 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003729out:
3730 btrfs_tree_unlock(left);
3731 free_extent_buffer(left);
3732 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003733}
3734
Chris Mason74123bd2007-02-02 11:05:29 -05003735/*
Chris Mason44871b12009-03-13 10:04:31 -04003736 * push some data in the path leaf to the left, trying to free up at
3737 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003738 *
3739 * max_slot can put a limit on how far into the leaf we'll push items. The
3740 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3741 * items
Chris Mason44871b12009-03-13 10:04:31 -04003742 */
3743static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003744 *root, struct btrfs_path *path, int min_data_size,
3745 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003746{
3747 struct extent_buffer *right = path->nodes[0];
3748 struct extent_buffer *left;
3749 int slot;
3750 int free_space;
3751 u32 right_nritems;
3752 int ret = 0;
3753
3754 slot = path->slots[1];
3755 if (slot == 0)
3756 return 1;
3757 if (!path->nodes[1])
3758 return 1;
3759
3760 right_nritems = btrfs_header_nritems(right);
3761 if (right_nritems == 0)
3762 return 1;
3763
3764 btrfs_assert_tree_locked(path->nodes[1]);
3765
3766 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003767 if (left == NULL)
3768 return 1;
3769
Chris Mason44871b12009-03-13 10:04:31 -04003770 btrfs_tree_lock(left);
3771 btrfs_set_lock_blocking(left);
3772
3773 free_space = btrfs_leaf_free_space(root, left);
3774 if (free_space < data_size) {
3775 ret = 1;
3776 goto out;
3777 }
3778
3779 /* cow and double check */
3780 ret = btrfs_cow_block(trans, root, left,
3781 path->nodes[1], slot - 1, &left);
3782 if (ret) {
3783 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003784 if (ret == -ENOSPC)
3785 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003786 goto out;
3787 }
3788
3789 free_space = btrfs_leaf_free_space(root, left);
3790 if (free_space < data_size) {
3791 ret = 1;
3792 goto out;
3793 }
3794
Chris Mason99d8f832010-07-07 10:51:48 -04003795 return __push_leaf_left(trans, root, path, min_data_size,
3796 empty, left, free_space, right_nritems,
3797 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003798out:
3799 btrfs_tree_unlock(left);
3800 free_extent_buffer(left);
3801 return ret;
3802}
3803
3804/*
Chris Mason74123bd2007-02-02 11:05:29 -05003805 * split the path's leaf in two, making sure there is at least data_size
3806 * available for the resulting leaf level of the path.
3807 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003808static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3809 struct btrfs_root *root,
3810 struct btrfs_path *path,
3811 struct extent_buffer *l,
3812 struct extent_buffer *right,
3813 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003815 int data_copy_size;
3816 int rt_data_off;
3817 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003818 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003819 struct btrfs_map_token token;
3820
3821 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003822
Chris Mason5f39d392007-10-15 16:14:19 -04003823 nritems = nritems - mid;
3824 btrfs_set_header_nritems(right, nritems);
3825 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3826
3827 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3828 btrfs_item_nr_offset(mid),
3829 nritems * sizeof(struct btrfs_item));
3830
3831 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003832 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3833 data_copy_size, btrfs_leaf_data(l) +
3834 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003835
Chris Mason5f39d392007-10-15 16:14:19 -04003836 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3837 btrfs_item_end_nr(l, mid);
3838
3839 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003840 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003841 u32 ioff;
3842
Chris Masoncfed81a2012-03-03 07:40:03 -05003843 ioff = btrfs_token_item_offset(right, item, &token);
3844 btrfs_set_token_item_offset(right, item,
3845 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003846 }
Chris Mason74123bd2007-02-02 11:05:29 -05003847
Chris Mason5f39d392007-10-15 16:14:19 -04003848 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003849 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003850 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003851 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003852
3853 btrfs_mark_buffer_dirty(right);
3854 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003855 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003856
Chris Masonbe0e5c02007-01-26 15:51:26 -05003857 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003858 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003859 free_extent_buffer(path->nodes[0]);
3860 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003861 path->slots[0] -= mid;
3862 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003863 } else {
3864 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003865 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003866 }
Chris Mason5f39d392007-10-15 16:14:19 -04003867
Chris Masoneb60cea2007-02-02 09:18:22 -05003868 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003869}
3870
3871/*
Chris Mason99d8f832010-07-07 10:51:48 -04003872 * double splits happen when we need to insert a big item in the middle
3873 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3874 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3875 * A B C
3876 *
3877 * We avoid this by trying to push the items on either side of our target
3878 * into the adjacent leaves. If all goes well we can avoid the double split
3879 * completely.
3880 */
3881static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3882 struct btrfs_root *root,
3883 struct btrfs_path *path,
3884 int data_size)
3885{
3886 int ret;
3887 int progress = 0;
3888 int slot;
3889 u32 nritems;
3890
3891 slot = path->slots[0];
3892
3893 /*
3894 * try to push all the items after our slot into the
3895 * right leaf
3896 */
3897 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3898 if (ret < 0)
3899 return ret;
3900
3901 if (ret == 0)
3902 progress++;
3903
3904 nritems = btrfs_header_nritems(path->nodes[0]);
3905 /*
3906 * our goal is to get our slot at the start or end of a leaf. If
3907 * we've done so we're done
3908 */
3909 if (path->slots[0] == 0 || path->slots[0] == nritems)
3910 return 0;
3911
3912 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3913 return 0;
3914
3915 /* try to push all the items before our slot into the next leaf */
3916 slot = path->slots[0];
3917 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3918 if (ret < 0)
3919 return ret;
3920
3921 if (ret == 0)
3922 progress++;
3923
3924 if (progress)
3925 return 0;
3926 return 1;
3927}
3928
3929/*
Chris Mason44871b12009-03-13 10:04:31 -04003930 * split the path's leaf in two, making sure there is at least data_size
3931 * available for the resulting leaf level of the path.
3932 *
3933 * returns 0 if all went well and < 0 on failure.
3934 */
3935static noinline int split_leaf(struct btrfs_trans_handle *trans,
3936 struct btrfs_root *root,
3937 struct btrfs_key *ins_key,
3938 struct btrfs_path *path, int data_size,
3939 int extend)
3940{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003941 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003942 struct extent_buffer *l;
3943 u32 nritems;
3944 int mid;
3945 int slot;
3946 struct extent_buffer *right;
3947 int ret = 0;
3948 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003949 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003950 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003951 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003952
Yan, Zhenga5719522009-09-24 09:17:31 -04003953 l = path->nodes[0];
3954 slot = path->slots[0];
3955 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3956 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3957 return -EOVERFLOW;
3958
Chris Mason44871b12009-03-13 10:04:31 -04003959 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003960 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003961 wret = push_leaf_right(trans, root, path, data_size,
3962 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003963 if (wret < 0)
3964 return wret;
3965 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003966 wret = push_leaf_left(trans, root, path, data_size,
3967 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003968 if (wret < 0)
3969 return wret;
3970 }
3971 l = path->nodes[0];
3972
3973 /* did the pushes work? */
3974 if (btrfs_leaf_free_space(root, l) >= data_size)
3975 return 0;
3976 }
3977
3978 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003979 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003980 if (ret)
3981 return ret;
3982 }
3983again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003984 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003985 l = path->nodes[0];
3986 slot = path->slots[0];
3987 nritems = btrfs_header_nritems(l);
3988 mid = (nritems + 1) / 2;
3989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003990 if (mid <= slot) {
3991 if (nritems == 1 ||
3992 leaf_space_used(l, mid, nritems - mid) + data_size >
3993 BTRFS_LEAF_DATA_SIZE(root)) {
3994 if (slot >= nritems) {
3995 split = 0;
3996 } else {
3997 mid = slot;
3998 if (mid != nritems &&
3999 leaf_space_used(l, mid, nritems - mid) +
4000 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004001 if (data_size && !tried_avoid_double)
4002 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004003 split = 2;
4004 }
4005 }
4006 }
4007 } else {
4008 if (leaf_space_used(l, 0, mid) + data_size >
4009 BTRFS_LEAF_DATA_SIZE(root)) {
4010 if (!extend && data_size && slot == 0) {
4011 split = 0;
4012 } else if ((extend || !data_size) && slot == 0) {
4013 mid = 1;
4014 } else {
4015 mid = slot;
4016 if (mid != nritems &&
4017 leaf_space_used(l, mid, nritems - mid) +
4018 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004019 if (data_size && !tried_avoid_double)
4020 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304021 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004022 }
4023 }
4024 }
4025 }
4026
4027 if (split == 0)
4028 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4029 else
4030 btrfs_item_key(l, &disk_key, mid);
4031
4032 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004033 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004034 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004035 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004036 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004037
4038 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004039
4040 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4041 btrfs_set_header_bytenr(right, right->start);
4042 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004044 btrfs_set_header_owner(right, root->root_key.objectid);
4045 btrfs_set_header_level(right, 0);
4046 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004047 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004048
4049 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004050 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004051 BTRFS_UUID_SIZE);
4052
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053 if (split == 0) {
4054 if (mid <= slot) {
4055 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004056 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004057 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004058 btrfs_tree_unlock(path->nodes[0]);
4059 free_extent_buffer(path->nodes[0]);
4060 path->nodes[0] = right;
4061 path->slots[0] = 0;
4062 path->slots[1] += 1;
4063 } else {
4064 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004065 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004066 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004067 btrfs_tree_unlock(path->nodes[0]);
4068 free_extent_buffer(path->nodes[0]);
4069 path->nodes[0] = right;
4070 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004071 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004072 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004073 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004074 btrfs_mark_buffer_dirty(right);
4075 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004076 }
4077
Jeff Mahoney143bede2012-03-01 14:56:26 +01004078 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004079
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004080 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004081 BUG_ON(num_doubles != 0);
4082 num_doubles++;
4083 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004084 }
Chris Mason44871b12009-03-13 10:04:31 -04004085
Jeff Mahoney143bede2012-03-01 14:56:26 +01004086 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004087
4088push_for_double:
4089 push_for_double_split(trans, root, path, data_size);
4090 tried_avoid_double = 1;
4091 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4092 return 0;
4093 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004094}
4095
Yan, Zhengad48fd752009-11-12 09:33:58 +00004096static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4097 struct btrfs_root *root,
4098 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004099{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004100 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004101 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004102 struct btrfs_file_extent_item *fi;
4103 u64 extent_len = 0;
4104 u32 item_size;
4105 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004106
4107 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004108 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4109
4110 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4111 key.type != BTRFS_EXTENT_CSUM_KEY);
4112
4113 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4114 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004115
4116 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004117 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4118 fi = btrfs_item_ptr(leaf, path->slots[0],
4119 struct btrfs_file_extent_item);
4120 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4121 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004122 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004123
Chris Mason459931e2008-12-10 09:10:46 -05004124 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004125 path->search_for_split = 1;
4126 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004127 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004128 if (ret < 0)
4129 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004130
Yan, Zhengad48fd752009-11-12 09:33:58 +00004131 ret = -EAGAIN;
4132 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004133 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004134 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4135 goto err;
4136
Chris Mason109f6ae2010-04-02 09:20:18 -04004137 /* the leaf has changed, it now has room. return now */
4138 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4139 goto err;
4140
Yan, Zhengad48fd752009-11-12 09:33:58 +00004141 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4142 fi = btrfs_item_ptr(leaf, path->slots[0],
4143 struct btrfs_file_extent_item);
4144 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4145 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004146 }
4147
Chris Masonb9473432009-03-13 11:00:37 -04004148 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004149 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004150 if (ret)
4151 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004152
Yan, Zhengad48fd752009-11-12 09:33:58 +00004153 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004154 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004155 return 0;
4156err:
4157 path->keep_locks = 0;
4158 return ret;
4159}
4160
4161static noinline int split_item(struct btrfs_trans_handle *trans,
4162 struct btrfs_root *root,
4163 struct btrfs_path *path,
4164 struct btrfs_key *new_key,
4165 unsigned long split_offset)
4166{
4167 struct extent_buffer *leaf;
4168 struct btrfs_item *item;
4169 struct btrfs_item *new_item;
4170 int slot;
4171 char *buf;
4172 u32 nritems;
4173 u32 item_size;
4174 u32 orig_offset;
4175 struct btrfs_disk_key disk_key;
4176
Chris Masonb9473432009-03-13 11:00:37 -04004177 leaf = path->nodes[0];
4178 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4179
Chris Masonb4ce94d2009-02-04 09:25:08 -05004180 btrfs_set_path_blocking(path);
4181
Ross Kirkdd3cc162013-09-16 15:58:09 +01004182 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004183 orig_offset = btrfs_item_offset(leaf, item);
4184 item_size = btrfs_item_size(leaf, item);
4185
Chris Mason459931e2008-12-10 09:10:46 -05004186 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004187 if (!buf)
4188 return -ENOMEM;
4189
Chris Mason459931e2008-12-10 09:10:46 -05004190 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4191 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004192
Chris Mason459931e2008-12-10 09:10:46 -05004193 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004194 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004195 if (slot != nritems) {
4196 /* shift the items */
4197 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004198 btrfs_item_nr_offset(slot),
4199 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004200 }
4201
4202 btrfs_cpu_key_to_disk(&disk_key, new_key);
4203 btrfs_set_item_key(leaf, &disk_key, slot);
4204
Ross Kirkdd3cc162013-09-16 15:58:09 +01004205 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004206
4207 btrfs_set_item_offset(leaf, new_item, orig_offset);
4208 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4209
4210 btrfs_set_item_offset(leaf, item,
4211 orig_offset + item_size - split_offset);
4212 btrfs_set_item_size(leaf, item, split_offset);
4213
4214 btrfs_set_header_nritems(leaf, nritems + 1);
4215
4216 /* write the data for the start of the original item */
4217 write_extent_buffer(leaf, buf,
4218 btrfs_item_ptr_offset(leaf, path->slots[0]),
4219 split_offset);
4220
4221 /* write the data for the new item */
4222 write_extent_buffer(leaf, buf + split_offset,
4223 btrfs_item_ptr_offset(leaf, slot),
4224 item_size - split_offset);
4225 btrfs_mark_buffer_dirty(leaf);
4226
Yan, Zhengad48fd752009-11-12 09:33:58 +00004227 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004228 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004229 return 0;
4230}
4231
4232/*
4233 * This function splits a single item into two items,
4234 * giving 'new_key' to the new item and splitting the
4235 * old one at split_offset (from the start of the item).
4236 *
4237 * The path may be released by this operation. After
4238 * the split, the path is pointing to the old item. The
4239 * new item is going to be in the same node as the old one.
4240 *
4241 * Note, the item being split must be smaller enough to live alone on
4242 * a tree block with room for one extra struct btrfs_item
4243 *
4244 * This allows us to split the item in place, keeping a lock on the
4245 * leaf the entire time.
4246 */
4247int btrfs_split_item(struct btrfs_trans_handle *trans,
4248 struct btrfs_root *root,
4249 struct btrfs_path *path,
4250 struct btrfs_key *new_key,
4251 unsigned long split_offset)
4252{
4253 int ret;
4254 ret = setup_leaf_for_split(trans, root, path,
4255 sizeof(struct btrfs_item));
4256 if (ret)
4257 return ret;
4258
4259 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004260 return ret;
4261}
4262
4263/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004264 * This function duplicate a item, giving 'new_key' to the new item.
4265 * It guarantees both items live in the same tree leaf and the new item
4266 * is contiguous with the original item.
4267 *
4268 * This allows us to split file extent in place, keeping a lock on the
4269 * leaf the entire time.
4270 */
4271int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4272 struct btrfs_root *root,
4273 struct btrfs_path *path,
4274 struct btrfs_key *new_key)
4275{
4276 struct extent_buffer *leaf;
4277 int ret;
4278 u32 item_size;
4279
4280 leaf = path->nodes[0];
4281 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4282 ret = setup_leaf_for_split(trans, root, path,
4283 item_size + sizeof(struct btrfs_item));
4284 if (ret)
4285 return ret;
4286
4287 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004288 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004289 item_size, item_size +
4290 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004291 leaf = path->nodes[0];
4292 memcpy_extent_buffer(leaf,
4293 btrfs_item_ptr_offset(leaf, path->slots[0]),
4294 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4295 item_size);
4296 return 0;
4297}
4298
4299/*
Chris Masond352ac62008-09-29 15:18:18 -04004300 * make the item pointed to by the path smaller. new_size indicates
4301 * how small to make it, and from_end tells us if we just chop bytes
4302 * off the end of the item or if we shift the item to chop bytes off
4303 * the front.
4304 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004305void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004306 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004307{
Chris Masonb18c6682007-04-17 13:26:50 -04004308 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004309 struct extent_buffer *leaf;
4310 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004311 u32 nritems;
4312 unsigned int data_end;
4313 unsigned int old_data_start;
4314 unsigned int old_size;
4315 unsigned int size_diff;
4316 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004317 struct btrfs_map_token token;
4318
4319 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004320
Chris Mason5f39d392007-10-15 16:14:19 -04004321 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004322 slot = path->slots[0];
4323
4324 old_size = btrfs_item_size_nr(leaf, slot);
4325 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004326 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004327
Chris Mason5f39d392007-10-15 16:14:19 -04004328 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004329 data_end = leaf_data_end(root, leaf);
4330
Chris Mason5f39d392007-10-15 16:14:19 -04004331 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004332
Chris Masonb18c6682007-04-17 13:26:50 -04004333 size_diff = old_size - new_size;
4334
4335 BUG_ON(slot < 0);
4336 BUG_ON(slot >= nritems);
4337
4338 /*
4339 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4340 */
4341 /* first correct the data pointers */
4342 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004343 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004344 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004345
Chris Masoncfed81a2012-03-03 07:40:03 -05004346 ioff = btrfs_token_item_offset(leaf, item, &token);
4347 btrfs_set_token_item_offset(leaf, item,
4348 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004349 }
Chris Masondb945352007-10-15 16:15:53 -04004350
Chris Masonb18c6682007-04-17 13:26:50 -04004351 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004352 if (from_end) {
4353 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4354 data_end + size_diff, btrfs_leaf_data(leaf) +
4355 data_end, old_data_start + new_size - data_end);
4356 } else {
4357 struct btrfs_disk_key disk_key;
4358 u64 offset;
4359
4360 btrfs_item_key(leaf, &disk_key, slot);
4361
4362 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4363 unsigned long ptr;
4364 struct btrfs_file_extent_item *fi;
4365
4366 fi = btrfs_item_ptr(leaf, slot,
4367 struct btrfs_file_extent_item);
4368 fi = (struct btrfs_file_extent_item *)(
4369 (unsigned long)fi - size_diff);
4370
4371 if (btrfs_file_extent_type(leaf, fi) ==
4372 BTRFS_FILE_EXTENT_INLINE) {
4373 ptr = btrfs_item_ptr_offset(leaf, slot);
4374 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004375 (unsigned long)fi,
4376 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004377 disk_bytenr));
4378 }
4379 }
4380
4381 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4382 data_end + size_diff, btrfs_leaf_data(leaf) +
4383 data_end, old_data_start - data_end);
4384
4385 offset = btrfs_disk_key_offset(&disk_key);
4386 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4387 btrfs_set_item_key(leaf, &disk_key, slot);
4388 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004389 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004390 }
Chris Mason5f39d392007-10-15 16:14:19 -04004391
Ross Kirkdd3cc162013-09-16 15:58:09 +01004392 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004393 btrfs_set_item_size(leaf, item, new_size);
4394 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004395
Chris Mason5f39d392007-10-15 16:14:19 -04004396 if (btrfs_leaf_free_space(root, leaf) < 0) {
4397 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004398 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004399 }
Chris Masonb18c6682007-04-17 13:26:50 -04004400}
4401
Chris Masond352ac62008-09-29 15:18:18 -04004402/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004403 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004404 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004405void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004406 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004407{
Chris Mason6567e832007-04-16 09:22:45 -04004408 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004409 struct extent_buffer *leaf;
4410 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004411 u32 nritems;
4412 unsigned int data_end;
4413 unsigned int old_data;
4414 unsigned int old_size;
4415 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004416 struct btrfs_map_token token;
4417
4418 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004419
Chris Mason5f39d392007-10-15 16:14:19 -04004420 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004421
Chris Mason5f39d392007-10-15 16:14:19 -04004422 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004423 data_end = leaf_data_end(root, leaf);
4424
Chris Mason5f39d392007-10-15 16:14:19 -04004425 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4426 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004427 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004428 }
Chris Mason6567e832007-04-16 09:22:45 -04004429 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004430 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004431
4432 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004433 if (slot >= nritems) {
4434 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004435 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4436 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004437 BUG_ON(1);
4438 }
Chris Mason6567e832007-04-16 09:22:45 -04004439
4440 /*
4441 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4442 */
4443 /* first correct the data pointers */
4444 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004445 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004446 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004447
Chris Masoncfed81a2012-03-03 07:40:03 -05004448 ioff = btrfs_token_item_offset(leaf, item, &token);
4449 btrfs_set_token_item_offset(leaf, item,
4450 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004451 }
Chris Mason5f39d392007-10-15 16:14:19 -04004452
Chris Mason6567e832007-04-16 09:22:45 -04004453 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004454 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004455 data_end - data_size, btrfs_leaf_data(leaf) +
4456 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004457
Chris Mason6567e832007-04-16 09:22:45 -04004458 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004459 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004460 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004461 btrfs_set_item_size(leaf, item, old_size + data_size);
4462 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004463
Chris Mason5f39d392007-10-15 16:14:19 -04004464 if (btrfs_leaf_free_space(root, leaf) < 0) {
4465 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004466 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004467 }
Chris Mason6567e832007-04-16 09:22:45 -04004468}
4469
Chris Mason74123bd2007-02-02 11:05:29 -05004470/*
Chris Mason44871b12009-03-13 10:04:31 -04004471 * this is a helper for btrfs_insert_empty_items, the main goal here is
4472 * to save stack depth by doing the bulk of the work in a function
4473 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004474 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004475void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004476 struct btrfs_key *cpu_key, u32 *data_size,
4477 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004478{
Chris Mason5f39d392007-10-15 16:14:19 -04004479 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004480 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004481 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004482 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004483 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004484 struct extent_buffer *leaf;
4485 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004486 struct btrfs_map_token token;
4487
4488 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004489
Chris Mason5f39d392007-10-15 16:14:19 -04004490 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004491 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004492
Chris Mason5f39d392007-10-15 16:14:19 -04004493 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004494 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004495
Chris Masonf25956c2008-09-12 15:32:53 -04004496 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004497 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004498 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004499 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004500 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004501 }
Chris Mason5f39d392007-10-15 16:14:19 -04004502
Chris Masonbe0e5c02007-01-26 15:51:26 -05004503 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004504 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004505
Chris Mason5f39d392007-10-15 16:14:19 -04004506 if (old_data < data_end) {
4507 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004508 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004509 slot, old_data, data_end);
4510 BUG_ON(1);
4511 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004512 /*
4513 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4514 */
4515 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004516 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004517 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004518
Ross Kirkdd3cc162013-09-16 15:58:09 +01004519 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004520 ioff = btrfs_token_item_offset(leaf, item, &token);
4521 btrfs_set_token_item_offset(leaf, item,
4522 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004523 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004524 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004525 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004526 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004527 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004528
4529 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004530 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004531 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004532 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004533 data_end = old_data;
4534 }
Chris Mason5f39d392007-10-15 16:14:19 -04004535
Chris Mason62e27492007-03-15 12:56:47 -04004536 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004537 for (i = 0; i < nr; i++) {
4538 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4539 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004540 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004541 btrfs_set_token_item_offset(leaf, item,
4542 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004543 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004544 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004545 }
Chris Mason44871b12009-03-13 10:04:31 -04004546
Chris Mason9c583092008-01-29 15:15:18 -05004547 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004548
Chris Mason5a01a2e2008-01-30 11:43:54 -05004549 if (slot == 0) {
4550 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004551 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004552 }
Chris Masonb9473432009-03-13 11:00:37 -04004553 btrfs_unlock_up_safe(path, 1);
4554 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004555
Chris Mason5f39d392007-10-15 16:14:19 -04004556 if (btrfs_leaf_free_space(root, leaf) < 0) {
4557 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004558 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004559 }
Chris Mason44871b12009-03-13 10:04:31 -04004560}
4561
4562/*
4563 * Given a key and some data, insert items into the tree.
4564 * This does all the path init required, making room in the tree if needed.
4565 */
4566int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4567 struct btrfs_root *root,
4568 struct btrfs_path *path,
4569 struct btrfs_key *cpu_key, u32 *data_size,
4570 int nr)
4571{
Chris Mason44871b12009-03-13 10:04:31 -04004572 int ret = 0;
4573 int slot;
4574 int i;
4575 u32 total_size = 0;
4576 u32 total_data = 0;
4577
4578 for (i = 0; i < nr; i++)
4579 total_data += data_size[i];
4580
4581 total_size = total_data + (nr * sizeof(struct btrfs_item));
4582 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4583 if (ret == 0)
4584 return -EEXIST;
4585 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004586 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004587
Chris Mason44871b12009-03-13 10:04:31 -04004588 slot = path->slots[0];
4589 BUG_ON(slot < 0);
4590
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004591 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004592 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004593 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004594}
4595
4596/*
4597 * Given a key and some data, insert an item into the tree.
4598 * This does all the path init required, making room in the tree if needed.
4599 */
Chris Masone089f052007-03-16 16:20:31 -04004600int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4601 *root, struct btrfs_key *cpu_key, void *data, u32
4602 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004603{
4604 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004605 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004606 struct extent_buffer *leaf;
4607 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004608
Chris Mason2c90e5d2007-04-02 10:50:19 -04004609 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004610 if (!path)
4611 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004612 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004613 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004614 leaf = path->nodes[0];
4615 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4616 write_extent_buffer(leaf, data, ptr, data_size);
4617 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004618 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004619 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004620 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004621}
4622
Chris Mason74123bd2007-02-02 11:05:29 -05004623/*
Chris Mason5de08d72007-02-24 06:24:44 -05004624 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004625 *
Chris Masond352ac62008-09-29 15:18:18 -04004626 * the tree should have been previously balanced so the deletion does not
4627 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004628 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004629static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4630 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004631{
Chris Mason5f39d392007-10-15 16:14:19 -04004632 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004633 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004634 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004635
Chris Mason5f39d392007-10-15 16:14:19 -04004636 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004637 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004638 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004639 tree_mod_log_eb_move(root->fs_info, parent, slot,
4640 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004641 memmove_extent_buffer(parent,
4642 btrfs_node_key_ptr_offset(slot),
4643 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004644 sizeof(struct btrfs_key_ptr) *
4645 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004646 } else if (level) {
4647 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004648 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004649 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004650 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004651
Chris Mason7518a232007-03-12 12:01:18 -04004652 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004653 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004654 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004655 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004656 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004657 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004658 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004659 struct btrfs_disk_key disk_key;
4660
4661 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004662 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004663 }
Chris Masond6025572007-03-30 14:27:56 -04004664 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004665}
4666
Chris Mason74123bd2007-02-02 11:05:29 -05004667/*
Chris Mason323ac952008-10-01 19:05:46 -04004668 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004669 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004670 *
4671 * This deletes the pointer in path->nodes[1] and frees the leaf
4672 * block extent. zero is returned if it all worked out, < 0 otherwise.
4673 *
4674 * The path must have already been setup for deleting the leaf, including
4675 * all the proper balancing. path->nodes[1] must be locked.
4676 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004677static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4678 struct btrfs_root *root,
4679 struct btrfs_path *path,
4680 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004681{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004682 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004683 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004684
Chris Mason4d081c42009-02-04 09:31:28 -05004685 /*
4686 * btrfs_free_extent is expensive, we want to make sure we
4687 * aren't holding any locks when we call it
4688 */
4689 btrfs_unlock_up_safe(path, 0);
4690
Yan, Zhengf0486c62010-05-16 10:46:25 -04004691 root_sub_used(root, leaf->len);
4692
Josef Bacik3083ee22012-03-09 16:01:49 -05004693 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004694 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004695 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004696}
4697/*
Chris Mason74123bd2007-02-02 11:05:29 -05004698 * delete the item at the leaf level in path. If that empties
4699 * the leaf, remove it from the tree
4700 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004701int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4702 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004703{
Chris Mason5f39d392007-10-15 16:14:19 -04004704 struct extent_buffer *leaf;
4705 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004706 int last_off;
4707 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004708 int ret = 0;
4709 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004710 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004711 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004712 struct btrfs_map_token token;
4713
4714 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715
Chris Mason5f39d392007-10-15 16:14:19 -04004716 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004717 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4718
4719 for (i = 0; i < nr; i++)
4720 dsize += btrfs_item_size_nr(leaf, slot + i);
4721
Chris Mason5f39d392007-10-15 16:14:19 -04004722 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004723
Chris Mason85e21ba2008-01-29 15:11:36 -05004724 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004725 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004726
4727 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004728 data_end + dsize,
4729 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004730 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004731
Chris Mason85e21ba2008-01-29 15:11:36 -05004732 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004733 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004734
Ross Kirkdd3cc162013-09-16 15:58:09 +01004735 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004736 ioff = btrfs_token_item_offset(leaf, item, &token);
4737 btrfs_set_token_item_offset(leaf, item,
4738 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004739 }
Chris Masondb945352007-10-15 16:15:53 -04004740
Chris Mason5f39d392007-10-15 16:14:19 -04004741 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004742 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004743 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004744 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004745 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004746 btrfs_set_header_nritems(leaf, nritems - nr);
4747 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004748
Chris Mason74123bd2007-02-02 11:05:29 -05004749 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004750 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004751 if (leaf == root->node) {
4752 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004753 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004754 btrfs_set_path_blocking(path);
4755 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004756 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004757 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004758 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004759 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004760 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004761 struct btrfs_disk_key disk_key;
4762
4763 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004764 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004765 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004766
Chris Mason74123bd2007-02-02 11:05:29 -05004767 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004768 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004769 /* push_leaf_left fixes the path.
4770 * make sure the path still points to our leaf
4771 * for possible call to del_ptr below
4772 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004773 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004774 extent_buffer_get(leaf);
4775
Chris Masonb9473432009-03-13 11:00:37 -04004776 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004777 wret = push_leaf_left(trans, root, path, 1, 1,
4778 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004779 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004780 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004781
4782 if (path->nodes[0] == leaf &&
4783 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004784 wret = push_leaf_right(trans, root, path, 1,
4785 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004786 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004787 ret = wret;
4788 }
Chris Mason5f39d392007-10-15 16:14:19 -04004789
4790 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004791 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004792 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004793 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004794 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004795 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004796 /* if we're still in the path, make sure
4797 * we're dirty. Otherwise, one of the
4798 * push_leaf functions must have already
4799 * dirtied this buffer
4800 */
4801 if (path->nodes[0] == leaf)
4802 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004803 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004804 }
Chris Masond5719762007-03-23 10:01:08 -04004805 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004806 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004807 }
4808 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004809 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004810}
4811
Chris Mason97571fd2007-02-24 13:39:08 -05004812/*
Chris Mason925baed2008-06-25 16:01:30 -04004813 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004814 * returns 0 if it found something or 1 if there are no lesser leaves.
4815 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004816 *
4817 * This may release the path, and so you may lose any locks held at the
4818 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004819 */
Stefan Behrens35a36212013-08-14 18:12:25 +02004820static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05004821{
Chris Mason925baed2008-06-25 16:01:30 -04004822 struct btrfs_key key;
4823 struct btrfs_disk_key found_key;
4824 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004825
Chris Mason925baed2008-06-25 16:01:30 -04004826 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004827
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004828 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004829 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004830 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004831 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004832 key.offset = (u64)-1;
4833 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004834 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004835 key.type = (u8)-1;
4836 key.offset = (u64)-1;
4837 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004838 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004839 }
Chris Mason7bb86312007-12-11 09:25:06 -05004840
David Sterbab3b4aa72011-04-21 01:20:15 +02004841 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004842 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4843 if (ret < 0)
4844 return ret;
4845 btrfs_item_key(path->nodes[0], &found_key, 0);
4846 ret = comp_keys(&found_key, &key);
4847 if (ret < 0)
4848 return 0;
4849 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004850}
4851
Chris Mason3f157a22008-06-25 16:01:31 -04004852/*
4853 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004854 * for nodes or leaves that are have a minimum transaction id.
4855 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004856 *
4857 * This does not cow, but it does stuff the starting key it finds back
4858 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4859 * key and get a writable path.
4860 *
4861 * This does lock as it descends, and path->keep_locks should be set
4862 * to 1 by the caller.
4863 *
4864 * This honors path->lowest_level to prevent descent past a given level
4865 * of the tree.
4866 *
Chris Masond352ac62008-09-29 15:18:18 -04004867 * min_trans indicates the oldest transaction that you are interested
4868 * in walking through. Any nodes or leaves older than min_trans are
4869 * skipped over (without reading them).
4870 *
Chris Mason3f157a22008-06-25 16:01:31 -04004871 * returns zero if something useful was found, < 0 on error and 1 if there
4872 * was nothing in the tree that matched the search criteria.
4873 */
4874int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004875 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004876 u64 min_trans)
4877{
4878 struct extent_buffer *cur;
4879 struct btrfs_key found_key;
4880 int slot;
Yan96524802008-07-24 12:19:49 -04004881 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004882 u32 nritems;
4883 int level;
4884 int ret = 1;
4885
Chris Mason934d3752008-12-08 16:43:10 -05004886 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004887again:
Chris Masonbd681512011-07-16 15:23:14 -04004888 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004889 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004890 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004891 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004892 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004893
4894 if (btrfs_header_generation(cur) < min_trans) {
4895 ret = 1;
4896 goto out;
4897 }
Chris Masond3977122009-01-05 21:25:51 -05004898 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004899 nritems = btrfs_header_nritems(cur);
4900 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004901 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004902
Chris Mason323ac952008-10-01 19:05:46 -04004903 /* at the lowest level, we're done, setup the path and exit */
4904 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004905 if (slot >= nritems)
4906 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004907 ret = 0;
4908 path->slots[level] = slot;
4909 btrfs_item_key_to_cpu(cur, &found_key, slot);
4910 goto out;
4911 }
Yan96524802008-07-24 12:19:49 -04004912 if (sret && slot > 0)
4913 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004914 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004915 * check this node pointer against the min_trans parameters.
4916 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004917 */
Chris Masond3977122009-01-05 21:25:51 -05004918 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004919 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004920
Chris Mason3f157a22008-06-25 16:01:31 -04004921 gen = btrfs_node_ptr_generation(cur, slot);
4922 if (gen < min_trans) {
4923 slot++;
4924 continue;
4925 }
Eric Sandeende78b512013-01-31 18:21:12 +00004926 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004927 }
Chris Masone02119d2008-09-05 16:13:11 -04004928find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004929 /*
4930 * we didn't find a candidate key in this node, walk forward
4931 * and find another one
4932 */
4933 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004934 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004935 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004936 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004937 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004938 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004939 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004940 goto again;
4941 } else {
4942 goto out;
4943 }
4944 }
4945 /* save our key for returning back */
4946 btrfs_node_key_to_cpu(cur, &found_key, slot);
4947 path->slots[level] = slot;
4948 if (level == path->lowest_level) {
4949 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004950 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004951 goto out;
4952 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004953 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004954 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004955 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004956
Chris Masonbd681512011-07-16 15:23:14 -04004957 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004958
Chris Masonbd681512011-07-16 15:23:14 -04004959 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004960 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004961 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004962 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004963 }
4964out:
4965 if (ret == 0)
4966 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004967 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004968 return ret;
4969}
4970
Alexander Block70698302012-06-05 21:07:48 +02004971static void tree_move_down(struct btrfs_root *root,
4972 struct btrfs_path *path,
4973 int *level, int root_level)
4974{
Chris Mason74dd17f2012-08-07 16:25:13 -04004975 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004976 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4977 path->slots[*level]);
4978 path->slots[*level - 1] = 0;
4979 (*level)--;
4980}
4981
4982static int tree_move_next_or_upnext(struct btrfs_root *root,
4983 struct btrfs_path *path,
4984 int *level, int root_level)
4985{
4986 int ret = 0;
4987 int nritems;
4988 nritems = btrfs_header_nritems(path->nodes[*level]);
4989
4990 path->slots[*level]++;
4991
Chris Mason74dd17f2012-08-07 16:25:13 -04004992 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004993 if (*level == root_level)
4994 return -1;
4995
4996 /* move upnext */
4997 path->slots[*level] = 0;
4998 free_extent_buffer(path->nodes[*level]);
4999 path->nodes[*level] = NULL;
5000 (*level)++;
5001 path->slots[*level]++;
5002
5003 nritems = btrfs_header_nritems(path->nodes[*level]);
5004 ret = 1;
5005 }
5006 return ret;
5007}
5008
5009/*
5010 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5011 * or down.
5012 */
5013static int tree_advance(struct btrfs_root *root,
5014 struct btrfs_path *path,
5015 int *level, int root_level,
5016 int allow_down,
5017 struct btrfs_key *key)
5018{
5019 int ret;
5020
5021 if (*level == 0 || !allow_down) {
5022 ret = tree_move_next_or_upnext(root, path, level, root_level);
5023 } else {
5024 tree_move_down(root, path, level, root_level);
5025 ret = 0;
5026 }
5027 if (ret >= 0) {
5028 if (*level == 0)
5029 btrfs_item_key_to_cpu(path->nodes[*level], key,
5030 path->slots[*level]);
5031 else
5032 btrfs_node_key_to_cpu(path->nodes[*level], key,
5033 path->slots[*level]);
5034 }
5035 return ret;
5036}
5037
5038static int tree_compare_item(struct btrfs_root *left_root,
5039 struct btrfs_path *left_path,
5040 struct btrfs_path *right_path,
5041 char *tmp_buf)
5042{
5043 int cmp;
5044 int len1, len2;
5045 unsigned long off1, off2;
5046
5047 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5048 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5049 if (len1 != len2)
5050 return 1;
5051
5052 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5053 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5054 right_path->slots[0]);
5055
5056 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5057
5058 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5059 if (cmp)
5060 return 1;
5061 return 0;
5062}
5063
5064#define ADVANCE 1
5065#define ADVANCE_ONLY_NEXT -1
5066
5067/*
5068 * This function compares two trees and calls the provided callback for
5069 * every changed/new/deleted item it finds.
5070 * If shared tree blocks are encountered, whole subtrees are skipped, making
5071 * the compare pretty fast on snapshotted subvolumes.
5072 *
5073 * This currently works on commit roots only. As commit roots are read only,
5074 * we don't do any locking. The commit roots are protected with transactions.
5075 * Transactions are ended and rejoined when a commit is tried in between.
5076 *
5077 * This function checks for modifications done to the trees while comparing.
5078 * If it detects a change, it aborts immediately.
5079 */
5080int btrfs_compare_trees(struct btrfs_root *left_root,
5081 struct btrfs_root *right_root,
5082 btrfs_changed_cb_t changed_cb, void *ctx)
5083{
5084 int ret;
5085 int cmp;
5086 struct btrfs_trans_handle *trans = NULL;
5087 struct btrfs_path *left_path = NULL;
5088 struct btrfs_path *right_path = NULL;
5089 struct btrfs_key left_key;
5090 struct btrfs_key right_key;
5091 char *tmp_buf = NULL;
5092 int left_root_level;
5093 int right_root_level;
5094 int left_level;
5095 int right_level;
5096 int left_end_reached;
5097 int right_end_reached;
5098 int advance_left;
5099 int advance_right;
5100 u64 left_blockptr;
5101 u64 right_blockptr;
5102 u64 left_start_ctransid;
5103 u64 right_start_ctransid;
5104 u64 ctransid;
5105
5106 left_path = btrfs_alloc_path();
5107 if (!left_path) {
5108 ret = -ENOMEM;
5109 goto out;
5110 }
5111 right_path = btrfs_alloc_path();
5112 if (!right_path) {
5113 ret = -ENOMEM;
5114 goto out;
5115 }
5116
5117 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5118 if (!tmp_buf) {
5119 ret = -ENOMEM;
5120 goto out;
5121 }
5122
5123 left_path->search_commit_root = 1;
5124 left_path->skip_locking = 1;
5125 right_path->search_commit_root = 1;
5126 right_path->skip_locking = 1;
5127
Anand Jain5f3ab902012-12-07 09:28:54 +00005128 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005129 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005130 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005131
Anand Jain5f3ab902012-12-07 09:28:54 +00005132 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005133 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005134 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005135
5136 trans = btrfs_join_transaction(left_root);
5137 if (IS_ERR(trans)) {
5138 ret = PTR_ERR(trans);
5139 trans = NULL;
5140 goto out;
5141 }
5142
5143 /*
5144 * Strategy: Go to the first items of both trees. Then do
5145 *
5146 * If both trees are at level 0
5147 * Compare keys of current items
5148 * If left < right treat left item as new, advance left tree
5149 * and repeat
5150 * If left > right treat right item as deleted, advance right tree
5151 * and repeat
5152 * If left == right do deep compare of items, treat as changed if
5153 * needed, advance both trees and repeat
5154 * If both trees are at the same level but not at level 0
5155 * Compare keys of current nodes/leafs
5156 * If left < right advance left tree and repeat
5157 * If left > right advance right tree and repeat
5158 * If left == right compare blockptrs of the next nodes/leafs
5159 * If they match advance both trees but stay at the same level
5160 * and repeat
5161 * If they don't match advance both trees while allowing to go
5162 * deeper and repeat
5163 * If tree levels are different
5164 * Advance the tree that needs it and repeat
5165 *
5166 * Advancing a tree means:
5167 * If we are at level 0, try to go to the next slot. If that's not
5168 * possible, go one level up and repeat. Stop when we found a level
5169 * where we could go to the next slot. We may at this point be on a
5170 * node or a leaf.
5171 *
5172 * If we are not at level 0 and not on shared tree blocks, go one
5173 * level deeper.
5174 *
5175 * If we are not at level 0 and on shared tree blocks, go one slot to
5176 * the right if possible or go up and right.
5177 */
5178
5179 left_level = btrfs_header_level(left_root->commit_root);
5180 left_root_level = left_level;
5181 left_path->nodes[left_level] = left_root->commit_root;
5182 extent_buffer_get(left_path->nodes[left_level]);
5183
5184 right_level = btrfs_header_level(right_root->commit_root);
5185 right_root_level = right_level;
5186 right_path->nodes[right_level] = right_root->commit_root;
5187 extent_buffer_get(right_path->nodes[right_level]);
5188
5189 if (left_level == 0)
5190 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5191 &left_key, left_path->slots[left_level]);
5192 else
5193 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5194 &left_key, left_path->slots[left_level]);
5195 if (right_level == 0)
5196 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5197 &right_key, right_path->slots[right_level]);
5198 else
5199 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5200 &right_key, right_path->slots[right_level]);
5201
5202 left_end_reached = right_end_reached = 0;
5203 advance_left = advance_right = 0;
5204
5205 while (1) {
5206 /*
5207 * We need to make sure the transaction does not get committed
5208 * while we do anything on commit roots. This means, we need to
5209 * join and leave transactions for every item that we process.
5210 */
5211 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5212 btrfs_release_path(left_path);
5213 btrfs_release_path(right_path);
5214
5215 ret = btrfs_end_transaction(trans, left_root);
5216 trans = NULL;
5217 if (ret < 0)
5218 goto out;
5219 }
5220 /* now rejoin the transaction */
5221 if (!trans) {
5222 trans = btrfs_join_transaction(left_root);
5223 if (IS_ERR(trans)) {
5224 ret = PTR_ERR(trans);
5225 trans = NULL;
5226 goto out;
5227 }
5228
Anand Jain5f3ab902012-12-07 09:28:54 +00005229 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005230 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005231 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005232 if (ctransid != left_start_ctransid)
5233 left_start_ctransid = 0;
5234
Anand Jain5f3ab902012-12-07 09:28:54 +00005235 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005236 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005237 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005238 if (ctransid != right_start_ctransid)
5239 right_start_ctransid = 0;
5240
5241 if (!left_start_ctransid || !right_start_ctransid) {
5242 WARN(1, KERN_WARNING
5243 "btrfs: btrfs_compare_tree detected "
5244 "a change in one of the trees while "
5245 "iterating. This is probably a "
5246 "bug.\n");
5247 ret = -EIO;
5248 goto out;
5249 }
5250
5251 /*
5252 * the commit root may have changed, so start again
5253 * where we stopped
5254 */
5255 left_path->lowest_level = left_level;
5256 right_path->lowest_level = right_level;
5257 ret = btrfs_search_slot(NULL, left_root,
5258 &left_key, left_path, 0, 0);
5259 if (ret < 0)
5260 goto out;
5261 ret = btrfs_search_slot(NULL, right_root,
5262 &right_key, right_path, 0, 0);
5263 if (ret < 0)
5264 goto out;
5265 }
5266
5267 if (advance_left && !left_end_reached) {
5268 ret = tree_advance(left_root, left_path, &left_level,
5269 left_root_level,
5270 advance_left != ADVANCE_ONLY_NEXT,
5271 &left_key);
5272 if (ret < 0)
5273 left_end_reached = ADVANCE;
5274 advance_left = 0;
5275 }
5276 if (advance_right && !right_end_reached) {
5277 ret = tree_advance(right_root, right_path, &right_level,
5278 right_root_level,
5279 advance_right != ADVANCE_ONLY_NEXT,
5280 &right_key);
5281 if (ret < 0)
5282 right_end_reached = ADVANCE;
5283 advance_right = 0;
5284 }
5285
5286 if (left_end_reached && right_end_reached) {
5287 ret = 0;
5288 goto out;
5289 } else if (left_end_reached) {
5290 if (right_level == 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 }
5299 advance_right = ADVANCE;
5300 continue;
5301 } else if (right_end_reached) {
5302 if (left_level == 0) {
5303 ret = changed_cb(left_root, right_root,
5304 left_path, right_path,
5305 &left_key,
5306 BTRFS_COMPARE_TREE_NEW,
5307 ctx);
5308 if (ret < 0)
5309 goto out;
5310 }
5311 advance_left = ADVANCE;
5312 continue;
5313 }
5314
5315 if (left_level == 0 && right_level == 0) {
5316 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5317 if (cmp < 0) {
5318 ret = changed_cb(left_root, right_root,
5319 left_path, right_path,
5320 &left_key,
5321 BTRFS_COMPARE_TREE_NEW,
5322 ctx);
5323 if (ret < 0)
5324 goto out;
5325 advance_left = ADVANCE;
5326 } else if (cmp > 0) {
5327 ret = changed_cb(left_root, right_root,
5328 left_path, right_path,
5329 &right_key,
5330 BTRFS_COMPARE_TREE_DELETED,
5331 ctx);
5332 if (ret < 0)
5333 goto out;
5334 advance_right = ADVANCE;
5335 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005336 enum btrfs_compare_tree_result cmp;
5337
Chris Mason74dd17f2012-08-07 16:25:13 -04005338 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005339 ret = tree_compare_item(left_root, left_path,
5340 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005341 if (ret)
5342 cmp = BTRFS_COMPARE_TREE_CHANGED;
5343 else
5344 cmp = BTRFS_COMPARE_TREE_SAME;
5345 ret = changed_cb(left_root, right_root,
5346 left_path, right_path,
5347 &left_key, cmp, ctx);
5348 if (ret < 0)
5349 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005350 advance_left = ADVANCE;
5351 advance_right = ADVANCE;
5352 }
5353 } else if (left_level == right_level) {
5354 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5355 if (cmp < 0) {
5356 advance_left = ADVANCE;
5357 } else if (cmp > 0) {
5358 advance_right = ADVANCE;
5359 } else {
5360 left_blockptr = btrfs_node_blockptr(
5361 left_path->nodes[left_level],
5362 left_path->slots[left_level]);
5363 right_blockptr = btrfs_node_blockptr(
5364 right_path->nodes[right_level],
5365 right_path->slots[right_level]);
5366 if (left_blockptr == right_blockptr) {
5367 /*
5368 * As we're on a shared block, don't
5369 * allow to go deeper.
5370 */
5371 advance_left = ADVANCE_ONLY_NEXT;
5372 advance_right = ADVANCE_ONLY_NEXT;
5373 } else {
5374 advance_left = ADVANCE;
5375 advance_right = ADVANCE;
5376 }
5377 }
5378 } else if (left_level < right_level) {
5379 advance_right = ADVANCE;
5380 } else {
5381 advance_left = ADVANCE;
5382 }
5383 }
5384
5385out:
5386 btrfs_free_path(left_path);
5387 btrfs_free_path(right_path);
5388 kfree(tmp_buf);
5389
5390 if (trans) {
5391 if (!ret)
5392 ret = btrfs_end_transaction(trans, left_root);
5393 else
5394 btrfs_end_transaction(trans, left_root);
5395 }
5396
5397 return ret;
5398}
5399
Chris Mason3f157a22008-06-25 16:01:31 -04005400/*
5401 * this is similar to btrfs_next_leaf, but does not try to preserve
5402 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005403 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005404 *
5405 * 0 is returned if another key is found, < 0 if there are any errors
5406 * and 1 is returned if there are no higher keys in the tree
5407 *
5408 * path->keep_locks should be set to 1 on the search made before
5409 * calling this function.
5410 */
Chris Masone7a84562008-06-25 16:01:31 -04005411int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005412 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005413{
Chris Masone7a84562008-06-25 16:01:31 -04005414 int slot;
5415 struct extent_buffer *c;
5416
Chris Mason934d3752008-12-08 16:43:10 -05005417 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005418 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005419 if (!path->nodes[level])
5420 return 1;
5421
5422 slot = path->slots[level] + 1;
5423 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005424next:
Chris Masone7a84562008-06-25 16:01:31 -04005425 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005426 int ret;
5427 int orig_lowest;
5428 struct btrfs_key cur_key;
5429 if (level + 1 >= BTRFS_MAX_LEVEL ||
5430 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005431 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005432
5433 if (path->locks[level + 1]) {
5434 level++;
5435 continue;
5436 }
5437
5438 slot = btrfs_header_nritems(c) - 1;
5439 if (level == 0)
5440 btrfs_item_key_to_cpu(c, &cur_key, slot);
5441 else
5442 btrfs_node_key_to_cpu(c, &cur_key, slot);
5443
5444 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005445 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005446 path->lowest_level = level;
5447 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5448 0, 0);
5449 path->lowest_level = orig_lowest;
5450 if (ret < 0)
5451 return ret;
5452
5453 c = path->nodes[level];
5454 slot = path->slots[level];
5455 if (ret == 0)
5456 slot++;
5457 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005458 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005459
Chris Masone7a84562008-06-25 16:01:31 -04005460 if (level == 0)
5461 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005462 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005463 u64 gen = btrfs_node_ptr_generation(c, slot);
5464
Chris Mason3f157a22008-06-25 16:01:31 -04005465 if (gen < min_trans) {
5466 slot++;
5467 goto next;
5468 }
Chris Masone7a84562008-06-25 16:01:31 -04005469 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005470 }
Chris Masone7a84562008-06-25 16:01:31 -04005471 return 0;
5472 }
5473 return 1;
5474}
5475
Chris Mason7bb86312007-12-11 09:25:06 -05005476/*
Chris Mason925baed2008-06-25 16:01:30 -04005477 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005478 * returns 0 if it found something or 1 if there are no greater leaves.
5479 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005480 */
Chris Mason234b63a2007-03-13 10:46:10 -04005481int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005482{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005483 return btrfs_next_old_leaf(root, path, 0);
5484}
5485
5486int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5487 u64 time_seq)
5488{
Chris Masond97e63b2007-02-20 16:40:44 -05005489 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005490 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005491 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005492 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005493 struct btrfs_key key;
5494 u32 nritems;
5495 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005496 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005497 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005498
5499 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005500 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005501 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005502
Chris Mason8e73f272009-04-03 10:14:18 -04005503 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5504again:
5505 level = 1;
5506 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005507 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005508 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005509
Chris Masona2135012008-06-25 16:01:30 -04005510 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005511 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005512
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005513 if (time_seq)
5514 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5515 else
5516 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005517 path->keep_locks = 0;
5518
5519 if (ret < 0)
5520 return ret;
5521
Chris Masona2135012008-06-25 16:01:30 -04005522 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005523 /*
5524 * by releasing the path above we dropped all our locks. A balance
5525 * could have added more items next to the key that used to be
5526 * at the very end of the block. So, check again here and
5527 * advance the path if there are now more items available.
5528 */
Chris Masona2135012008-06-25 16:01:30 -04005529 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005530 if (ret == 0)
5531 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005532 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005533 goto done;
5534 }
Chris Masond97e63b2007-02-20 16:40:44 -05005535
Chris Masond3977122009-01-05 21:25:51 -05005536 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005537 if (!path->nodes[level]) {
5538 ret = 1;
5539 goto done;
5540 }
Chris Mason5f39d392007-10-15 16:14:19 -04005541
Chris Masond97e63b2007-02-20 16:40:44 -05005542 slot = path->slots[level] + 1;
5543 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005544 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005545 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005546 if (level == BTRFS_MAX_LEVEL) {
5547 ret = 1;
5548 goto done;
5549 }
Chris Masond97e63b2007-02-20 16:40:44 -05005550 continue;
5551 }
Chris Mason5f39d392007-10-15 16:14:19 -04005552
Chris Mason925baed2008-06-25 16:01:30 -04005553 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005554 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005555 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005556 }
Chris Mason5f39d392007-10-15 16:14:19 -04005557
Chris Mason8e73f272009-04-03 10:14:18 -04005558 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005559 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005560 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005561 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005562 if (ret == -EAGAIN)
5563 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005564
Chris Mason76a05b32009-05-14 13:24:30 -04005565 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005566 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005567 goto done;
5568 }
5569
Chris Mason5cd57b22008-06-25 16:01:30 -04005570 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005571 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005572 if (!ret && time_seq) {
5573 /*
5574 * If we don't get the lock, we may be racing
5575 * with push_leaf_left, holding that lock while
5576 * itself waiting for the leaf we've currently
5577 * locked. To solve this situation, we give up
5578 * on our lock and cycle.
5579 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005580 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005581 btrfs_release_path(path);
5582 cond_resched();
5583 goto again;
5584 }
Chris Mason8e73f272009-04-03 10:14:18 -04005585 if (!ret) {
5586 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005587 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005588 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005589 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005590 }
Chris Mason31533fb2011-07-26 16:01:59 -04005591 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005592 }
Chris Masond97e63b2007-02-20 16:40:44 -05005593 break;
5594 }
5595 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005596 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005597 level--;
5598 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005599 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005600 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005601
Chris Mason5f39d392007-10-15 16:14:19 -04005602 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005603 path->nodes[level] = next;
5604 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005605 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005606 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005607 if (!level)
5608 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005609
Chris Mason8e73f272009-04-03 10:14:18 -04005610 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005611 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005612 if (ret == -EAGAIN)
5613 goto again;
5614
Chris Mason76a05b32009-05-14 13:24:30 -04005615 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005616 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005617 goto done;
5618 }
5619
Chris Mason5cd57b22008-06-25 16:01:30 -04005620 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005621 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005622 if (!ret) {
5623 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005624 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005625 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005626 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005627 }
Chris Mason31533fb2011-07-26 16:01:59 -04005628 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005629 }
Chris Masond97e63b2007-02-20 16:40:44 -05005630 }
Chris Mason8e73f272009-04-03 10:14:18 -04005631 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005632done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005633 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005634 path->leave_spinning = old_spinning;
5635 if (!old_spinning)
5636 btrfs_set_path_blocking(path);
5637
5638 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005639}
Chris Mason0b86a832008-03-24 15:01:56 -04005640
Chris Mason3f157a22008-06-25 16:01:31 -04005641/*
5642 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5643 * searching until it gets past min_objectid or finds an item of 'type'
5644 *
5645 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5646 */
Chris Mason0b86a832008-03-24 15:01:56 -04005647int btrfs_previous_item(struct btrfs_root *root,
5648 struct btrfs_path *path, u64 min_objectid,
5649 int type)
5650{
5651 struct btrfs_key found_key;
5652 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005653 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005654 int ret;
5655
Chris Masond3977122009-01-05 21:25:51 -05005656 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005657 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005658 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005659 ret = btrfs_prev_leaf(root, path);
5660 if (ret != 0)
5661 return ret;
5662 } else {
5663 path->slots[0]--;
5664 }
5665 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005666 nritems = btrfs_header_nritems(leaf);
5667 if (nritems == 0)
5668 return 1;
5669 if (path->slots[0] == nritems)
5670 path->slots[0]--;
5671
Chris Mason0b86a832008-03-24 15:01:56 -04005672 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005673 if (found_key.objectid < min_objectid)
5674 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005675 if (found_key.type == type)
5676 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005677 if (found_key.objectid == min_objectid &&
5678 found_key.type < type)
5679 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005680 }
5681 return 1;
5682}