blob: 61b5bcd57b7e320624c2778d2051db03b79f2282 [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
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +0200277 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(cow),
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
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +0200999 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(cow),
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);
Josef Bacik416bc652013-04-23 14:17:42 -04001288 if (!old || !extent_buffer_uptodate(old)) {
1289 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);
1292 WARN_ON(1);
1293 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001294 eb = btrfs_clone_extent_buffer(old);
1295 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001296 }
1297 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001298 btrfs_tree_read_unlock(eb_root);
1299 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001300 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001301 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001302 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001303 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001304 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001305 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001306 }
1307
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001308 if (!eb)
1309 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001310 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001311 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001312 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001313 btrfs_set_header_bytenr(eb, eb->start);
1314 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001315 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001316 btrfs_set_header_level(eb, old_root->level);
1317 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001318 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001319 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001320 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001321 else
1322 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001323 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001324
1325 return eb;
1326}
1327
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001328int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1329{
1330 struct tree_mod_elem *tm;
1331 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001332 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001333
Jan Schmidt30b04632013-04-13 13:19:54 +00001334 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001335 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1336 level = tm->old_root.level;
1337 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001338 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001339 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001340 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001341
1342 return level;
1343}
1344
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001345static inline int should_cow_block(struct btrfs_trans_handle *trans,
1346 struct btrfs_root *root,
1347 struct extent_buffer *buf)
1348{
Liu Bof1ebcc72011-11-14 20:48:06 -05001349 /* ensure we can see the force_cow */
1350 smp_rmb();
1351
1352 /*
1353 * We do not need to cow a block if
1354 * 1) this block is not created or changed in this transaction;
1355 * 2) this block does not belong to TREE_RELOC tree;
1356 * 3) the root is not forced COW.
1357 *
1358 * What is forced COW:
1359 * when we create snapshot during commiting the transaction,
1360 * after we've finished coping src root, we must COW the shared
1361 * block to ensure the metadata consistency.
1362 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001363 if (btrfs_header_generation(buf) == trans->transid &&
1364 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1365 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001366 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1367 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001368 return 0;
1369 return 1;
1370}
1371
Chris Masond352ac62008-09-29 15:18:18 -04001372/*
1373 * cows a single block, see __btrfs_cow_block for the real work.
1374 * This version of it has extra checks so that a block isn't cow'd more than
1375 * once per transaction, as long as it hasn't been written yet
1376 */
Chris Masond3977122009-01-05 21:25:51 -05001377noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001378 struct btrfs_root *root, struct extent_buffer *buf,
1379 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001380 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001381{
Chris Mason6702ed42007-08-07 16:15:09 -04001382 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001383 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001384
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001385 if (trans->transaction != root->fs_info->running_transaction)
1386 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001387 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001388 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001389
1390 if (trans->transid != root->fs_info->generation)
1391 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001392 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001393
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001395 *cow_ret = buf;
1396 return 0;
1397 }
Chris Masonc4876852009-02-04 09:24:25 -05001398
Chris Mason0b86a832008-03-24 15:01:56 -04001399 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001400
1401 if (parent)
1402 btrfs_set_lock_blocking(parent);
1403 btrfs_set_lock_blocking(buf);
1404
Chris Masonf510cfe2007-10-15 16:14:48 -04001405 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001406 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001407
1408 trace_btrfs_cow_block(root, buf, *cow_ret);
1409
Chris Masonf510cfe2007-10-15 16:14:48 -04001410 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001411}
1412
Chris Masond352ac62008-09-29 15:18:18 -04001413/*
1414 * helper function for defrag to decide if two blocks pointed to by a
1415 * node are actually close by
1416 */
Chris Mason6b800532007-10-15 16:17:34 -04001417static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001418{
Chris Mason6b800532007-10-15 16:17:34 -04001419 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001420 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001421 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001422 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001423 return 0;
1424}
1425
Chris Mason081e9572007-11-06 10:26:24 -05001426/*
1427 * compare two keys in a memcmp fashion
1428 */
1429static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1430{
1431 struct btrfs_key k1;
1432
1433 btrfs_disk_key_to_cpu(&k1, disk);
1434
Diego Calleja20736ab2009-07-24 11:06:52 -04001435 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001436}
1437
Josef Bacikf3465ca2008-11-12 14:19:50 -05001438/*
1439 * same as comp_keys only with two btrfs_key's
1440 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001442{
1443 if (k1->objectid > k2->objectid)
1444 return 1;
1445 if (k1->objectid < k2->objectid)
1446 return -1;
1447 if (k1->type > k2->type)
1448 return 1;
1449 if (k1->type < k2->type)
1450 return -1;
1451 if (k1->offset > k2->offset)
1452 return 1;
1453 if (k1->offset < k2->offset)
1454 return -1;
1455 return 0;
1456}
Chris Mason081e9572007-11-06 10:26:24 -05001457
Chris Masond352ac62008-09-29 15:18:18 -04001458/*
1459 * this is used by the defrag code to go through all the
1460 * leaves pointed to by a node and reallocate them so that
1461 * disk order is close to key order
1462 */
Chris Mason6702ed42007-08-07 16:15:09 -04001463int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001464 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001465 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001466 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001467{
Chris Mason6b800532007-10-15 16:17:34 -04001468 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001469 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001470 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001471 u64 search_start = *last_ret;
1472 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001473 u64 other;
1474 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001475 int end_slot;
1476 int i;
1477 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001478 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001479 int uptodate;
1480 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001481 int progress_passed = 0;
1482 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001483
Chris Mason5708b952007-10-25 15:43:18 -04001484 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001485
Julia Lawall6c1500f2012-11-03 20:30:18 +00001486 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1487 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001488
Chris Mason6b800532007-10-15 16:17:34 -04001489 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001490 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001491 end_slot = parent_nritems;
1492
1493 if (parent_nritems == 1)
1494 return 0;
1495
Chris Masonb4ce94d2009-02-04 09:25:08 -05001496 btrfs_set_lock_blocking(parent);
1497
Chris Mason6702ed42007-08-07 16:15:09 -04001498 for (i = start_slot; i < end_slot; i++) {
1499 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001500
Chris Mason081e9572007-11-06 10:26:24 -05001501 btrfs_node_key(parent, &disk_key, i);
1502 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1503 continue;
1504
1505 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001506 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001507 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001508 if (last_block == 0)
1509 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001510
Chris Mason6702ed42007-08-07 16:15:09 -04001511 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001512 other = btrfs_node_blockptr(parent, i - 1);
1513 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001514 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001515 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001516 other = btrfs_node_blockptr(parent, i + 1);
1517 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001518 }
Chris Masone9d0b132007-08-10 14:06:19 -04001519 if (close) {
1520 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001521 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001522 }
Chris Mason6702ed42007-08-07 16:15:09 -04001523
Chris Mason6b800532007-10-15 16:17:34 -04001524 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1525 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001526 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001527 else
1528 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001529 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001530 if (!cur) {
1531 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001532 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001533 if (!cur || !extent_buffer_uptodate(cur)) {
1534 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001535 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001536 }
Chris Mason6b800532007-10-15 16:17:34 -04001537 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001538 err = btrfs_read_buffer(cur, gen);
1539 if (err) {
1540 free_extent_buffer(cur);
1541 return err;
1542 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001543 }
Chris Mason6702ed42007-08-07 16:15:09 -04001544 }
Chris Masone9d0b132007-08-10 14:06:19 -04001545 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001546 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001547
Chris Masone7a84562008-06-25 16:01:31 -04001548 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001549 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001550 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001551 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001552 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001553 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001554 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001555 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001556 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001557 break;
Yan252c38f2007-08-29 09:11:44 -04001558 }
Chris Masone7a84562008-06-25 16:01:31 -04001559 search_start = cur->start;
1560 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001561 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001562 btrfs_tree_unlock(cur);
1563 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001564 }
1565 return err;
1566}
1567
Chris Mason74123bd2007-02-02 11:05:29 -05001568/*
1569 * The leaf data grows from end-to-front in the node.
1570 * this returns the address of the start of the last item,
1571 * which is the stop of the leaf data stack
1572 */
Chris Mason123abc82007-03-14 14:14:43 -04001573static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001574 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001575{
Chris Mason5f39d392007-10-15 16:14:19 -04001576 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001577 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001578 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001579 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001580}
1581
Chris Masonaa5d6be2007-02-28 16:35:06 -05001582
Chris Mason74123bd2007-02-02 11:05:29 -05001583/*
Chris Mason5f39d392007-10-15 16:14:19 -04001584 * search for key in the extent_buffer. The items start at offset p,
1585 * and they are item_size apart. There are 'max' items in p.
1586 *
Chris Mason74123bd2007-02-02 11:05:29 -05001587 * the slot in the array is returned via slot, and it points to
1588 * the place where you would insert key if it is not found in
1589 * the array.
1590 *
1591 * slot may point to max if the key is bigger than all of the keys
1592 */
Chris Masone02119d2008-09-05 16:13:11 -04001593static noinline int generic_bin_search(struct extent_buffer *eb,
1594 unsigned long p,
1595 int item_size, struct btrfs_key *key,
1596 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001597{
1598 int low = 0;
1599 int high = max;
1600 int mid;
1601 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001602 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001603 struct btrfs_disk_key unaligned;
1604 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001605 char *kaddr = NULL;
1606 unsigned long map_start = 0;
1607 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001608 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001609
Chris Masond3977122009-01-05 21:25:51 -05001610 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001611 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001612 offset = p + mid * item_size;
1613
Chris Masona6591712011-07-19 12:04:14 -04001614 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001615 (offset + sizeof(struct btrfs_disk_key)) >
1616 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001617
1618 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001619 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001620 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001621
Chris Mason479965d2007-10-15 16:14:27 -04001622 if (!err) {
1623 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1624 map_start);
1625 } else {
1626 read_extent_buffer(eb, &unaligned,
1627 offset, sizeof(unaligned));
1628 tmp = &unaligned;
1629 }
1630
Chris Mason5f39d392007-10-15 16:14:19 -04001631 } else {
1632 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1633 map_start);
1634 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001635 ret = comp_keys(tmp, key);
1636
1637 if (ret < 0)
1638 low = mid + 1;
1639 else if (ret > 0)
1640 high = mid;
1641 else {
1642 *slot = mid;
1643 return 0;
1644 }
1645 }
1646 *slot = low;
1647 return 1;
1648}
1649
Chris Mason97571fd2007-02-24 13:39:08 -05001650/*
1651 * simple bin_search frontend that does the right thing for
1652 * leaves vs nodes
1653 */
Chris Mason5f39d392007-10-15 16:14:19 -04001654static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1655 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001656{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001657 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001658 return generic_bin_search(eb,
1659 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001660 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001661 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001662 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001663 else
Chris Mason5f39d392007-10-15 16:14:19 -04001664 return generic_bin_search(eb,
1665 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001666 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001667 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001668 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001669}
1670
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001671int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1672 int level, int *slot)
1673{
1674 return bin_search(eb, key, level, slot);
1675}
1676
Yan, Zhengf0486c62010-05-16 10:46:25 -04001677static void root_add_used(struct btrfs_root *root, u32 size)
1678{
1679 spin_lock(&root->accounting_lock);
1680 btrfs_set_root_used(&root->root_item,
1681 btrfs_root_used(&root->root_item) + size);
1682 spin_unlock(&root->accounting_lock);
1683}
1684
1685static void root_sub_used(struct btrfs_root *root, u32 size)
1686{
1687 spin_lock(&root->accounting_lock);
1688 btrfs_set_root_used(&root->root_item,
1689 btrfs_root_used(&root->root_item) - size);
1690 spin_unlock(&root->accounting_lock);
1691}
1692
Chris Masond352ac62008-09-29 15:18:18 -04001693/* given a node and slot number, this reads the blocks it points to. The
1694 * extent buffer is returned with a reference taken (but unlocked).
1695 * NULL is returned on error.
1696 */
Chris Masone02119d2008-09-05 16:13:11 -04001697static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001698 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001699{
Chris Masonca7a79a2008-05-12 12:59:19 -04001700 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001701 struct extent_buffer *eb;
1702
Chris Masonbb803952007-03-01 12:04:21 -05001703 if (slot < 0)
1704 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001705 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001706 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001707
1708 BUG_ON(level == 0);
1709
Josef Bacik416bc652013-04-23 14:17:42 -04001710 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1711 btrfs_level_size(root, level - 1),
1712 btrfs_node_ptr_generation(parent, slot));
1713 if (eb && !extent_buffer_uptodate(eb)) {
1714 free_extent_buffer(eb);
1715 eb = NULL;
1716 }
1717
1718 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001719}
1720
Chris Masond352ac62008-09-29 15:18:18 -04001721/*
1722 * node level balancing, used to make sure nodes are in proper order for
1723 * item deletion. We balance from the top down, so we have to make sure
1724 * that a deletion won't leave an node completely empty later on.
1725 */
Chris Masone02119d2008-09-05 16:13:11 -04001726static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001727 struct btrfs_root *root,
1728 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001729{
Chris Mason5f39d392007-10-15 16:14:19 -04001730 struct extent_buffer *right = NULL;
1731 struct extent_buffer *mid;
1732 struct extent_buffer *left = NULL;
1733 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001734 int ret = 0;
1735 int wret;
1736 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001737 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001738 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001739
1740 if (level == 0)
1741 return 0;
1742
Chris Mason5f39d392007-10-15 16:14:19 -04001743 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001744
Chris Masonbd681512011-07-16 15:23:14 -04001745 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1746 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001747 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1748
Chris Mason1d4f8a02007-03-13 09:28:32 -04001749 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001750
Li Zefana05a9bb2011-09-06 16:55:34 +08001751 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001752 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001753 pslot = path->slots[level + 1];
1754 }
Chris Masonbb803952007-03-01 12:04:21 -05001755
Chris Mason40689472007-03-17 14:29:23 -04001756 /*
1757 * deal with the case where there is only one pointer in the root
1758 * by promoting the node below to a root
1759 */
Chris Mason5f39d392007-10-15 16:14:19 -04001760 if (!parent) {
1761 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001764 return 0;
1765
1766 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001767 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001768 if (!child) {
1769 ret = -EROFS;
1770 btrfs_std_error(root->fs_info, ret);
1771 goto enospc;
1772 }
1773
Chris Mason925baed2008-06-25 16:01:30 -04001774 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001775 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001776 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001777 if (ret) {
1778 btrfs_tree_unlock(child);
1779 free_extent_buffer(child);
1780 goto enospc;
1781 }
Yan2f375ab2008-02-01 14:58:07 -05001782
Jan Schmidt90f8d622013-04-13 13:19:53 +00001783 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001784 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001785
Chris Mason0b86a832008-03-24 15:01:56 -04001786 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001787 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001788
Chris Mason925baed2008-06-25 16:01:30 -04001789 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001790 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001791 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001792 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001793 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001794 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001795
1796 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001797 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001798 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001799 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001800 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001801 }
Chris Mason5f39d392007-10-15 16:14:19 -04001802 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001803 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001804 return 0;
1805
Chris Mason5f39d392007-10-15 16:14:19 -04001806 left = read_node_slot(root, parent, pslot - 1);
1807 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001808 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001809 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001810 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001811 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001812 if (wret) {
1813 ret = wret;
1814 goto enospc;
1815 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001816 }
Chris Mason5f39d392007-10-15 16:14:19 -04001817 right = read_node_slot(root, parent, pslot + 1);
1818 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001819 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001821 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001822 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001823 if (wret) {
1824 ret = wret;
1825 goto enospc;
1826 }
1827 }
1828
1829 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001830 if (left) {
1831 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001832 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001833 if (wret < 0)
1834 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001835 }
Chris Mason79f95c82007-03-01 15:16:26 -05001836
1837 /*
1838 * then try to empty the right most buffer into the middle
1839 */
Chris Mason5f39d392007-10-15 16:14:19 -04001840 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001841 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001842 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001843 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001844 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001845 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001846 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001847 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001848 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001849 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001850 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001851 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001852 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001853 struct btrfs_disk_key right_key;
1854 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001855 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001856 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001857 btrfs_set_node_key(parent, &right_key, pslot + 1);
1858 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001859 }
1860 }
Chris Mason5f39d392007-10-15 16:14:19 -04001861 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001862 /*
1863 * we're not allowed to leave a node with one item in the
1864 * tree during a delete. A deletion from lower in the tree
1865 * could try to delete the only pointer in this node.
1866 * So, pull some keys from the left.
1867 * There has to be a left pointer at this point because
1868 * otherwise we would have pulled some pointers from the
1869 * right
1870 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001871 if (!left) {
1872 ret = -EROFS;
1873 btrfs_std_error(root->fs_info, ret);
1874 goto enospc;
1875 }
Chris Mason5f39d392007-10-15 16:14:19 -04001876 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001877 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001878 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001879 goto enospc;
1880 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001881 if (wret == 1) {
1882 wret = push_node_left(trans, root, left, mid, 1);
1883 if (wret < 0)
1884 ret = wret;
1885 }
Chris Mason79f95c82007-03-01 15:16:26 -05001886 BUG_ON(wret == 1);
1887 }
Chris Mason5f39d392007-10-15 16:14:19 -04001888 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001889 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001890 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001891 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001892 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001893 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001894 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001895 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001896 } else {
1897 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001898 struct btrfs_disk_key mid_key;
1899 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001900 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001901 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001902 btrfs_set_node_key(parent, &mid_key, pslot);
1903 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001904 }
Chris Masonbb803952007-03-01 12:04:21 -05001905
Chris Mason79f95c82007-03-01 15:16:26 -05001906 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001907 if (left) {
1908 if (btrfs_header_nritems(left) > orig_slot) {
1909 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001910 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001911 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001912 path->slots[level + 1] -= 1;
1913 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001914 if (mid) {
1915 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001916 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001917 }
Chris Masonbb803952007-03-01 12:04:21 -05001918 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001919 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001920 path->slots[level] = orig_slot;
1921 }
1922 }
Chris Mason79f95c82007-03-01 15:16:26 -05001923 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001924 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001925 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001926 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001927enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001928 if (right) {
1929 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001930 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001931 }
1932 if (left) {
1933 if (path->nodes[level] != left)
1934 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001935 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001936 }
Chris Masonbb803952007-03-01 12:04:21 -05001937 return ret;
1938}
1939
Chris Masond352ac62008-09-29 15:18:18 -04001940/* Node balancing for insertion. Here we only split or push nodes around
1941 * when they are completely full. This is also done top down, so we
1942 * have to be pessimistic.
1943 */
Chris Masond3977122009-01-05 21:25:51 -05001944static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001945 struct btrfs_root *root,
1946 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001947{
Chris Mason5f39d392007-10-15 16:14:19 -04001948 struct extent_buffer *right = NULL;
1949 struct extent_buffer *mid;
1950 struct extent_buffer *left = NULL;
1951 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001952 int ret = 0;
1953 int wret;
1954 int pslot;
1955 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001956
1957 if (level == 0)
1958 return 1;
1959
Chris Mason5f39d392007-10-15 16:14:19 -04001960 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001961 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001962
Li Zefana05a9bb2011-09-06 16:55:34 +08001963 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001964 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001965 pslot = path->slots[level + 1];
1966 }
Chris Masone66f7092007-04-20 13:16:02 -04001967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001969 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001972
1973 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001974 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001975 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001976
1977 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001978 btrfs_set_lock_blocking(left);
1979
Chris Mason5f39d392007-10-15 16:14:19 -04001980 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001981 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1982 wret = 1;
1983 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001984 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001985 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001986 if (ret)
1987 wret = 1;
1988 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001989 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001990 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001991 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001992 }
Chris Masone66f7092007-04-20 13:16:02 -04001993 if (wret < 0)
1994 ret = wret;
1995 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001996 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001997 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001998 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001999 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002000 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002001 btrfs_set_node_key(parent, &disk_key, pslot);
2002 btrfs_mark_buffer_dirty(parent);
2003 if (btrfs_header_nritems(left) > orig_slot) {
2004 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002005 path->slots[level + 1] -= 1;
2006 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002007 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002008 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002009 } else {
2010 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002011 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002012 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002013 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002014 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002015 }
Chris Masone66f7092007-04-20 13:16:02 -04002016 return 0;
2017 }
Chris Mason925baed2008-06-25 16:01:30 -04002018 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002019 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002020 }
Chris Mason925baed2008-06-25 16:01:30 -04002021 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002022
2023 /*
2024 * then try to empty the right most buffer into the middle
2025 */
Chris Mason5f39d392007-10-15 16:14:19 -04002026 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002027 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002028
Chris Mason925baed2008-06-25 16:01:30 -04002029 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002030 btrfs_set_lock_blocking(right);
2031
Chris Mason5f39d392007-10-15 16:14:19 -04002032 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002033 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2034 wret = 1;
2035 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002036 ret = btrfs_cow_block(trans, root, right,
2037 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002038 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002039 if (ret)
2040 wret = 1;
2041 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002042 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002043 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002044 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002045 }
Chris Masone66f7092007-04-20 13:16:02 -04002046 if (wret < 0)
2047 ret = wret;
2048 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002049 struct btrfs_disk_key disk_key;
2050
2051 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002052 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002053 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002054 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2055 btrfs_mark_buffer_dirty(parent);
2056
2057 if (btrfs_header_nritems(mid) <= orig_slot) {
2058 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002059 path->slots[level + 1] += 1;
2060 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002061 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002062 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002063 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002064 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002065 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002067 }
Chris Masone66f7092007-04-20 13:16:02 -04002068 return 0;
2069 }
Chris Mason925baed2008-06-25 16:01:30 -04002070 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002071 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002072 }
Chris Masone66f7092007-04-20 13:16:02 -04002073 return 1;
2074}
2075
Chris Mason74123bd2007-02-02 11:05:29 -05002076/*
Chris Masond352ac62008-09-29 15:18:18 -04002077 * readahead one full node of leaves, finding things that are close
2078 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002079 */
Chris Masonc8c42862009-04-03 10:14:18 -04002080static void reada_for_search(struct btrfs_root *root,
2081 struct btrfs_path *path,
2082 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002083{
Chris Mason5f39d392007-10-15 16:14:19 -04002084 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002085 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002086 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002087 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002088 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002089 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002090 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002091 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002092 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002093 u32 nr;
2094 u32 blocksize;
2095 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002096
Chris Masona6b6e752007-10-15 16:22:39 -04002097 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002098 return;
2099
Chris Mason6702ed42007-08-07 16:15:09 -04002100 if (!path->nodes[level])
2101 return;
2102
Chris Mason5f39d392007-10-15 16:14:19 -04002103 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002104
Chris Mason3c69fae2007-08-07 15:52:22 -04002105 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002106 blocksize = btrfs_level_size(root, level - 1);
2107 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 if (eb) {
2109 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002110 return;
2111 }
2112
Chris Masona7175312009-01-22 09:23:10 -05002113 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002114
Chris Mason5f39d392007-10-15 16:14:19 -04002115 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002116 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002117
Chris Masond3977122009-01-05 21:25:51 -05002118 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002119 if (direction < 0) {
2120 if (nr == 0)
2121 break;
2122 nr--;
2123 } else if (direction > 0) {
2124 nr++;
2125 if (nr >= nritems)
2126 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002127 }
Chris Mason01f46652007-12-21 16:24:26 -05002128 if (path->reada < 0 && objectid) {
2129 btrfs_node_key(node, &disk_key, nr);
2130 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2131 break;
2132 }
Chris Mason6b800532007-10-15 16:17:34 -04002133 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002134 if ((search <= target && target - search <= 65536) ||
2135 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002136 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002137 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002138 nread += blocksize;
2139 }
2140 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002141 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002142 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002143 }
2144}
Chris Mason925baed2008-06-25 16:01:30 -04002145
Josef Bacik0b088512013-06-17 14:23:02 -04002146static noinline void reada_for_balance(struct btrfs_root *root,
2147 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002148{
2149 int slot;
2150 int nritems;
2151 struct extent_buffer *parent;
2152 struct extent_buffer *eb;
2153 u64 gen;
2154 u64 block1 = 0;
2155 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002156 int blocksize;
2157
Chris Mason8c594ea2009-04-20 15:50:10 -04002158 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002159 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002160 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002161
2162 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002163 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002164 blocksize = btrfs_level_size(root, level);
2165
2166 if (slot > 0) {
2167 block1 = btrfs_node_blockptr(parent, slot - 1);
2168 gen = btrfs_node_ptr_generation(parent, slot - 1);
2169 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002170 /*
2171 * if we get -eagain from btrfs_buffer_uptodate, we
2172 * don't want to return eagain here. That will loop
2173 * forever
2174 */
2175 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002176 block1 = 0;
2177 free_extent_buffer(eb);
2178 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002179 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002180 block2 = btrfs_node_blockptr(parent, slot + 1);
2181 gen = btrfs_node_ptr_generation(parent, slot + 1);
2182 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002183 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002184 block2 = 0;
2185 free_extent_buffer(eb);
2186 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002187
Josef Bacik0b088512013-06-17 14:23:02 -04002188 if (block1)
2189 readahead_tree_block(root, block1, blocksize, 0);
2190 if (block2)
2191 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002192}
2193
2194
2195/*
Chris Masond3977122009-01-05 21:25:51 -05002196 * when we walk down the tree, it is usually safe to unlock the higher layers
2197 * in the tree. The exceptions are when our path goes through slot 0, because
2198 * operations on the tree might require changing key pointers higher up in the
2199 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002200 *
Chris Masond3977122009-01-05 21:25:51 -05002201 * callers might also have set path->keep_locks, which tells this code to keep
2202 * the lock if the path points to the last slot in the block. This is part of
2203 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002204 *
Chris Masond3977122009-01-05 21:25:51 -05002205 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2206 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002207 */
Chris Masone02119d2008-09-05 16:13:11 -04002208static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002209 int lowest_unlock, int min_write_lock_level,
2210 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002211{
2212 int i;
2213 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002214 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002215 struct extent_buffer *t;
2216
2217 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2218 if (!path->nodes[i])
2219 break;
2220 if (!path->locks[i])
2221 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002222 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002223 skip_level = i + 1;
2224 continue;
2225 }
Chris Mason051e1b92008-06-25 16:01:30 -04002226 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002227 u32 nritems;
2228 t = path->nodes[i];
2229 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002230 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002231 skip_level = i + 1;
2232 continue;
2233 }
2234 }
Chris Mason051e1b92008-06-25 16:01:30 -04002235 if (skip_level < i && i >= lowest_unlock)
2236 no_skips = 1;
2237
Chris Mason925baed2008-06-25 16:01:30 -04002238 t = path->nodes[i];
2239 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002240 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002241 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002242 if (write_lock_level &&
2243 i > min_write_lock_level &&
2244 i <= *write_lock_level) {
2245 *write_lock_level = i - 1;
2246 }
Chris Mason925baed2008-06-25 16:01:30 -04002247 }
2248 }
2249}
2250
Chris Mason3c69fae2007-08-07 15:52:22 -04002251/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002252 * This releases any locks held in the path starting at level and
2253 * going all the way up to the root.
2254 *
2255 * btrfs_search_slot will keep the lock held on higher nodes in a few
2256 * corner cases, such as COW of the block at slot zero in the node. This
2257 * ignores those rules, and it should only be called when there are no
2258 * more updates to be done higher up in the tree.
2259 */
2260noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2261{
2262 int i;
2263
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002264 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002265 return;
2266
2267 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2268 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002269 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002270 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002271 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002272 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002273 path->locks[i] = 0;
2274 }
2275}
2276
2277/*
Chris Masonc8c42862009-04-03 10:14:18 -04002278 * helper function for btrfs_search_slot. The goal is to find a block
2279 * in cache without setting the path to blocking. If we find the block
2280 * we return zero and the path is unchanged.
2281 *
2282 * If we can't find the block, we set the path blocking and do some
2283 * reada. -EAGAIN is returned and the search must be repeated.
2284 */
2285static int
2286read_block_for_search(struct btrfs_trans_handle *trans,
2287 struct btrfs_root *root, struct btrfs_path *p,
2288 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002289 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002290{
2291 u64 blocknr;
2292 u64 gen;
2293 u32 blocksize;
2294 struct extent_buffer *b = *eb_ret;
2295 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002296 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002297
2298 blocknr = btrfs_node_blockptr(b, slot);
2299 gen = btrfs_node_ptr_generation(b, slot);
2300 blocksize = btrfs_level_size(root, level - 1);
2301
2302 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002303 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002304 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002305 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2306 *eb_ret = tmp;
2307 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002308 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002309
2310 /* the pages were up to date, but we failed
2311 * the generation number check. Do a full
2312 * read for the generation number that is correct.
2313 * We must do this without dropping locks so
2314 * we can trust our generation number
2315 */
2316 btrfs_set_path_blocking(p);
2317
2318 /* now we're allowed to do a blocking uptodate check */
2319 ret = btrfs_read_buffer(tmp, gen);
2320 if (!ret) {
2321 *eb_ret = tmp;
2322 return 0;
2323 }
2324 free_extent_buffer(tmp);
2325 btrfs_release_path(p);
2326 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002327 }
2328
2329 /*
2330 * reduce lock contention at high levels
2331 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002332 * we read. Don't release the lock on the current
2333 * level because we need to walk this node to figure
2334 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002335 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002336 btrfs_unlock_up_safe(p, level + 1);
2337 btrfs_set_path_blocking(p);
2338
Chris Masoncb449212010-10-24 11:01:27 -04002339 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002340 if (p->reada)
2341 reada_for_search(root, p, level, slot, key->objectid);
2342
David Sterbab3b4aa72011-04-21 01:20:15 +02002343 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002344
2345 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002346 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002347 if (tmp) {
2348 /*
2349 * If the read above didn't mark this buffer up to date,
2350 * it will never end up being up to date. Set ret to EIO now
2351 * and give up so that our caller doesn't loop forever
2352 * on our EAGAINs.
2353 */
Chris Masonb9fab912012-05-06 07:23:47 -04002354 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002355 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002356 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002357 }
2358 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002359}
2360
2361/*
2362 * helper function for btrfs_search_slot. This does all of the checks
2363 * for node-level blocks and does any balancing required based on
2364 * the ins_len.
2365 *
2366 * If no extra work was required, zero is returned. If we had to
2367 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2368 * start over
2369 */
2370static int
2371setup_nodes_for_search(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002373 struct extent_buffer *b, int level, int ins_len,
2374 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002375{
2376 int ret;
2377 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2378 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2379 int sret;
2380
Chris Masonbd681512011-07-16 15:23:14 -04002381 if (*write_lock_level < level + 1) {
2382 *write_lock_level = level + 1;
2383 btrfs_release_path(p);
2384 goto again;
2385 }
2386
Chris Masonc8c42862009-04-03 10:14:18 -04002387 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002388 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002389 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002390 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002391
2392 BUG_ON(sret > 0);
2393 if (sret) {
2394 ret = sret;
2395 goto done;
2396 }
2397 b = p->nodes[level];
2398 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002399 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002400 int sret;
2401
Chris Masonbd681512011-07-16 15:23:14 -04002402 if (*write_lock_level < level + 1) {
2403 *write_lock_level = level + 1;
2404 btrfs_release_path(p);
2405 goto again;
2406 }
2407
Chris Masonc8c42862009-04-03 10:14:18 -04002408 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002409 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002410 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002411 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002412
2413 if (sret) {
2414 ret = sret;
2415 goto done;
2416 }
2417 b = p->nodes[level];
2418 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002419 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002420 goto again;
2421 }
2422 BUG_ON(btrfs_header_nritems(b) == 1);
2423 }
2424 return 0;
2425
2426again:
2427 ret = -EAGAIN;
2428done:
2429 return ret;
2430}
2431
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002432static void key_search_validate(struct extent_buffer *b,
2433 struct btrfs_key *key,
2434 int level)
2435{
2436#ifdef CONFIG_BTRFS_ASSERT
2437 struct btrfs_disk_key disk_key;
2438
2439 btrfs_cpu_key_to_disk(&disk_key, key);
2440
2441 if (level == 0)
2442 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2443 offsetof(struct btrfs_leaf, items[0].key),
2444 sizeof(disk_key)));
2445 else
2446 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2447 offsetof(struct btrfs_node, ptrs[0].key),
2448 sizeof(disk_key)));
2449#endif
2450}
2451
2452static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2453 int level, int *prev_cmp, int *slot)
2454{
2455 if (*prev_cmp != 0) {
2456 *prev_cmp = bin_search(b, key, level, slot);
2457 return *prev_cmp;
2458 }
2459
2460 key_search_validate(b, key, level);
2461 *slot = 0;
2462
2463 return 0;
2464}
2465
Chris Masonc8c42862009-04-03 10:14:18 -04002466/*
Chris Mason74123bd2007-02-02 11:05:29 -05002467 * look for key in the tree. path is filled in with nodes along the way
2468 * if key is found, we return zero and you can find the item in the leaf
2469 * level of the path (level 0)
2470 *
2471 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002472 * be inserted, and 1 is returned. If there are other errors during the
2473 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002474 *
2475 * if ins_len > 0, nodes and leaves will be split as we walk down the
2476 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2477 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002478 */
Chris Masone089f052007-03-16 16:20:31 -04002479int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2480 *root, struct btrfs_key *key, struct btrfs_path *p, int
2481 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482{
Chris Mason5f39d392007-10-15 16:14:19 -04002483 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002484 int slot;
2485 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002486 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002487 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002488 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002489 int root_lock;
2490 /* everything at write_lock_level or lower must be write locked */
2491 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002492 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002493 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002494 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002495
Chris Mason6702ed42007-08-07 16:15:09 -04002496 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002497 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002498 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002499
Chris Masonbd681512011-07-16 15:23:14 -04002500 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002501 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002502
Chris Masonbd681512011-07-16 15:23:14 -04002503 /* when we are removing items, we might have to go up to level
2504 * two as we update tree pointers Make sure we keep write
2505 * for those levels as well
2506 */
2507 write_lock_level = 2;
2508 } else if (ins_len > 0) {
2509 /*
2510 * for inserting items, make sure we have a write lock on
2511 * level 1 so we can update keys
2512 */
2513 write_lock_level = 1;
2514 }
2515
2516 if (!cow)
2517 write_lock_level = -1;
2518
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002519 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002520 write_lock_level = BTRFS_MAX_LEVEL;
2521
Chris Masonf7c79f32012-03-19 15:54:38 -04002522 min_write_lock_level = write_lock_level;
2523
Chris Masonbb803952007-03-01 12:04:21 -05002524again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002525 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002526 /*
2527 * we try very hard to do read locks on the root
2528 */
2529 root_lock = BTRFS_READ_LOCK;
2530 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002531 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002532 /*
2533 * the commit roots are read only
2534 * so we always do read locks
2535 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002536 b = root->commit_root;
2537 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002538 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002539 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002540 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002542 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002544 level = btrfs_header_level(b);
2545 } else {
2546 /* we don't know the level of the root node
2547 * until we actually have it read locked
2548 */
2549 b = btrfs_read_lock_root_node(root);
2550 level = btrfs_header_level(b);
2551 if (level <= write_lock_level) {
2552 /* whoops, must trade for write lock */
2553 btrfs_tree_read_unlock(b);
2554 free_extent_buffer(b);
2555 b = btrfs_lock_root_node(root);
2556 root_lock = BTRFS_WRITE_LOCK;
2557
2558 /* the level might have changed, check again */
2559 level = btrfs_header_level(b);
2560 }
2561 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002562 }
Chris Masonbd681512011-07-16 15:23:14 -04002563 p->nodes[level] = b;
2564 if (!p->skip_locking)
2565 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002566
Chris Masoneb60cea2007-02-02 09:18:22 -05002567 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002568 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002569
2570 /*
2571 * setup the path here so we can release it under lock
2572 * contention with the cow code
2573 */
Chris Mason02217ed2007-03-02 16:08:05 -05002574 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002575 /*
2576 * if we don't really need to cow this block
2577 * then we don't want to set the path blocking,
2578 * so we test it here
2579 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002580 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002581 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002582
Chris Masonb4ce94d2009-02-04 09:25:08 -05002583 btrfs_set_path_blocking(p);
2584
Chris Masonbd681512011-07-16 15:23:14 -04002585 /*
2586 * must have write locks on this node and the
2587 * parent
2588 */
Josef Bacik5124e002012-11-07 13:44:13 -05002589 if (level > write_lock_level ||
2590 (level + 1 > write_lock_level &&
2591 level + 1 < BTRFS_MAX_LEVEL &&
2592 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002593 write_lock_level = level + 1;
2594 btrfs_release_path(p);
2595 goto again;
2596 }
2597
Yan Zheng33c66f42009-07-22 09:59:00 -04002598 err = btrfs_cow_block(trans, root, b,
2599 p->nodes[level + 1],
2600 p->slots[level + 1], &b);
2601 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002602 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002603 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002604 }
Chris Mason02217ed2007-03-02 16:08:05 -05002605 }
Chris Mason65b51a02008-08-01 15:11:20 -04002606cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002607 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002608
Chris Masoneb60cea2007-02-02 09:18:22 -05002609 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002610 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002611
2612 /*
2613 * we have a lock on b and as long as we aren't changing
2614 * the tree, there is no way to for the items in b to change.
2615 * It is safe to drop the lock on our parent before we
2616 * go through the expensive btree search on b.
2617 *
2618 * If cow is true, then we might be changing slot zero,
2619 * which may require changing the parent. So, we can't
2620 * drop the lock until after we know which slot we're
2621 * operating on.
2622 */
2623 if (!cow)
2624 btrfs_unlock_up_safe(p, level + 1);
2625
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002626 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002627
Chris Mason5f39d392007-10-15 16:14:19 -04002628 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002629 int dec = 0;
2630 if (ret && slot > 0) {
2631 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002632 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002633 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002634 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002635 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002636 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002637 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002638 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002639 if (err) {
2640 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002641 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002642 }
Chris Masonc8c42862009-04-03 10:14:18 -04002643 b = p->nodes[level];
2644 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002645
Chris Masonbd681512011-07-16 15:23:14 -04002646 /*
2647 * slot 0 is special, if we change the key
2648 * we have to update the parent pointer
2649 * which means we must have a write lock
2650 * on the parent
2651 */
2652 if (slot == 0 && cow &&
2653 write_lock_level < level + 1) {
2654 write_lock_level = level + 1;
2655 btrfs_release_path(p);
2656 goto again;
2657 }
2658
Chris Masonf7c79f32012-03-19 15:54:38 -04002659 unlock_up(p, level, lowest_unlock,
2660 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002661
Chris Mason925baed2008-06-25 16:01:30 -04002662 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002663 if (dec)
2664 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002665 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002666 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002667
Yan Zheng33c66f42009-07-22 09:59:00 -04002668 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002669 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002670 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002671 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002672 if (err) {
2673 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002674 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002675 }
Chris Mason76a05b32009-05-14 13:24:30 -04002676
Chris Masonb4ce94d2009-02-04 09:25:08 -05002677 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002678 level = btrfs_header_level(b);
2679 if (level <= write_lock_level) {
2680 err = btrfs_try_tree_write_lock(b);
2681 if (!err) {
2682 btrfs_set_path_blocking(p);
2683 btrfs_tree_lock(b);
2684 btrfs_clear_path_blocking(p, b,
2685 BTRFS_WRITE_LOCK);
2686 }
2687 p->locks[level] = BTRFS_WRITE_LOCK;
2688 } else {
2689 err = btrfs_try_tree_read_lock(b);
2690 if (!err) {
2691 btrfs_set_path_blocking(p);
2692 btrfs_tree_read_lock(b);
2693 btrfs_clear_path_blocking(p, b,
2694 BTRFS_READ_LOCK);
2695 }
2696 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002697 }
Chris Masonbd681512011-07-16 15:23:14 -04002698 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002699 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002700 } else {
2701 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002702 if (ins_len > 0 &&
2703 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002704 if (write_lock_level < 1) {
2705 write_lock_level = 1;
2706 btrfs_release_path(p);
2707 goto again;
2708 }
2709
Chris Masonb4ce94d2009-02-04 09:25:08 -05002710 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002711 err = split_leaf(trans, root, key,
2712 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002713 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002714
Yan Zheng33c66f42009-07-22 09:59:00 -04002715 BUG_ON(err > 0);
2716 if (err) {
2717 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002718 goto done;
2719 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002720 }
Chris Mason459931e2008-12-10 09:10:46 -05002721 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002722 unlock_up(p, level, lowest_unlock,
2723 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002724 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002725 }
2726 }
Chris Mason65b51a02008-08-01 15:11:20 -04002727 ret = 1;
2728done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002729 /*
2730 * we don't really know what they plan on doing with the path
2731 * from here on, so for now just mark it as blocking
2732 */
Chris Masonb9473432009-03-13 11:00:37 -04002733 if (!p->leave_spinning)
2734 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002735 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002736 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002737 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002738}
2739
Chris Mason74123bd2007-02-02 11:05:29 -05002740/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002741 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2742 * current state of the tree together with the operations recorded in the tree
2743 * modification log to search for the key in a previous version of this tree, as
2744 * denoted by the time_seq parameter.
2745 *
2746 * Naturally, there is no support for insert, delete or cow operations.
2747 *
2748 * The resulting path and return value will be set up as if we called
2749 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2750 */
2751int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2752 struct btrfs_path *p, u64 time_seq)
2753{
2754 struct extent_buffer *b;
2755 int slot;
2756 int ret;
2757 int err;
2758 int level;
2759 int lowest_unlock = 1;
2760 u8 lowest_level = 0;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002761 int prev_cmp;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002762
2763 lowest_level = p->lowest_level;
2764 WARN_ON(p->nodes[0] != NULL);
2765
2766 if (p->search_commit_root) {
2767 BUG_ON(time_seq);
2768 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2769 }
2770
2771again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002772 prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002773 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002774 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002775 p->locks[level] = BTRFS_READ_LOCK;
2776
2777 while (b) {
2778 level = btrfs_header_level(b);
2779 p->nodes[level] = b;
2780 btrfs_clear_path_blocking(p, NULL, 0);
2781
2782 /*
2783 * we have a lock on b and as long as we aren't changing
2784 * the tree, there is no way to for the items in b to change.
2785 * It is safe to drop the lock on our parent before we
2786 * go through the expensive btree search on b.
2787 */
2788 btrfs_unlock_up_safe(p, level + 1);
2789
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002790 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002791
2792 if (level != 0) {
2793 int dec = 0;
2794 if (ret && slot > 0) {
2795 dec = 1;
2796 slot -= 1;
2797 }
2798 p->slots[level] = slot;
2799 unlock_up(p, level, lowest_unlock, 0, NULL);
2800
2801 if (level == lowest_level) {
2802 if (dec)
2803 p->slots[level]++;
2804 goto done;
2805 }
2806
2807 err = read_block_for_search(NULL, root, p, &b, level,
2808 slot, key, time_seq);
2809 if (err == -EAGAIN)
2810 goto again;
2811 if (err) {
2812 ret = err;
2813 goto done;
2814 }
2815
2816 level = btrfs_header_level(b);
2817 err = btrfs_try_tree_read_lock(b);
2818 if (!err) {
2819 btrfs_set_path_blocking(p);
2820 btrfs_tree_read_lock(b);
2821 btrfs_clear_path_blocking(p, b,
2822 BTRFS_READ_LOCK);
2823 }
Josef Bacik9ec72672013-08-07 16:57:23 -04002824 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04002825 if (!b) {
2826 ret = -ENOMEM;
2827 goto done;
2828 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002829 p->locks[level] = BTRFS_READ_LOCK;
2830 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002831 } else {
2832 p->slots[level] = slot;
2833 unlock_up(p, level, lowest_unlock, 0, NULL);
2834 goto done;
2835 }
2836 }
2837 ret = 1;
2838done:
2839 if (!p->leave_spinning)
2840 btrfs_set_path_blocking(p);
2841 if (ret < 0)
2842 btrfs_release_path(p);
2843
2844 return ret;
2845}
2846
2847/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002848 * helper to use instead of search slot if no exact match is needed but
2849 * instead the next or previous item should be returned.
2850 * When find_higher is true, the next higher item is returned, the next lower
2851 * otherwise.
2852 * When return_any and find_higher are both true, and no higher item is found,
2853 * return the next lower instead.
2854 * When return_any is true and find_higher is false, and no lower item is found,
2855 * return the next higher instead.
2856 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2857 * < 0 on error
2858 */
2859int btrfs_search_slot_for_read(struct btrfs_root *root,
2860 struct btrfs_key *key, struct btrfs_path *p,
2861 int find_higher, int return_any)
2862{
2863 int ret;
2864 struct extent_buffer *leaf;
2865
2866again:
2867 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2868 if (ret <= 0)
2869 return ret;
2870 /*
2871 * a return value of 1 means the path is at the position where the
2872 * item should be inserted. Normally this is the next bigger item,
2873 * but in case the previous item is the last in a leaf, path points
2874 * to the first free slot in the previous leaf, i.e. at an invalid
2875 * item.
2876 */
2877 leaf = p->nodes[0];
2878
2879 if (find_higher) {
2880 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2881 ret = btrfs_next_leaf(root, p);
2882 if (ret <= 0)
2883 return ret;
2884 if (!return_any)
2885 return 1;
2886 /*
2887 * no higher item found, return the next
2888 * lower instead
2889 */
2890 return_any = 0;
2891 find_higher = 0;
2892 btrfs_release_path(p);
2893 goto again;
2894 }
2895 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002896 if (p->slots[0] == 0) {
2897 ret = btrfs_prev_leaf(root, p);
2898 if (ret < 0)
2899 return ret;
2900 if (!ret) {
2901 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2902 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002903 }
Arne Jansene6793762011-09-13 11:18:10 +02002904 if (!return_any)
2905 return 1;
2906 /*
2907 * no lower item found, return the next
2908 * higher instead
2909 */
2910 return_any = 0;
2911 find_higher = 1;
2912 btrfs_release_path(p);
2913 goto again;
2914 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002915 --p->slots[0];
2916 }
2917 }
2918 return 0;
2919}
2920
2921/*
Chris Mason74123bd2007-02-02 11:05:29 -05002922 * adjust the pointers going up the tree, starting at level
2923 * making sure the right key of each node is points to 'key'.
2924 * This is used after shifting pointers to the left, so it stops
2925 * fixing up pointers when a given leaf/node is not in slot 0 of the
2926 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002927 *
Chris Mason74123bd2007-02-02 11:05:29 -05002928 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002929static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002930 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002931{
2932 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002933 struct extent_buffer *t;
2934
Chris Mason234b63a2007-03-13 10:46:10 -04002935 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002936 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002937 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002938 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002939 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002940 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002941 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002942 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002943 if (tslot != 0)
2944 break;
2945 }
2946}
2947
Chris Mason74123bd2007-02-02 11:05:29 -05002948/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002949 * update item key.
2950 *
2951 * This function isn't completely safe. It's the caller's responsibility
2952 * that the new key won't break the order
2953 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002954void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002955 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002956{
2957 struct btrfs_disk_key disk_key;
2958 struct extent_buffer *eb;
2959 int slot;
2960
2961 eb = path->nodes[0];
2962 slot = path->slots[0];
2963 if (slot > 0) {
2964 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002965 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002966 }
2967 if (slot < btrfs_header_nritems(eb) - 1) {
2968 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002969 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002970 }
2971
2972 btrfs_cpu_key_to_disk(&disk_key, new_key);
2973 btrfs_set_item_key(eb, &disk_key, slot);
2974 btrfs_mark_buffer_dirty(eb);
2975 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002976 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002977}
2978
2979/*
Chris Mason74123bd2007-02-02 11:05:29 -05002980 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002981 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002982 *
2983 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2984 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002985 */
Chris Mason98ed5172008-01-03 10:01:48 -05002986static int push_node_left(struct btrfs_trans_handle *trans,
2987 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002988 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002989{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002990 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002991 int src_nritems;
2992 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002993 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002994
Chris Mason5f39d392007-10-15 16:14:19 -04002995 src_nritems = btrfs_header_nritems(src);
2996 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002997 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002998 WARN_ON(btrfs_header_generation(src) != trans->transid);
2999 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003000
Chris Masonbce4eae2008-04-24 14:42:46 -04003001 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003002 return 1;
3003
Chris Masond3977122009-01-05 21:25:51 -05003004 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003005 return 1;
3006
Chris Masonbce4eae2008-04-24 14:42:46 -04003007 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003008 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003009 if (push_items < src_nritems) {
3010 /* leave at least 8 pointers in the node if
3011 * we aren't going to empty it
3012 */
3013 if (src_nritems - push_items < 8) {
3014 if (push_items <= 8)
3015 return 1;
3016 push_items -= 8;
3017 }
3018 }
3019 } else
3020 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003021
Jan Schmidtf2304752012-05-26 11:43:17 +02003022 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003023 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003024 copy_extent_buffer(dst, src,
3025 btrfs_node_key_ptr_offset(dst_nritems),
3026 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003027 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003028
Chris Masonbb803952007-03-01 12:04:21 -05003029 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003030 /*
3031 * don't call tree_mod_log_eb_move here, key removal was already
3032 * fully logged by tree_mod_log_eb_copy above.
3033 */
Chris Mason5f39d392007-10-15 16:14:19 -04003034 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3035 btrfs_node_key_ptr_offset(push_items),
3036 (src_nritems - push_items) *
3037 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003038 }
Chris Mason5f39d392007-10-15 16:14:19 -04003039 btrfs_set_header_nritems(src, src_nritems - push_items);
3040 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3041 btrfs_mark_buffer_dirty(src);
3042 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003043
Chris Masonbb803952007-03-01 12:04:21 -05003044 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003045}
3046
Chris Mason97571fd2007-02-24 13:39:08 -05003047/*
Chris Mason79f95c82007-03-01 15:16:26 -05003048 * try to push data from one node into the next node right in the
3049 * tree.
3050 *
3051 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3052 * error, and > 0 if there was no room in the right hand block.
3053 *
3054 * this will only push up to 1/2 the contents of the left node over
3055 */
Chris Mason5f39d392007-10-15 16:14:19 -04003056static int balance_node_right(struct btrfs_trans_handle *trans,
3057 struct btrfs_root *root,
3058 struct extent_buffer *dst,
3059 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003060{
Chris Mason79f95c82007-03-01 15:16:26 -05003061 int push_items = 0;
3062 int max_push;
3063 int src_nritems;
3064 int dst_nritems;
3065 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003066
Chris Mason7bb86312007-12-11 09:25:06 -05003067 WARN_ON(btrfs_header_generation(src) != trans->transid);
3068 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3069
Chris Mason5f39d392007-10-15 16:14:19 -04003070 src_nritems = btrfs_header_nritems(src);
3071 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003072 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003073 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003074 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003075
Chris Masond3977122009-01-05 21:25:51 -05003076 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003077 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003078
3079 max_push = src_nritems / 2 + 1;
3080 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003081 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003082 return 1;
Yan252c38f2007-08-29 09:11:44 -04003083
Chris Mason79f95c82007-03-01 15:16:26 -05003084 if (max_push < push_items)
3085 push_items = max_push;
3086
Jan Schmidtf2304752012-05-26 11:43:17 +02003087 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003088 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3089 btrfs_node_key_ptr_offset(0),
3090 (dst_nritems) *
3091 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003092
Jan Schmidtf2304752012-05-26 11:43:17 +02003093 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003094 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003095 copy_extent_buffer(dst, src,
3096 btrfs_node_key_ptr_offset(0),
3097 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003098 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003099
Chris Mason5f39d392007-10-15 16:14:19 -04003100 btrfs_set_header_nritems(src, src_nritems - push_items);
3101 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003102
Chris Mason5f39d392007-10-15 16:14:19 -04003103 btrfs_mark_buffer_dirty(src);
3104 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003105
Chris Mason79f95c82007-03-01 15:16:26 -05003106 return ret;
3107}
3108
3109/*
Chris Mason97571fd2007-02-24 13:39:08 -05003110 * helper function to insert a new root level in the tree.
3111 * A new node is allocated, and a single item is inserted to
3112 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003113 *
3114 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003115 */
Chris Masond3977122009-01-05 21:25:51 -05003116static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003117 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003118 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003119{
Chris Mason7bb86312007-12-11 09:25:06 -05003120 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003121 struct extent_buffer *lower;
3122 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003123 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003124 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003125
3126 BUG_ON(path->nodes[level]);
3127 BUG_ON(path->nodes[level-1] != root->node);
3128
Chris Mason7bb86312007-12-11 09:25:06 -05003129 lower = path->nodes[level-1];
3130 if (level == 1)
3131 btrfs_item_key(lower, &lower_key, 0);
3132 else
3133 btrfs_node_key(lower, &lower_key, 0);
3134
Zheng Yan31840ae2008-09-23 13:14:14 -04003135 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003137 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003138 if (IS_ERR(c))
3139 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003140
Yan, Zhengf0486c62010-05-16 10:46:25 -04003141 root_add_used(root, root->nodesize);
3142
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003143 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003144 btrfs_set_header_nritems(c, 1);
3145 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003146 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003147 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003148 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003149 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003150
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +02003151 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(c),
Chris Mason5f39d392007-10-15 16:14:19 -04003152 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003153
3154 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003155 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003156
Chris Mason5f39d392007-10-15 16:14:19 -04003157 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003158 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003159 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003160 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003161
3162 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003163
3164 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003165
Chris Mason925baed2008-06-25 16:01:30 -04003166 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003167 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003168 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003169
3170 /* the super has an extra ref to root->node */
3171 free_extent_buffer(old);
3172
Chris Mason0b86a832008-03-24 15:01:56 -04003173 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003174 extent_buffer_get(c);
3175 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003176 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003177 path->slots[level] = 0;
3178 return 0;
3179}
3180
Chris Mason74123bd2007-02-02 11:05:29 -05003181/*
3182 * worker function to insert a single pointer in a node.
3183 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003184 *
Chris Mason74123bd2007-02-02 11:05:29 -05003185 * slot and level indicate where you want the key to go, and
3186 * blocknr is the block the key points to.
3187 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003188static void insert_ptr(struct btrfs_trans_handle *trans,
3189 struct btrfs_root *root, struct btrfs_path *path,
3190 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003191 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003192{
Chris Mason5f39d392007-10-15 16:14:19 -04003193 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003194 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003195 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003196
3197 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003198 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003199 lower = path->nodes[level];
3200 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003201 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003202 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003203 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003204 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003205 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3206 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003207 memmove_extent_buffer(lower,
3208 btrfs_node_key_ptr_offset(slot + 1),
3209 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003210 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003211 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003212 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003213 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003214 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003215 BUG_ON(ret < 0);
3216 }
Chris Mason5f39d392007-10-15 16:14:19 -04003217 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003218 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003219 WARN_ON(trans->transid == 0);
3220 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003221 btrfs_set_header_nritems(lower, nritems + 1);
3222 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003223}
3224
Chris Mason97571fd2007-02-24 13:39:08 -05003225/*
3226 * split the node at the specified level in path in two.
3227 * The path is corrected to point to the appropriate node after the split
3228 *
3229 * Before splitting this tries to make some room in the node by pushing
3230 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003231 *
3232 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003233 */
Chris Masone02119d2008-09-05 16:13:11 -04003234static noinline int split_node(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root,
3236 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003237{
Chris Mason5f39d392007-10-15 16:14:19 -04003238 struct extent_buffer *c;
3239 struct extent_buffer *split;
3240 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003241 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003242 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003243 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003244
Chris Mason5f39d392007-10-15 16:14:19 -04003245 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003246 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003247 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003248 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003249 * trying to split the root, lets make a new one
3250 *
Liu Bofdd99c72013-05-22 12:06:51 +00003251 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003252 * insert_new_root, because that root buffer will be kept as a
3253 * normal node. We are going to log removal of half of the
3254 * elements below with tree_mod_log_eb_copy. We're holding a
3255 * tree lock on the buffer, which is why we cannot race with
3256 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003257 */
Liu Bofdd99c72013-05-22 12:06:51 +00003258 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003259 if (ret)
3260 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003261 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003262 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003263 c = path->nodes[level];
3264 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003265 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003266 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003267 if (ret < 0)
3268 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003269 }
Chris Masone66f7092007-04-20 13:16:02 -04003270
Chris Mason5f39d392007-10-15 16:14:19 -04003271 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003272 mid = (c_nritems + 1) / 2;
3273 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003274
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003275 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003276 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003277 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003278 if (IS_ERR(split))
3279 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003280
Yan, Zhengf0486c62010-05-16 10:46:25 -04003281 root_add_used(root, root->nodesize);
3282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003283 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003284 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003285 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003286 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003287 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003288 btrfs_set_header_owner(split, root->root_key.objectid);
3289 write_extent_buffer(split, root->fs_info->fsid,
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +02003290 btrfs_header_fsid(split), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003291 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003292 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003293 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003294
Jan Schmidt90f8d622013-04-13 13:19:53 +00003295 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003296 copy_extent_buffer(split, c,
3297 btrfs_node_key_ptr_offset(0),
3298 btrfs_node_key_ptr_offset(mid),
3299 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3300 btrfs_set_header_nritems(split, c_nritems - mid);
3301 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003302 ret = 0;
3303
Chris Mason5f39d392007-10-15 16:14:19 -04003304 btrfs_mark_buffer_dirty(c);
3305 btrfs_mark_buffer_dirty(split);
3306
Jeff Mahoney143bede2012-03-01 14:56:26 +01003307 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003308 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003309
Chris Mason5de08d72007-02-24 06:24:44 -05003310 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003311 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003312 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003313 free_extent_buffer(c);
3314 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003315 path->slots[level + 1] += 1;
3316 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003317 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003318 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003319 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003320 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003321}
3322
Chris Mason74123bd2007-02-02 11:05:29 -05003323/*
3324 * how many bytes are required to store the items in a leaf. start
3325 * and nr indicate which items in the leaf to check. This totals up the
3326 * space used both by the item structs and the item data
3327 */
Chris Mason5f39d392007-10-15 16:14:19 -04003328static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003329{
Josef Bacik41be1f32012-10-15 13:43:18 -04003330 struct btrfs_item *start_item;
3331 struct btrfs_item *end_item;
3332 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003333 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003334 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003335 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003336
3337 if (!nr)
3338 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003339 btrfs_init_map_token(&token);
3340 start_item = btrfs_item_nr(l, start);
3341 end_item = btrfs_item_nr(l, end);
3342 data_len = btrfs_token_item_offset(l, start_item, &token) +
3343 btrfs_token_item_size(l, start_item, &token);
3344 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003345 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003346 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003347 return data_len;
3348}
3349
Chris Mason74123bd2007-02-02 11:05:29 -05003350/*
Chris Masond4dbff92007-04-04 14:08:15 -04003351 * The space between the end of the leaf items and
3352 * the start of the leaf data. IOW, how much room
3353 * the leaf has left for both items and data
3354 */
Chris Masond3977122009-01-05 21:25:51 -05003355noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003356 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003357{
Chris Mason5f39d392007-10-15 16:14:19 -04003358 int nritems = btrfs_header_nritems(leaf);
3359 int ret;
3360 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3361 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003362 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3363 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003364 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003365 leaf_space_used(leaf, 0, nritems), nritems);
3366 }
3367 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003368}
3369
Chris Mason99d8f832010-07-07 10:51:48 -04003370/*
3371 * min slot controls the lowest index we're willing to push to the
3372 * right. We'll push up to and including min_slot, but no lower
3373 */
Chris Mason44871b12009-03-13 10:04:31 -04003374static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3375 struct btrfs_root *root,
3376 struct btrfs_path *path,
3377 int data_size, int empty,
3378 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003379 int free_space, u32 left_nritems,
3380 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003381{
Chris Mason5f39d392007-10-15 16:14:19 -04003382 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003383 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003384 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003385 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003386 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003387 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003388 int push_space = 0;
3389 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003390 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003391 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003392 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003393 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003394 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003395
Chris Masoncfed81a2012-03-03 07:40:03 -05003396 btrfs_init_map_token(&token);
3397
Chris Mason34a38212007-11-07 13:31:03 -05003398 if (empty)
3399 nr = 0;
3400 else
Chris Mason99d8f832010-07-07 10:51:48 -04003401 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003402
Zheng Yan31840ae2008-09-23 13:14:14 -04003403 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003404 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003405
Chris Mason44871b12009-03-13 10:04:31 -04003406 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003407 i = left_nritems - 1;
3408 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003409 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003410
Zheng Yan31840ae2008-09-23 13:14:14 -04003411 if (!empty && push_items > 0) {
3412 if (path->slots[0] > i)
3413 break;
3414 if (path->slots[0] == i) {
3415 int space = btrfs_leaf_free_space(root, left);
3416 if (space + push_space * 2 > free_space)
3417 break;
3418 }
3419 }
3420
Chris Mason00ec4c52007-02-24 12:47:20 -05003421 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003422 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003423
Chris Masondb945352007-10-15 16:15:53 -04003424 this_item_size = btrfs_item_size(left, item);
3425 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003426 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003427
Chris Mason00ec4c52007-02-24 12:47:20 -05003428 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003429 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003430 if (i == 0)
3431 break;
3432 i--;
Chris Masondb945352007-10-15 16:15:53 -04003433 }
Chris Mason5f39d392007-10-15 16:14:19 -04003434
Chris Mason925baed2008-06-25 16:01:30 -04003435 if (push_items == 0)
3436 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003437
Julia Lawall6c1500f2012-11-03 20:30:18 +00003438 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003439
Chris Mason00ec4c52007-02-24 12:47:20 -05003440 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003441 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003442
Chris Mason5f39d392007-10-15 16:14:19 -04003443 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003444 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003445
Chris Mason00ec4c52007-02-24 12:47:20 -05003446 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003447 data_end = leaf_data_end(root, right);
3448 memmove_extent_buffer(right,
3449 btrfs_leaf_data(right) + data_end - push_space,
3450 btrfs_leaf_data(right) + data_end,
3451 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3452
Chris Mason00ec4c52007-02-24 12:47:20 -05003453 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003454 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003455 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3456 btrfs_leaf_data(left) + leaf_data_end(root, left),
3457 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003458
3459 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3460 btrfs_item_nr_offset(0),
3461 right_nritems * sizeof(struct btrfs_item));
3462
Chris Mason00ec4c52007-02-24 12:47:20 -05003463 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003464 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3465 btrfs_item_nr_offset(left_nritems - push_items),
3466 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003467
3468 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003469 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003470 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003471 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003472 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003473 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003474 push_space -= btrfs_token_item_size(right, item, &token);
3475 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003476 }
3477
Chris Mason7518a232007-03-12 12:01:18 -04003478 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003479 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003480
Chris Mason34a38212007-11-07 13:31:03 -05003481 if (left_nritems)
3482 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003483 else
3484 clean_tree_block(trans, root, left);
3485
Chris Mason5f39d392007-10-15 16:14:19 -04003486 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 btrfs_item_key(right, &disk_key, 0);
3489 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003490 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003491
Chris Mason00ec4c52007-02-24 12:47:20 -05003492 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003493 if (path->slots[0] >= left_nritems) {
3494 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003495 if (btrfs_header_nritems(path->nodes[0]) == 0)
3496 clean_tree_block(trans, root, path->nodes[0]);
3497 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003498 free_extent_buffer(path->nodes[0]);
3499 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003500 path->slots[1] += 1;
3501 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003502 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003503 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003504 }
3505 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003506
3507out_unlock:
3508 btrfs_tree_unlock(right);
3509 free_extent_buffer(right);
3510 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003511}
Chris Mason925baed2008-06-25 16:01:30 -04003512
Chris Mason00ec4c52007-02-24 12:47:20 -05003513/*
Chris Mason44871b12009-03-13 10:04:31 -04003514 * push some data in the path leaf to the right, trying to free up at
3515 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3516 *
3517 * returns 1 if the push failed because the other node didn't have enough
3518 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003519 *
3520 * this will push starting from min_slot to the end of the leaf. It won't
3521 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003522 */
3523static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003524 *root, struct btrfs_path *path,
3525 int min_data_size, int data_size,
3526 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003527{
3528 struct extent_buffer *left = path->nodes[0];
3529 struct extent_buffer *right;
3530 struct extent_buffer *upper;
3531 int slot;
3532 int free_space;
3533 u32 left_nritems;
3534 int ret;
3535
3536 if (!path->nodes[1])
3537 return 1;
3538
3539 slot = path->slots[1];
3540 upper = path->nodes[1];
3541 if (slot >= btrfs_header_nritems(upper) - 1)
3542 return 1;
3543
3544 btrfs_assert_tree_locked(path->nodes[1]);
3545
3546 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003547 if (right == NULL)
3548 return 1;
3549
Chris Mason44871b12009-03-13 10:04:31 -04003550 btrfs_tree_lock(right);
3551 btrfs_set_lock_blocking(right);
3552
3553 free_space = btrfs_leaf_free_space(root, right);
3554 if (free_space < data_size)
3555 goto out_unlock;
3556
3557 /* cow and double check */
3558 ret = btrfs_cow_block(trans, root, right, upper,
3559 slot + 1, &right);
3560 if (ret)
3561 goto out_unlock;
3562
3563 free_space = btrfs_leaf_free_space(root, right);
3564 if (free_space < data_size)
3565 goto out_unlock;
3566
3567 left_nritems = btrfs_header_nritems(left);
3568 if (left_nritems == 0)
3569 goto out_unlock;
3570
Chris Mason99d8f832010-07-07 10:51:48 -04003571 return __push_leaf_right(trans, root, path, min_data_size, empty,
3572 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003573out_unlock:
3574 btrfs_tree_unlock(right);
3575 free_extent_buffer(right);
3576 return 1;
3577}
3578
3579/*
Chris Mason74123bd2007-02-02 11:05:29 -05003580 * push some data in the path leaf to the left, trying to free up at
3581 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003582 *
3583 * max_slot can put a limit on how far into the leaf we'll push items. The
3584 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3585 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003586 */
Chris Mason44871b12009-03-13 10:04:31 -04003587static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3588 struct btrfs_root *root,
3589 struct btrfs_path *path, int data_size,
3590 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003591 int free_space, u32 right_nritems,
3592 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593{
Chris Mason5f39d392007-10-15 16:14:19 -04003594 struct btrfs_disk_key disk_key;
3595 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003596 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 int push_space = 0;
3598 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003599 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003600 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003601 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003602 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003603 u32 this_item_size;
3604 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003605 struct btrfs_map_token token;
3606
3607 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003608
Chris Mason34a38212007-11-07 13:31:03 -05003609 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003610 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003611 else
Chris Mason99d8f832010-07-07 10:51:48 -04003612 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003613
3614 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003615 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003616
Zheng Yan31840ae2008-09-23 13:14:14 -04003617 if (!empty && push_items > 0) {
3618 if (path->slots[0] < i)
3619 break;
3620 if (path->slots[0] == i) {
3621 int space = btrfs_leaf_free_space(root, right);
3622 if (space + push_space * 2 > free_space)
3623 break;
3624 }
3625 }
3626
Chris Masonbe0e5c02007-01-26 15:51:26 -05003627 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003628 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003629
3630 this_item_size = btrfs_item_size(right, item);
3631 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632 break;
Chris Masondb945352007-10-15 16:15:53 -04003633
Chris Masonbe0e5c02007-01-26 15:51:26 -05003634 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003635 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003636 }
Chris Masondb945352007-10-15 16:15:53 -04003637
Chris Masonbe0e5c02007-01-26 15:51:26 -05003638 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003639 ret = 1;
3640 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641 }
Chris Mason34a38212007-11-07 13:31:03 -05003642 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003643 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003644
Chris Masonbe0e5c02007-01-26 15:51:26 -05003645 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003646 copy_extent_buffer(left, right,
3647 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3648 btrfs_item_nr_offset(0),
3649 push_items * sizeof(struct btrfs_item));
3650
Chris Mason123abc82007-03-14 14:14:43 -04003651 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003652 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003653
3654 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003655 leaf_data_end(root, left) - push_space,
3656 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003657 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003658 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003659 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003660 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003661
Chris Masondb945352007-10-15 16:15:53 -04003662 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003663 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003664 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003665
Chris Mason5f39d392007-10-15 16:14:19 -04003666 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003667
Chris Masoncfed81a2012-03-03 07:40:03 -05003668 ioff = btrfs_token_item_offset(left, item, &token);
3669 btrfs_set_token_item_offset(left, item,
3670 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3671 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003672 }
Chris Mason5f39d392007-10-15 16:14:19 -04003673 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003674
3675 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003676 if (push_items > right_nritems)
3677 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003678 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003679
Chris Mason34a38212007-11-07 13:31:03 -05003680 if (push_items < right_nritems) {
3681 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3682 leaf_data_end(root, right);
3683 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3684 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3685 btrfs_leaf_data(right) +
3686 leaf_data_end(root, right), push_space);
3687
3688 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003689 btrfs_item_nr_offset(push_items),
3690 (btrfs_header_nritems(right) - push_items) *
3691 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003692 }
Yaneef1c492007-11-26 10:58:13 -05003693 right_nritems -= push_items;
3694 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003695 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003696 for (i = 0; i < right_nritems; i++) {
3697 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003698
Chris Masoncfed81a2012-03-03 07:40:03 -05003699 push_space = push_space - btrfs_token_item_size(right,
3700 item, &token);
3701 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003702 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003703
Chris Mason5f39d392007-10-15 16:14:19 -04003704 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003705 if (right_nritems)
3706 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003707 else
3708 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003709
Chris Mason5f39d392007-10-15 16:14:19 -04003710 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003711 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003712
3713 /* then fixup the leaf pointer in the path */
3714 if (path->slots[0] < push_items) {
3715 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003716 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003717 free_extent_buffer(path->nodes[0]);
3718 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003719 path->slots[1] -= 1;
3720 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003721 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003722 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003723 path->slots[0] -= push_items;
3724 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003725 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003726 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003727out:
3728 btrfs_tree_unlock(left);
3729 free_extent_buffer(left);
3730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003731}
3732
Chris Mason74123bd2007-02-02 11:05:29 -05003733/*
Chris Mason44871b12009-03-13 10:04:31 -04003734 * push some data in the path leaf to the left, trying to free up at
3735 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003736 *
3737 * max_slot can put a limit on how far into the leaf we'll push items. The
3738 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3739 * items
Chris Mason44871b12009-03-13 10:04:31 -04003740 */
3741static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003742 *root, struct btrfs_path *path, int min_data_size,
3743 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003744{
3745 struct extent_buffer *right = path->nodes[0];
3746 struct extent_buffer *left;
3747 int slot;
3748 int free_space;
3749 u32 right_nritems;
3750 int ret = 0;
3751
3752 slot = path->slots[1];
3753 if (slot == 0)
3754 return 1;
3755 if (!path->nodes[1])
3756 return 1;
3757
3758 right_nritems = btrfs_header_nritems(right);
3759 if (right_nritems == 0)
3760 return 1;
3761
3762 btrfs_assert_tree_locked(path->nodes[1]);
3763
3764 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003765 if (left == NULL)
3766 return 1;
3767
Chris Mason44871b12009-03-13 10:04:31 -04003768 btrfs_tree_lock(left);
3769 btrfs_set_lock_blocking(left);
3770
3771 free_space = btrfs_leaf_free_space(root, left);
3772 if (free_space < data_size) {
3773 ret = 1;
3774 goto out;
3775 }
3776
3777 /* cow and double check */
3778 ret = btrfs_cow_block(trans, root, left,
3779 path->nodes[1], slot - 1, &left);
3780 if (ret) {
3781 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003782 if (ret == -ENOSPC)
3783 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003784 goto out;
3785 }
3786
3787 free_space = btrfs_leaf_free_space(root, left);
3788 if (free_space < data_size) {
3789 ret = 1;
3790 goto out;
3791 }
3792
Chris Mason99d8f832010-07-07 10:51:48 -04003793 return __push_leaf_left(trans, root, path, min_data_size,
3794 empty, left, free_space, right_nritems,
3795 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003796out:
3797 btrfs_tree_unlock(left);
3798 free_extent_buffer(left);
3799 return ret;
3800}
3801
3802/*
Chris Mason74123bd2007-02-02 11:05:29 -05003803 * split the path's leaf in two, making sure there is at least data_size
3804 * available for the resulting leaf level of the path.
3805 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003806static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3807 struct btrfs_root *root,
3808 struct btrfs_path *path,
3809 struct extent_buffer *l,
3810 struct extent_buffer *right,
3811 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003812{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003813 int data_copy_size;
3814 int rt_data_off;
3815 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003816 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003817 struct btrfs_map_token token;
3818
3819 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003820
Chris Mason5f39d392007-10-15 16:14:19 -04003821 nritems = nritems - mid;
3822 btrfs_set_header_nritems(right, nritems);
3823 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3824
3825 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3826 btrfs_item_nr_offset(mid),
3827 nritems * sizeof(struct btrfs_item));
3828
3829 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003830 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3831 data_copy_size, btrfs_leaf_data(l) +
3832 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003833
Chris Mason5f39d392007-10-15 16:14:19 -04003834 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3835 btrfs_item_end_nr(l, mid);
3836
3837 for (i = 0; i < nritems; i++) {
3838 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003839 u32 ioff;
3840
Chris Masoncfed81a2012-03-03 07:40:03 -05003841 ioff = btrfs_token_item_offset(right, item, &token);
3842 btrfs_set_token_item_offset(right, item,
3843 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003844 }
Chris Mason74123bd2007-02-02 11:05:29 -05003845
Chris Mason5f39d392007-10-15 16:14:19 -04003846 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003847 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003848 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003849 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003850
3851 btrfs_mark_buffer_dirty(right);
3852 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003853 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003856 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003857 free_extent_buffer(path->nodes[0]);
3858 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003859 path->slots[0] -= mid;
3860 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003861 } else {
3862 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003863 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003864 }
Chris Mason5f39d392007-10-15 16:14:19 -04003865
Chris Masoneb60cea2007-02-02 09:18:22 -05003866 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003867}
3868
3869/*
Chris Mason99d8f832010-07-07 10:51:48 -04003870 * double splits happen when we need to insert a big item in the middle
3871 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3872 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3873 * A B C
3874 *
3875 * We avoid this by trying to push the items on either side of our target
3876 * into the adjacent leaves. If all goes well we can avoid the double split
3877 * completely.
3878 */
3879static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3880 struct btrfs_root *root,
3881 struct btrfs_path *path,
3882 int data_size)
3883{
3884 int ret;
3885 int progress = 0;
3886 int slot;
3887 u32 nritems;
3888
3889 slot = path->slots[0];
3890
3891 /*
3892 * try to push all the items after our slot into the
3893 * right leaf
3894 */
3895 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3896 if (ret < 0)
3897 return ret;
3898
3899 if (ret == 0)
3900 progress++;
3901
3902 nritems = btrfs_header_nritems(path->nodes[0]);
3903 /*
3904 * our goal is to get our slot at the start or end of a leaf. If
3905 * we've done so we're done
3906 */
3907 if (path->slots[0] == 0 || path->slots[0] == nritems)
3908 return 0;
3909
3910 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3911 return 0;
3912
3913 /* try to push all the items before our slot into the next leaf */
3914 slot = path->slots[0];
3915 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3916 if (ret < 0)
3917 return ret;
3918
3919 if (ret == 0)
3920 progress++;
3921
3922 if (progress)
3923 return 0;
3924 return 1;
3925}
3926
3927/*
Chris Mason44871b12009-03-13 10:04:31 -04003928 * split the path's leaf in two, making sure there is at least data_size
3929 * available for the resulting leaf level of the path.
3930 *
3931 * returns 0 if all went well and < 0 on failure.
3932 */
3933static noinline int split_leaf(struct btrfs_trans_handle *trans,
3934 struct btrfs_root *root,
3935 struct btrfs_key *ins_key,
3936 struct btrfs_path *path, int data_size,
3937 int extend)
3938{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003939 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003940 struct extent_buffer *l;
3941 u32 nritems;
3942 int mid;
3943 int slot;
3944 struct extent_buffer *right;
3945 int ret = 0;
3946 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003947 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003948 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003949 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003950
Yan, Zhenga5719522009-09-24 09:17:31 -04003951 l = path->nodes[0];
3952 slot = path->slots[0];
3953 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3954 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3955 return -EOVERFLOW;
3956
Chris Mason44871b12009-03-13 10:04:31 -04003957 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003958 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003959 wret = push_leaf_right(trans, root, path, data_size,
3960 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003961 if (wret < 0)
3962 return wret;
3963 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003964 wret = push_leaf_left(trans, root, path, data_size,
3965 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003966 if (wret < 0)
3967 return wret;
3968 }
3969 l = path->nodes[0];
3970
3971 /* did the pushes work? */
3972 if (btrfs_leaf_free_space(root, l) >= data_size)
3973 return 0;
3974 }
3975
3976 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003977 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003978 if (ret)
3979 return ret;
3980 }
3981again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003982 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003983 l = path->nodes[0];
3984 slot = path->slots[0];
3985 nritems = btrfs_header_nritems(l);
3986 mid = (nritems + 1) / 2;
3987
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003988 if (mid <= slot) {
3989 if (nritems == 1 ||
3990 leaf_space_used(l, mid, nritems - mid) + data_size >
3991 BTRFS_LEAF_DATA_SIZE(root)) {
3992 if (slot >= nritems) {
3993 split = 0;
3994 } else {
3995 mid = slot;
3996 if (mid != nritems &&
3997 leaf_space_used(l, mid, nritems - mid) +
3998 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003999 if (data_size && !tried_avoid_double)
4000 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004001 split = 2;
4002 }
4003 }
4004 }
4005 } else {
4006 if (leaf_space_used(l, 0, mid) + data_size >
4007 BTRFS_LEAF_DATA_SIZE(root)) {
4008 if (!extend && data_size && slot == 0) {
4009 split = 0;
4010 } else if ((extend || !data_size) && slot == 0) {
4011 mid = 1;
4012 } else {
4013 mid = slot;
4014 if (mid != nritems &&
4015 leaf_space_used(l, mid, nritems - mid) +
4016 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004017 if (data_size && !tried_avoid_double)
4018 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004019 split = 2 ;
4020 }
4021 }
4022 }
4023 }
4024
4025 if (split == 0)
4026 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4027 else
4028 btrfs_item_key(l, &disk_key, mid);
4029
4030 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004031 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004032 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004033 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004034 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004035
4036 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004037
4038 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4039 btrfs_set_header_bytenr(right, right->start);
4040 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004042 btrfs_set_header_owner(right, root->root_key.objectid);
4043 btrfs_set_header_level(right, 0);
4044 write_extent_buffer(right, root->fs_info->fsid,
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +02004045 btrfs_header_fsid(right), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004046
4047 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004048 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004049 BTRFS_UUID_SIZE);
4050
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051 if (split == 0) {
4052 if (mid <= slot) {
4053 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004054 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004055 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004056 btrfs_tree_unlock(path->nodes[0]);
4057 free_extent_buffer(path->nodes[0]);
4058 path->nodes[0] = right;
4059 path->slots[0] = 0;
4060 path->slots[1] += 1;
4061 } else {
4062 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004063 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004064 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 btrfs_tree_unlock(path->nodes[0]);
4066 free_extent_buffer(path->nodes[0]);
4067 path->nodes[0] = right;
4068 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004069 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004070 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004071 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072 btrfs_mark_buffer_dirty(right);
4073 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004074 }
4075
Jeff Mahoney143bede2012-03-01 14:56:26 +01004076 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004077
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004078 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004079 BUG_ON(num_doubles != 0);
4080 num_doubles++;
4081 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004082 }
Chris Mason44871b12009-03-13 10:04:31 -04004083
Jeff Mahoney143bede2012-03-01 14:56:26 +01004084 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004085
4086push_for_double:
4087 push_for_double_split(trans, root, path, data_size);
4088 tried_avoid_double = 1;
4089 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4090 return 0;
4091 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004092}
4093
Yan, Zhengad48fd752009-11-12 09:33:58 +00004094static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *root,
4096 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004097{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004098 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004099 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004100 struct btrfs_file_extent_item *fi;
4101 u64 extent_len = 0;
4102 u32 item_size;
4103 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004104
4105 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004106 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4107
4108 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4109 key.type != BTRFS_EXTENT_CSUM_KEY);
4110
4111 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4112 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004113
4114 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004115 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4116 fi = btrfs_item_ptr(leaf, path->slots[0],
4117 struct btrfs_file_extent_item);
4118 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4119 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004120 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004121
Chris Mason459931e2008-12-10 09:10:46 -05004122 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004123 path->search_for_split = 1;
4124 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004125 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004126 if (ret < 0)
4127 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004128
Yan, Zhengad48fd752009-11-12 09:33:58 +00004129 ret = -EAGAIN;
4130 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004131 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004132 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4133 goto err;
4134
Chris Mason109f6ae2010-04-02 09:20:18 -04004135 /* the leaf has changed, it now has room. return now */
4136 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4137 goto err;
4138
Yan, Zhengad48fd752009-11-12 09:33:58 +00004139 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4140 fi = btrfs_item_ptr(leaf, path->slots[0],
4141 struct btrfs_file_extent_item);
4142 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4143 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004144 }
4145
Chris Masonb9473432009-03-13 11:00:37 -04004146 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004147 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004148 if (ret)
4149 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004150
Yan, Zhengad48fd752009-11-12 09:33:58 +00004151 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004152 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004153 return 0;
4154err:
4155 path->keep_locks = 0;
4156 return ret;
4157}
4158
4159static noinline int split_item(struct btrfs_trans_handle *trans,
4160 struct btrfs_root *root,
4161 struct btrfs_path *path,
4162 struct btrfs_key *new_key,
4163 unsigned long split_offset)
4164{
4165 struct extent_buffer *leaf;
4166 struct btrfs_item *item;
4167 struct btrfs_item *new_item;
4168 int slot;
4169 char *buf;
4170 u32 nritems;
4171 u32 item_size;
4172 u32 orig_offset;
4173 struct btrfs_disk_key disk_key;
4174
Chris Masonb9473432009-03-13 11:00:37 -04004175 leaf = path->nodes[0];
4176 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4177
Chris Masonb4ce94d2009-02-04 09:25:08 -05004178 btrfs_set_path_blocking(path);
4179
Chris Mason459931e2008-12-10 09:10:46 -05004180 item = btrfs_item_nr(leaf, path->slots[0]);
4181 orig_offset = btrfs_item_offset(leaf, item);
4182 item_size = btrfs_item_size(leaf, item);
4183
Chris Mason459931e2008-12-10 09:10:46 -05004184 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004185 if (!buf)
4186 return -ENOMEM;
4187
Chris Mason459931e2008-12-10 09:10:46 -05004188 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4189 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004190
Chris Mason459931e2008-12-10 09:10:46 -05004191 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004192 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004193 if (slot != nritems) {
4194 /* shift the items */
4195 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004196 btrfs_item_nr_offset(slot),
4197 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004198 }
4199
4200 btrfs_cpu_key_to_disk(&disk_key, new_key);
4201 btrfs_set_item_key(leaf, &disk_key, slot);
4202
4203 new_item = btrfs_item_nr(leaf, slot);
4204
4205 btrfs_set_item_offset(leaf, new_item, orig_offset);
4206 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4207
4208 btrfs_set_item_offset(leaf, item,
4209 orig_offset + item_size - split_offset);
4210 btrfs_set_item_size(leaf, item, split_offset);
4211
4212 btrfs_set_header_nritems(leaf, nritems + 1);
4213
4214 /* write the data for the start of the original item */
4215 write_extent_buffer(leaf, buf,
4216 btrfs_item_ptr_offset(leaf, path->slots[0]),
4217 split_offset);
4218
4219 /* write the data for the new item */
4220 write_extent_buffer(leaf, buf + split_offset,
4221 btrfs_item_ptr_offset(leaf, slot),
4222 item_size - split_offset);
4223 btrfs_mark_buffer_dirty(leaf);
4224
Yan, Zhengad48fd752009-11-12 09:33:58 +00004225 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004226 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004227 return 0;
4228}
4229
4230/*
4231 * This function splits a single item into two items,
4232 * giving 'new_key' to the new item and splitting the
4233 * old one at split_offset (from the start of the item).
4234 *
4235 * The path may be released by this operation. After
4236 * the split, the path is pointing to the old item. The
4237 * new item is going to be in the same node as the old one.
4238 *
4239 * Note, the item being split must be smaller enough to live alone on
4240 * a tree block with room for one extra struct btrfs_item
4241 *
4242 * This allows us to split the item in place, keeping a lock on the
4243 * leaf the entire time.
4244 */
4245int btrfs_split_item(struct btrfs_trans_handle *trans,
4246 struct btrfs_root *root,
4247 struct btrfs_path *path,
4248 struct btrfs_key *new_key,
4249 unsigned long split_offset)
4250{
4251 int ret;
4252 ret = setup_leaf_for_split(trans, root, path,
4253 sizeof(struct btrfs_item));
4254 if (ret)
4255 return ret;
4256
4257 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004258 return ret;
4259}
4260
4261/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004262 * This function duplicate a item, giving 'new_key' to the new item.
4263 * It guarantees both items live in the same tree leaf and the new item
4264 * is contiguous with the original item.
4265 *
4266 * This allows us to split file extent in place, keeping a lock on the
4267 * leaf the entire time.
4268 */
4269int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4270 struct btrfs_root *root,
4271 struct btrfs_path *path,
4272 struct btrfs_key *new_key)
4273{
4274 struct extent_buffer *leaf;
4275 int ret;
4276 u32 item_size;
4277
4278 leaf = path->nodes[0];
4279 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4280 ret = setup_leaf_for_split(trans, root, path,
4281 item_size + sizeof(struct btrfs_item));
4282 if (ret)
4283 return ret;
4284
4285 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004286 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004287 item_size, item_size +
4288 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004289 leaf = path->nodes[0];
4290 memcpy_extent_buffer(leaf,
4291 btrfs_item_ptr_offset(leaf, path->slots[0]),
4292 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4293 item_size);
4294 return 0;
4295}
4296
4297/*
Chris Masond352ac62008-09-29 15:18:18 -04004298 * make the item pointed to by the path smaller. new_size indicates
4299 * how small to make it, and from_end tells us if we just chop bytes
4300 * off the end of the item or if we shift the item to chop bytes off
4301 * the front.
4302 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004303void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004304 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004305{
Chris Masonb18c6682007-04-17 13:26:50 -04004306 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004307 struct extent_buffer *leaf;
4308 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004309 u32 nritems;
4310 unsigned int data_end;
4311 unsigned int old_data_start;
4312 unsigned int old_size;
4313 unsigned int size_diff;
4314 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004315 struct btrfs_map_token token;
4316
4317 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004318
Chris Mason5f39d392007-10-15 16:14:19 -04004319 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004320 slot = path->slots[0];
4321
4322 old_size = btrfs_item_size_nr(leaf, slot);
4323 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004324 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004325
Chris Mason5f39d392007-10-15 16:14:19 -04004326 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004327 data_end = leaf_data_end(root, leaf);
4328
Chris Mason5f39d392007-10-15 16:14:19 -04004329 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004330
Chris Masonb18c6682007-04-17 13:26:50 -04004331 size_diff = old_size - new_size;
4332
4333 BUG_ON(slot < 0);
4334 BUG_ON(slot >= nritems);
4335
4336 /*
4337 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4338 */
4339 /* first correct the data pointers */
4340 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004341 u32 ioff;
4342 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004343
Chris Masoncfed81a2012-03-03 07:40:03 -05004344 ioff = btrfs_token_item_offset(leaf, item, &token);
4345 btrfs_set_token_item_offset(leaf, item,
4346 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004347 }
Chris Masondb945352007-10-15 16:15:53 -04004348
Chris Masonb18c6682007-04-17 13:26:50 -04004349 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004350 if (from_end) {
4351 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4352 data_end + size_diff, btrfs_leaf_data(leaf) +
4353 data_end, old_data_start + new_size - data_end);
4354 } else {
4355 struct btrfs_disk_key disk_key;
4356 u64 offset;
4357
4358 btrfs_item_key(leaf, &disk_key, slot);
4359
4360 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4361 unsigned long ptr;
4362 struct btrfs_file_extent_item *fi;
4363
4364 fi = btrfs_item_ptr(leaf, slot,
4365 struct btrfs_file_extent_item);
4366 fi = (struct btrfs_file_extent_item *)(
4367 (unsigned long)fi - size_diff);
4368
4369 if (btrfs_file_extent_type(leaf, fi) ==
4370 BTRFS_FILE_EXTENT_INLINE) {
4371 ptr = btrfs_item_ptr_offset(leaf, slot);
4372 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004373 (unsigned long)fi,
4374 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004375 disk_bytenr));
4376 }
4377 }
4378
4379 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4380 data_end + size_diff, btrfs_leaf_data(leaf) +
4381 data_end, old_data_start - data_end);
4382
4383 offset = btrfs_disk_key_offset(&disk_key);
4384 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4385 btrfs_set_item_key(leaf, &disk_key, slot);
4386 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004387 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004388 }
Chris Mason5f39d392007-10-15 16:14:19 -04004389
4390 item = btrfs_item_nr(leaf, slot);
4391 btrfs_set_item_size(leaf, item, new_size);
4392 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004393
Chris Mason5f39d392007-10-15 16:14:19 -04004394 if (btrfs_leaf_free_space(root, leaf) < 0) {
4395 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004396 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004397 }
Chris Masonb18c6682007-04-17 13:26:50 -04004398}
4399
Chris Masond352ac62008-09-29 15:18:18 -04004400/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004401 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004402 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004403void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004404 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004405{
Chris Mason6567e832007-04-16 09:22:45 -04004406 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004407 struct extent_buffer *leaf;
4408 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004409 u32 nritems;
4410 unsigned int data_end;
4411 unsigned int old_data;
4412 unsigned int old_size;
4413 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004414 struct btrfs_map_token token;
4415
4416 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004417
Chris Mason5f39d392007-10-15 16:14:19 -04004418 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004419
Chris Mason5f39d392007-10-15 16:14:19 -04004420 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004421 data_end = leaf_data_end(root, leaf);
4422
Chris Mason5f39d392007-10-15 16:14:19 -04004423 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4424 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004425 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004426 }
Chris Mason6567e832007-04-16 09:22:45 -04004427 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004428 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004429
4430 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004431 if (slot >= nritems) {
4432 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004433 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4434 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004435 BUG_ON(1);
4436 }
Chris Mason6567e832007-04-16 09:22:45 -04004437
4438 /*
4439 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4440 */
4441 /* first correct the data pointers */
4442 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004443 u32 ioff;
4444 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004445
Chris Masoncfed81a2012-03-03 07:40:03 -05004446 ioff = btrfs_token_item_offset(leaf, item, &token);
4447 btrfs_set_token_item_offset(leaf, item,
4448 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004449 }
Chris Mason5f39d392007-10-15 16:14:19 -04004450
Chris Mason6567e832007-04-16 09:22:45 -04004451 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004452 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004453 data_end - data_size, btrfs_leaf_data(leaf) +
4454 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004455
Chris Mason6567e832007-04-16 09:22:45 -04004456 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004457 old_size = btrfs_item_size_nr(leaf, slot);
4458 item = btrfs_item_nr(leaf, slot);
4459 btrfs_set_item_size(leaf, item, old_size + data_size);
4460 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004461
Chris Mason5f39d392007-10-15 16:14:19 -04004462 if (btrfs_leaf_free_space(root, leaf) < 0) {
4463 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004464 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004465 }
Chris Mason6567e832007-04-16 09:22:45 -04004466}
4467
Chris Mason74123bd2007-02-02 11:05:29 -05004468/*
Chris Mason44871b12009-03-13 10:04:31 -04004469 * this is a helper for btrfs_insert_empty_items, the main goal here is
4470 * to save stack depth by doing the bulk of the work in a function
4471 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004472 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004473void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004474 struct btrfs_key *cpu_key, u32 *data_size,
4475 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004476{
Chris Mason5f39d392007-10-15 16:14:19 -04004477 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004478 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004479 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004480 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004481 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004482 struct extent_buffer *leaf;
4483 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004484 struct btrfs_map_token token;
4485
4486 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004487
Chris Mason5f39d392007-10-15 16:14:19 -04004488 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004489 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004490
Chris Mason5f39d392007-10-15 16:14:19 -04004491 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004492 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004493
Chris Masonf25956c2008-09-12 15:32:53 -04004494 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004495 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004496 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004497 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004498 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004499 }
Chris Mason5f39d392007-10-15 16:14:19 -04004500
Chris Masonbe0e5c02007-01-26 15:51:26 -05004501 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004502 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004503
Chris Mason5f39d392007-10-15 16:14:19 -04004504 if (old_data < data_end) {
4505 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004506 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004507 slot, old_data, data_end);
4508 BUG_ON(1);
4509 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004510 /*
4511 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4512 */
4513 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004514 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004515 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004516
Chris Mason5f39d392007-10-15 16:14:19 -04004517 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004518 ioff = btrfs_token_item_offset(leaf, item, &token);
4519 btrfs_set_token_item_offset(leaf, item,
4520 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004521 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004522 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004523 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004524 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004525 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004526
4527 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004528 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004529 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004530 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004531 data_end = old_data;
4532 }
Chris Mason5f39d392007-10-15 16:14:19 -04004533
Chris Mason62e27492007-03-15 12:56:47 -04004534 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004535 for (i = 0; i < nr; i++) {
4536 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4537 btrfs_set_item_key(leaf, &disk_key, slot + i);
4538 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004539 btrfs_set_token_item_offset(leaf, item,
4540 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004541 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004542 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004543 }
Chris Mason44871b12009-03-13 10:04:31 -04004544
Chris Mason9c583092008-01-29 15:15:18 -05004545 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004546
Chris Mason5a01a2e2008-01-30 11:43:54 -05004547 if (slot == 0) {
4548 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004549 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004550 }
Chris Masonb9473432009-03-13 11:00:37 -04004551 btrfs_unlock_up_safe(path, 1);
4552 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004553
Chris Mason5f39d392007-10-15 16:14:19 -04004554 if (btrfs_leaf_free_space(root, leaf) < 0) {
4555 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004556 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004557 }
Chris Mason44871b12009-03-13 10:04:31 -04004558}
4559
4560/*
4561 * Given a key and some data, insert items into the tree.
4562 * This does all the path init required, making room in the tree if needed.
4563 */
4564int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4565 struct btrfs_root *root,
4566 struct btrfs_path *path,
4567 struct btrfs_key *cpu_key, u32 *data_size,
4568 int nr)
4569{
Chris Mason44871b12009-03-13 10:04:31 -04004570 int ret = 0;
4571 int slot;
4572 int i;
4573 u32 total_size = 0;
4574 u32 total_data = 0;
4575
4576 for (i = 0; i < nr; i++)
4577 total_data += data_size[i];
4578
4579 total_size = total_data + (nr * sizeof(struct btrfs_item));
4580 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4581 if (ret == 0)
4582 return -EEXIST;
4583 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004584 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004585
Chris Mason44871b12009-03-13 10:04:31 -04004586 slot = path->slots[0];
4587 BUG_ON(slot < 0);
4588
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004589 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004590 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004591 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004592}
4593
4594/*
4595 * Given a key and some data, insert an item into the tree.
4596 * This does all the path init required, making room in the tree if needed.
4597 */
Chris Masone089f052007-03-16 16:20:31 -04004598int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4599 *root, struct btrfs_key *cpu_key, void *data, u32
4600 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004601{
4602 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004603 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004604 struct extent_buffer *leaf;
4605 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004606
Chris Mason2c90e5d2007-04-02 10:50:19 -04004607 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004608 if (!path)
4609 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004610 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004611 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004612 leaf = path->nodes[0];
4613 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4614 write_extent_buffer(leaf, data, ptr, data_size);
4615 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004616 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004617 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004618 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004619}
4620
Chris Mason74123bd2007-02-02 11:05:29 -05004621/*
Chris Mason5de08d72007-02-24 06:24:44 -05004622 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004623 *
Chris Masond352ac62008-09-29 15:18:18 -04004624 * the tree should have been previously balanced so the deletion does not
4625 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004626 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004627static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4628 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004629{
Chris Mason5f39d392007-10-15 16:14:19 -04004630 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004631 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004632 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004633
Chris Mason5f39d392007-10-15 16:14:19 -04004634 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004635 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004636 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004637 tree_mod_log_eb_move(root->fs_info, parent, slot,
4638 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004639 memmove_extent_buffer(parent,
4640 btrfs_node_key_ptr_offset(slot),
4641 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004642 sizeof(struct btrfs_key_ptr) *
4643 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004644 } else if (level) {
4645 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004646 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004647 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004648 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004649
Chris Mason7518a232007-03-12 12:01:18 -04004650 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004651 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004652 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004653 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004654 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004655 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004656 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004657 struct btrfs_disk_key disk_key;
4658
4659 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004660 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004661 }
Chris Masond6025572007-03-30 14:27:56 -04004662 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004663}
4664
Chris Mason74123bd2007-02-02 11:05:29 -05004665/*
Chris Mason323ac952008-10-01 19:05:46 -04004666 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004667 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004668 *
4669 * This deletes the pointer in path->nodes[1] and frees the leaf
4670 * block extent. zero is returned if it all worked out, < 0 otherwise.
4671 *
4672 * The path must have already been setup for deleting the leaf, including
4673 * all the proper balancing. path->nodes[1] must be locked.
4674 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004675static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4676 struct btrfs_root *root,
4677 struct btrfs_path *path,
4678 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004679{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004680 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004681 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004682
Chris Mason4d081c42009-02-04 09:31:28 -05004683 /*
4684 * btrfs_free_extent is expensive, we want to make sure we
4685 * aren't holding any locks when we call it
4686 */
4687 btrfs_unlock_up_safe(path, 0);
4688
Yan, Zhengf0486c62010-05-16 10:46:25 -04004689 root_sub_used(root, leaf->len);
4690
Josef Bacik3083ee22012-03-09 16:01:49 -05004691 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004692 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004693 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004694}
4695/*
Chris Mason74123bd2007-02-02 11:05:29 -05004696 * delete the item at the leaf level in path. If that empties
4697 * the leaf, remove it from the tree
4698 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004699int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4700 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004701{
Chris Mason5f39d392007-10-15 16:14:19 -04004702 struct extent_buffer *leaf;
4703 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004704 int last_off;
4705 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004706 int ret = 0;
4707 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004708 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004709 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004710 struct btrfs_map_token token;
4711
4712 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004713
Chris Mason5f39d392007-10-15 16:14:19 -04004714 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004715 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4716
4717 for (i = 0; i < nr; i++)
4718 dsize += btrfs_item_size_nr(leaf, slot + i);
4719
Chris Mason5f39d392007-10-15 16:14:19 -04004720 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004721
Chris Mason85e21ba2008-01-29 15:11:36 -05004722 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004723 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004724
4725 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004726 data_end + dsize,
4727 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004729
Chris Mason85e21ba2008-01-29 15:11:36 -05004730 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004731 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004732
Chris Mason5f39d392007-10-15 16:14:19 -04004733 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004734 ioff = btrfs_token_item_offset(leaf, item, &token);
4735 btrfs_set_token_item_offset(leaf, item,
4736 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004737 }
Chris Masondb945352007-10-15 16:15:53 -04004738
Chris Mason5f39d392007-10-15 16:14:19 -04004739 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004740 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004741 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004742 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004743 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004744 btrfs_set_header_nritems(leaf, nritems - nr);
4745 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004746
Chris Mason74123bd2007-02-02 11:05:29 -05004747 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004748 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004749 if (leaf == root->node) {
4750 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004751 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004752 btrfs_set_path_blocking(path);
4753 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004754 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004755 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004756 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004757 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004758 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004759 struct btrfs_disk_key disk_key;
4760
4761 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004762 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004763 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004764
Chris Mason74123bd2007-02-02 11:05:29 -05004765 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004766 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004767 /* push_leaf_left fixes the path.
4768 * make sure the path still points to our leaf
4769 * for possible call to del_ptr below
4770 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004771 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004772 extent_buffer_get(leaf);
4773
Chris Masonb9473432009-03-13 11:00:37 -04004774 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004775 wret = push_leaf_left(trans, root, path, 1, 1,
4776 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004777 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004778 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004779
4780 if (path->nodes[0] == leaf &&
4781 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004782 wret = push_leaf_right(trans, root, path, 1,
4783 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004784 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004785 ret = wret;
4786 }
Chris Mason5f39d392007-10-15 16:14:19 -04004787
4788 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004789 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004790 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004791 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004792 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004793 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004794 /* if we're still in the path, make sure
4795 * we're dirty. Otherwise, one of the
4796 * push_leaf functions must have already
4797 * dirtied this buffer
4798 */
4799 if (path->nodes[0] == leaf)
4800 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004801 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004802 }
Chris Masond5719762007-03-23 10:01:08 -04004803 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004804 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004805 }
4806 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004807 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004808}
4809
Chris Mason97571fd2007-02-24 13:39:08 -05004810/*
Chris Mason925baed2008-06-25 16:01:30 -04004811 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004812 * returns 0 if it found something or 1 if there are no lesser leaves.
4813 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004814 *
4815 * This may release the path, and so you may lose any locks held at the
4816 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004817 */
Stefan Behrens35a36212013-08-14 18:12:25 +02004818static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05004819{
Chris Mason925baed2008-06-25 16:01:30 -04004820 struct btrfs_key key;
4821 struct btrfs_disk_key found_key;
4822 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004823
Chris Mason925baed2008-06-25 16:01:30 -04004824 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004825
Chris Mason925baed2008-06-25 16:01:30 -04004826 if (key.offset > 0)
4827 key.offset--;
4828 else if (key.type > 0)
4829 key.type--;
4830 else if (key.objectid > 0)
4831 key.objectid--;
4832 else
4833 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004834
David Sterbab3b4aa72011-04-21 01:20:15 +02004835 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004836 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4837 if (ret < 0)
4838 return ret;
4839 btrfs_item_key(path->nodes[0], &found_key, 0);
4840 ret = comp_keys(&found_key, &key);
4841 if (ret < 0)
4842 return 0;
4843 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004844}
4845
Chris Mason3f157a22008-06-25 16:01:31 -04004846/*
4847 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004848 * for nodes or leaves that are have a minimum transaction id.
4849 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004850 *
4851 * This does not cow, but it does stuff the starting key it finds back
4852 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4853 * key and get a writable path.
4854 *
4855 * This does lock as it descends, and path->keep_locks should be set
4856 * to 1 by the caller.
4857 *
4858 * This honors path->lowest_level to prevent descent past a given level
4859 * of the tree.
4860 *
Chris Masond352ac62008-09-29 15:18:18 -04004861 * min_trans indicates the oldest transaction that you are interested
4862 * in walking through. Any nodes or leaves older than min_trans are
4863 * skipped over (without reading them).
4864 *
Chris Mason3f157a22008-06-25 16:01:31 -04004865 * returns zero if something useful was found, < 0 on error and 1 if there
4866 * was nothing in the tree that matched the search criteria.
4867 */
4868int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004869 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004870 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004871 u64 min_trans)
4872{
4873 struct extent_buffer *cur;
4874 struct btrfs_key found_key;
4875 int slot;
Yan96524802008-07-24 12:19:49 -04004876 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004877 u32 nritems;
4878 int level;
4879 int ret = 1;
4880
Chris Mason934d3752008-12-08 16:43:10 -05004881 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004882again:
Chris Masonbd681512011-07-16 15:23:14 -04004883 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004884 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004885 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004886 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004887 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004888
4889 if (btrfs_header_generation(cur) < min_trans) {
4890 ret = 1;
4891 goto out;
4892 }
Chris Masond3977122009-01-05 21:25:51 -05004893 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004894 nritems = btrfs_header_nritems(cur);
4895 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004896 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004897
Chris Mason323ac952008-10-01 19:05:46 -04004898 /* at the lowest level, we're done, setup the path and exit */
4899 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004900 if (slot >= nritems)
4901 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004902 ret = 0;
4903 path->slots[level] = slot;
4904 btrfs_item_key_to_cpu(cur, &found_key, slot);
4905 goto out;
4906 }
Yan96524802008-07-24 12:19:49 -04004907 if (sret && slot > 0)
4908 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004909 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004910 * check this node pointer against the min_trans parameters.
4911 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004912 */
Chris Masond3977122009-01-05 21:25:51 -05004913 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004914 u64 blockptr;
4915 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004916
Chris Mason3f157a22008-06-25 16:01:31 -04004917 blockptr = btrfs_node_blockptr(cur, slot);
4918 gen = btrfs_node_ptr_generation(cur, slot);
4919 if (gen < min_trans) {
4920 slot++;
4921 continue;
4922 }
Eric Sandeende78b512013-01-31 18:21:12 +00004923 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004924 }
Chris Masone02119d2008-09-05 16:13:11 -04004925find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004926 /*
4927 * we didn't find a candidate key in this node, walk forward
4928 * and find another one
4929 */
4930 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004931 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004932 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004933 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004934 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004935 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004936 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004937 goto again;
4938 } else {
4939 goto out;
4940 }
4941 }
4942 /* save our key for returning back */
4943 btrfs_node_key_to_cpu(cur, &found_key, slot);
4944 path->slots[level] = slot;
4945 if (level == path->lowest_level) {
4946 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004947 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004948 goto out;
4949 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004950 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004951 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004952 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004953
Chris Masonbd681512011-07-16 15:23:14 -04004954 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004955
Chris Masonbd681512011-07-16 15:23:14 -04004956 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004957 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004958 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004959 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004960 }
4961out:
4962 if (ret == 0)
4963 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004964 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004965 return ret;
4966}
4967
Alexander Block70698302012-06-05 21:07:48 +02004968static void tree_move_down(struct btrfs_root *root,
4969 struct btrfs_path *path,
4970 int *level, int root_level)
4971{
Chris Mason74dd17f2012-08-07 16:25:13 -04004972 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004973 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4974 path->slots[*level]);
4975 path->slots[*level - 1] = 0;
4976 (*level)--;
4977}
4978
4979static int tree_move_next_or_upnext(struct btrfs_root *root,
4980 struct btrfs_path *path,
4981 int *level, int root_level)
4982{
4983 int ret = 0;
4984 int nritems;
4985 nritems = btrfs_header_nritems(path->nodes[*level]);
4986
4987 path->slots[*level]++;
4988
Chris Mason74dd17f2012-08-07 16:25:13 -04004989 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004990 if (*level == root_level)
4991 return -1;
4992
4993 /* move upnext */
4994 path->slots[*level] = 0;
4995 free_extent_buffer(path->nodes[*level]);
4996 path->nodes[*level] = NULL;
4997 (*level)++;
4998 path->slots[*level]++;
4999
5000 nritems = btrfs_header_nritems(path->nodes[*level]);
5001 ret = 1;
5002 }
5003 return ret;
5004}
5005
5006/*
5007 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5008 * or down.
5009 */
5010static int tree_advance(struct btrfs_root *root,
5011 struct btrfs_path *path,
5012 int *level, int root_level,
5013 int allow_down,
5014 struct btrfs_key *key)
5015{
5016 int ret;
5017
5018 if (*level == 0 || !allow_down) {
5019 ret = tree_move_next_or_upnext(root, path, level, root_level);
5020 } else {
5021 tree_move_down(root, path, level, root_level);
5022 ret = 0;
5023 }
5024 if (ret >= 0) {
5025 if (*level == 0)
5026 btrfs_item_key_to_cpu(path->nodes[*level], key,
5027 path->slots[*level]);
5028 else
5029 btrfs_node_key_to_cpu(path->nodes[*level], key,
5030 path->slots[*level]);
5031 }
5032 return ret;
5033}
5034
5035static int tree_compare_item(struct btrfs_root *left_root,
5036 struct btrfs_path *left_path,
5037 struct btrfs_path *right_path,
5038 char *tmp_buf)
5039{
5040 int cmp;
5041 int len1, len2;
5042 unsigned long off1, off2;
5043
5044 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5045 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5046 if (len1 != len2)
5047 return 1;
5048
5049 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5050 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5051 right_path->slots[0]);
5052
5053 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5054
5055 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5056 if (cmp)
5057 return 1;
5058 return 0;
5059}
5060
5061#define ADVANCE 1
5062#define ADVANCE_ONLY_NEXT -1
5063
5064/*
5065 * This function compares two trees and calls the provided callback for
5066 * every changed/new/deleted item it finds.
5067 * If shared tree blocks are encountered, whole subtrees are skipped, making
5068 * the compare pretty fast on snapshotted subvolumes.
5069 *
5070 * This currently works on commit roots only. As commit roots are read only,
5071 * we don't do any locking. The commit roots are protected with transactions.
5072 * Transactions are ended and rejoined when a commit is tried in between.
5073 *
5074 * This function checks for modifications done to the trees while comparing.
5075 * If it detects a change, it aborts immediately.
5076 */
5077int btrfs_compare_trees(struct btrfs_root *left_root,
5078 struct btrfs_root *right_root,
5079 btrfs_changed_cb_t changed_cb, void *ctx)
5080{
5081 int ret;
5082 int cmp;
5083 struct btrfs_trans_handle *trans = NULL;
5084 struct btrfs_path *left_path = NULL;
5085 struct btrfs_path *right_path = NULL;
5086 struct btrfs_key left_key;
5087 struct btrfs_key right_key;
5088 char *tmp_buf = NULL;
5089 int left_root_level;
5090 int right_root_level;
5091 int left_level;
5092 int right_level;
5093 int left_end_reached;
5094 int right_end_reached;
5095 int advance_left;
5096 int advance_right;
5097 u64 left_blockptr;
5098 u64 right_blockptr;
5099 u64 left_start_ctransid;
5100 u64 right_start_ctransid;
5101 u64 ctransid;
5102
5103 left_path = btrfs_alloc_path();
5104 if (!left_path) {
5105 ret = -ENOMEM;
5106 goto out;
5107 }
5108 right_path = btrfs_alloc_path();
5109 if (!right_path) {
5110 ret = -ENOMEM;
5111 goto out;
5112 }
5113
5114 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5115 if (!tmp_buf) {
5116 ret = -ENOMEM;
5117 goto out;
5118 }
5119
5120 left_path->search_commit_root = 1;
5121 left_path->skip_locking = 1;
5122 right_path->search_commit_root = 1;
5123 right_path->skip_locking = 1;
5124
Anand Jain5f3ab902012-12-07 09:28:54 +00005125 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005126 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005127 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005128
Anand Jain5f3ab902012-12-07 09:28:54 +00005129 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005130 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005131 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005132
5133 trans = btrfs_join_transaction(left_root);
5134 if (IS_ERR(trans)) {
5135 ret = PTR_ERR(trans);
5136 trans = NULL;
5137 goto out;
5138 }
5139
5140 /*
5141 * Strategy: Go to the first items of both trees. Then do
5142 *
5143 * If both trees are at level 0
5144 * Compare keys of current items
5145 * If left < right treat left item as new, advance left tree
5146 * and repeat
5147 * If left > right treat right item as deleted, advance right tree
5148 * and repeat
5149 * If left == right do deep compare of items, treat as changed if
5150 * needed, advance both trees and repeat
5151 * If both trees are at the same level but not at level 0
5152 * Compare keys of current nodes/leafs
5153 * If left < right advance left tree and repeat
5154 * If left > right advance right tree and repeat
5155 * If left == right compare blockptrs of the next nodes/leafs
5156 * If they match advance both trees but stay at the same level
5157 * and repeat
5158 * If they don't match advance both trees while allowing to go
5159 * deeper and repeat
5160 * If tree levels are different
5161 * Advance the tree that needs it and repeat
5162 *
5163 * Advancing a tree means:
5164 * If we are at level 0, try to go to the next slot. If that's not
5165 * possible, go one level up and repeat. Stop when we found a level
5166 * where we could go to the next slot. We may at this point be on a
5167 * node or a leaf.
5168 *
5169 * If we are not at level 0 and not on shared tree blocks, go one
5170 * level deeper.
5171 *
5172 * If we are not at level 0 and on shared tree blocks, go one slot to
5173 * the right if possible or go up and right.
5174 */
5175
5176 left_level = btrfs_header_level(left_root->commit_root);
5177 left_root_level = left_level;
5178 left_path->nodes[left_level] = left_root->commit_root;
5179 extent_buffer_get(left_path->nodes[left_level]);
5180
5181 right_level = btrfs_header_level(right_root->commit_root);
5182 right_root_level = right_level;
5183 right_path->nodes[right_level] = right_root->commit_root;
5184 extent_buffer_get(right_path->nodes[right_level]);
5185
5186 if (left_level == 0)
5187 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5188 &left_key, left_path->slots[left_level]);
5189 else
5190 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5191 &left_key, left_path->slots[left_level]);
5192 if (right_level == 0)
5193 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5194 &right_key, right_path->slots[right_level]);
5195 else
5196 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5197 &right_key, right_path->slots[right_level]);
5198
5199 left_end_reached = right_end_reached = 0;
5200 advance_left = advance_right = 0;
5201
5202 while (1) {
5203 /*
5204 * We need to make sure the transaction does not get committed
5205 * while we do anything on commit roots. This means, we need to
5206 * join and leave transactions for every item that we process.
5207 */
5208 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5209 btrfs_release_path(left_path);
5210 btrfs_release_path(right_path);
5211
5212 ret = btrfs_end_transaction(trans, left_root);
5213 trans = NULL;
5214 if (ret < 0)
5215 goto out;
5216 }
5217 /* now rejoin the transaction */
5218 if (!trans) {
5219 trans = btrfs_join_transaction(left_root);
5220 if (IS_ERR(trans)) {
5221 ret = PTR_ERR(trans);
5222 trans = NULL;
5223 goto out;
5224 }
5225
Anand Jain5f3ab902012-12-07 09:28:54 +00005226 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005227 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005228 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005229 if (ctransid != left_start_ctransid)
5230 left_start_ctransid = 0;
5231
Anand Jain5f3ab902012-12-07 09:28:54 +00005232 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005233 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005234 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005235 if (ctransid != right_start_ctransid)
5236 right_start_ctransid = 0;
5237
5238 if (!left_start_ctransid || !right_start_ctransid) {
5239 WARN(1, KERN_WARNING
5240 "btrfs: btrfs_compare_tree detected "
5241 "a change in one of the trees while "
5242 "iterating. This is probably a "
5243 "bug.\n");
5244 ret = -EIO;
5245 goto out;
5246 }
5247
5248 /*
5249 * the commit root may have changed, so start again
5250 * where we stopped
5251 */
5252 left_path->lowest_level = left_level;
5253 right_path->lowest_level = right_level;
5254 ret = btrfs_search_slot(NULL, left_root,
5255 &left_key, left_path, 0, 0);
5256 if (ret < 0)
5257 goto out;
5258 ret = btrfs_search_slot(NULL, right_root,
5259 &right_key, right_path, 0, 0);
5260 if (ret < 0)
5261 goto out;
5262 }
5263
5264 if (advance_left && !left_end_reached) {
5265 ret = tree_advance(left_root, left_path, &left_level,
5266 left_root_level,
5267 advance_left != ADVANCE_ONLY_NEXT,
5268 &left_key);
5269 if (ret < 0)
5270 left_end_reached = ADVANCE;
5271 advance_left = 0;
5272 }
5273 if (advance_right && !right_end_reached) {
5274 ret = tree_advance(right_root, right_path, &right_level,
5275 right_root_level,
5276 advance_right != ADVANCE_ONLY_NEXT,
5277 &right_key);
5278 if (ret < 0)
5279 right_end_reached = ADVANCE;
5280 advance_right = 0;
5281 }
5282
5283 if (left_end_reached && right_end_reached) {
5284 ret = 0;
5285 goto out;
5286 } else if (left_end_reached) {
5287 if (right_level == 0) {
5288 ret = changed_cb(left_root, right_root,
5289 left_path, right_path,
5290 &right_key,
5291 BTRFS_COMPARE_TREE_DELETED,
5292 ctx);
5293 if (ret < 0)
5294 goto out;
5295 }
5296 advance_right = ADVANCE;
5297 continue;
5298 } else if (right_end_reached) {
5299 if (left_level == 0) {
5300 ret = changed_cb(left_root, right_root,
5301 left_path, right_path,
5302 &left_key,
5303 BTRFS_COMPARE_TREE_NEW,
5304 ctx);
5305 if (ret < 0)
5306 goto out;
5307 }
5308 advance_left = ADVANCE;
5309 continue;
5310 }
5311
5312 if (left_level == 0 && right_level == 0) {
5313 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5314 if (cmp < 0) {
5315 ret = changed_cb(left_root, right_root,
5316 left_path, right_path,
5317 &left_key,
5318 BTRFS_COMPARE_TREE_NEW,
5319 ctx);
5320 if (ret < 0)
5321 goto out;
5322 advance_left = ADVANCE;
5323 } else if (cmp > 0) {
5324 ret = changed_cb(left_root, right_root,
5325 left_path, right_path,
5326 &right_key,
5327 BTRFS_COMPARE_TREE_DELETED,
5328 ctx);
5329 if (ret < 0)
5330 goto out;
5331 advance_right = ADVANCE;
5332 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005333 enum btrfs_compare_tree_result cmp;
5334
Chris Mason74dd17f2012-08-07 16:25:13 -04005335 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005336 ret = tree_compare_item(left_root, left_path,
5337 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005338 if (ret)
5339 cmp = BTRFS_COMPARE_TREE_CHANGED;
5340 else
5341 cmp = BTRFS_COMPARE_TREE_SAME;
5342 ret = changed_cb(left_root, right_root,
5343 left_path, right_path,
5344 &left_key, cmp, ctx);
5345 if (ret < 0)
5346 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005347 advance_left = ADVANCE;
5348 advance_right = ADVANCE;
5349 }
5350 } else if (left_level == right_level) {
5351 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5352 if (cmp < 0) {
5353 advance_left = ADVANCE;
5354 } else if (cmp > 0) {
5355 advance_right = ADVANCE;
5356 } else {
5357 left_blockptr = btrfs_node_blockptr(
5358 left_path->nodes[left_level],
5359 left_path->slots[left_level]);
5360 right_blockptr = btrfs_node_blockptr(
5361 right_path->nodes[right_level],
5362 right_path->slots[right_level]);
5363 if (left_blockptr == right_blockptr) {
5364 /*
5365 * As we're on a shared block, don't
5366 * allow to go deeper.
5367 */
5368 advance_left = ADVANCE_ONLY_NEXT;
5369 advance_right = ADVANCE_ONLY_NEXT;
5370 } else {
5371 advance_left = ADVANCE;
5372 advance_right = ADVANCE;
5373 }
5374 }
5375 } else if (left_level < right_level) {
5376 advance_right = ADVANCE;
5377 } else {
5378 advance_left = ADVANCE;
5379 }
5380 }
5381
5382out:
5383 btrfs_free_path(left_path);
5384 btrfs_free_path(right_path);
5385 kfree(tmp_buf);
5386
5387 if (trans) {
5388 if (!ret)
5389 ret = btrfs_end_transaction(trans, left_root);
5390 else
5391 btrfs_end_transaction(trans, left_root);
5392 }
5393
5394 return ret;
5395}
5396
Chris Mason3f157a22008-06-25 16:01:31 -04005397/*
5398 * this is similar to btrfs_next_leaf, but does not try to preserve
5399 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005400 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005401 *
5402 * 0 is returned if another key is found, < 0 if there are any errors
5403 * and 1 is returned if there are no higher keys in the tree
5404 *
5405 * path->keep_locks should be set to 1 on the search made before
5406 * calling this function.
5407 */
Chris Masone7a84562008-06-25 16:01:31 -04005408int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005409 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005410{
Chris Masone7a84562008-06-25 16:01:31 -04005411 int slot;
5412 struct extent_buffer *c;
5413
Chris Mason934d3752008-12-08 16:43:10 -05005414 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005415 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005416 if (!path->nodes[level])
5417 return 1;
5418
5419 slot = path->slots[level] + 1;
5420 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005421next:
Chris Masone7a84562008-06-25 16:01:31 -04005422 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005423 int ret;
5424 int orig_lowest;
5425 struct btrfs_key cur_key;
5426 if (level + 1 >= BTRFS_MAX_LEVEL ||
5427 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005428 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005429
5430 if (path->locks[level + 1]) {
5431 level++;
5432 continue;
5433 }
5434
5435 slot = btrfs_header_nritems(c) - 1;
5436 if (level == 0)
5437 btrfs_item_key_to_cpu(c, &cur_key, slot);
5438 else
5439 btrfs_node_key_to_cpu(c, &cur_key, slot);
5440
5441 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005442 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005443 path->lowest_level = level;
5444 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5445 0, 0);
5446 path->lowest_level = orig_lowest;
5447 if (ret < 0)
5448 return ret;
5449
5450 c = path->nodes[level];
5451 slot = path->slots[level];
5452 if (ret == 0)
5453 slot++;
5454 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005455 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005456
Chris Masone7a84562008-06-25 16:01:31 -04005457 if (level == 0)
5458 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005459 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005460 u64 gen = btrfs_node_ptr_generation(c, slot);
5461
Chris Mason3f157a22008-06-25 16:01:31 -04005462 if (gen < min_trans) {
5463 slot++;
5464 goto next;
5465 }
Chris Masone7a84562008-06-25 16:01:31 -04005466 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005467 }
Chris Masone7a84562008-06-25 16:01:31 -04005468 return 0;
5469 }
5470 return 1;
5471}
5472
Chris Mason7bb86312007-12-11 09:25:06 -05005473/*
Chris Mason925baed2008-06-25 16:01:30 -04005474 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005475 * returns 0 if it found something or 1 if there are no greater leaves.
5476 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005477 */
Chris Mason234b63a2007-03-13 10:46:10 -04005478int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005479{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005480 return btrfs_next_old_leaf(root, path, 0);
5481}
5482
5483int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5484 u64 time_seq)
5485{
Chris Masond97e63b2007-02-20 16:40:44 -05005486 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005487 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005488 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005489 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005490 struct btrfs_key key;
5491 u32 nritems;
5492 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005493 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005494 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005495
5496 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005497 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005498 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005499
Chris Mason8e73f272009-04-03 10:14:18 -04005500 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5501again:
5502 level = 1;
5503 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005504 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005505 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005506
Chris Masona2135012008-06-25 16:01:30 -04005507 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005508 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005509
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005510 if (time_seq)
5511 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5512 else
5513 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005514 path->keep_locks = 0;
5515
5516 if (ret < 0)
5517 return ret;
5518
Chris Masona2135012008-06-25 16:01:30 -04005519 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005520 /*
5521 * by releasing the path above we dropped all our locks. A balance
5522 * could have added more items next to the key that used to be
5523 * at the very end of the block. So, check again here and
5524 * advance the path if there are now more items available.
5525 */
Chris Masona2135012008-06-25 16:01:30 -04005526 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005527 if (ret == 0)
5528 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005529 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005530 goto done;
5531 }
Chris Masond97e63b2007-02-20 16:40:44 -05005532
Chris Masond3977122009-01-05 21:25:51 -05005533 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005534 if (!path->nodes[level]) {
5535 ret = 1;
5536 goto done;
5537 }
Chris Mason5f39d392007-10-15 16:14:19 -04005538
Chris Masond97e63b2007-02-20 16:40:44 -05005539 slot = path->slots[level] + 1;
5540 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005541 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005542 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005543 if (level == BTRFS_MAX_LEVEL) {
5544 ret = 1;
5545 goto done;
5546 }
Chris Masond97e63b2007-02-20 16:40:44 -05005547 continue;
5548 }
Chris Mason5f39d392007-10-15 16:14:19 -04005549
Chris Mason925baed2008-06-25 16:01:30 -04005550 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005551 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005552 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005553 }
Chris Mason5f39d392007-10-15 16:14:19 -04005554
Chris Mason8e73f272009-04-03 10:14:18 -04005555 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005556 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005557 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005558 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005559 if (ret == -EAGAIN)
5560 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005561
Chris Mason76a05b32009-05-14 13:24:30 -04005562 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005563 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005564 goto done;
5565 }
5566
Chris Mason5cd57b22008-06-25 16:01:30 -04005567 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005568 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005569 if (!ret && time_seq) {
5570 /*
5571 * If we don't get the lock, we may be racing
5572 * with push_leaf_left, holding that lock while
5573 * itself waiting for the leaf we've currently
5574 * locked. To solve this situation, we give up
5575 * on our lock and cycle.
5576 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005577 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005578 btrfs_release_path(path);
5579 cond_resched();
5580 goto again;
5581 }
Chris Mason8e73f272009-04-03 10:14:18 -04005582 if (!ret) {
5583 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005584 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005585 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005586 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005587 }
Chris Mason31533fb2011-07-26 16:01:59 -04005588 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005589 }
Chris Masond97e63b2007-02-20 16:40:44 -05005590 break;
5591 }
5592 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005593 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005594 level--;
5595 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005596 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005597 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005598
Chris Mason5f39d392007-10-15 16:14:19 -04005599 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005600 path->nodes[level] = next;
5601 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005602 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005603 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005604 if (!level)
5605 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005606
Chris Mason8e73f272009-04-03 10:14:18 -04005607 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005608 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005609 if (ret == -EAGAIN)
5610 goto again;
5611
Chris Mason76a05b32009-05-14 13:24:30 -04005612 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005613 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005614 goto done;
5615 }
5616
Chris Mason5cd57b22008-06-25 16:01:30 -04005617 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005618 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005619 if (!ret) {
5620 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005621 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005622 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005623 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005624 }
Chris Mason31533fb2011-07-26 16:01:59 -04005625 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005626 }
Chris Masond97e63b2007-02-20 16:40:44 -05005627 }
Chris Mason8e73f272009-04-03 10:14:18 -04005628 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005629done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005630 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005631 path->leave_spinning = old_spinning;
5632 if (!old_spinning)
5633 btrfs_set_path_blocking(path);
5634
5635 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005636}
Chris Mason0b86a832008-03-24 15:01:56 -04005637
Chris Mason3f157a22008-06-25 16:01:31 -04005638/*
5639 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5640 * searching until it gets past min_objectid or finds an item of 'type'
5641 *
5642 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5643 */
Chris Mason0b86a832008-03-24 15:01:56 -04005644int btrfs_previous_item(struct btrfs_root *root,
5645 struct btrfs_path *path, u64 min_objectid,
5646 int type)
5647{
5648 struct btrfs_key found_key;
5649 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005650 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005651 int ret;
5652
Chris Masond3977122009-01-05 21:25:51 -05005653 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005654 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005655 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005656 ret = btrfs_prev_leaf(root, path);
5657 if (ret != 0)
5658 return ret;
5659 } else {
5660 path->slots[0]--;
5661 }
5662 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005663 nritems = btrfs_header_nritems(leaf);
5664 if (nritems == 0)
5665 return 1;
5666 if (path->slots[0] == nritems)
5667 path->slots[0]--;
5668
Chris Mason0b86a832008-03-24 15:01:56 -04005669 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005670 if (found_key.objectid < min_objectid)
5671 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005672 if (found_key.type == type)
5673 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005674 if (found_key.objectid == min_objectid &&
5675 found_key.type < type)
5676 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005677 }
5678 return 1;
5679}