blob: 0d5c686f2b9847ac851be45765b65be70e02a8a1 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000040static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
41 int level, int slot);
Jan Schmidtf2304752012-05-26 11:43:17 +020042static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
43 struct extent_buffer *eb);
Eric Sandeen48a3b632013-04-25 20:41:01 +000044static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Masond97e63b2007-02-20 16:40:44 -050045
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050049 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040050 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040051}
52
Chris Masonb4ce94d2009-02-04 09:25:08 -050053/*
54 * set all locked nodes in the path to blocking locks. This should
55 * be done before scheduling
56 */
57noinline void btrfs_set_path_blocking(struct btrfs_path *p)
58{
59 int i;
60 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040061 if (!p->nodes[i] || !p->locks[i])
62 continue;
63 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
64 if (p->locks[i] == BTRFS_READ_LOCK)
65 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
66 else if (p->locks[i] == BTRFS_WRITE_LOCK)
67 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050068 }
69}
70
71/*
72 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050073 *
74 * held is used to keep lockdep happy, when lockdep is enabled
75 * we set held to a blocking lock before we go around and
76 * retake all the spinlocks in the path. You can safely use NULL
77 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050078 */
Chris Mason4008c042009-02-12 14:09:45 -050079noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040080 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050081{
82 int i;
Chris Mason4008c042009-02-12 14:09:45 -050083
84#ifdef CONFIG_DEBUG_LOCK_ALLOC
85 /* lockdep really cares that we take all of these spinlocks
86 * in the right order. If any of the locks in the path are not
87 * currently blocking, it is going to complain. So, make really
88 * really sure by forcing the path to blocking before we clear
89 * the path blocking.
90 */
Chris Masonbd681512011-07-16 15:23:14 -040091 if (held) {
92 btrfs_set_lock_blocking_rw(held, held_rw);
93 if (held_rw == BTRFS_WRITE_LOCK)
94 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
95 else if (held_rw == BTRFS_READ_LOCK)
96 held_rw = BTRFS_READ_LOCK_BLOCKING;
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098 btrfs_set_path_blocking(p);
99#endif
100
101 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400102 if (p->nodes[i] && p->locks[i]) {
103 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
104 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
105 p->locks[i] = BTRFS_WRITE_LOCK;
106 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
107 p->locks[i] = BTRFS_READ_LOCK;
108 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500109 }
Chris Mason4008c042009-02-12 14:09:45 -0500110
111#ifdef CONFIG_DEBUG_LOCK_ALLOC
112 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400113 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500114#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400118void btrfs_free_path(struct btrfs_path *p)
119{
Jesper Juhlff175d52010-12-25 21:22:30 +0000120 if (!p)
121 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200122 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400123 kmem_cache_free(btrfs_path_cachep, p);
124}
125
Chris Masond352ac62008-09-29 15:18:18 -0400126/*
127 * path release drops references on the extent buffers in the path
128 * and it drops any locks held by this path
129 *
130 * It is safe to call this on paths that no locks or extent buffers held.
131 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200132noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500133{
134 int i;
Chris Masona2135012008-06-25 16:01:30 -0400135
Chris Mason234b63a2007-03-13 10:46:10 -0400136 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400137 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400139 continue;
140 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400141 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400142 p->locks[i] = 0;
143 }
Chris Mason5f39d392007-10-15 16:14:19 -0400144 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400145 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500146 }
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/*
150 * safely gets a reference on the root node of a tree. A lock
151 * is not taken, so a concurrent writer may put a different node
152 * at the root of the tree. See btrfs_lock_root_node for the
153 * looping required.
154 *
155 * The extent buffer returned by this has a reference taken, so
156 * it won't disappear. It may stop being the root of the tree
157 * at any time because there are no locks held.
158 */
Chris Mason925baed2008-06-25 16:01:30 -0400159struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
160{
161 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400162
Josef Bacik3083ee22012-03-09 16:01:49 -0500163 while (1) {
164 rcu_read_lock();
165 eb = rcu_dereference(root->node);
166
167 /*
168 * RCU really hurts here, we could free up the root node because
169 * it was cow'ed but we may not get the new root node yet so do
170 * the inc_not_zero dance and if it doesn't work then
171 * synchronize_rcu and try again.
172 */
173 if (atomic_inc_not_zero(&eb->refs)) {
174 rcu_read_unlock();
175 break;
176 }
177 rcu_read_unlock();
178 synchronize_rcu();
179 }
Chris Mason925baed2008-06-25 16:01:30 -0400180 return eb;
181}
182
Chris Masond352ac62008-09-29 15:18:18 -0400183/* loop around taking references on and locking the root node of the
184 * tree until you end up with a lock on the root. A locked buffer
185 * is returned, with a reference held.
186 */
Chris Mason925baed2008-06-25 16:01:30 -0400187struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
188{
189 struct extent_buffer *eb;
190
Chris Masond3977122009-01-05 21:25:51 -0500191 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400192 eb = btrfs_root_node(root);
193 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400194 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400195 break;
Chris Mason925baed2008-06-25 16:01:30 -0400196 btrfs_tree_unlock(eb);
197 free_extent_buffer(eb);
198 }
199 return eb;
200}
201
Chris Masonbd681512011-07-16 15:23:14 -0400202/* loop around taking references on and locking the root node of the
203 * tree until you end up with a lock on the root. A locked buffer
204 * is returned, with a reference held.
205 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000206static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400207{
208 struct extent_buffer *eb;
209
210 while (1) {
211 eb = btrfs_root_node(root);
212 btrfs_tree_read_lock(eb);
213 if (eb == root->node)
214 break;
215 btrfs_tree_read_unlock(eb);
216 free_extent_buffer(eb);
217 }
218 return eb;
219}
220
Chris Masond352ac62008-09-29 15:18:18 -0400221/* cowonly root (everything not a reference counted cow subvolume), just get
222 * put onto a simple dirty list. transaction.c walks this to make sure they
223 * get properly updated on disk.
224 */
Chris Mason0b86a832008-03-24 15:01:56 -0400225static void add_root_to_dirty_list(struct btrfs_root *root)
226{
Chris Masone5846fc2012-05-03 12:08:48 -0400227 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400228 if (root->track_dirty && list_empty(&root->dirty_list)) {
229 list_add(&root->dirty_list,
230 &root->fs_info->dirty_cowonly_roots);
231 }
Chris Masone5846fc2012-05-03 12:08:48 -0400232 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400233}
234
Chris Masond352ac62008-09-29 15:18:18 -0400235/*
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
239 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500240int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
244{
245 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 int ret = 0;
247 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249
250 WARN_ON(root->ref_cows && trans->transid !=
251 root->fs_info->running_transaction->transid);
252 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
253
254 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 if (level == 0)
256 btrfs_item_key(buf, &disk_key, 0);
257 else
258 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400259
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
261 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200262 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 return PTR_ERR(cow);
265
266 copy_extent_buffer(cow, buf, 0, 0, cow->len);
267 btrfs_set_header_bytenr(cow, cow->start);
268 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
270 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
271 BTRFS_HEADER_FLAG_RELOC);
272 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
273 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
274 else
275 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500276
Yan Zheng2b820322008-11-17 21:11:30 -0500277 write_extent_buffer(cow, root->fs_info->fsid,
278 (unsigned long)btrfs_header_fsid(cow),
279 BTRFS_FSID_SIZE);
280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200283 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200285 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 if (ret)
288 return ret;
289
290 btrfs_mark_buffer_dirty(cow);
291 *cow_ret = cow;
292 return 0;
293}
294
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200295enum mod_log_op {
296 MOD_LOG_KEY_REPLACE,
297 MOD_LOG_KEY_ADD,
298 MOD_LOG_KEY_REMOVE,
299 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
300 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
301 MOD_LOG_MOVE_KEYS,
302 MOD_LOG_ROOT_REPLACE,
303};
304
305struct tree_mod_move {
306 int dst_slot;
307 int nr_items;
308};
309
310struct tree_mod_root {
311 u64 logical;
312 u8 level;
313};
314
315struct tree_mod_elem {
316 struct rb_node node;
317 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200318 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200319 enum mod_log_op op;
320
321 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
322 int slot;
323
324 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
325 u64 generation;
326
327 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
328 struct btrfs_disk_key key;
329 u64 blockptr;
330
331 /* this is used for op == MOD_LOG_MOVE_KEYS */
332 struct tree_mod_move move;
333
334 /* this is used for op == MOD_LOG_ROOT_REPLACE */
335 struct tree_mod_root old_root;
336};
337
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200339{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200340 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200341}
342
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200344{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200345 read_unlock(&fs_info->tree_mod_log_lock);
346}
347
348static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
349{
350 write_lock(&fs_info->tree_mod_log_lock);
351}
352
353static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
354{
355 write_unlock(&fs_info->tree_mod_log_lock);
356}
357
358/*
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000359 * Increment the upper half of tree_mod_seq, set lower half zero.
360 *
361 * Must be called with fs_info->tree_mod_seq_lock held.
362 */
363static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
364{
365 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
366 seq &= 0xffffffff00000000ull;
367 seq += 1ull << 32;
368 atomic64_set(&fs_info->tree_mod_seq, seq);
369 return seq;
370}
371
372/*
373 * Increment the lower half of tree_mod_seq.
374 *
375 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
376 * are generated should not technically require a spin lock here. (Rationale:
377 * incrementing the minor while incrementing the major seq number is between its
378 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
379 * just returns a unique sequence number as usual.) We have decided to leave
380 * that requirement in here and rethink it once we notice it really imposes a
381 * problem on some workload.
382 */
383static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
384{
385 return atomic64_inc_return(&fs_info->tree_mod_seq);
386}
387
388/*
389 * return the last minor in the previous major tree_mod_seq number
390 */
391u64 btrfs_tree_mod_seq_prev(u64 seq)
392{
393 return (seq & 0xffffffff00000000ull) - 1ull;
394}
395
396/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397 * This adds a new blocker to the tree mod log's blocker list if the @elem
398 * passed does not already have a sequence number set. So when a caller expects
399 * to record tree modifications, it should ensure to set elem->seq to zero
400 * before calling btrfs_get_tree_mod_seq.
401 * Returns a fresh, unused tree log modification sequence number, even if no new
402 * blocker was added.
403 */
404u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
405 struct seq_list *elem)
406{
407 u64 seq;
408
409 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200411 if (!elem->seq) {
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000412 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200413 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
414 }
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000415 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200417 tree_mod_log_write_unlock(fs_info);
418
419 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420}
421
422void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
423 struct seq_list *elem)
424{
425 struct rb_root *tm_root;
426 struct rb_node *node;
427 struct rb_node *next;
428 struct seq_list *cur_elem;
429 struct tree_mod_elem *tm;
430 u64 min_seq = (u64)-1;
431 u64 seq_putting = elem->seq;
432
433 if (!seq_putting)
434 return;
435
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436 spin_lock(&fs_info->tree_mod_seq_lock);
437 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200438 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200439
440 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200441 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442 if (seq_putting > cur_elem->seq) {
443 /*
444 * blocker with lower sequence number exists, we
445 * cannot remove anything from the log
446 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200447 spin_unlock(&fs_info->tree_mod_seq_lock);
448 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200449 }
450 min_seq = cur_elem->seq;
451 }
452 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200453 spin_unlock(&fs_info->tree_mod_seq_lock);
454
455 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456 * anything that's lower than the lowest existing (read: blocked)
457 * sequence number can be removed from the tree.
458 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200459 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460 tm_root = &fs_info->tree_mod_log;
461 for (node = rb_first(tm_root); node; node = next) {
462 next = rb_next(node);
463 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200464 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 continue;
466 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200467 kfree(tm);
468 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200469 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200470}
471
472/*
473 * key order of the log:
474 * index -> sequence
475 *
476 * the index is the shifted logical of the *new* root node for root replace
477 * operations, or the shifted logical of the affected block for all other
478 * operations.
479 */
480static noinline int
481__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
482{
483 struct rb_root *tm_root;
484 struct rb_node **new;
485 struct rb_node *parent = NULL;
486 struct tree_mod_elem *cur;
Josef Bacikc8cc6342013-07-01 16:18:19 -0400487 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200488
Josef Bacikc8cc6342013-07-01 16:18:19 -0400489 BUG_ON(!tm);
490
491 tree_mod_log_write_lock(fs_info);
492 if (list_empty(&fs_info->tree_mod_seq_list)) {
493 tree_mod_log_write_unlock(fs_info);
494 /*
495 * Ok we no longer care about logging modifications, free up tm
496 * and return 0. Any callers shouldn't be using tm after
497 * calling tree_mod_log_insert, but if they do we can just
498 * change this to return a special error code to let the callers
499 * do their own thing.
500 */
501 kfree(tm);
502 return 0;
503 }
504
505 spin_lock(&fs_info->tree_mod_seq_lock);
506 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
507 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200508
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200509 tm_root = &fs_info->tree_mod_log;
510 new = &tm_root->rb_node;
511 while (*new) {
512 cur = container_of(*new, struct tree_mod_elem, node);
513 parent = *new;
514 if (cur->index < tm->index)
515 new = &((*new)->rb_left);
516 else if (cur->index > tm->index)
517 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200518 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200519 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200520 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200521 new = &((*new)->rb_right);
522 else {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400523 ret = -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200524 kfree(tm);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400525 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200526 }
527 }
528
529 rb_link_node(&tm->node, parent, new);
530 rb_insert_color(&tm->node, tm_root);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400531out:
532 tree_mod_log_write_unlock(fs_info);
533 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200534}
535
Jan Schmidt097b8a72012-06-21 11:08:04 +0200536/*
537 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
538 * returns zero with the tree_mod_log_lock acquired. The caller must hold
539 * this until all tree mod log insertions are recorded in the rb tree and then
540 * call tree_mod_log_write_unlock() to release.
541 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200542static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
543 struct extent_buffer *eb) {
544 smp_mb();
545 if (list_empty(&(fs_info)->tree_mod_seq_list))
546 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200547 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200548 return 1;
549 return 0;
550}
551
Jan Schmidt097b8a72012-06-21 11:08:04 +0200552static inline int
553__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
554 struct extent_buffer *eb, int slot,
555 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200557 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200558
Josef Bacikc8cc6342013-07-01 16:18:19 -0400559 tm = kzalloc(sizeof(*tm), flags);
560 if (!tm)
561 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200562
563 tm->index = eb->start >> PAGE_CACHE_SHIFT;
564 if (op != MOD_LOG_KEY_ADD) {
565 btrfs_node_key(eb, &tm->key, slot);
566 tm->blockptr = btrfs_node_blockptr(eb, slot);
567 }
568 tm->op = op;
569 tm->slot = slot;
570 tm->generation = btrfs_node_ptr_generation(eb, slot);
571
Jan Schmidt097b8a72012-06-21 11:08:04 +0200572 return __tree_mod_log_insert(fs_info, tm);
573}
574
575static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400576tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
577 struct extent_buffer *eb, int slot,
578 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200579{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200580 if (tree_mod_dont_log(fs_info, eb))
581 return 0;
582
Josef Bacikc8cc6342013-07-01 16:18:19 -0400583 return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200584}
585
586static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200587tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
588 struct extent_buffer *eb, int dst_slot, int src_slot,
589 int nr_items, gfp_t flags)
590{
591 struct tree_mod_elem *tm;
592 int ret;
593 int i;
594
Jan Schmidtf3956942012-05-31 15:02:32 +0200595 if (tree_mod_dont_log(fs_info, eb))
596 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200597
Jan Schmidt01763a22012-10-23 15:02:12 +0200598 /*
599 * When we override something during the move, we log these removals.
600 * This can only happen when we move towards the beginning of the
601 * buffer, i.e. dst_slot < src_slot.
602 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200603 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400604 ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
605 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200606 BUG_ON(ret < 0);
607 }
608
Josef Bacikc8cc6342013-07-01 16:18:19 -0400609 tm = kzalloc(sizeof(*tm), flags);
610 if (!tm)
611 return -ENOMEM;
Jan Schmidtf3956942012-05-31 15:02:32 +0200612
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200613 tm->index = eb->start >> PAGE_CACHE_SHIFT;
614 tm->slot = src_slot;
615 tm->move.dst_slot = dst_slot;
616 tm->move.nr_items = nr_items;
617 tm->op = MOD_LOG_MOVE_KEYS;
618
Josef Bacikc8cc6342013-07-01 16:18:19 -0400619 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200620}
621
Jan Schmidt097b8a72012-06-21 11:08:04 +0200622static inline void
623__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
624{
625 int i;
626 u32 nritems;
627 int ret;
628
Chris Masonb12a3b12012-08-07 15:34:49 -0400629 if (btrfs_header_level(eb) == 0)
630 return;
631
Jan Schmidt097b8a72012-06-21 11:08:04 +0200632 nritems = btrfs_header_nritems(eb);
633 for (i = nritems - 1; i >= 0; i--) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400634 ret = __tree_mod_log_insert_key(fs_info, eb, i,
635 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200636 BUG_ON(ret < 0);
637 }
638}
639
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200640static noinline int
641tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
642 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000643 struct extent_buffer *new_root, gfp_t flags,
644 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200645{
646 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200647
Jan Schmidt097b8a72012-06-21 11:08:04 +0200648 if (tree_mod_dont_log(fs_info, NULL))
649 return 0;
650
Jan Schmidt90f8d622013-04-13 13:19:53 +0000651 if (log_removal)
652 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000653
Josef Bacikc8cc6342013-07-01 16:18:19 -0400654 tm = kzalloc(sizeof(*tm), flags);
655 if (!tm)
656 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200657
658 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
659 tm->old_root.logical = old_root->start;
660 tm->old_root.level = btrfs_header_level(old_root);
661 tm->generation = btrfs_header_generation(old_root);
662 tm->op = MOD_LOG_ROOT_REPLACE;
663
Josef Bacikc8cc6342013-07-01 16:18:19 -0400664 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200665}
666
667static struct tree_mod_elem *
668__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
669 int smallest)
670{
671 struct rb_root *tm_root;
672 struct rb_node *node;
673 struct tree_mod_elem *cur = NULL;
674 struct tree_mod_elem *found = NULL;
675 u64 index = start >> PAGE_CACHE_SHIFT;
676
Jan Schmidt097b8a72012-06-21 11:08:04 +0200677 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200678 tm_root = &fs_info->tree_mod_log;
679 node = tm_root->rb_node;
680 while (node) {
681 cur = container_of(node, struct tree_mod_elem, node);
682 if (cur->index < index) {
683 node = node->rb_left;
684 } else if (cur->index > index) {
685 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200686 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200687 node = node->rb_left;
688 } else if (!smallest) {
689 /* we want the node with the highest seq */
690 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200691 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200692 found = cur;
693 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200694 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200695 /* we want the node with the smallest seq */
696 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200697 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200698 found = cur;
699 node = node->rb_right;
700 } else {
701 found = cur;
702 break;
703 }
704 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200705 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200706
707 return found;
708}
709
710/*
711 * this returns the element from the log with the smallest time sequence
712 * value that's in the log (the oldest log item). any element with a time
713 * sequence lower than min_seq will be ignored.
714 */
715static struct tree_mod_elem *
716tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
717 u64 min_seq)
718{
719 return __tree_mod_log_search(fs_info, start, min_seq, 1);
720}
721
722/*
723 * this returns the element from the log with the largest time sequence
724 * value that's in the log (the most recent log item). any element with
725 * a time sequence lower than min_seq will be ignored.
726 */
727static struct tree_mod_elem *
728tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
729{
730 return __tree_mod_log_search(fs_info, start, min_seq, 0);
731}
732
Jan Schmidt097b8a72012-06-21 11:08:04 +0200733static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200734tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
735 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000736 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200737{
738 int ret;
739 int i;
740
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200741 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200742 return;
743
Josef Bacikc8cc6342013-07-01 16:18:19 -0400744 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200745 return;
746
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200747 for (i = 0; i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400748 ret = __tree_mod_log_insert_key(fs_info, src,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000749 i + src_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400750 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Jan Schmidt90f8d622013-04-13 13:19:53 +0000751 BUG_ON(ret < 0);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400752 ret = __tree_mod_log_insert_key(fs_info, dst,
Jan Schmidt097b8a72012-06-21 11:08:04 +0200753 i + dst_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400754 MOD_LOG_KEY_ADD,
755 GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200756 BUG_ON(ret < 0);
757 }
758}
759
760static inline void
761tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
762 int dst_offset, int src_offset, int nr_items)
763{
764 int ret;
765 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
766 nr_items, GFP_NOFS);
767 BUG_ON(ret < 0);
768}
769
Jan Schmidt097b8a72012-06-21 11:08:04 +0200770static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200771tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000772 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200773{
774 int ret;
775
Josef Bacikc8cc6342013-07-01 16:18:19 -0400776 ret = __tree_mod_log_insert_key(fs_info, eb, slot,
777 MOD_LOG_KEY_REPLACE,
778 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200779 BUG_ON(ret < 0);
780}
781
Jan Schmidt097b8a72012-06-21 11:08:04 +0200782static noinline void
783tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200784{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200785 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200786 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200787 __tree_mod_log_free_eb(fs_info, eb);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200788}
789
Jan Schmidt097b8a72012-06-21 11:08:04 +0200790static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200791tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000792 struct extent_buffer *new_root_node,
793 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200794{
795 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200796 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000797 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200798 BUG_ON(ret < 0);
799}
800
Chris Masond352ac62008-09-29 15:18:18 -0400801/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400802 * check if the tree block can be shared by multiple trees
803 */
804int btrfs_block_can_be_shared(struct btrfs_root *root,
805 struct extent_buffer *buf)
806{
807 /*
808 * Tree blocks not in refernece counted trees and tree roots
809 * are never shared. If a block was allocated after the last
810 * snapshot and the block was not allocated by tree relocation,
811 * we know the block is not shared.
812 */
813 if (root->ref_cows &&
814 buf != root->node && buf != root->commit_root &&
815 (btrfs_header_generation(buf) <=
816 btrfs_root_last_snapshot(&root->root_item) ||
817 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
818 return 1;
819#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
820 if (root->ref_cows &&
821 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
822 return 1;
823#endif
824 return 0;
825}
826
827static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
828 struct btrfs_root *root,
829 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400830 struct extent_buffer *cow,
831 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400832{
833 u64 refs;
834 u64 owner;
835 u64 flags;
836 u64 new_flags = 0;
837 int ret;
838
839 /*
840 * Backrefs update rules:
841 *
842 * Always use full backrefs for extent pointers in tree block
843 * allocated by tree relocation.
844 *
845 * If a shared tree block is no longer referenced by its owner
846 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
847 * use full backrefs for extent pointers in tree block.
848 *
849 * If a tree block is been relocating
850 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
851 * use full backrefs for extent pointers in tree block.
852 * The reason for this is some operations (such as drop tree)
853 * are only allowed for blocks use full backrefs.
854 */
855
856 if (btrfs_block_can_be_shared(root, buf)) {
857 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500858 btrfs_header_level(buf), 1,
859 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700860 if (ret)
861 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700862 if (refs == 0) {
863 ret = -EROFS;
864 btrfs_std_error(root->fs_info, ret);
865 return ret;
866 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 } else {
868 refs = 1;
869 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
870 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
871 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
872 else
873 flags = 0;
874 }
875
876 owner = btrfs_header_owner(buf);
877 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
878 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
879
880 if (refs > 1) {
881 if ((owner == root->root_key.objectid ||
882 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
883 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200884 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886
887 if (root->root_key.objectid ==
888 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200889 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200891 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893 }
894 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
895 } else {
896
897 if (root->root_key.objectid ==
898 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200899 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200901 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400903 }
904 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400905 int level = btrfs_header_level(buf);
906
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 ret = btrfs_set_disk_extent_flags(trans, root,
908 buf->start,
909 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400910 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700911 if (ret)
912 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400913 }
914 } else {
915 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
916 if (root->root_key.objectid ==
917 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200918 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200920 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200922 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100923 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 }
925 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400926 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 }
928 return 0;
929}
930
931/*
Chris Masond3977122009-01-05 21:25:51 -0500932 * does the dirty work in cow of a single block. The parent block (if
933 * supplied) is updated to point to the new cow copy. The new buffer is marked
934 * dirty and returned locked. If you modify the block it needs to be marked
935 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400936 *
937 * search_start -- an allocation hint for the new block
938 *
Chris Masond3977122009-01-05 21:25:51 -0500939 * empty_size -- a hint that you plan on doing more cow. This is the size in
940 * bytes the allocator should try to find free next to the block it returns.
941 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400942 */
Chris Masond3977122009-01-05 21:25:51 -0500943static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400944 struct btrfs_root *root,
945 struct extent_buffer *buf,
946 struct extent_buffer *parent, int parent_slot,
947 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400948 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400949{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400951 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700952 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400953 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400954 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400955 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400956
Chris Mason925baed2008-06-25 16:01:30 -0400957 if (*cow_ret == buf)
958 unlock_orig = 1;
959
Chris Masonb9447ef2009-03-09 11:45:38 -0400960 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400961
Chris Mason7bb86312007-12-11 09:25:06 -0500962 WARN_ON(root->ref_cows && trans->transid !=
963 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400964 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400965
Chris Mason7bb86312007-12-11 09:25:06 -0500966 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400967
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 if (level == 0)
969 btrfs_item_key(buf, &disk_key, 0);
970 else
971 btrfs_node_key(buf, &disk_key, 0);
972
973 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
974 if (parent)
975 parent_start = parent->start;
976 else
977 parent_start = 0;
978 } else
979 parent_start = 0;
980
981 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
982 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200983 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400984 if (IS_ERR(cow))
985 return PTR_ERR(cow);
986
Chris Masonb4ce94d2009-02-04 09:25:08 -0500987 /* cow is set to blocking by btrfs_init_new_buffer */
988
Chris Mason5f39d392007-10-15 16:14:19 -0400989 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400990 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400991 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
993 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
994 BTRFS_HEADER_FLAG_RELOC);
995 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
996 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
997 else
998 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400999
Yan Zheng2b820322008-11-17 21:11:30 -05001000 write_extent_buffer(cow, root->fs_info->fsid,
1001 (unsigned long)btrfs_header_fsid(cow),
1002 BTRFS_FSID_SIZE);
1003
Mark Fashehbe1a5562011-08-08 13:20:18 -07001004 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001005 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001007 return ret;
1008 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001009
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001010 if (root->ref_cows)
1011 btrfs_reloc_cow_block(trans, root, buf, cow);
1012
Chris Mason6702ed42007-08-07 16:15:09 -04001013 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001014 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1016 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1017 parent_start = buf->start;
1018 else
1019 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001022 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001023 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001024
Yan, Zhengf0486c62010-05-16 10:46:25 -04001025 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001026 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001027 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001028 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001029 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1031 parent_start = parent->start;
1032 else
1033 parent_start = 0;
1034
1035 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001036 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001037 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001038 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001039 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001040 btrfs_set_node_ptr_generation(parent, parent_slot,
1041 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001042 btrfs_mark_buffer_dirty(parent);
Josef Bacik7fb7d76f2013-07-01 16:10:16 -04001043 if (last_ref)
1044 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001045 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001046 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001047 }
Chris Mason925baed2008-06-25 16:01:30 -04001048 if (unlock_orig)
1049 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001050 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001051 btrfs_mark_buffer_dirty(cow);
1052 *cow_ret = cow;
1053 return 0;
1054}
1055
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001056/*
1057 * returns the logical address of the oldest predecessor of the given root.
1058 * entries older than time_seq are ignored.
1059 */
1060static struct tree_mod_elem *
1061__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001062 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001063{
1064 struct tree_mod_elem *tm;
1065 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001066 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001067 int looped = 0;
1068
1069 if (!time_seq)
1070 return 0;
1071
1072 /*
1073 * the very last operation that's logged for a root is the replacement
1074 * operation (if it is replaced at all). this has the index of the *new*
1075 * root, making it the very first operation that's logged for this root.
1076 */
1077 while (1) {
1078 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1079 time_seq);
1080 if (!looped && !tm)
1081 return 0;
1082 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001083 * if there are no tree operation for the oldest root, we simply
1084 * return it. this should only happen if that (old) root is at
1085 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001086 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001087 if (!tm)
1088 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001089
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001090 /*
1091 * if there's an operation that's not a root replacement, we
1092 * found the oldest version of our root. normally, we'll find a
1093 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1094 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001095 if (tm->op != MOD_LOG_ROOT_REPLACE)
1096 break;
1097
1098 found = tm;
1099 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001100 looped = 1;
1101 }
1102
Jan Schmidta95236d2012-06-05 16:41:24 +02001103 /* if there's no old root to return, return what we found instead */
1104 if (!found)
1105 found = tm;
1106
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001107 return found;
1108}
1109
1110/*
1111 * tm is a pointer to the first operation to rewind within eb. then, all
1112 * previous operations will be rewinded (until we reach something older than
1113 * time_seq).
1114 */
1115static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001116__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1117 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001118{
1119 u32 n;
1120 struct rb_node *next;
1121 struct tree_mod_elem *tm = first_tm;
1122 unsigned long o_dst;
1123 unsigned long o_src;
1124 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1125
1126 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001127 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001128 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001129 /*
1130 * all the operations are recorded with the operator used for
1131 * the modification. as we're going backwards, we do the
1132 * opposite of each operation here.
1133 */
1134 switch (tm->op) {
1135 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1136 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001137 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001138 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001139 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001140 btrfs_set_node_key(eb, &tm->key, tm->slot);
1141 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1142 btrfs_set_node_ptr_generation(eb, tm->slot,
1143 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001144 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001145 break;
1146 case MOD_LOG_KEY_REPLACE:
1147 BUG_ON(tm->slot >= n);
1148 btrfs_set_node_key(eb, &tm->key, tm->slot);
1149 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1150 btrfs_set_node_ptr_generation(eb, tm->slot,
1151 tm->generation);
1152 break;
1153 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001154 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 n--;
1156 break;
1157 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001158 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1159 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1160 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001161 tm->move.nr_items * p_size);
1162 break;
1163 case MOD_LOG_ROOT_REPLACE:
1164 /*
1165 * this operation is special. for roots, this must be
1166 * handled explicitly before rewinding.
1167 * for non-roots, this operation may exist if the node
1168 * was a root: root A -> child B; then A gets empty and
1169 * B is promoted to the new root. in the mod log, we'll
1170 * have a root-replace operation for B, a tree block
1171 * that is no root. we simply ignore that operation.
1172 */
1173 break;
1174 }
1175 next = rb_next(&tm->node);
1176 if (!next)
1177 break;
1178 tm = container_of(next, struct tree_mod_elem, node);
1179 if (tm->index != first_tm->index)
1180 break;
1181 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001182 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001183 btrfs_set_header_nritems(eb, n);
1184}
1185
Jan Schmidt47fb0912013-04-13 13:19:55 +00001186/*
1187 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1188 * is returned. If rewind operations happen, a fresh buffer is returned. The
1189 * returned buffer is always read-locked. If the returned buffer is not the
1190 * input buffer, the lock on the input buffer is released and the input buffer
1191 * is freed (its refcount is decremented).
1192 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001193static struct extent_buffer *
1194tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1195 u64 time_seq)
1196{
1197 struct extent_buffer *eb_rewin;
1198 struct tree_mod_elem *tm;
1199
1200 if (!time_seq)
1201 return eb;
1202
1203 if (btrfs_header_level(eb) == 0)
1204 return eb;
1205
1206 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1207 if (!tm)
1208 return eb;
1209
1210 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1211 BUG_ON(tm->slot != 0);
1212 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1213 fs_info->tree_root->nodesize);
1214 BUG_ON(!eb_rewin);
1215 btrfs_set_header_bytenr(eb_rewin, eb->start);
1216 btrfs_set_header_backref_rev(eb_rewin,
1217 btrfs_header_backref_rev(eb));
1218 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001219 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001220 } else {
1221 eb_rewin = btrfs_clone_extent_buffer(eb);
1222 BUG_ON(!eb_rewin);
1223 }
1224
Jan Schmidt47fb0912013-04-13 13:19:55 +00001225 btrfs_tree_read_unlock(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001226 free_extent_buffer(eb);
1227
Jan Schmidt47fb0912013-04-13 13:19:55 +00001228 extent_buffer_get(eb_rewin);
1229 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001230 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001231 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001232 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001233
1234 return eb_rewin;
1235}
1236
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001237/*
1238 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1239 * value. If there are no changes, the current root->root_node is returned. If
1240 * anything changed in between, there's a fresh buffer allocated on which the
1241 * rewind operations are done. In any case, the returned buffer is read locked.
1242 * Returns NULL on error (with no locks held).
1243 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001244static inline struct extent_buffer *
1245get_old_root(struct btrfs_root *root, u64 time_seq)
1246{
1247 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001248 struct extent_buffer *eb = NULL;
1249 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001250 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001251 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001252 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001253 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001254 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001255
Jan Schmidt30b04632013-04-13 13:19:54 +00001256 eb_root = btrfs_read_lock_root_node(root);
1257 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001258 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001259 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001260
Jan Schmidta95236d2012-06-05 16:41:24 +02001261 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1262 old_root = &tm->old_root;
1263 old_generation = tm->generation;
1264 logical = old_root->logical;
1265 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001266 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001267 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268
Jan Schmidta95236d2012-06-05 16:41:24 +02001269 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001270 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001271 btrfs_tree_read_unlock(eb_root);
1272 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001273 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001274 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001275 if (!old || !extent_buffer_uptodate(old)) {
1276 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001277 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1278 logical);
1279 WARN_ON(1);
1280 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001281 eb = btrfs_clone_extent_buffer(old);
1282 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001283 }
1284 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001285 btrfs_tree_read_unlock(eb_root);
1286 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001287 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001288 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001289 eb = btrfs_clone_extent_buffer(eb_root);
1290 btrfs_tree_read_unlock(eb_root);
1291 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001292 }
1293
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001294 if (!eb)
1295 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001296 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001297 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001298 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001299 btrfs_set_header_bytenr(eb, eb->start);
1300 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001301 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001302 btrfs_set_header_level(eb, old_root->level);
1303 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001304 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001305 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001306 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001307 else
1308 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001309 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001310
1311 return eb;
1312}
1313
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001314int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1315{
1316 struct tree_mod_elem *tm;
1317 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001318 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001319
Jan Schmidt30b04632013-04-13 13:19:54 +00001320 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001321 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1322 level = tm->old_root.level;
1323 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001324 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001325 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001326 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001327
1328 return level;
1329}
1330
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001331static inline int should_cow_block(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root,
1333 struct extent_buffer *buf)
1334{
Liu Bof1ebcc72011-11-14 20:48:06 -05001335 /* ensure we can see the force_cow */
1336 smp_rmb();
1337
1338 /*
1339 * We do not need to cow a block if
1340 * 1) this block is not created or changed in this transaction;
1341 * 2) this block does not belong to TREE_RELOC tree;
1342 * 3) the root is not forced COW.
1343 *
1344 * What is forced COW:
1345 * when we create snapshot during commiting the transaction,
1346 * after we've finished coping src root, we must COW the shared
1347 * block to ensure the metadata consistency.
1348 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349 if (btrfs_header_generation(buf) == trans->transid &&
1350 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1351 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001352 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1353 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001354 return 0;
1355 return 1;
1356}
1357
Chris Masond352ac62008-09-29 15:18:18 -04001358/*
1359 * cows a single block, see __btrfs_cow_block for the real work.
1360 * This version of it has extra checks so that a block isn't cow'd more than
1361 * once per transaction, as long as it hasn't been written yet
1362 */
Chris Masond3977122009-01-05 21:25:51 -05001363noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001364 struct btrfs_root *root, struct extent_buffer *buf,
1365 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001366 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001367{
Chris Mason6702ed42007-08-07 16:15:09 -04001368 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001369 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001370
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001371 if (trans->transaction != root->fs_info->running_transaction)
1372 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001373 (unsigned long long)trans->transid,
1374 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001375 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001376
1377 if (trans->transid != root->fs_info->generation)
1378 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001379 (unsigned long long)trans->transid,
1380 (unsigned long long)root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001383 *cow_ret = buf;
1384 return 0;
1385 }
Chris Masonc4876852009-02-04 09:24:25 -05001386
Chris Mason0b86a832008-03-24 15:01:56 -04001387 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001388
1389 if (parent)
1390 btrfs_set_lock_blocking(parent);
1391 btrfs_set_lock_blocking(buf);
1392
Chris Masonf510cfe2007-10-15 16:14:48 -04001393 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001394 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001395
1396 trace_btrfs_cow_block(root, buf, *cow_ret);
1397
Chris Masonf510cfe2007-10-15 16:14:48 -04001398 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001399}
1400
Chris Masond352ac62008-09-29 15:18:18 -04001401/*
1402 * helper function for defrag to decide if two blocks pointed to by a
1403 * node are actually close by
1404 */
Chris Mason6b800532007-10-15 16:17:34 -04001405static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001406{
Chris Mason6b800532007-10-15 16:17:34 -04001407 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001408 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001409 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001410 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001411 return 0;
1412}
1413
Chris Mason081e9572007-11-06 10:26:24 -05001414/*
1415 * compare two keys in a memcmp fashion
1416 */
1417static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1418{
1419 struct btrfs_key k1;
1420
1421 btrfs_disk_key_to_cpu(&k1, disk);
1422
Diego Calleja20736ab2009-07-24 11:06:52 -04001423 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001424}
1425
Josef Bacikf3465ca2008-11-12 14:19:50 -05001426/*
1427 * same as comp_keys only with two btrfs_key's
1428 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001430{
1431 if (k1->objectid > k2->objectid)
1432 return 1;
1433 if (k1->objectid < k2->objectid)
1434 return -1;
1435 if (k1->type > k2->type)
1436 return 1;
1437 if (k1->type < k2->type)
1438 return -1;
1439 if (k1->offset > k2->offset)
1440 return 1;
1441 if (k1->offset < k2->offset)
1442 return -1;
1443 return 0;
1444}
Chris Mason081e9572007-11-06 10:26:24 -05001445
Chris Masond352ac62008-09-29 15:18:18 -04001446/*
1447 * this is used by the defrag code to go through all the
1448 * leaves pointed to by a node and reallocate them so that
1449 * disk order is close to key order
1450 */
Chris Mason6702ed42007-08-07 16:15:09 -04001451int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001452 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001453 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001454 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001455{
Chris Mason6b800532007-10-15 16:17:34 -04001456 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001457 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001458 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001459 u64 search_start = *last_ret;
1460 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001461 u64 other;
1462 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001463 int end_slot;
1464 int i;
1465 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001466 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001467 int uptodate;
1468 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001469 int progress_passed = 0;
1470 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001471
Chris Mason5708b952007-10-25 15:43:18 -04001472 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001473
Julia Lawall6c1500f2012-11-03 20:30:18 +00001474 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1475 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001476
Chris Mason6b800532007-10-15 16:17:34 -04001477 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001478 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001479 end_slot = parent_nritems;
1480
1481 if (parent_nritems == 1)
1482 return 0;
1483
Chris Masonb4ce94d2009-02-04 09:25:08 -05001484 btrfs_set_lock_blocking(parent);
1485
Chris Mason6702ed42007-08-07 16:15:09 -04001486 for (i = start_slot; i < end_slot; i++) {
1487 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001488
Chris Mason081e9572007-11-06 10:26:24 -05001489 btrfs_node_key(parent, &disk_key, i);
1490 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1491 continue;
1492
1493 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001494 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001495 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001496 if (last_block == 0)
1497 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001498
Chris Mason6702ed42007-08-07 16:15:09 -04001499 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001500 other = btrfs_node_blockptr(parent, i - 1);
1501 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001502 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001503 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001504 other = btrfs_node_blockptr(parent, i + 1);
1505 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001506 }
Chris Masone9d0b132007-08-10 14:06:19 -04001507 if (close) {
1508 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001509 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001510 }
Chris Mason6702ed42007-08-07 16:15:09 -04001511
Chris Mason6b800532007-10-15 16:17:34 -04001512 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1513 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001514 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001515 else
1516 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001517 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001518 if (!cur) {
1519 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001520 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001521 if (!cur || !extent_buffer_uptodate(cur)) {
1522 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001523 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001524 }
Chris Mason6b800532007-10-15 16:17:34 -04001525 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001526 err = btrfs_read_buffer(cur, gen);
1527 if (err) {
1528 free_extent_buffer(cur);
1529 return err;
1530 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001531 }
Chris Mason6702ed42007-08-07 16:15:09 -04001532 }
Chris Masone9d0b132007-08-10 14:06:19 -04001533 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001534 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001535
Chris Masone7a84562008-06-25 16:01:31 -04001536 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001537 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001538 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001539 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001540 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001541 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001542 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001543 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001544 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001545 break;
Yan252c38f2007-08-29 09:11:44 -04001546 }
Chris Masone7a84562008-06-25 16:01:31 -04001547 search_start = cur->start;
1548 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001549 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001550 btrfs_tree_unlock(cur);
1551 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001552 }
1553 return err;
1554}
1555
Chris Mason74123bd2007-02-02 11:05:29 -05001556/*
1557 * The leaf data grows from end-to-front in the node.
1558 * this returns the address of the start of the last item,
1559 * which is the stop of the leaf data stack
1560 */
Chris Mason123abc82007-03-14 14:14:43 -04001561static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001562 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563{
Chris Mason5f39d392007-10-15 16:14:19 -04001564 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001565 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001566 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001567 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001568}
1569
Chris Masonaa5d6be2007-02-28 16:35:06 -05001570
Chris Mason74123bd2007-02-02 11:05:29 -05001571/*
Chris Mason5f39d392007-10-15 16:14:19 -04001572 * search for key in the extent_buffer. The items start at offset p,
1573 * and they are item_size apart. There are 'max' items in p.
1574 *
Chris Mason74123bd2007-02-02 11:05:29 -05001575 * the slot in the array is returned via slot, and it points to
1576 * the place where you would insert key if it is not found in
1577 * the array.
1578 *
1579 * slot may point to max if the key is bigger than all of the keys
1580 */
Chris Masone02119d2008-09-05 16:13:11 -04001581static noinline int generic_bin_search(struct extent_buffer *eb,
1582 unsigned long p,
1583 int item_size, struct btrfs_key *key,
1584 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001585{
1586 int low = 0;
1587 int high = max;
1588 int mid;
1589 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001590 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001591 struct btrfs_disk_key unaligned;
1592 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001593 char *kaddr = NULL;
1594 unsigned long map_start = 0;
1595 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001596 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001597
Chris Masond3977122009-01-05 21:25:51 -05001598 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001599 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 offset = p + mid * item_size;
1601
Chris Masona6591712011-07-19 12:04:14 -04001602 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001603 (offset + sizeof(struct btrfs_disk_key)) >
1604 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001605
1606 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001607 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001608 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001609
Chris Mason479965d2007-10-15 16:14:27 -04001610 if (!err) {
1611 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1612 map_start);
1613 } else {
1614 read_extent_buffer(eb, &unaligned,
1615 offset, sizeof(unaligned));
1616 tmp = &unaligned;
1617 }
1618
Chris Mason5f39d392007-10-15 16:14:19 -04001619 } else {
1620 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1621 map_start);
1622 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001623 ret = comp_keys(tmp, key);
1624
1625 if (ret < 0)
1626 low = mid + 1;
1627 else if (ret > 0)
1628 high = mid;
1629 else {
1630 *slot = mid;
1631 return 0;
1632 }
1633 }
1634 *slot = low;
1635 return 1;
1636}
1637
Chris Mason97571fd2007-02-24 13:39:08 -05001638/*
1639 * simple bin_search frontend that does the right thing for
1640 * leaves vs nodes
1641 */
Chris Mason5f39d392007-10-15 16:14:19 -04001642static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1643 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001644{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001645 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001646 return generic_bin_search(eb,
1647 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001648 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001649 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001650 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001651 else
Chris Mason5f39d392007-10-15 16:14:19 -04001652 return generic_bin_search(eb,
1653 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001654 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001655 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001656 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001657}
1658
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1660 int level, int *slot)
1661{
1662 return bin_search(eb, key, level, slot);
1663}
1664
Yan, Zhengf0486c62010-05-16 10:46:25 -04001665static void root_add_used(struct btrfs_root *root, u32 size)
1666{
1667 spin_lock(&root->accounting_lock);
1668 btrfs_set_root_used(&root->root_item,
1669 btrfs_root_used(&root->root_item) + size);
1670 spin_unlock(&root->accounting_lock);
1671}
1672
1673static void root_sub_used(struct btrfs_root *root, u32 size)
1674{
1675 spin_lock(&root->accounting_lock);
1676 btrfs_set_root_used(&root->root_item,
1677 btrfs_root_used(&root->root_item) - size);
1678 spin_unlock(&root->accounting_lock);
1679}
1680
Chris Masond352ac62008-09-29 15:18:18 -04001681/* given a node and slot number, this reads the blocks it points to. The
1682 * extent buffer is returned with a reference taken (but unlocked).
1683 * NULL is returned on error.
1684 */
Chris Masone02119d2008-09-05 16:13:11 -04001685static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001686 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001687{
Chris Masonca7a79a2008-05-12 12:59:19 -04001688 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001689 struct extent_buffer *eb;
1690
Chris Masonbb803952007-03-01 12:04:21 -05001691 if (slot < 0)
1692 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001693 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001694 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001695
1696 BUG_ON(level == 0);
1697
Josef Bacik416bc652013-04-23 14:17:42 -04001698 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1699 btrfs_level_size(root, level - 1),
1700 btrfs_node_ptr_generation(parent, slot));
1701 if (eb && !extent_buffer_uptodate(eb)) {
1702 free_extent_buffer(eb);
1703 eb = NULL;
1704 }
1705
1706 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001707}
1708
Chris Masond352ac62008-09-29 15:18:18 -04001709/*
1710 * node level balancing, used to make sure nodes are in proper order for
1711 * item deletion. We balance from the top down, so we have to make sure
1712 * that a deletion won't leave an node completely empty later on.
1713 */
Chris Masone02119d2008-09-05 16:13:11 -04001714static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001715 struct btrfs_root *root,
1716 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001717{
Chris Mason5f39d392007-10-15 16:14:19 -04001718 struct extent_buffer *right = NULL;
1719 struct extent_buffer *mid;
1720 struct extent_buffer *left = NULL;
1721 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001722 int ret = 0;
1723 int wret;
1724 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001725 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001726 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001727
1728 if (level == 0)
1729 return 0;
1730
Chris Mason5f39d392007-10-15 16:14:19 -04001731 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001732
Chris Masonbd681512011-07-16 15:23:14 -04001733 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1734 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001735 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1736
Chris Mason1d4f8a02007-03-13 09:28:32 -04001737 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001738
Li Zefana05a9bb2011-09-06 16:55:34 +08001739 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001740 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001741 pslot = path->slots[level + 1];
1742 }
Chris Masonbb803952007-03-01 12:04:21 -05001743
Chris Mason40689472007-03-17 14:29:23 -04001744 /*
1745 * deal with the case where there is only one pointer in the root
1746 * by promoting the node below to a root
1747 */
Chris Mason5f39d392007-10-15 16:14:19 -04001748 if (!parent) {
1749 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001750
Chris Mason5f39d392007-10-15 16:14:19 -04001751 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001752 return 0;
1753
1754 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001755 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001756 if (!child) {
1757 ret = -EROFS;
1758 btrfs_std_error(root->fs_info, ret);
1759 goto enospc;
1760 }
1761
Chris Mason925baed2008-06-25 16:01:30 -04001762 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001763 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001764 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001765 if (ret) {
1766 btrfs_tree_unlock(child);
1767 free_extent_buffer(child);
1768 goto enospc;
1769 }
Yan2f375ab2008-02-01 14:58:07 -05001770
Jan Schmidt90f8d622013-04-13 13:19:53 +00001771 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001772 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001773
Chris Mason0b86a832008-03-24 15:01:56 -04001774 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001775 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001776
Chris Mason925baed2008-06-25 16:01:30 -04001777 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001778 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001779 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001780 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001781 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001782 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001783
1784 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001785 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001786 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001787 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001788 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001789 }
Chris Mason5f39d392007-10-15 16:14:19 -04001790 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001791 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001792 return 0;
1793
Chris Mason5f39d392007-10-15 16:14:19 -04001794 left = read_node_slot(root, parent, pslot - 1);
1795 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001796 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001797 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001798 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001799 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001800 if (wret) {
1801 ret = wret;
1802 goto enospc;
1803 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001804 }
Chris Mason5f39d392007-10-15 16:14:19 -04001805 right = read_node_slot(root, parent, pslot + 1);
1806 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001807 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001808 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001809 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001810 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001811 if (wret) {
1812 ret = wret;
1813 goto enospc;
1814 }
1815 }
1816
1817 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001818 if (left) {
1819 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001820 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001821 if (wret < 0)
1822 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001823 }
Chris Mason79f95c82007-03-01 15:16:26 -05001824
1825 /*
1826 * then try to empty the right most buffer into the middle
1827 */
Chris Mason5f39d392007-10-15 16:14:19 -04001828 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001829 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001830 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001831 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001832 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001833 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001834 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001835 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001836 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001837 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001838 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001839 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001840 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001841 struct btrfs_disk_key right_key;
1842 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001843 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001844 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001845 btrfs_set_node_key(parent, &right_key, pslot + 1);
1846 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001847 }
1848 }
Chris Mason5f39d392007-10-15 16:14:19 -04001849 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001850 /*
1851 * we're not allowed to leave a node with one item in the
1852 * tree during a delete. A deletion from lower in the tree
1853 * could try to delete the only pointer in this node.
1854 * So, pull some keys from the left.
1855 * There has to be a left pointer at this point because
1856 * otherwise we would have pulled some pointers from the
1857 * right
1858 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001859 if (!left) {
1860 ret = -EROFS;
1861 btrfs_std_error(root->fs_info, ret);
1862 goto enospc;
1863 }
Chris Mason5f39d392007-10-15 16:14:19 -04001864 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001865 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001866 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001867 goto enospc;
1868 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001869 if (wret == 1) {
1870 wret = push_node_left(trans, root, left, mid, 1);
1871 if (wret < 0)
1872 ret = wret;
1873 }
Chris Mason79f95c82007-03-01 15:16:26 -05001874 BUG_ON(wret == 1);
1875 }
Chris Mason5f39d392007-10-15 16:14:19 -04001876 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001877 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001878 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001879 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001880 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001881 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001882 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001883 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001884 } else {
1885 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001886 struct btrfs_disk_key mid_key;
1887 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001888 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001889 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001890 btrfs_set_node_key(parent, &mid_key, pslot);
1891 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001892 }
Chris Masonbb803952007-03-01 12:04:21 -05001893
Chris Mason79f95c82007-03-01 15:16:26 -05001894 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001895 if (left) {
1896 if (btrfs_header_nritems(left) > orig_slot) {
1897 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001898 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001899 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001900 path->slots[level + 1] -= 1;
1901 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001902 if (mid) {
1903 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001904 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001905 }
Chris Masonbb803952007-03-01 12:04:21 -05001906 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001907 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001908 path->slots[level] = orig_slot;
1909 }
1910 }
Chris Mason79f95c82007-03-01 15:16:26 -05001911 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001912 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001913 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001914 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001915enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001916 if (right) {
1917 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001918 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001919 }
1920 if (left) {
1921 if (path->nodes[level] != left)
1922 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001923 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001924 }
Chris Masonbb803952007-03-01 12:04:21 -05001925 return ret;
1926}
1927
Chris Masond352ac62008-09-29 15:18:18 -04001928/* Node balancing for insertion. Here we only split or push nodes around
1929 * when they are completely full. This is also done top down, so we
1930 * have to be pessimistic.
1931 */
Chris Masond3977122009-01-05 21:25:51 -05001932static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001933 struct btrfs_root *root,
1934 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001935{
Chris Mason5f39d392007-10-15 16:14:19 -04001936 struct extent_buffer *right = NULL;
1937 struct extent_buffer *mid;
1938 struct extent_buffer *left = NULL;
1939 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001940 int ret = 0;
1941 int wret;
1942 int pslot;
1943 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001944
1945 if (level == 0)
1946 return 1;
1947
Chris Mason5f39d392007-10-15 16:14:19 -04001948 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001949 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001950
Li Zefana05a9bb2011-09-06 16:55:34 +08001951 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001952 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001953 pslot = path->slots[level + 1];
1954 }
Chris Masone66f7092007-04-20 13:16:02 -04001955
Chris Mason5f39d392007-10-15 16:14:19 -04001956 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001957 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001958
Chris Mason5f39d392007-10-15 16:14:19 -04001959 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001960
1961 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001962 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001963 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001964
1965 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001966 btrfs_set_lock_blocking(left);
1967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001969 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1970 wret = 1;
1971 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001972 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001973 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001974 if (ret)
1975 wret = 1;
1976 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001977 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001978 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001979 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001980 }
Chris Masone66f7092007-04-20 13:16:02 -04001981 if (wret < 0)
1982 ret = wret;
1983 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001984 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001985 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001986 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001987 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001988 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001989 btrfs_set_node_key(parent, &disk_key, pslot);
1990 btrfs_mark_buffer_dirty(parent);
1991 if (btrfs_header_nritems(left) > orig_slot) {
1992 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001993 path->slots[level + 1] -= 1;
1994 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001995 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001996 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001997 } else {
1998 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001999 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002000 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002001 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002002 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002003 }
Chris Masone66f7092007-04-20 13:16:02 -04002004 return 0;
2005 }
Chris Mason925baed2008-06-25 16:01:30 -04002006 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002007 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002008 }
Chris Mason925baed2008-06-25 16:01:30 -04002009 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002010
2011 /*
2012 * then try to empty the right most buffer into the middle
2013 */
Chris Mason5f39d392007-10-15 16:14:19 -04002014 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002015 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002016
Chris Mason925baed2008-06-25 16:01:30 -04002017 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002018 btrfs_set_lock_blocking(right);
2019
Chris Mason5f39d392007-10-15 16:14:19 -04002020 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002021 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2022 wret = 1;
2023 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002024 ret = btrfs_cow_block(trans, root, right,
2025 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002026 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002027 if (ret)
2028 wret = 1;
2029 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002030 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002031 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002032 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002033 }
Chris Masone66f7092007-04-20 13:16:02 -04002034 if (wret < 0)
2035 ret = wret;
2036 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002037 struct btrfs_disk_key disk_key;
2038
2039 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002040 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002041 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002042 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2043 btrfs_mark_buffer_dirty(parent);
2044
2045 if (btrfs_header_nritems(mid) <= orig_slot) {
2046 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002047 path->slots[level + 1] += 1;
2048 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002049 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002050 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002052 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002053 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002054 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002055 }
Chris Masone66f7092007-04-20 13:16:02 -04002056 return 0;
2057 }
Chris Mason925baed2008-06-25 16:01:30 -04002058 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002059 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002060 }
Chris Masone66f7092007-04-20 13:16:02 -04002061 return 1;
2062}
2063
Chris Mason74123bd2007-02-02 11:05:29 -05002064/*
Chris Masond352ac62008-09-29 15:18:18 -04002065 * readahead one full node of leaves, finding things that are close
2066 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002067 */
Chris Masonc8c42862009-04-03 10:14:18 -04002068static void reada_for_search(struct btrfs_root *root,
2069 struct btrfs_path *path,
2070 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002071{
Chris Mason5f39d392007-10-15 16:14:19 -04002072 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002073 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002074 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002075 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002076 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002077 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002078 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002079 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002080 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002081 u32 nr;
2082 u32 blocksize;
2083 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002084
Chris Masona6b6e752007-10-15 16:22:39 -04002085 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002086 return;
2087
Chris Mason6702ed42007-08-07 16:15:09 -04002088 if (!path->nodes[level])
2089 return;
2090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002092
Chris Mason3c69fae2007-08-07 15:52:22 -04002093 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002094 blocksize = btrfs_level_size(root, level - 1);
2095 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002096 if (eb) {
2097 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002098 return;
2099 }
2100
Chris Masona7175312009-01-22 09:23:10 -05002101 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002102
Chris Mason5f39d392007-10-15 16:14:19 -04002103 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002104 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002105
Chris Masond3977122009-01-05 21:25:51 -05002106 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002107 if (direction < 0) {
2108 if (nr == 0)
2109 break;
2110 nr--;
2111 } else if (direction > 0) {
2112 nr++;
2113 if (nr >= nritems)
2114 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002115 }
Chris Mason01f46652007-12-21 16:24:26 -05002116 if (path->reada < 0 && objectid) {
2117 btrfs_node_key(node, &disk_key, nr);
2118 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2119 break;
2120 }
Chris Mason6b800532007-10-15 16:17:34 -04002121 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002122 if ((search <= target && target - search <= 65536) ||
2123 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002124 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002125 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002126 nread += blocksize;
2127 }
2128 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002129 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002130 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002131 }
2132}
Chris Mason925baed2008-06-25 16:01:30 -04002133
Josef Bacik0b088512013-06-17 14:23:02 -04002134static noinline void reada_for_balance(struct btrfs_root *root,
2135 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002136{
2137 int slot;
2138 int nritems;
2139 struct extent_buffer *parent;
2140 struct extent_buffer *eb;
2141 u64 gen;
2142 u64 block1 = 0;
2143 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002144 int blocksize;
2145
Chris Mason8c594ea2009-04-20 15:50:10 -04002146 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002147 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002148 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002149
2150 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002151 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002152 blocksize = btrfs_level_size(root, level);
2153
2154 if (slot > 0) {
2155 block1 = btrfs_node_blockptr(parent, slot - 1);
2156 gen = btrfs_node_ptr_generation(parent, slot - 1);
2157 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002158 /*
2159 * if we get -eagain from btrfs_buffer_uptodate, we
2160 * don't want to return eagain here. That will loop
2161 * forever
2162 */
2163 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002164 block1 = 0;
2165 free_extent_buffer(eb);
2166 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002167 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002168 block2 = btrfs_node_blockptr(parent, slot + 1);
2169 gen = btrfs_node_ptr_generation(parent, slot + 1);
2170 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002171 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002172 block2 = 0;
2173 free_extent_buffer(eb);
2174 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002175
Josef Bacik0b088512013-06-17 14:23:02 -04002176 if (block1)
2177 readahead_tree_block(root, block1, blocksize, 0);
2178 if (block2)
2179 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002180}
2181
2182
2183/*
Chris Masond3977122009-01-05 21:25:51 -05002184 * when we walk down the tree, it is usually safe to unlock the higher layers
2185 * in the tree. The exceptions are when our path goes through slot 0, because
2186 * operations on the tree might require changing key pointers higher up in the
2187 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002188 *
Chris Masond3977122009-01-05 21:25:51 -05002189 * callers might also have set path->keep_locks, which tells this code to keep
2190 * the lock if the path points to the last slot in the block. This is part of
2191 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002192 *
Chris Masond3977122009-01-05 21:25:51 -05002193 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2194 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002195 */
Chris Masone02119d2008-09-05 16:13:11 -04002196static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002197 int lowest_unlock, int min_write_lock_level,
2198 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002199{
2200 int i;
2201 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002202 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002203 struct extent_buffer *t;
2204
2205 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2206 if (!path->nodes[i])
2207 break;
2208 if (!path->locks[i])
2209 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002210 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002211 skip_level = i + 1;
2212 continue;
2213 }
Chris Mason051e1b92008-06-25 16:01:30 -04002214 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002215 u32 nritems;
2216 t = path->nodes[i];
2217 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002218 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002219 skip_level = i + 1;
2220 continue;
2221 }
2222 }
Chris Mason051e1b92008-06-25 16:01:30 -04002223 if (skip_level < i && i >= lowest_unlock)
2224 no_skips = 1;
2225
Chris Mason925baed2008-06-25 16:01:30 -04002226 t = path->nodes[i];
2227 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002228 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002229 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002230 if (write_lock_level &&
2231 i > min_write_lock_level &&
2232 i <= *write_lock_level) {
2233 *write_lock_level = i - 1;
2234 }
Chris Mason925baed2008-06-25 16:01:30 -04002235 }
2236 }
2237}
2238
Chris Mason3c69fae2007-08-07 15:52:22 -04002239/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002240 * This releases any locks held in the path starting at level and
2241 * going all the way up to the root.
2242 *
2243 * btrfs_search_slot will keep the lock held on higher nodes in a few
2244 * corner cases, such as COW of the block at slot zero in the node. This
2245 * ignores those rules, and it should only be called when there are no
2246 * more updates to be done higher up in the tree.
2247 */
2248noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2249{
2250 int i;
2251
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002252 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002253 return;
2254
2255 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2256 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002257 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002258 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002259 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002260 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002261 path->locks[i] = 0;
2262 }
2263}
2264
2265/*
Chris Masonc8c42862009-04-03 10:14:18 -04002266 * helper function for btrfs_search_slot. The goal is to find a block
2267 * in cache without setting the path to blocking. If we find the block
2268 * we return zero and the path is unchanged.
2269 *
2270 * If we can't find the block, we set the path blocking and do some
2271 * reada. -EAGAIN is returned and the search must be repeated.
2272 */
2273static int
2274read_block_for_search(struct btrfs_trans_handle *trans,
2275 struct btrfs_root *root, struct btrfs_path *p,
2276 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002277 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002278{
2279 u64 blocknr;
2280 u64 gen;
2281 u32 blocksize;
2282 struct extent_buffer *b = *eb_ret;
2283 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002284 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002285
2286 blocknr = btrfs_node_blockptr(b, slot);
2287 gen = btrfs_node_ptr_generation(b, slot);
2288 blocksize = btrfs_level_size(root, level - 1);
2289
2290 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002291 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002292 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002293 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2294 *eb_ret = tmp;
2295 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002296 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002297
2298 /* the pages were up to date, but we failed
2299 * the generation number check. Do a full
2300 * read for the generation number that is correct.
2301 * We must do this without dropping locks so
2302 * we can trust our generation number
2303 */
2304 btrfs_set_path_blocking(p);
2305
2306 /* now we're allowed to do a blocking uptodate check */
2307 ret = btrfs_read_buffer(tmp, gen);
2308 if (!ret) {
2309 *eb_ret = tmp;
2310 return 0;
2311 }
2312 free_extent_buffer(tmp);
2313 btrfs_release_path(p);
2314 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002315 }
2316
2317 /*
2318 * reduce lock contention at high levels
2319 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002320 * we read. Don't release the lock on the current
2321 * level because we need to walk this node to figure
2322 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002323 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002324 btrfs_unlock_up_safe(p, level + 1);
2325 btrfs_set_path_blocking(p);
2326
Chris Masoncb449212010-10-24 11:01:27 -04002327 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002328 if (p->reada)
2329 reada_for_search(root, p, level, slot, key->objectid);
2330
David Sterbab3b4aa72011-04-21 01:20:15 +02002331 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002332
2333 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002334 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002335 if (tmp) {
2336 /*
2337 * If the read above didn't mark this buffer up to date,
2338 * it will never end up being up to date. Set ret to EIO now
2339 * and give up so that our caller doesn't loop forever
2340 * on our EAGAINs.
2341 */
Chris Masonb9fab912012-05-06 07:23:47 -04002342 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002343 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002344 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002345 }
2346 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002347}
2348
2349/*
2350 * helper function for btrfs_search_slot. This does all of the checks
2351 * for node-level blocks and does any balancing required based on
2352 * the ins_len.
2353 *
2354 * If no extra work was required, zero is returned. If we had to
2355 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2356 * start over
2357 */
2358static int
2359setup_nodes_for_search(struct btrfs_trans_handle *trans,
2360 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002361 struct extent_buffer *b, int level, int ins_len,
2362 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002363{
2364 int ret;
2365 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2366 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2367 int sret;
2368
Chris Masonbd681512011-07-16 15:23:14 -04002369 if (*write_lock_level < level + 1) {
2370 *write_lock_level = level + 1;
2371 btrfs_release_path(p);
2372 goto again;
2373 }
2374
Chris Masonc8c42862009-04-03 10:14:18 -04002375 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002376 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002377 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002378 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002379
2380 BUG_ON(sret > 0);
2381 if (sret) {
2382 ret = sret;
2383 goto done;
2384 }
2385 b = p->nodes[level];
2386 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002387 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002388 int sret;
2389
Chris Masonbd681512011-07-16 15:23:14 -04002390 if (*write_lock_level < level + 1) {
2391 *write_lock_level = level + 1;
2392 btrfs_release_path(p);
2393 goto again;
2394 }
2395
Chris Masonc8c42862009-04-03 10:14:18 -04002396 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002397 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002398 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002399 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002400
2401 if (sret) {
2402 ret = sret;
2403 goto done;
2404 }
2405 b = p->nodes[level];
2406 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002407 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002408 goto again;
2409 }
2410 BUG_ON(btrfs_header_nritems(b) == 1);
2411 }
2412 return 0;
2413
2414again:
2415 ret = -EAGAIN;
2416done:
2417 return ret;
2418}
2419
2420/*
Chris Mason74123bd2007-02-02 11:05:29 -05002421 * look for key in the tree. path is filled in with nodes along the way
2422 * if key is found, we return zero and you can find the item in the leaf
2423 * level of the path (level 0)
2424 *
2425 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002426 * be inserted, and 1 is returned. If there are other errors during the
2427 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002428 *
2429 * if ins_len > 0, nodes and leaves will be split as we walk down the
2430 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2431 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002432 */
Chris Masone089f052007-03-16 16:20:31 -04002433int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2434 *root, struct btrfs_key *key, struct btrfs_path *p, int
2435 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002436{
Chris Mason5f39d392007-10-15 16:14:19 -04002437 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002438 int slot;
2439 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002440 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002441 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002442 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002443 int root_lock;
2444 /* everything at write_lock_level or lower must be write locked */
2445 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002446 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002447 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002448
Chris Mason6702ed42007-08-07 16:15:09 -04002449 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002450 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002451 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002452
Chris Masonbd681512011-07-16 15:23:14 -04002453 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002454 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002455
Chris Masonbd681512011-07-16 15:23:14 -04002456 /* when we are removing items, we might have to go up to level
2457 * two as we update tree pointers Make sure we keep write
2458 * for those levels as well
2459 */
2460 write_lock_level = 2;
2461 } else if (ins_len > 0) {
2462 /*
2463 * for inserting items, make sure we have a write lock on
2464 * level 1 so we can update keys
2465 */
2466 write_lock_level = 1;
2467 }
2468
2469 if (!cow)
2470 write_lock_level = -1;
2471
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002472 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002473 write_lock_level = BTRFS_MAX_LEVEL;
2474
Chris Masonf7c79f32012-03-19 15:54:38 -04002475 min_write_lock_level = write_lock_level;
2476
Chris Masonbb803952007-03-01 12:04:21 -05002477again:
Chris Masonbd681512011-07-16 15:23:14 -04002478 /*
2479 * we try very hard to do read locks on the root
2480 */
2481 root_lock = BTRFS_READ_LOCK;
2482 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002483 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002484 /*
2485 * the commit roots are read only
2486 * so we always do read locks
2487 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002488 b = root->commit_root;
2489 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002490 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002491 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002492 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002493 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002494 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002495 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002496 level = btrfs_header_level(b);
2497 } else {
2498 /* we don't know the level of the root node
2499 * until we actually have it read locked
2500 */
2501 b = btrfs_read_lock_root_node(root);
2502 level = btrfs_header_level(b);
2503 if (level <= write_lock_level) {
2504 /* whoops, must trade for write lock */
2505 btrfs_tree_read_unlock(b);
2506 free_extent_buffer(b);
2507 b = btrfs_lock_root_node(root);
2508 root_lock = BTRFS_WRITE_LOCK;
2509
2510 /* the level might have changed, check again */
2511 level = btrfs_header_level(b);
2512 }
2513 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002514 }
Chris Masonbd681512011-07-16 15:23:14 -04002515 p->nodes[level] = b;
2516 if (!p->skip_locking)
2517 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002518
Chris Masoneb60cea2007-02-02 09:18:22 -05002519 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002520 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002521
2522 /*
2523 * setup the path here so we can release it under lock
2524 * contention with the cow code
2525 */
Chris Mason02217ed2007-03-02 16:08:05 -05002526 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002527 /*
2528 * if we don't really need to cow this block
2529 * then we don't want to set the path blocking,
2530 * so we test it here
2531 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002532 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002533 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002534
Chris Masonb4ce94d2009-02-04 09:25:08 -05002535 btrfs_set_path_blocking(p);
2536
Chris Masonbd681512011-07-16 15:23:14 -04002537 /*
2538 * must have write locks on this node and the
2539 * parent
2540 */
Josef Bacik5124e002012-11-07 13:44:13 -05002541 if (level > write_lock_level ||
2542 (level + 1 > write_lock_level &&
2543 level + 1 < BTRFS_MAX_LEVEL &&
2544 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002545 write_lock_level = level + 1;
2546 btrfs_release_path(p);
2547 goto again;
2548 }
2549
Yan Zheng33c66f42009-07-22 09:59:00 -04002550 err = btrfs_cow_block(trans, root, b,
2551 p->nodes[level + 1],
2552 p->slots[level + 1], &b);
2553 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002554 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002555 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002556 }
Chris Mason02217ed2007-03-02 16:08:05 -05002557 }
Chris Mason65b51a02008-08-01 15:11:20 -04002558cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002559 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002560
Chris Masoneb60cea2007-02-02 09:18:22 -05002561 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002562 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002563
2564 /*
2565 * we have a lock on b and as long as we aren't changing
2566 * the tree, there is no way to for the items in b to change.
2567 * It is safe to drop the lock on our parent before we
2568 * go through the expensive btree search on b.
2569 *
2570 * If cow is true, then we might be changing slot zero,
2571 * which may require changing the parent. So, we can't
2572 * drop the lock until after we know which slot we're
2573 * operating on.
2574 */
2575 if (!cow)
2576 btrfs_unlock_up_safe(p, level + 1);
2577
Chris Mason5f39d392007-10-15 16:14:19 -04002578 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002579
Chris Mason5f39d392007-10-15 16:14:19 -04002580 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002581 int dec = 0;
2582 if (ret && slot > 0) {
2583 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002584 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002585 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002587 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002588 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002589 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002590 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002591 if (err) {
2592 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002593 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002594 }
Chris Masonc8c42862009-04-03 10:14:18 -04002595 b = p->nodes[level];
2596 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002597
Chris Masonbd681512011-07-16 15:23:14 -04002598 /*
2599 * slot 0 is special, if we change the key
2600 * we have to update the parent pointer
2601 * which means we must have a write lock
2602 * on the parent
2603 */
2604 if (slot == 0 && cow &&
2605 write_lock_level < level + 1) {
2606 write_lock_level = level + 1;
2607 btrfs_release_path(p);
2608 goto again;
2609 }
2610
Chris Masonf7c79f32012-03-19 15:54:38 -04002611 unlock_up(p, level, lowest_unlock,
2612 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002613
Chris Mason925baed2008-06-25 16:01:30 -04002614 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002615 if (dec)
2616 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002617 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002618 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002619
Yan Zheng33c66f42009-07-22 09:59:00 -04002620 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002621 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002622 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002623 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002624 if (err) {
2625 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002626 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002627 }
Chris Mason76a05b32009-05-14 13:24:30 -04002628
Chris Masonb4ce94d2009-02-04 09:25:08 -05002629 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002630 level = btrfs_header_level(b);
2631 if (level <= write_lock_level) {
2632 err = btrfs_try_tree_write_lock(b);
2633 if (!err) {
2634 btrfs_set_path_blocking(p);
2635 btrfs_tree_lock(b);
2636 btrfs_clear_path_blocking(p, b,
2637 BTRFS_WRITE_LOCK);
2638 }
2639 p->locks[level] = BTRFS_WRITE_LOCK;
2640 } else {
2641 err = btrfs_try_tree_read_lock(b);
2642 if (!err) {
2643 btrfs_set_path_blocking(p);
2644 btrfs_tree_read_lock(b);
2645 btrfs_clear_path_blocking(p, b,
2646 BTRFS_READ_LOCK);
2647 }
2648 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002649 }
Chris Masonbd681512011-07-16 15:23:14 -04002650 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002651 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002652 } else {
2653 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002654 if (ins_len > 0 &&
2655 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002656 if (write_lock_level < 1) {
2657 write_lock_level = 1;
2658 btrfs_release_path(p);
2659 goto again;
2660 }
2661
Chris Masonb4ce94d2009-02-04 09:25:08 -05002662 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002663 err = split_leaf(trans, root, key,
2664 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002665 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002666
Yan Zheng33c66f42009-07-22 09:59:00 -04002667 BUG_ON(err > 0);
2668 if (err) {
2669 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002670 goto done;
2671 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002672 }
Chris Mason459931e2008-12-10 09:10:46 -05002673 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002674 unlock_up(p, level, lowest_unlock,
2675 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002676 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002677 }
2678 }
Chris Mason65b51a02008-08-01 15:11:20 -04002679 ret = 1;
2680done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002681 /*
2682 * we don't really know what they plan on doing with the path
2683 * from here on, so for now just mark it as blocking
2684 */
Chris Masonb9473432009-03-13 11:00:37 -04002685 if (!p->leave_spinning)
2686 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002687 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002688 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002689 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002690}
2691
Chris Mason74123bd2007-02-02 11:05:29 -05002692/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002693 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2694 * current state of the tree together with the operations recorded in the tree
2695 * modification log to search for the key in a previous version of this tree, as
2696 * denoted by the time_seq parameter.
2697 *
2698 * Naturally, there is no support for insert, delete or cow operations.
2699 *
2700 * The resulting path and return value will be set up as if we called
2701 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2702 */
2703int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2704 struct btrfs_path *p, u64 time_seq)
2705{
2706 struct extent_buffer *b;
2707 int slot;
2708 int ret;
2709 int err;
2710 int level;
2711 int lowest_unlock = 1;
2712 u8 lowest_level = 0;
2713
2714 lowest_level = p->lowest_level;
2715 WARN_ON(p->nodes[0] != NULL);
2716
2717 if (p->search_commit_root) {
2718 BUG_ON(time_seq);
2719 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2720 }
2721
2722again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002723 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002724 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002725 p->locks[level] = BTRFS_READ_LOCK;
2726
2727 while (b) {
2728 level = btrfs_header_level(b);
2729 p->nodes[level] = b;
2730 btrfs_clear_path_blocking(p, NULL, 0);
2731
2732 /*
2733 * we have a lock on b and as long as we aren't changing
2734 * the tree, there is no way to for the items in b to change.
2735 * It is safe to drop the lock on our parent before we
2736 * go through the expensive btree search on b.
2737 */
2738 btrfs_unlock_up_safe(p, level + 1);
2739
2740 ret = bin_search(b, key, level, &slot);
2741
2742 if (level != 0) {
2743 int dec = 0;
2744 if (ret && slot > 0) {
2745 dec = 1;
2746 slot -= 1;
2747 }
2748 p->slots[level] = slot;
2749 unlock_up(p, level, lowest_unlock, 0, NULL);
2750
2751 if (level == lowest_level) {
2752 if (dec)
2753 p->slots[level]++;
2754 goto done;
2755 }
2756
2757 err = read_block_for_search(NULL, root, p, &b, level,
2758 slot, key, time_seq);
2759 if (err == -EAGAIN)
2760 goto again;
2761 if (err) {
2762 ret = err;
2763 goto done;
2764 }
2765
2766 level = btrfs_header_level(b);
2767 err = btrfs_try_tree_read_lock(b);
2768 if (!err) {
2769 btrfs_set_path_blocking(p);
2770 btrfs_tree_read_lock(b);
2771 btrfs_clear_path_blocking(p, b,
2772 BTRFS_READ_LOCK);
2773 }
Jan Schmidt47fb0912013-04-13 13:19:55 +00002774 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002775 p->locks[level] = BTRFS_READ_LOCK;
2776 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002777 } else {
2778 p->slots[level] = slot;
2779 unlock_up(p, level, lowest_unlock, 0, NULL);
2780 goto done;
2781 }
2782 }
2783 ret = 1;
2784done:
2785 if (!p->leave_spinning)
2786 btrfs_set_path_blocking(p);
2787 if (ret < 0)
2788 btrfs_release_path(p);
2789
2790 return ret;
2791}
2792
2793/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002794 * helper to use instead of search slot if no exact match is needed but
2795 * instead the next or previous item should be returned.
2796 * When find_higher is true, the next higher item is returned, the next lower
2797 * otherwise.
2798 * When return_any and find_higher are both true, and no higher item is found,
2799 * return the next lower instead.
2800 * When return_any is true and find_higher is false, and no lower item is found,
2801 * return the next higher instead.
2802 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2803 * < 0 on error
2804 */
2805int btrfs_search_slot_for_read(struct btrfs_root *root,
2806 struct btrfs_key *key, struct btrfs_path *p,
2807 int find_higher, int return_any)
2808{
2809 int ret;
2810 struct extent_buffer *leaf;
2811
2812again:
2813 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2814 if (ret <= 0)
2815 return ret;
2816 /*
2817 * a return value of 1 means the path is at the position where the
2818 * item should be inserted. Normally this is the next bigger item,
2819 * but in case the previous item is the last in a leaf, path points
2820 * to the first free slot in the previous leaf, i.e. at an invalid
2821 * item.
2822 */
2823 leaf = p->nodes[0];
2824
2825 if (find_higher) {
2826 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2827 ret = btrfs_next_leaf(root, p);
2828 if (ret <= 0)
2829 return ret;
2830 if (!return_any)
2831 return 1;
2832 /*
2833 * no higher item found, return the next
2834 * lower instead
2835 */
2836 return_any = 0;
2837 find_higher = 0;
2838 btrfs_release_path(p);
2839 goto again;
2840 }
2841 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002842 if (p->slots[0] == 0) {
2843 ret = btrfs_prev_leaf(root, p);
2844 if (ret < 0)
2845 return ret;
2846 if (!ret) {
2847 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2848 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002849 }
Arne Jansene6793762011-09-13 11:18:10 +02002850 if (!return_any)
2851 return 1;
2852 /*
2853 * no lower item found, return the next
2854 * higher instead
2855 */
2856 return_any = 0;
2857 find_higher = 1;
2858 btrfs_release_path(p);
2859 goto again;
2860 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002861 --p->slots[0];
2862 }
2863 }
2864 return 0;
2865}
2866
2867/*
Chris Mason74123bd2007-02-02 11:05:29 -05002868 * adjust the pointers going up the tree, starting at level
2869 * making sure the right key of each node is points to 'key'.
2870 * This is used after shifting pointers to the left, so it stops
2871 * fixing up pointers when a given leaf/node is not in slot 0 of the
2872 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002873 *
Chris Mason74123bd2007-02-02 11:05:29 -05002874 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002875static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002876 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002877{
2878 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002879 struct extent_buffer *t;
2880
Chris Mason234b63a2007-03-13 10:46:10 -04002881 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002882 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002883 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002884 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002885 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002886 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002887 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002888 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002889 if (tslot != 0)
2890 break;
2891 }
2892}
2893
Chris Mason74123bd2007-02-02 11:05:29 -05002894/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002895 * update item key.
2896 *
2897 * This function isn't completely safe. It's the caller's responsibility
2898 * that the new key won't break the order
2899 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002900void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002901 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002902{
2903 struct btrfs_disk_key disk_key;
2904 struct extent_buffer *eb;
2905 int slot;
2906
2907 eb = path->nodes[0];
2908 slot = path->slots[0];
2909 if (slot > 0) {
2910 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002911 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002912 }
2913 if (slot < btrfs_header_nritems(eb) - 1) {
2914 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002915 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002916 }
2917
2918 btrfs_cpu_key_to_disk(&disk_key, new_key);
2919 btrfs_set_item_key(eb, &disk_key, slot);
2920 btrfs_mark_buffer_dirty(eb);
2921 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002922 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002923}
2924
2925/*
Chris Mason74123bd2007-02-02 11:05:29 -05002926 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002927 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002928 *
2929 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2930 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002931 */
Chris Mason98ed5172008-01-03 10:01:48 -05002932static int push_node_left(struct btrfs_trans_handle *trans,
2933 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002934 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002935{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002936 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002937 int src_nritems;
2938 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002939 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002940
Chris Mason5f39d392007-10-15 16:14:19 -04002941 src_nritems = btrfs_header_nritems(src);
2942 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002943 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002944 WARN_ON(btrfs_header_generation(src) != trans->transid);
2945 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002946
Chris Masonbce4eae2008-04-24 14:42:46 -04002947 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002948 return 1;
2949
Chris Masond3977122009-01-05 21:25:51 -05002950 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002951 return 1;
2952
Chris Masonbce4eae2008-04-24 14:42:46 -04002953 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002954 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002955 if (push_items < src_nritems) {
2956 /* leave at least 8 pointers in the node if
2957 * we aren't going to empty it
2958 */
2959 if (src_nritems - push_items < 8) {
2960 if (push_items <= 8)
2961 return 1;
2962 push_items -= 8;
2963 }
2964 }
2965 } else
2966 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002967
Jan Schmidtf2304752012-05-26 11:43:17 +02002968 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00002969 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002970 copy_extent_buffer(dst, src,
2971 btrfs_node_key_ptr_offset(dst_nritems),
2972 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002973 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002974
Chris Masonbb803952007-03-01 12:04:21 -05002975 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02002976 /*
2977 * don't call tree_mod_log_eb_move here, key removal was already
2978 * fully logged by tree_mod_log_eb_copy above.
2979 */
Chris Mason5f39d392007-10-15 16:14:19 -04002980 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2981 btrfs_node_key_ptr_offset(push_items),
2982 (src_nritems - push_items) *
2983 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002984 }
Chris Mason5f39d392007-10-15 16:14:19 -04002985 btrfs_set_header_nritems(src, src_nritems - push_items);
2986 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2987 btrfs_mark_buffer_dirty(src);
2988 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002989
Chris Masonbb803952007-03-01 12:04:21 -05002990 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002991}
2992
Chris Mason97571fd2007-02-24 13:39:08 -05002993/*
Chris Mason79f95c82007-03-01 15:16:26 -05002994 * try to push data from one node into the next node right in the
2995 * tree.
2996 *
2997 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2998 * error, and > 0 if there was no room in the right hand block.
2999 *
3000 * this will only push up to 1/2 the contents of the left node over
3001 */
Chris Mason5f39d392007-10-15 16:14:19 -04003002static int balance_node_right(struct btrfs_trans_handle *trans,
3003 struct btrfs_root *root,
3004 struct extent_buffer *dst,
3005 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003006{
Chris Mason79f95c82007-03-01 15:16:26 -05003007 int push_items = 0;
3008 int max_push;
3009 int src_nritems;
3010 int dst_nritems;
3011 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003012
Chris Mason7bb86312007-12-11 09:25:06 -05003013 WARN_ON(btrfs_header_generation(src) != trans->transid);
3014 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3015
Chris Mason5f39d392007-10-15 16:14:19 -04003016 src_nritems = btrfs_header_nritems(src);
3017 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003018 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003019 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003020 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003021
Chris Masond3977122009-01-05 21:25:51 -05003022 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003023 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003024
3025 max_push = src_nritems / 2 + 1;
3026 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003027 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003028 return 1;
Yan252c38f2007-08-29 09:11:44 -04003029
Chris Mason79f95c82007-03-01 15:16:26 -05003030 if (max_push < push_items)
3031 push_items = max_push;
3032
Jan Schmidtf2304752012-05-26 11:43:17 +02003033 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003034 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3035 btrfs_node_key_ptr_offset(0),
3036 (dst_nritems) *
3037 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003038
Jan Schmidtf2304752012-05-26 11:43:17 +02003039 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003040 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003041 copy_extent_buffer(dst, src,
3042 btrfs_node_key_ptr_offset(0),
3043 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003044 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003045
Chris Mason5f39d392007-10-15 16:14:19 -04003046 btrfs_set_header_nritems(src, src_nritems - push_items);
3047 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003048
Chris Mason5f39d392007-10-15 16:14:19 -04003049 btrfs_mark_buffer_dirty(src);
3050 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003051
Chris Mason79f95c82007-03-01 15:16:26 -05003052 return ret;
3053}
3054
3055/*
Chris Mason97571fd2007-02-24 13:39:08 -05003056 * helper function to insert a new root level in the tree.
3057 * A new node is allocated, and a single item is inserted to
3058 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003059 *
3060 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003061 */
Chris Masond3977122009-01-05 21:25:51 -05003062static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003063 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003064 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003065{
Chris Mason7bb86312007-12-11 09:25:06 -05003066 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003067 struct extent_buffer *lower;
3068 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003069 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003070 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003071
3072 BUG_ON(path->nodes[level]);
3073 BUG_ON(path->nodes[level-1] != root->node);
3074
Chris Mason7bb86312007-12-11 09:25:06 -05003075 lower = path->nodes[level-1];
3076 if (level == 1)
3077 btrfs_item_key(lower, &lower_key, 0);
3078 else
3079 btrfs_node_key(lower, &lower_key, 0);
3080
Zheng Yan31840ae2008-09-23 13:14:14 -04003081 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003082 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003083 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003084 if (IS_ERR(c))
3085 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003086
Yan, Zhengf0486c62010-05-16 10:46:25 -04003087 root_add_used(root, root->nodesize);
3088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003089 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003090 btrfs_set_header_nritems(c, 1);
3091 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003092 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003093 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003094 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003095 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003096
Chris Mason5f39d392007-10-15 16:14:19 -04003097 write_extent_buffer(c, root->fs_info->fsid,
3098 (unsigned long)btrfs_header_fsid(c),
3099 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003100
3101 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3102 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3103 BTRFS_UUID_SIZE);
3104
Chris Mason5f39d392007-10-15 16:14:19 -04003105 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003106 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003107 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003108 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003109
3110 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003111
3112 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003113
Chris Mason925baed2008-06-25 16:01:30 -04003114 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003115 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003116 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003117
3118 /* the super has an extra ref to root->node */
3119 free_extent_buffer(old);
3120
Chris Mason0b86a832008-03-24 15:01:56 -04003121 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003122 extent_buffer_get(c);
3123 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003124 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003125 path->slots[level] = 0;
3126 return 0;
3127}
3128
Chris Mason74123bd2007-02-02 11:05:29 -05003129/*
3130 * worker function to insert a single pointer in a node.
3131 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003132 *
Chris Mason74123bd2007-02-02 11:05:29 -05003133 * slot and level indicate where you want the key to go, and
3134 * blocknr is the block the key points to.
3135 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003136static void insert_ptr(struct btrfs_trans_handle *trans,
3137 struct btrfs_root *root, struct btrfs_path *path,
3138 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003139 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003140{
Chris Mason5f39d392007-10-15 16:14:19 -04003141 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003142 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003143 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003144
3145 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003146 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003147 lower = path->nodes[level];
3148 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003149 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003150 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003151 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003152 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003153 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3154 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003155 memmove_extent_buffer(lower,
3156 btrfs_node_key_ptr_offset(slot + 1),
3157 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003158 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003159 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003160 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003161 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003162 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003163 BUG_ON(ret < 0);
3164 }
Chris Mason5f39d392007-10-15 16:14:19 -04003165 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003166 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003167 WARN_ON(trans->transid == 0);
3168 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003169 btrfs_set_header_nritems(lower, nritems + 1);
3170 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003171}
3172
Chris Mason97571fd2007-02-24 13:39:08 -05003173/*
3174 * split the node at the specified level in path in two.
3175 * The path is corrected to point to the appropriate node after the split
3176 *
3177 * Before splitting this tries to make some room in the node by pushing
3178 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003179 *
3180 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003181 */
Chris Masone02119d2008-09-05 16:13:11 -04003182static noinline int split_node(struct btrfs_trans_handle *trans,
3183 struct btrfs_root *root,
3184 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003185{
Chris Mason5f39d392007-10-15 16:14:19 -04003186 struct extent_buffer *c;
3187 struct extent_buffer *split;
3188 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003189 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003190 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003191 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003192
Chris Mason5f39d392007-10-15 16:14:19 -04003193 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003194 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003195 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003196 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003197 * trying to split the root, lets make a new one
3198 *
Liu Bofdd99c72013-05-22 12:06:51 +00003199 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003200 * insert_new_root, because that root buffer will be kept as a
3201 * normal node. We are going to log removal of half of the
3202 * elements below with tree_mod_log_eb_copy. We're holding a
3203 * tree lock on the buffer, which is why we cannot race with
3204 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003205 */
Liu Bofdd99c72013-05-22 12:06:51 +00003206 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003207 if (ret)
3208 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003209 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003210 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003211 c = path->nodes[level];
3212 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003213 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003214 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003215 if (ret < 0)
3216 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003217 }
Chris Masone66f7092007-04-20 13:16:02 -04003218
Chris Mason5f39d392007-10-15 16:14:19 -04003219 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003220 mid = (c_nritems + 1) / 2;
3221 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003222
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003223 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003224 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003225 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003226 if (IS_ERR(split))
3227 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003228
Yan, Zhengf0486c62010-05-16 10:46:25 -04003229 root_add_used(root, root->nodesize);
3230
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003231 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003232 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003233 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003234 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003235 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003236 btrfs_set_header_owner(split, root->root_key.objectid);
3237 write_extent_buffer(split, root->fs_info->fsid,
3238 (unsigned long)btrfs_header_fsid(split),
3239 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003240 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3241 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3242 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003243
Jan Schmidt90f8d622013-04-13 13:19:53 +00003244 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003245 copy_extent_buffer(split, c,
3246 btrfs_node_key_ptr_offset(0),
3247 btrfs_node_key_ptr_offset(mid),
3248 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3249 btrfs_set_header_nritems(split, c_nritems - mid);
3250 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003251 ret = 0;
3252
Chris Mason5f39d392007-10-15 16:14:19 -04003253 btrfs_mark_buffer_dirty(c);
3254 btrfs_mark_buffer_dirty(split);
3255
Jeff Mahoney143bede2012-03-01 14:56:26 +01003256 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003257 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003258
Chris Mason5de08d72007-02-24 06:24:44 -05003259 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003260 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003261 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003262 free_extent_buffer(c);
3263 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003264 path->slots[level + 1] += 1;
3265 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003266 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003267 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003268 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003269 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003270}
3271
Chris Mason74123bd2007-02-02 11:05:29 -05003272/*
3273 * how many bytes are required to store the items in a leaf. start
3274 * and nr indicate which items in the leaf to check. This totals up the
3275 * space used both by the item structs and the item data
3276 */
Chris Mason5f39d392007-10-15 16:14:19 -04003277static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003278{
Josef Bacik41be1f32012-10-15 13:43:18 -04003279 struct btrfs_item *start_item;
3280 struct btrfs_item *end_item;
3281 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003282 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003283 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003284 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003285
3286 if (!nr)
3287 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003288 btrfs_init_map_token(&token);
3289 start_item = btrfs_item_nr(l, start);
3290 end_item = btrfs_item_nr(l, end);
3291 data_len = btrfs_token_item_offset(l, start_item, &token) +
3292 btrfs_token_item_size(l, start_item, &token);
3293 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003294 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003295 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003296 return data_len;
3297}
3298
Chris Mason74123bd2007-02-02 11:05:29 -05003299/*
Chris Masond4dbff92007-04-04 14:08:15 -04003300 * The space between the end of the leaf items and
3301 * the start of the leaf data. IOW, how much room
3302 * the leaf has left for both items and data
3303 */
Chris Masond3977122009-01-05 21:25:51 -05003304noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003305 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003306{
Chris Mason5f39d392007-10-15 16:14:19 -04003307 int nritems = btrfs_header_nritems(leaf);
3308 int ret;
3309 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3310 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003311 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3312 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003313 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003314 leaf_space_used(leaf, 0, nritems), nritems);
3315 }
3316 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003317}
3318
Chris Mason99d8f832010-07-07 10:51:48 -04003319/*
3320 * min slot controls the lowest index we're willing to push to the
3321 * right. We'll push up to and including min_slot, but no lower
3322 */
Chris Mason44871b12009-03-13 10:04:31 -04003323static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3324 struct btrfs_root *root,
3325 struct btrfs_path *path,
3326 int data_size, int empty,
3327 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003328 int free_space, u32 left_nritems,
3329 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003330{
Chris Mason5f39d392007-10-15 16:14:19 -04003331 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003332 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003333 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003334 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003335 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003336 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003337 int push_space = 0;
3338 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003339 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003340 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003341 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003342 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003343 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003344
Chris Masoncfed81a2012-03-03 07:40:03 -05003345 btrfs_init_map_token(&token);
3346
Chris Mason34a38212007-11-07 13:31:03 -05003347 if (empty)
3348 nr = 0;
3349 else
Chris Mason99d8f832010-07-07 10:51:48 -04003350 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003351
Zheng Yan31840ae2008-09-23 13:14:14 -04003352 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003353 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003354
Chris Mason44871b12009-03-13 10:04:31 -04003355 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003356 i = left_nritems - 1;
3357 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003358 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003359
Zheng Yan31840ae2008-09-23 13:14:14 -04003360 if (!empty && push_items > 0) {
3361 if (path->slots[0] > i)
3362 break;
3363 if (path->slots[0] == i) {
3364 int space = btrfs_leaf_free_space(root, left);
3365 if (space + push_space * 2 > free_space)
3366 break;
3367 }
3368 }
3369
Chris Mason00ec4c52007-02-24 12:47:20 -05003370 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003371 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003372
Chris Masondb945352007-10-15 16:15:53 -04003373 this_item_size = btrfs_item_size(left, item);
3374 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003375 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003376
Chris Mason00ec4c52007-02-24 12:47:20 -05003377 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003378 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003379 if (i == 0)
3380 break;
3381 i--;
Chris Masondb945352007-10-15 16:15:53 -04003382 }
Chris Mason5f39d392007-10-15 16:14:19 -04003383
Chris Mason925baed2008-06-25 16:01:30 -04003384 if (push_items == 0)
3385 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003386
Julia Lawall6c1500f2012-11-03 20:30:18 +00003387 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003388
Chris Mason00ec4c52007-02-24 12:47:20 -05003389 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003390 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003391
Chris Mason5f39d392007-10-15 16:14:19 -04003392 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003393 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003394
Chris Mason00ec4c52007-02-24 12:47:20 -05003395 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003396 data_end = leaf_data_end(root, right);
3397 memmove_extent_buffer(right,
3398 btrfs_leaf_data(right) + data_end - push_space,
3399 btrfs_leaf_data(right) + data_end,
3400 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3401
Chris Mason00ec4c52007-02-24 12:47:20 -05003402 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003403 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003404 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3405 btrfs_leaf_data(left) + leaf_data_end(root, left),
3406 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003407
3408 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3409 btrfs_item_nr_offset(0),
3410 right_nritems * sizeof(struct btrfs_item));
3411
Chris Mason00ec4c52007-02-24 12:47:20 -05003412 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003413 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3414 btrfs_item_nr_offset(left_nritems - push_items),
3415 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003416
3417 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003418 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003419 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003420 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003421 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003422 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003423 push_space -= btrfs_token_item_size(right, item, &token);
3424 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003425 }
3426
Chris Mason7518a232007-03-12 12:01:18 -04003427 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003428 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003429
Chris Mason34a38212007-11-07 13:31:03 -05003430 if (left_nritems)
3431 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003432 else
3433 clean_tree_block(trans, root, left);
3434
Chris Mason5f39d392007-10-15 16:14:19 -04003435 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003436
Chris Mason5f39d392007-10-15 16:14:19 -04003437 btrfs_item_key(right, &disk_key, 0);
3438 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003439 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003440
Chris Mason00ec4c52007-02-24 12:47:20 -05003441 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003442 if (path->slots[0] >= left_nritems) {
3443 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003444 if (btrfs_header_nritems(path->nodes[0]) == 0)
3445 clean_tree_block(trans, root, path->nodes[0]);
3446 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003447 free_extent_buffer(path->nodes[0]);
3448 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003449 path->slots[1] += 1;
3450 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003451 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003452 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003453 }
3454 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003455
3456out_unlock:
3457 btrfs_tree_unlock(right);
3458 free_extent_buffer(right);
3459 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003460}
Chris Mason925baed2008-06-25 16:01:30 -04003461
Chris Mason00ec4c52007-02-24 12:47:20 -05003462/*
Chris Mason44871b12009-03-13 10:04:31 -04003463 * push some data in the path leaf to the right, trying to free up at
3464 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3465 *
3466 * returns 1 if the push failed because the other node didn't have enough
3467 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003468 *
3469 * this will push starting from min_slot to the end of the leaf. It won't
3470 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003471 */
3472static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003473 *root, struct btrfs_path *path,
3474 int min_data_size, int data_size,
3475 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003476{
3477 struct extent_buffer *left = path->nodes[0];
3478 struct extent_buffer *right;
3479 struct extent_buffer *upper;
3480 int slot;
3481 int free_space;
3482 u32 left_nritems;
3483 int ret;
3484
3485 if (!path->nodes[1])
3486 return 1;
3487
3488 slot = path->slots[1];
3489 upper = path->nodes[1];
3490 if (slot >= btrfs_header_nritems(upper) - 1)
3491 return 1;
3492
3493 btrfs_assert_tree_locked(path->nodes[1]);
3494
3495 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003496 if (right == NULL)
3497 return 1;
3498
Chris Mason44871b12009-03-13 10:04:31 -04003499 btrfs_tree_lock(right);
3500 btrfs_set_lock_blocking(right);
3501
3502 free_space = btrfs_leaf_free_space(root, right);
3503 if (free_space < data_size)
3504 goto out_unlock;
3505
3506 /* cow and double check */
3507 ret = btrfs_cow_block(trans, root, right, upper,
3508 slot + 1, &right);
3509 if (ret)
3510 goto out_unlock;
3511
3512 free_space = btrfs_leaf_free_space(root, right);
3513 if (free_space < data_size)
3514 goto out_unlock;
3515
3516 left_nritems = btrfs_header_nritems(left);
3517 if (left_nritems == 0)
3518 goto out_unlock;
3519
Chris Mason99d8f832010-07-07 10:51:48 -04003520 return __push_leaf_right(trans, root, path, min_data_size, empty,
3521 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003522out_unlock:
3523 btrfs_tree_unlock(right);
3524 free_extent_buffer(right);
3525 return 1;
3526}
3527
3528/*
Chris Mason74123bd2007-02-02 11:05:29 -05003529 * push some data in the path leaf to the left, trying to free up at
3530 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003531 *
3532 * max_slot can put a limit on how far into the leaf we'll push items. The
3533 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3534 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003535 */
Chris Mason44871b12009-03-13 10:04:31 -04003536static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3537 struct btrfs_root *root,
3538 struct btrfs_path *path, int data_size,
3539 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003540 int free_space, u32 right_nritems,
3541 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003542{
Chris Mason5f39d392007-10-15 16:14:19 -04003543 struct btrfs_disk_key disk_key;
3544 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003545 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003546 int push_space = 0;
3547 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003548 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003549 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003550 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003551 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003552 u32 this_item_size;
3553 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003554 struct btrfs_map_token token;
3555
3556 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003557
Chris Mason34a38212007-11-07 13:31:03 -05003558 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003559 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003560 else
Chris Mason99d8f832010-07-07 10:51:48 -04003561 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003562
3563 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003564 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003565
Zheng Yan31840ae2008-09-23 13:14:14 -04003566 if (!empty && push_items > 0) {
3567 if (path->slots[0] < i)
3568 break;
3569 if (path->slots[0] == i) {
3570 int space = btrfs_leaf_free_space(root, right);
3571 if (space + push_space * 2 > free_space)
3572 break;
3573 }
3574 }
3575
Chris Masonbe0e5c02007-01-26 15:51:26 -05003576 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003577 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003578
3579 this_item_size = btrfs_item_size(right, item);
3580 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003581 break;
Chris Masondb945352007-10-15 16:15:53 -04003582
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003584 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003585 }
Chris Masondb945352007-10-15 16:15:53 -04003586
Chris Masonbe0e5c02007-01-26 15:51:26 -05003587 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003588 ret = 1;
3589 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003590 }
Chris Mason34a38212007-11-07 13:31:03 -05003591 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003592 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003593
Chris Masonbe0e5c02007-01-26 15:51:26 -05003594 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003595 copy_extent_buffer(left, right,
3596 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3597 btrfs_item_nr_offset(0),
3598 push_items * sizeof(struct btrfs_item));
3599
Chris Mason123abc82007-03-14 14:14:43 -04003600 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003601 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003602
3603 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003604 leaf_data_end(root, left) - push_space,
3605 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003606 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003607 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003608 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003609 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003610
Chris Masondb945352007-10-15 16:15:53 -04003611 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003612 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003613 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003616
Chris Masoncfed81a2012-03-03 07:40:03 -05003617 ioff = btrfs_token_item_offset(left, item, &token);
3618 btrfs_set_token_item_offset(left, item,
3619 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3620 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003621 }
Chris Mason5f39d392007-10-15 16:14:19 -04003622 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003623
3624 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003625 if (push_items > right_nritems)
3626 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003627 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003628
Chris Mason34a38212007-11-07 13:31:03 -05003629 if (push_items < right_nritems) {
3630 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3631 leaf_data_end(root, right);
3632 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3633 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3634 btrfs_leaf_data(right) +
3635 leaf_data_end(root, right), push_space);
3636
3637 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003638 btrfs_item_nr_offset(push_items),
3639 (btrfs_header_nritems(right) - push_items) *
3640 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003641 }
Yaneef1c492007-11-26 10:58:13 -05003642 right_nritems -= push_items;
3643 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003644 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003645 for (i = 0; i < right_nritems; i++) {
3646 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003647
Chris Masoncfed81a2012-03-03 07:40:03 -05003648 push_space = push_space - btrfs_token_item_size(right,
3649 item, &token);
3650 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003651 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003652
Chris Mason5f39d392007-10-15 16:14:19 -04003653 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003654 if (right_nritems)
3655 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003656 else
3657 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003658
Chris Mason5f39d392007-10-15 16:14:19 -04003659 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003660 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003661
3662 /* then fixup the leaf pointer in the path */
3663 if (path->slots[0] < push_items) {
3664 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003665 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003666 free_extent_buffer(path->nodes[0]);
3667 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003668 path->slots[1] -= 1;
3669 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003670 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003671 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003672 path->slots[0] -= push_items;
3673 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003674 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003675 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003676out:
3677 btrfs_tree_unlock(left);
3678 free_extent_buffer(left);
3679 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003680}
3681
Chris Mason74123bd2007-02-02 11:05:29 -05003682/*
Chris Mason44871b12009-03-13 10:04:31 -04003683 * push some data in the path leaf to the left, trying to free up at
3684 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003685 *
3686 * max_slot can put a limit on how far into the leaf we'll push items. The
3687 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3688 * items
Chris Mason44871b12009-03-13 10:04:31 -04003689 */
3690static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003691 *root, struct btrfs_path *path, int min_data_size,
3692 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003693{
3694 struct extent_buffer *right = path->nodes[0];
3695 struct extent_buffer *left;
3696 int slot;
3697 int free_space;
3698 u32 right_nritems;
3699 int ret = 0;
3700
3701 slot = path->slots[1];
3702 if (slot == 0)
3703 return 1;
3704 if (!path->nodes[1])
3705 return 1;
3706
3707 right_nritems = btrfs_header_nritems(right);
3708 if (right_nritems == 0)
3709 return 1;
3710
3711 btrfs_assert_tree_locked(path->nodes[1]);
3712
3713 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003714 if (left == NULL)
3715 return 1;
3716
Chris Mason44871b12009-03-13 10:04:31 -04003717 btrfs_tree_lock(left);
3718 btrfs_set_lock_blocking(left);
3719
3720 free_space = btrfs_leaf_free_space(root, left);
3721 if (free_space < data_size) {
3722 ret = 1;
3723 goto out;
3724 }
3725
3726 /* cow and double check */
3727 ret = btrfs_cow_block(trans, root, left,
3728 path->nodes[1], slot - 1, &left);
3729 if (ret) {
3730 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003731 if (ret == -ENOSPC)
3732 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003733 goto out;
3734 }
3735
3736 free_space = btrfs_leaf_free_space(root, left);
3737 if (free_space < data_size) {
3738 ret = 1;
3739 goto out;
3740 }
3741
Chris Mason99d8f832010-07-07 10:51:48 -04003742 return __push_leaf_left(trans, root, path, min_data_size,
3743 empty, left, free_space, right_nritems,
3744 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003745out:
3746 btrfs_tree_unlock(left);
3747 free_extent_buffer(left);
3748 return ret;
3749}
3750
3751/*
Chris Mason74123bd2007-02-02 11:05:29 -05003752 * split the path's leaf in two, making sure there is at least data_size
3753 * available for the resulting leaf level of the path.
3754 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003755static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3756 struct btrfs_root *root,
3757 struct btrfs_path *path,
3758 struct extent_buffer *l,
3759 struct extent_buffer *right,
3760 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003761{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003762 int data_copy_size;
3763 int rt_data_off;
3764 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003765 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003766 struct btrfs_map_token token;
3767
3768 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003769
Chris Mason5f39d392007-10-15 16:14:19 -04003770 nritems = nritems - mid;
3771 btrfs_set_header_nritems(right, nritems);
3772 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3773
3774 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3775 btrfs_item_nr_offset(mid),
3776 nritems * sizeof(struct btrfs_item));
3777
3778 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003779 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3780 data_copy_size, btrfs_leaf_data(l) +
3781 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003782
Chris Mason5f39d392007-10-15 16:14:19 -04003783 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3784 btrfs_item_end_nr(l, mid);
3785
3786 for (i = 0; i < nritems; i++) {
3787 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003788 u32 ioff;
3789
Chris Masoncfed81a2012-03-03 07:40:03 -05003790 ioff = btrfs_token_item_offset(right, item, &token);
3791 btrfs_set_token_item_offset(right, item,
3792 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003793 }
Chris Mason74123bd2007-02-02 11:05:29 -05003794
Chris Mason5f39d392007-10-15 16:14:19 -04003795 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003796 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003797 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003798 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003799
3800 btrfs_mark_buffer_dirty(right);
3801 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003802 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Chris Masonbe0e5c02007-01-26 15:51:26 -05003804 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003805 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003806 free_extent_buffer(path->nodes[0]);
3807 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003808 path->slots[0] -= mid;
3809 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003810 } else {
3811 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003812 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003813 }
Chris Mason5f39d392007-10-15 16:14:19 -04003814
Chris Masoneb60cea2007-02-02 09:18:22 -05003815 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003816}
3817
3818/*
Chris Mason99d8f832010-07-07 10:51:48 -04003819 * double splits happen when we need to insert a big item in the middle
3820 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3821 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3822 * A B C
3823 *
3824 * We avoid this by trying to push the items on either side of our target
3825 * into the adjacent leaves. If all goes well we can avoid the double split
3826 * completely.
3827 */
3828static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3829 struct btrfs_root *root,
3830 struct btrfs_path *path,
3831 int data_size)
3832{
3833 int ret;
3834 int progress = 0;
3835 int slot;
3836 u32 nritems;
3837
3838 slot = path->slots[0];
3839
3840 /*
3841 * try to push all the items after our slot into the
3842 * right leaf
3843 */
3844 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3845 if (ret < 0)
3846 return ret;
3847
3848 if (ret == 0)
3849 progress++;
3850
3851 nritems = btrfs_header_nritems(path->nodes[0]);
3852 /*
3853 * our goal is to get our slot at the start or end of a leaf. If
3854 * we've done so we're done
3855 */
3856 if (path->slots[0] == 0 || path->slots[0] == nritems)
3857 return 0;
3858
3859 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3860 return 0;
3861
3862 /* try to push all the items before our slot into the next leaf */
3863 slot = path->slots[0];
3864 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3865 if (ret < 0)
3866 return ret;
3867
3868 if (ret == 0)
3869 progress++;
3870
3871 if (progress)
3872 return 0;
3873 return 1;
3874}
3875
3876/*
Chris Mason44871b12009-03-13 10:04:31 -04003877 * split the path's leaf in two, making sure there is at least data_size
3878 * available for the resulting leaf level of the path.
3879 *
3880 * returns 0 if all went well and < 0 on failure.
3881 */
3882static noinline int split_leaf(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root,
3884 struct btrfs_key *ins_key,
3885 struct btrfs_path *path, int data_size,
3886 int extend)
3887{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003888 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003889 struct extent_buffer *l;
3890 u32 nritems;
3891 int mid;
3892 int slot;
3893 struct extent_buffer *right;
3894 int ret = 0;
3895 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003896 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003897 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003898 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003899
Yan, Zhenga5719522009-09-24 09:17:31 -04003900 l = path->nodes[0];
3901 slot = path->slots[0];
3902 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3903 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3904 return -EOVERFLOW;
3905
Chris Mason44871b12009-03-13 10:04:31 -04003906 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003907 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003908 wret = push_leaf_right(trans, root, path, data_size,
3909 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003910 if (wret < 0)
3911 return wret;
3912 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003913 wret = push_leaf_left(trans, root, path, data_size,
3914 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003915 if (wret < 0)
3916 return wret;
3917 }
3918 l = path->nodes[0];
3919
3920 /* did the pushes work? */
3921 if (btrfs_leaf_free_space(root, l) >= data_size)
3922 return 0;
3923 }
3924
3925 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003926 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003927 if (ret)
3928 return ret;
3929 }
3930again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003931 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003932 l = path->nodes[0];
3933 slot = path->slots[0];
3934 nritems = btrfs_header_nritems(l);
3935 mid = (nritems + 1) / 2;
3936
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003937 if (mid <= slot) {
3938 if (nritems == 1 ||
3939 leaf_space_used(l, mid, nritems - mid) + data_size >
3940 BTRFS_LEAF_DATA_SIZE(root)) {
3941 if (slot >= nritems) {
3942 split = 0;
3943 } else {
3944 mid = slot;
3945 if (mid != nritems &&
3946 leaf_space_used(l, mid, nritems - mid) +
3947 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003948 if (data_size && !tried_avoid_double)
3949 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003950 split = 2;
3951 }
3952 }
3953 }
3954 } else {
3955 if (leaf_space_used(l, 0, mid) + data_size >
3956 BTRFS_LEAF_DATA_SIZE(root)) {
3957 if (!extend && data_size && slot == 0) {
3958 split = 0;
3959 } else if ((extend || !data_size) && slot == 0) {
3960 mid = 1;
3961 } else {
3962 mid = slot;
3963 if (mid != nritems &&
3964 leaf_space_used(l, mid, nritems - mid) +
3965 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003966 if (data_size && !tried_avoid_double)
3967 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003968 split = 2 ;
3969 }
3970 }
3971 }
3972 }
3973
3974 if (split == 0)
3975 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3976 else
3977 btrfs_item_key(l, &disk_key, mid);
3978
3979 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003980 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003981 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003982 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003983 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003984
3985 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003986
3987 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3988 btrfs_set_header_bytenr(right, right->start);
3989 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003990 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003991 btrfs_set_header_owner(right, root->root_key.objectid);
3992 btrfs_set_header_level(right, 0);
3993 write_extent_buffer(right, root->fs_info->fsid,
3994 (unsigned long)btrfs_header_fsid(right),
3995 BTRFS_FSID_SIZE);
3996
3997 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3998 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3999 BTRFS_UUID_SIZE);
4000
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004001 if (split == 0) {
4002 if (mid <= slot) {
4003 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004004 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004005 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004006 btrfs_tree_unlock(path->nodes[0]);
4007 free_extent_buffer(path->nodes[0]);
4008 path->nodes[0] = right;
4009 path->slots[0] = 0;
4010 path->slots[1] += 1;
4011 } else {
4012 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004013 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004014 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004015 btrfs_tree_unlock(path->nodes[0]);
4016 free_extent_buffer(path->nodes[0]);
4017 path->nodes[0] = right;
4018 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004019 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004020 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004021 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004022 btrfs_mark_buffer_dirty(right);
4023 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004024 }
4025
Jeff Mahoney143bede2012-03-01 14:56:26 +01004026 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004027
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004029 BUG_ON(num_doubles != 0);
4030 num_doubles++;
4031 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004032 }
Chris Mason44871b12009-03-13 10:04:31 -04004033
Jeff Mahoney143bede2012-03-01 14:56:26 +01004034 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004035
4036push_for_double:
4037 push_for_double_split(trans, root, path, data_size);
4038 tried_avoid_double = 1;
4039 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4040 return 0;
4041 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004042}
4043
Yan, Zhengad48fd752009-11-12 09:33:58 +00004044static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4045 struct btrfs_root *root,
4046 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004047{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004048 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004049 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004050 struct btrfs_file_extent_item *fi;
4051 u64 extent_len = 0;
4052 u32 item_size;
4053 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004054
4055 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004056 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4057
4058 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4059 key.type != BTRFS_EXTENT_CSUM_KEY);
4060
4061 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4062 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004063
4064 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004065 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4066 fi = btrfs_item_ptr(leaf, path->slots[0],
4067 struct btrfs_file_extent_item);
4068 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4069 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004070 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004071
Chris Mason459931e2008-12-10 09:10:46 -05004072 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004073 path->search_for_split = 1;
4074 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004075 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004076 if (ret < 0)
4077 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004078
Yan, Zhengad48fd752009-11-12 09:33:58 +00004079 ret = -EAGAIN;
4080 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004081 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004082 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4083 goto err;
4084
Chris Mason109f6ae2010-04-02 09:20:18 -04004085 /* the leaf has changed, it now has room. return now */
4086 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4087 goto err;
4088
Yan, Zhengad48fd752009-11-12 09:33:58 +00004089 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4090 fi = btrfs_item_ptr(leaf, path->slots[0],
4091 struct btrfs_file_extent_item);
4092 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4093 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004094 }
4095
Chris Masonb9473432009-03-13 11:00:37 -04004096 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004097 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004098 if (ret)
4099 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004100
Yan, Zhengad48fd752009-11-12 09:33:58 +00004101 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004102 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004103 return 0;
4104err:
4105 path->keep_locks = 0;
4106 return ret;
4107}
4108
4109static noinline int split_item(struct btrfs_trans_handle *trans,
4110 struct btrfs_root *root,
4111 struct btrfs_path *path,
4112 struct btrfs_key *new_key,
4113 unsigned long split_offset)
4114{
4115 struct extent_buffer *leaf;
4116 struct btrfs_item *item;
4117 struct btrfs_item *new_item;
4118 int slot;
4119 char *buf;
4120 u32 nritems;
4121 u32 item_size;
4122 u32 orig_offset;
4123 struct btrfs_disk_key disk_key;
4124
Chris Masonb9473432009-03-13 11:00:37 -04004125 leaf = path->nodes[0];
4126 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4127
Chris Masonb4ce94d2009-02-04 09:25:08 -05004128 btrfs_set_path_blocking(path);
4129
Chris Mason459931e2008-12-10 09:10:46 -05004130 item = btrfs_item_nr(leaf, path->slots[0]);
4131 orig_offset = btrfs_item_offset(leaf, item);
4132 item_size = btrfs_item_size(leaf, item);
4133
Chris Mason459931e2008-12-10 09:10:46 -05004134 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004135 if (!buf)
4136 return -ENOMEM;
4137
Chris Mason459931e2008-12-10 09:10:46 -05004138 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4139 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004140
Chris Mason459931e2008-12-10 09:10:46 -05004141 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004142 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004143 if (slot != nritems) {
4144 /* shift the items */
4145 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004146 btrfs_item_nr_offset(slot),
4147 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004148 }
4149
4150 btrfs_cpu_key_to_disk(&disk_key, new_key);
4151 btrfs_set_item_key(leaf, &disk_key, slot);
4152
4153 new_item = btrfs_item_nr(leaf, slot);
4154
4155 btrfs_set_item_offset(leaf, new_item, orig_offset);
4156 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4157
4158 btrfs_set_item_offset(leaf, item,
4159 orig_offset + item_size - split_offset);
4160 btrfs_set_item_size(leaf, item, split_offset);
4161
4162 btrfs_set_header_nritems(leaf, nritems + 1);
4163
4164 /* write the data for the start of the original item */
4165 write_extent_buffer(leaf, buf,
4166 btrfs_item_ptr_offset(leaf, path->slots[0]),
4167 split_offset);
4168
4169 /* write the data for the new item */
4170 write_extent_buffer(leaf, buf + split_offset,
4171 btrfs_item_ptr_offset(leaf, slot),
4172 item_size - split_offset);
4173 btrfs_mark_buffer_dirty(leaf);
4174
Yan, Zhengad48fd752009-11-12 09:33:58 +00004175 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004176 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004177 return 0;
4178}
4179
4180/*
4181 * This function splits a single item into two items,
4182 * giving 'new_key' to the new item and splitting the
4183 * old one at split_offset (from the start of the item).
4184 *
4185 * The path may be released by this operation. After
4186 * the split, the path is pointing to the old item. The
4187 * new item is going to be in the same node as the old one.
4188 *
4189 * Note, the item being split must be smaller enough to live alone on
4190 * a tree block with room for one extra struct btrfs_item
4191 *
4192 * This allows us to split the item in place, keeping a lock on the
4193 * leaf the entire time.
4194 */
4195int btrfs_split_item(struct btrfs_trans_handle *trans,
4196 struct btrfs_root *root,
4197 struct btrfs_path *path,
4198 struct btrfs_key *new_key,
4199 unsigned long split_offset)
4200{
4201 int ret;
4202 ret = setup_leaf_for_split(trans, root, path,
4203 sizeof(struct btrfs_item));
4204 if (ret)
4205 return ret;
4206
4207 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004208 return ret;
4209}
4210
4211/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004212 * This function duplicate a item, giving 'new_key' to the new item.
4213 * It guarantees both items live in the same tree leaf and the new item
4214 * is contiguous with the original item.
4215 *
4216 * This allows us to split file extent in place, keeping a lock on the
4217 * leaf the entire time.
4218 */
4219int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4220 struct btrfs_root *root,
4221 struct btrfs_path *path,
4222 struct btrfs_key *new_key)
4223{
4224 struct extent_buffer *leaf;
4225 int ret;
4226 u32 item_size;
4227
4228 leaf = path->nodes[0];
4229 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4230 ret = setup_leaf_for_split(trans, root, path,
4231 item_size + sizeof(struct btrfs_item));
4232 if (ret)
4233 return ret;
4234
4235 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004236 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004237 item_size, item_size +
4238 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004239 leaf = path->nodes[0];
4240 memcpy_extent_buffer(leaf,
4241 btrfs_item_ptr_offset(leaf, path->slots[0]),
4242 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4243 item_size);
4244 return 0;
4245}
4246
4247/*
Chris Masond352ac62008-09-29 15:18:18 -04004248 * make the item pointed to by the path smaller. new_size indicates
4249 * how small to make it, and from_end tells us if we just chop bytes
4250 * off the end of the item or if we shift the item to chop bytes off
4251 * the front.
4252 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004253void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004254 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004255{
Chris Masonb18c6682007-04-17 13:26:50 -04004256 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004257 struct extent_buffer *leaf;
4258 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004259 u32 nritems;
4260 unsigned int data_end;
4261 unsigned int old_data_start;
4262 unsigned int old_size;
4263 unsigned int size_diff;
4264 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004265 struct btrfs_map_token token;
4266
4267 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004268
Chris Mason5f39d392007-10-15 16:14:19 -04004269 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004270 slot = path->slots[0];
4271
4272 old_size = btrfs_item_size_nr(leaf, slot);
4273 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004274 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004275
Chris Mason5f39d392007-10-15 16:14:19 -04004276 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004277 data_end = leaf_data_end(root, leaf);
4278
Chris Mason5f39d392007-10-15 16:14:19 -04004279 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004280
Chris Masonb18c6682007-04-17 13:26:50 -04004281 size_diff = old_size - new_size;
4282
4283 BUG_ON(slot < 0);
4284 BUG_ON(slot >= nritems);
4285
4286 /*
4287 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4288 */
4289 /* first correct the data pointers */
4290 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004291 u32 ioff;
4292 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004293
Chris Masoncfed81a2012-03-03 07:40:03 -05004294 ioff = btrfs_token_item_offset(leaf, item, &token);
4295 btrfs_set_token_item_offset(leaf, item,
4296 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004297 }
Chris Masondb945352007-10-15 16:15:53 -04004298
Chris Masonb18c6682007-04-17 13:26:50 -04004299 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004300 if (from_end) {
4301 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4302 data_end + size_diff, btrfs_leaf_data(leaf) +
4303 data_end, old_data_start + new_size - data_end);
4304 } else {
4305 struct btrfs_disk_key disk_key;
4306 u64 offset;
4307
4308 btrfs_item_key(leaf, &disk_key, slot);
4309
4310 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4311 unsigned long ptr;
4312 struct btrfs_file_extent_item *fi;
4313
4314 fi = btrfs_item_ptr(leaf, slot,
4315 struct btrfs_file_extent_item);
4316 fi = (struct btrfs_file_extent_item *)(
4317 (unsigned long)fi - size_diff);
4318
4319 if (btrfs_file_extent_type(leaf, fi) ==
4320 BTRFS_FILE_EXTENT_INLINE) {
4321 ptr = btrfs_item_ptr_offset(leaf, slot);
4322 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004323 (unsigned long)fi,
4324 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004325 disk_bytenr));
4326 }
4327 }
4328
4329 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4330 data_end + size_diff, btrfs_leaf_data(leaf) +
4331 data_end, old_data_start - data_end);
4332
4333 offset = btrfs_disk_key_offset(&disk_key);
4334 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4335 btrfs_set_item_key(leaf, &disk_key, slot);
4336 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004337 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004338 }
Chris Mason5f39d392007-10-15 16:14:19 -04004339
4340 item = btrfs_item_nr(leaf, slot);
4341 btrfs_set_item_size(leaf, item, new_size);
4342 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004343
Chris Mason5f39d392007-10-15 16:14:19 -04004344 if (btrfs_leaf_free_space(root, leaf) < 0) {
4345 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004346 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004347 }
Chris Masonb18c6682007-04-17 13:26:50 -04004348}
4349
Chris Masond352ac62008-09-29 15:18:18 -04004350/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004351 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004352 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004353void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004354 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004355{
Chris Mason6567e832007-04-16 09:22:45 -04004356 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004357 struct extent_buffer *leaf;
4358 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004359 u32 nritems;
4360 unsigned int data_end;
4361 unsigned int old_data;
4362 unsigned int old_size;
4363 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004364 struct btrfs_map_token token;
4365
4366 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004367
Chris Mason5f39d392007-10-15 16:14:19 -04004368 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004369
Chris Mason5f39d392007-10-15 16:14:19 -04004370 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004371 data_end = leaf_data_end(root, leaf);
4372
Chris Mason5f39d392007-10-15 16:14:19 -04004373 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4374 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004375 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004376 }
Chris Mason6567e832007-04-16 09:22:45 -04004377 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004378 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004379
4380 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004381 if (slot >= nritems) {
4382 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004383 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4384 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004385 BUG_ON(1);
4386 }
Chris Mason6567e832007-04-16 09:22:45 -04004387
4388 /*
4389 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4390 */
4391 /* first correct the data pointers */
4392 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004393 u32 ioff;
4394 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004395
Chris Masoncfed81a2012-03-03 07:40:03 -05004396 ioff = btrfs_token_item_offset(leaf, item, &token);
4397 btrfs_set_token_item_offset(leaf, item,
4398 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004399 }
Chris Mason5f39d392007-10-15 16:14:19 -04004400
Chris Mason6567e832007-04-16 09:22:45 -04004401 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004402 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004403 data_end - data_size, btrfs_leaf_data(leaf) +
4404 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004405
Chris Mason6567e832007-04-16 09:22:45 -04004406 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004407 old_size = btrfs_item_size_nr(leaf, slot);
4408 item = btrfs_item_nr(leaf, slot);
4409 btrfs_set_item_size(leaf, item, old_size + data_size);
4410 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004411
Chris Mason5f39d392007-10-15 16:14:19 -04004412 if (btrfs_leaf_free_space(root, leaf) < 0) {
4413 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004414 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004415 }
Chris Mason6567e832007-04-16 09:22:45 -04004416}
4417
Chris Mason74123bd2007-02-02 11:05:29 -05004418/*
Chris Mason44871b12009-03-13 10:04:31 -04004419 * this is a helper for btrfs_insert_empty_items, the main goal here is
4420 * to save stack depth by doing the bulk of the work in a function
4421 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004422 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004423void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004424 struct btrfs_key *cpu_key, u32 *data_size,
4425 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004426{
Chris Mason5f39d392007-10-15 16:14:19 -04004427 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004428 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004429 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004430 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004431 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004432 struct extent_buffer *leaf;
4433 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004434 struct btrfs_map_token token;
4435
4436 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004437
Chris Mason5f39d392007-10-15 16:14:19 -04004438 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004439 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004440
Chris Mason5f39d392007-10-15 16:14:19 -04004441 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004442 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004443
Chris Masonf25956c2008-09-12 15:32:53 -04004444 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004445 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004446 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004447 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004448 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004449 }
Chris Mason5f39d392007-10-15 16:14:19 -04004450
Chris Masonbe0e5c02007-01-26 15:51:26 -05004451 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004452 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004453
Chris Mason5f39d392007-10-15 16:14:19 -04004454 if (old_data < data_end) {
4455 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004456 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004457 slot, old_data, data_end);
4458 BUG_ON(1);
4459 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004460 /*
4461 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4462 */
4463 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004464 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004465 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004466
Chris Mason5f39d392007-10-15 16:14:19 -04004467 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004468 ioff = btrfs_token_item_offset(leaf, item, &token);
4469 btrfs_set_token_item_offset(leaf, item,
4470 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004471 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004472 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004473 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004474 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004475 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004476
4477 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004478 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004479 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004480 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004481 data_end = old_data;
4482 }
Chris Mason5f39d392007-10-15 16:14:19 -04004483
Chris Mason62e27492007-03-15 12:56:47 -04004484 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004485 for (i = 0; i < nr; i++) {
4486 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4487 btrfs_set_item_key(leaf, &disk_key, slot + i);
4488 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004489 btrfs_set_token_item_offset(leaf, item,
4490 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004491 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004492 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004493 }
Chris Mason44871b12009-03-13 10:04:31 -04004494
Chris Mason9c583092008-01-29 15:15:18 -05004495 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004496
Chris Mason5a01a2e2008-01-30 11:43:54 -05004497 if (slot == 0) {
4498 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004499 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004500 }
Chris Masonb9473432009-03-13 11:00:37 -04004501 btrfs_unlock_up_safe(path, 1);
4502 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004503
Chris Mason5f39d392007-10-15 16:14:19 -04004504 if (btrfs_leaf_free_space(root, leaf) < 0) {
4505 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004506 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004507 }
Chris Mason44871b12009-03-13 10:04:31 -04004508}
4509
4510/*
4511 * Given a key and some data, insert items into the tree.
4512 * This does all the path init required, making room in the tree if needed.
4513 */
4514int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4515 struct btrfs_root *root,
4516 struct btrfs_path *path,
4517 struct btrfs_key *cpu_key, u32 *data_size,
4518 int nr)
4519{
Chris Mason44871b12009-03-13 10:04:31 -04004520 int ret = 0;
4521 int slot;
4522 int i;
4523 u32 total_size = 0;
4524 u32 total_data = 0;
4525
4526 for (i = 0; i < nr; i++)
4527 total_data += data_size[i];
4528
4529 total_size = total_data + (nr * sizeof(struct btrfs_item));
4530 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4531 if (ret == 0)
4532 return -EEXIST;
4533 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004534 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004535
Chris Mason44871b12009-03-13 10:04:31 -04004536 slot = path->slots[0];
4537 BUG_ON(slot < 0);
4538
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004539 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004540 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004541 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004542}
4543
4544/*
4545 * Given a key and some data, insert an item into the tree.
4546 * This does all the path init required, making room in the tree if needed.
4547 */
Chris Masone089f052007-03-16 16:20:31 -04004548int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4549 *root, struct btrfs_key *cpu_key, void *data, u32
4550 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004551{
4552 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004553 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004554 struct extent_buffer *leaf;
4555 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004556
Chris Mason2c90e5d2007-04-02 10:50:19 -04004557 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004558 if (!path)
4559 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004560 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004561 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004562 leaf = path->nodes[0];
4563 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4564 write_extent_buffer(leaf, data, ptr, data_size);
4565 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004566 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004567 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004568 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004569}
4570
Chris Mason74123bd2007-02-02 11:05:29 -05004571/*
Chris Mason5de08d72007-02-24 06:24:44 -05004572 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004573 *
Chris Masond352ac62008-09-29 15:18:18 -04004574 * the tree should have been previously balanced so the deletion does not
4575 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004576 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004577static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4578 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004579{
Chris Mason5f39d392007-10-15 16:14:19 -04004580 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004581 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004582 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004583
Chris Mason5f39d392007-10-15 16:14:19 -04004584 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004585 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004586 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004587 tree_mod_log_eb_move(root->fs_info, parent, slot,
4588 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004589 memmove_extent_buffer(parent,
4590 btrfs_node_key_ptr_offset(slot),
4591 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004592 sizeof(struct btrfs_key_ptr) *
4593 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004594 } else if (level) {
4595 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004596 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004597 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004598 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004599
Chris Mason7518a232007-03-12 12:01:18 -04004600 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004601 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004602 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004603 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004604 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004605 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004606 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004607 struct btrfs_disk_key disk_key;
4608
4609 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004610 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004611 }
Chris Masond6025572007-03-30 14:27:56 -04004612 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004613}
4614
Chris Mason74123bd2007-02-02 11:05:29 -05004615/*
Chris Mason323ac952008-10-01 19:05:46 -04004616 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004617 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004618 *
4619 * This deletes the pointer in path->nodes[1] and frees the leaf
4620 * block extent. zero is returned if it all worked out, < 0 otherwise.
4621 *
4622 * The path must have already been setup for deleting the leaf, including
4623 * all the proper balancing. path->nodes[1] must be locked.
4624 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004625static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4626 struct btrfs_root *root,
4627 struct btrfs_path *path,
4628 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004629{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004630 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004631 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004632
Chris Mason4d081c42009-02-04 09:31:28 -05004633 /*
4634 * btrfs_free_extent is expensive, we want to make sure we
4635 * aren't holding any locks when we call it
4636 */
4637 btrfs_unlock_up_safe(path, 0);
4638
Yan, Zhengf0486c62010-05-16 10:46:25 -04004639 root_sub_used(root, leaf->len);
4640
Josef Bacik3083ee22012-03-09 16:01:49 -05004641 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004642 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004643 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004644}
4645/*
Chris Mason74123bd2007-02-02 11:05:29 -05004646 * delete the item at the leaf level in path. If that empties
4647 * the leaf, remove it from the tree
4648 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004649int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4650 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004651{
Chris Mason5f39d392007-10-15 16:14:19 -04004652 struct extent_buffer *leaf;
4653 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004654 int last_off;
4655 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004656 int ret = 0;
4657 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004659 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004660 struct btrfs_map_token token;
4661
4662 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004663
Chris Mason5f39d392007-10-15 16:14:19 -04004664 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004665 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4666
4667 for (i = 0; i < nr; i++)
4668 dsize += btrfs_item_size_nr(leaf, slot + i);
4669
Chris Mason5f39d392007-10-15 16:14:19 -04004670 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004671
Chris Mason85e21ba2008-01-29 15:11:36 -05004672 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004673 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004674
4675 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004676 data_end + dsize,
4677 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004678 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004679
Chris Mason85e21ba2008-01-29 15:11:36 -05004680 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004681 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004682
Chris Mason5f39d392007-10-15 16:14:19 -04004683 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004684 ioff = btrfs_token_item_offset(leaf, item, &token);
4685 btrfs_set_token_item_offset(leaf, item,
4686 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004687 }
Chris Masondb945352007-10-15 16:15:53 -04004688
Chris Mason5f39d392007-10-15 16:14:19 -04004689 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004690 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004691 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004692 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004693 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004694 btrfs_set_header_nritems(leaf, nritems - nr);
4695 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004696
Chris Mason74123bd2007-02-02 11:05:29 -05004697 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004698 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004699 if (leaf == root->node) {
4700 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004701 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004702 btrfs_set_path_blocking(path);
4703 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004704 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004705 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004706 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004707 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004708 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004709 struct btrfs_disk_key disk_key;
4710
4711 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004712 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004713 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004714
Chris Mason74123bd2007-02-02 11:05:29 -05004715 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004716 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004717 /* push_leaf_left fixes the path.
4718 * make sure the path still points to our leaf
4719 * for possible call to del_ptr below
4720 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004721 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004722 extent_buffer_get(leaf);
4723
Chris Masonb9473432009-03-13 11:00:37 -04004724 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004725 wret = push_leaf_left(trans, root, path, 1, 1,
4726 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004727 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004728 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004729
4730 if (path->nodes[0] == leaf &&
4731 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004732 wret = push_leaf_right(trans, root, path, 1,
4733 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004734 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004735 ret = wret;
4736 }
Chris Mason5f39d392007-10-15 16:14:19 -04004737
4738 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004739 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004740 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004741 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004742 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004743 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004744 /* if we're still in the path, make sure
4745 * we're dirty. Otherwise, one of the
4746 * push_leaf functions must have already
4747 * dirtied this buffer
4748 */
4749 if (path->nodes[0] == leaf)
4750 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004751 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004752 }
Chris Masond5719762007-03-23 10:01:08 -04004753 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004754 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004755 }
4756 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004757 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004758}
4759
Chris Mason97571fd2007-02-24 13:39:08 -05004760/*
Chris Mason925baed2008-06-25 16:01:30 -04004761 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004762 * returns 0 if it found something or 1 if there are no lesser leaves.
4763 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004764 *
4765 * This may release the path, and so you may lose any locks held at the
4766 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004767 */
4768int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4769{
Chris Mason925baed2008-06-25 16:01:30 -04004770 struct btrfs_key key;
4771 struct btrfs_disk_key found_key;
4772 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004773
Chris Mason925baed2008-06-25 16:01:30 -04004774 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004775
Chris Mason925baed2008-06-25 16:01:30 -04004776 if (key.offset > 0)
4777 key.offset--;
4778 else if (key.type > 0)
4779 key.type--;
4780 else if (key.objectid > 0)
4781 key.objectid--;
4782 else
4783 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004784
David Sterbab3b4aa72011-04-21 01:20:15 +02004785 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004786 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4787 if (ret < 0)
4788 return ret;
4789 btrfs_item_key(path->nodes[0], &found_key, 0);
4790 ret = comp_keys(&found_key, &key);
4791 if (ret < 0)
4792 return 0;
4793 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004794}
4795
Chris Mason3f157a22008-06-25 16:01:31 -04004796/*
4797 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004798 * for nodes or leaves that are have a minimum transaction id.
4799 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004800 *
4801 * This does not cow, but it does stuff the starting key it finds back
4802 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4803 * key and get a writable path.
4804 *
4805 * This does lock as it descends, and path->keep_locks should be set
4806 * to 1 by the caller.
4807 *
4808 * This honors path->lowest_level to prevent descent past a given level
4809 * of the tree.
4810 *
Chris Masond352ac62008-09-29 15:18:18 -04004811 * min_trans indicates the oldest transaction that you are interested
4812 * in walking through. Any nodes or leaves older than min_trans are
4813 * skipped over (without reading them).
4814 *
Chris Mason3f157a22008-06-25 16:01:31 -04004815 * returns zero if something useful was found, < 0 on error and 1 if there
4816 * was nothing in the tree that matched the search criteria.
4817 */
4818int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004819 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004820 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004821 u64 min_trans)
4822{
4823 struct extent_buffer *cur;
4824 struct btrfs_key found_key;
4825 int slot;
Yan96524802008-07-24 12:19:49 -04004826 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004827 u32 nritems;
4828 int level;
4829 int ret = 1;
4830
Chris Mason934d3752008-12-08 16:43:10 -05004831 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004832again:
Chris Masonbd681512011-07-16 15:23:14 -04004833 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004834 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004835 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004836 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004837 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004838
4839 if (btrfs_header_generation(cur) < min_trans) {
4840 ret = 1;
4841 goto out;
4842 }
Chris Masond3977122009-01-05 21:25:51 -05004843 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004844 nritems = btrfs_header_nritems(cur);
4845 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004846 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004847
Chris Mason323ac952008-10-01 19:05:46 -04004848 /* at the lowest level, we're done, setup the path and exit */
4849 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004850 if (slot >= nritems)
4851 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004852 ret = 0;
4853 path->slots[level] = slot;
4854 btrfs_item_key_to_cpu(cur, &found_key, slot);
4855 goto out;
4856 }
Yan96524802008-07-24 12:19:49 -04004857 if (sret && slot > 0)
4858 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004859 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004860 * check this node pointer against the min_trans parameters.
4861 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004862 */
Chris Masond3977122009-01-05 21:25:51 -05004863 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004864 u64 blockptr;
4865 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004866
Chris Mason3f157a22008-06-25 16:01:31 -04004867 blockptr = btrfs_node_blockptr(cur, slot);
4868 gen = btrfs_node_ptr_generation(cur, slot);
4869 if (gen < min_trans) {
4870 slot++;
4871 continue;
4872 }
Eric Sandeende78b512013-01-31 18:21:12 +00004873 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004874 }
Chris Masone02119d2008-09-05 16:13:11 -04004875find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004876 /*
4877 * we didn't find a candidate key in this node, walk forward
4878 * and find another one
4879 */
4880 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004881 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004882 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004883 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004884 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004885 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004886 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004887 goto again;
4888 } else {
4889 goto out;
4890 }
4891 }
4892 /* save our key for returning back */
4893 btrfs_node_key_to_cpu(cur, &found_key, slot);
4894 path->slots[level] = slot;
4895 if (level == path->lowest_level) {
4896 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004897 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004898 goto out;
4899 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004900 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004901 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004902 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004903
Chris Masonbd681512011-07-16 15:23:14 -04004904 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004905
Chris Masonbd681512011-07-16 15:23:14 -04004906 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004907 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004908 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004909 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004910 }
4911out:
4912 if (ret == 0)
4913 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004914 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004915 return ret;
4916}
4917
Alexander Block70698302012-06-05 21:07:48 +02004918static void tree_move_down(struct btrfs_root *root,
4919 struct btrfs_path *path,
4920 int *level, int root_level)
4921{
Chris Mason74dd17f2012-08-07 16:25:13 -04004922 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004923 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4924 path->slots[*level]);
4925 path->slots[*level - 1] = 0;
4926 (*level)--;
4927}
4928
4929static int tree_move_next_or_upnext(struct btrfs_root *root,
4930 struct btrfs_path *path,
4931 int *level, int root_level)
4932{
4933 int ret = 0;
4934 int nritems;
4935 nritems = btrfs_header_nritems(path->nodes[*level]);
4936
4937 path->slots[*level]++;
4938
Chris Mason74dd17f2012-08-07 16:25:13 -04004939 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004940 if (*level == root_level)
4941 return -1;
4942
4943 /* move upnext */
4944 path->slots[*level] = 0;
4945 free_extent_buffer(path->nodes[*level]);
4946 path->nodes[*level] = NULL;
4947 (*level)++;
4948 path->slots[*level]++;
4949
4950 nritems = btrfs_header_nritems(path->nodes[*level]);
4951 ret = 1;
4952 }
4953 return ret;
4954}
4955
4956/*
4957 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
4958 * or down.
4959 */
4960static int tree_advance(struct btrfs_root *root,
4961 struct btrfs_path *path,
4962 int *level, int root_level,
4963 int allow_down,
4964 struct btrfs_key *key)
4965{
4966 int ret;
4967
4968 if (*level == 0 || !allow_down) {
4969 ret = tree_move_next_or_upnext(root, path, level, root_level);
4970 } else {
4971 tree_move_down(root, path, level, root_level);
4972 ret = 0;
4973 }
4974 if (ret >= 0) {
4975 if (*level == 0)
4976 btrfs_item_key_to_cpu(path->nodes[*level], key,
4977 path->slots[*level]);
4978 else
4979 btrfs_node_key_to_cpu(path->nodes[*level], key,
4980 path->slots[*level]);
4981 }
4982 return ret;
4983}
4984
4985static int tree_compare_item(struct btrfs_root *left_root,
4986 struct btrfs_path *left_path,
4987 struct btrfs_path *right_path,
4988 char *tmp_buf)
4989{
4990 int cmp;
4991 int len1, len2;
4992 unsigned long off1, off2;
4993
4994 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
4995 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
4996 if (len1 != len2)
4997 return 1;
4998
4999 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5000 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5001 right_path->slots[0]);
5002
5003 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5004
5005 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5006 if (cmp)
5007 return 1;
5008 return 0;
5009}
5010
5011#define ADVANCE 1
5012#define ADVANCE_ONLY_NEXT -1
5013
5014/*
5015 * This function compares two trees and calls the provided callback for
5016 * every changed/new/deleted item it finds.
5017 * If shared tree blocks are encountered, whole subtrees are skipped, making
5018 * the compare pretty fast on snapshotted subvolumes.
5019 *
5020 * This currently works on commit roots only. As commit roots are read only,
5021 * we don't do any locking. The commit roots are protected with transactions.
5022 * Transactions are ended and rejoined when a commit is tried in between.
5023 *
5024 * This function checks for modifications done to the trees while comparing.
5025 * If it detects a change, it aborts immediately.
5026 */
5027int btrfs_compare_trees(struct btrfs_root *left_root,
5028 struct btrfs_root *right_root,
5029 btrfs_changed_cb_t changed_cb, void *ctx)
5030{
5031 int ret;
5032 int cmp;
5033 struct btrfs_trans_handle *trans = NULL;
5034 struct btrfs_path *left_path = NULL;
5035 struct btrfs_path *right_path = NULL;
5036 struct btrfs_key left_key;
5037 struct btrfs_key right_key;
5038 char *tmp_buf = NULL;
5039 int left_root_level;
5040 int right_root_level;
5041 int left_level;
5042 int right_level;
5043 int left_end_reached;
5044 int right_end_reached;
5045 int advance_left;
5046 int advance_right;
5047 u64 left_blockptr;
5048 u64 right_blockptr;
5049 u64 left_start_ctransid;
5050 u64 right_start_ctransid;
5051 u64 ctransid;
5052
5053 left_path = btrfs_alloc_path();
5054 if (!left_path) {
5055 ret = -ENOMEM;
5056 goto out;
5057 }
5058 right_path = btrfs_alloc_path();
5059 if (!right_path) {
5060 ret = -ENOMEM;
5061 goto out;
5062 }
5063
5064 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5065 if (!tmp_buf) {
5066 ret = -ENOMEM;
5067 goto out;
5068 }
5069
5070 left_path->search_commit_root = 1;
5071 left_path->skip_locking = 1;
5072 right_path->search_commit_root = 1;
5073 right_path->skip_locking = 1;
5074
Anand Jain5f3ab902012-12-07 09:28:54 +00005075 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005076 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005077 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005078
Anand Jain5f3ab902012-12-07 09:28:54 +00005079 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005080 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005081 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005082
5083 trans = btrfs_join_transaction(left_root);
5084 if (IS_ERR(trans)) {
5085 ret = PTR_ERR(trans);
5086 trans = NULL;
5087 goto out;
5088 }
5089
5090 /*
5091 * Strategy: Go to the first items of both trees. Then do
5092 *
5093 * If both trees are at level 0
5094 * Compare keys of current items
5095 * If left < right treat left item as new, advance left tree
5096 * and repeat
5097 * If left > right treat right item as deleted, advance right tree
5098 * and repeat
5099 * If left == right do deep compare of items, treat as changed if
5100 * needed, advance both trees and repeat
5101 * If both trees are at the same level but not at level 0
5102 * Compare keys of current nodes/leafs
5103 * If left < right advance left tree and repeat
5104 * If left > right advance right tree and repeat
5105 * If left == right compare blockptrs of the next nodes/leafs
5106 * If they match advance both trees but stay at the same level
5107 * and repeat
5108 * If they don't match advance both trees while allowing to go
5109 * deeper and repeat
5110 * If tree levels are different
5111 * Advance the tree that needs it and repeat
5112 *
5113 * Advancing a tree means:
5114 * If we are at level 0, try to go to the next slot. If that's not
5115 * possible, go one level up and repeat. Stop when we found a level
5116 * where we could go to the next slot. We may at this point be on a
5117 * node or a leaf.
5118 *
5119 * If we are not at level 0 and not on shared tree blocks, go one
5120 * level deeper.
5121 *
5122 * If we are not at level 0 and on shared tree blocks, go one slot to
5123 * the right if possible or go up and right.
5124 */
5125
5126 left_level = btrfs_header_level(left_root->commit_root);
5127 left_root_level = left_level;
5128 left_path->nodes[left_level] = left_root->commit_root;
5129 extent_buffer_get(left_path->nodes[left_level]);
5130
5131 right_level = btrfs_header_level(right_root->commit_root);
5132 right_root_level = right_level;
5133 right_path->nodes[right_level] = right_root->commit_root;
5134 extent_buffer_get(right_path->nodes[right_level]);
5135
5136 if (left_level == 0)
5137 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5138 &left_key, left_path->slots[left_level]);
5139 else
5140 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5141 &left_key, left_path->slots[left_level]);
5142 if (right_level == 0)
5143 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5144 &right_key, right_path->slots[right_level]);
5145 else
5146 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5147 &right_key, right_path->slots[right_level]);
5148
5149 left_end_reached = right_end_reached = 0;
5150 advance_left = advance_right = 0;
5151
5152 while (1) {
5153 /*
5154 * We need to make sure the transaction does not get committed
5155 * while we do anything on commit roots. This means, we need to
5156 * join and leave transactions for every item that we process.
5157 */
5158 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5159 btrfs_release_path(left_path);
5160 btrfs_release_path(right_path);
5161
5162 ret = btrfs_end_transaction(trans, left_root);
5163 trans = NULL;
5164 if (ret < 0)
5165 goto out;
5166 }
5167 /* now rejoin the transaction */
5168 if (!trans) {
5169 trans = btrfs_join_transaction(left_root);
5170 if (IS_ERR(trans)) {
5171 ret = PTR_ERR(trans);
5172 trans = NULL;
5173 goto out;
5174 }
5175
Anand Jain5f3ab902012-12-07 09:28:54 +00005176 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005177 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005178 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005179 if (ctransid != left_start_ctransid)
5180 left_start_ctransid = 0;
5181
Anand Jain5f3ab902012-12-07 09:28:54 +00005182 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005183 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005184 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005185 if (ctransid != right_start_ctransid)
5186 right_start_ctransid = 0;
5187
5188 if (!left_start_ctransid || !right_start_ctransid) {
5189 WARN(1, KERN_WARNING
5190 "btrfs: btrfs_compare_tree detected "
5191 "a change in one of the trees while "
5192 "iterating. This is probably a "
5193 "bug.\n");
5194 ret = -EIO;
5195 goto out;
5196 }
5197
5198 /*
5199 * the commit root may have changed, so start again
5200 * where we stopped
5201 */
5202 left_path->lowest_level = left_level;
5203 right_path->lowest_level = right_level;
5204 ret = btrfs_search_slot(NULL, left_root,
5205 &left_key, left_path, 0, 0);
5206 if (ret < 0)
5207 goto out;
5208 ret = btrfs_search_slot(NULL, right_root,
5209 &right_key, right_path, 0, 0);
5210 if (ret < 0)
5211 goto out;
5212 }
5213
5214 if (advance_left && !left_end_reached) {
5215 ret = tree_advance(left_root, left_path, &left_level,
5216 left_root_level,
5217 advance_left != ADVANCE_ONLY_NEXT,
5218 &left_key);
5219 if (ret < 0)
5220 left_end_reached = ADVANCE;
5221 advance_left = 0;
5222 }
5223 if (advance_right && !right_end_reached) {
5224 ret = tree_advance(right_root, right_path, &right_level,
5225 right_root_level,
5226 advance_right != ADVANCE_ONLY_NEXT,
5227 &right_key);
5228 if (ret < 0)
5229 right_end_reached = ADVANCE;
5230 advance_right = 0;
5231 }
5232
5233 if (left_end_reached && right_end_reached) {
5234 ret = 0;
5235 goto out;
5236 } else if (left_end_reached) {
5237 if (right_level == 0) {
5238 ret = changed_cb(left_root, right_root,
5239 left_path, right_path,
5240 &right_key,
5241 BTRFS_COMPARE_TREE_DELETED,
5242 ctx);
5243 if (ret < 0)
5244 goto out;
5245 }
5246 advance_right = ADVANCE;
5247 continue;
5248 } else if (right_end_reached) {
5249 if (left_level == 0) {
5250 ret = changed_cb(left_root, right_root,
5251 left_path, right_path,
5252 &left_key,
5253 BTRFS_COMPARE_TREE_NEW,
5254 ctx);
5255 if (ret < 0)
5256 goto out;
5257 }
5258 advance_left = ADVANCE;
5259 continue;
5260 }
5261
5262 if (left_level == 0 && right_level == 0) {
5263 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5264 if (cmp < 0) {
5265 ret = changed_cb(left_root, right_root,
5266 left_path, right_path,
5267 &left_key,
5268 BTRFS_COMPARE_TREE_NEW,
5269 ctx);
5270 if (ret < 0)
5271 goto out;
5272 advance_left = ADVANCE;
5273 } else if (cmp > 0) {
5274 ret = changed_cb(left_root, right_root,
5275 left_path, right_path,
5276 &right_key,
5277 BTRFS_COMPARE_TREE_DELETED,
5278 ctx);
5279 if (ret < 0)
5280 goto out;
5281 advance_right = ADVANCE;
5282 } else {
Chris Mason74dd17f2012-08-07 16:25:13 -04005283 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005284 ret = tree_compare_item(left_root, left_path,
5285 right_path, tmp_buf);
5286 if (ret) {
Chris Mason74dd17f2012-08-07 16:25:13 -04005287 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005288 ret = changed_cb(left_root, right_root,
5289 left_path, right_path,
5290 &left_key,
5291 BTRFS_COMPARE_TREE_CHANGED,
5292 ctx);
5293 if (ret < 0)
5294 goto out;
5295 }
5296 advance_left = ADVANCE;
5297 advance_right = ADVANCE;
5298 }
5299 } else if (left_level == right_level) {
5300 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5301 if (cmp < 0) {
5302 advance_left = ADVANCE;
5303 } else if (cmp > 0) {
5304 advance_right = ADVANCE;
5305 } else {
5306 left_blockptr = btrfs_node_blockptr(
5307 left_path->nodes[left_level],
5308 left_path->slots[left_level]);
5309 right_blockptr = btrfs_node_blockptr(
5310 right_path->nodes[right_level],
5311 right_path->slots[right_level]);
5312 if (left_blockptr == right_blockptr) {
5313 /*
5314 * As we're on a shared block, don't
5315 * allow to go deeper.
5316 */
5317 advance_left = ADVANCE_ONLY_NEXT;
5318 advance_right = ADVANCE_ONLY_NEXT;
5319 } else {
5320 advance_left = ADVANCE;
5321 advance_right = ADVANCE;
5322 }
5323 }
5324 } else if (left_level < right_level) {
5325 advance_right = ADVANCE;
5326 } else {
5327 advance_left = ADVANCE;
5328 }
5329 }
5330
5331out:
5332 btrfs_free_path(left_path);
5333 btrfs_free_path(right_path);
5334 kfree(tmp_buf);
5335
5336 if (trans) {
5337 if (!ret)
5338 ret = btrfs_end_transaction(trans, left_root);
5339 else
5340 btrfs_end_transaction(trans, left_root);
5341 }
5342
5343 return ret;
5344}
5345
Chris Mason3f157a22008-06-25 16:01:31 -04005346/*
5347 * this is similar to btrfs_next_leaf, but does not try to preserve
5348 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005349 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005350 *
5351 * 0 is returned if another key is found, < 0 if there are any errors
5352 * and 1 is returned if there are no higher keys in the tree
5353 *
5354 * path->keep_locks should be set to 1 on the search made before
5355 * calling this function.
5356 */
Chris Masone7a84562008-06-25 16:01:31 -04005357int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005358 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005359{
Chris Masone7a84562008-06-25 16:01:31 -04005360 int slot;
5361 struct extent_buffer *c;
5362
Chris Mason934d3752008-12-08 16:43:10 -05005363 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005364 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005365 if (!path->nodes[level])
5366 return 1;
5367
5368 slot = path->slots[level] + 1;
5369 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005370next:
Chris Masone7a84562008-06-25 16:01:31 -04005371 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005372 int ret;
5373 int orig_lowest;
5374 struct btrfs_key cur_key;
5375 if (level + 1 >= BTRFS_MAX_LEVEL ||
5376 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005377 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005378
5379 if (path->locks[level + 1]) {
5380 level++;
5381 continue;
5382 }
5383
5384 slot = btrfs_header_nritems(c) - 1;
5385 if (level == 0)
5386 btrfs_item_key_to_cpu(c, &cur_key, slot);
5387 else
5388 btrfs_node_key_to_cpu(c, &cur_key, slot);
5389
5390 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005391 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005392 path->lowest_level = level;
5393 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5394 0, 0);
5395 path->lowest_level = orig_lowest;
5396 if (ret < 0)
5397 return ret;
5398
5399 c = path->nodes[level];
5400 slot = path->slots[level];
5401 if (ret == 0)
5402 slot++;
5403 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005404 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005405
Chris Masone7a84562008-06-25 16:01:31 -04005406 if (level == 0)
5407 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005408 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005409 u64 gen = btrfs_node_ptr_generation(c, slot);
5410
Chris Mason3f157a22008-06-25 16:01:31 -04005411 if (gen < min_trans) {
5412 slot++;
5413 goto next;
5414 }
Chris Masone7a84562008-06-25 16:01:31 -04005415 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005416 }
Chris Masone7a84562008-06-25 16:01:31 -04005417 return 0;
5418 }
5419 return 1;
5420}
5421
Chris Mason7bb86312007-12-11 09:25:06 -05005422/*
Chris Mason925baed2008-06-25 16:01:30 -04005423 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005424 * returns 0 if it found something or 1 if there are no greater leaves.
5425 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005426 */
Chris Mason234b63a2007-03-13 10:46:10 -04005427int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005428{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005429 return btrfs_next_old_leaf(root, path, 0);
5430}
5431
5432int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5433 u64 time_seq)
5434{
Chris Masond97e63b2007-02-20 16:40:44 -05005435 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005436 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005437 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005438 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005439 struct btrfs_key key;
5440 u32 nritems;
5441 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005442 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005443 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005444
5445 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005446 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005447 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005448
Chris Mason8e73f272009-04-03 10:14:18 -04005449 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5450again:
5451 level = 1;
5452 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005453 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005454 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005455
Chris Masona2135012008-06-25 16:01:30 -04005456 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005457 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005458
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005459 if (time_seq)
5460 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5461 else
5462 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005463 path->keep_locks = 0;
5464
5465 if (ret < 0)
5466 return ret;
5467
Chris Masona2135012008-06-25 16:01:30 -04005468 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005469 /*
5470 * by releasing the path above we dropped all our locks. A balance
5471 * could have added more items next to the key that used to be
5472 * at the very end of the block. So, check again here and
5473 * advance the path if there are now more items available.
5474 */
Chris Masona2135012008-06-25 16:01:30 -04005475 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005476 if (ret == 0)
5477 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005478 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005479 goto done;
5480 }
Chris Masond97e63b2007-02-20 16:40:44 -05005481
Chris Masond3977122009-01-05 21:25:51 -05005482 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005483 if (!path->nodes[level]) {
5484 ret = 1;
5485 goto done;
5486 }
Chris Mason5f39d392007-10-15 16:14:19 -04005487
Chris Masond97e63b2007-02-20 16:40:44 -05005488 slot = path->slots[level] + 1;
5489 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005490 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005491 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005492 if (level == BTRFS_MAX_LEVEL) {
5493 ret = 1;
5494 goto done;
5495 }
Chris Masond97e63b2007-02-20 16:40:44 -05005496 continue;
5497 }
Chris Mason5f39d392007-10-15 16:14:19 -04005498
Chris Mason925baed2008-06-25 16:01:30 -04005499 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005500 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005501 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005502 }
Chris Mason5f39d392007-10-15 16:14:19 -04005503
Chris Mason8e73f272009-04-03 10:14:18 -04005504 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005505 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005506 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005507 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005508 if (ret == -EAGAIN)
5509 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005510
Chris Mason76a05b32009-05-14 13:24:30 -04005511 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005512 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005513 goto done;
5514 }
5515
Chris Mason5cd57b22008-06-25 16:01:30 -04005516 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005517 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005518 if (!ret && time_seq) {
5519 /*
5520 * If we don't get the lock, we may be racing
5521 * with push_leaf_left, holding that lock while
5522 * itself waiting for the leaf we've currently
5523 * locked. To solve this situation, we give up
5524 * on our lock and cycle.
5525 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005526 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005527 btrfs_release_path(path);
5528 cond_resched();
5529 goto again;
5530 }
Chris Mason8e73f272009-04-03 10:14:18 -04005531 if (!ret) {
5532 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005533 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005534 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005535 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005536 }
Chris Mason31533fb2011-07-26 16:01:59 -04005537 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005538 }
Chris Masond97e63b2007-02-20 16:40:44 -05005539 break;
5540 }
5541 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005542 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005543 level--;
5544 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005545 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005546 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005547
Chris Mason5f39d392007-10-15 16:14:19 -04005548 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005549 path->nodes[level] = next;
5550 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005551 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005552 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005553 if (!level)
5554 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005555
Chris Mason8e73f272009-04-03 10:14:18 -04005556 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005557 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005558 if (ret == -EAGAIN)
5559 goto again;
5560
Chris Mason76a05b32009-05-14 13:24:30 -04005561 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005562 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005563 goto done;
5564 }
5565
Chris Mason5cd57b22008-06-25 16:01:30 -04005566 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005567 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005568 if (!ret) {
5569 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005570 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005571 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005572 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005573 }
Chris Mason31533fb2011-07-26 16:01:59 -04005574 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005575 }
Chris Masond97e63b2007-02-20 16:40:44 -05005576 }
Chris Mason8e73f272009-04-03 10:14:18 -04005577 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005578done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005579 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005580 path->leave_spinning = old_spinning;
5581 if (!old_spinning)
5582 btrfs_set_path_blocking(path);
5583
5584 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005585}
Chris Mason0b86a832008-03-24 15:01:56 -04005586
Chris Mason3f157a22008-06-25 16:01:31 -04005587/*
5588 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5589 * searching until it gets past min_objectid or finds an item of 'type'
5590 *
5591 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5592 */
Chris Mason0b86a832008-03-24 15:01:56 -04005593int btrfs_previous_item(struct btrfs_root *root,
5594 struct btrfs_path *path, u64 min_objectid,
5595 int type)
5596{
5597 struct btrfs_key found_key;
5598 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005599 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005600 int ret;
5601
Chris Masond3977122009-01-05 21:25:51 -05005602 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005603 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005604 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005605 ret = btrfs_prev_leaf(root, path);
5606 if (ret != 0)
5607 return ret;
5608 } else {
5609 path->slots[0]--;
5610 }
5611 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005612 nritems = btrfs_header_nritems(leaf);
5613 if (nritems == 0)
5614 return 1;
5615 if (path->slots[0] == nritems)
5616 path->slots[0]--;
5617
Chris Mason0b86a832008-03-24 15:01:56 -04005618 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005619 if (found_key.objectid < min_objectid)
5620 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005621 if (found_key.type == type)
5622 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005623 if (found_key.objectid == min_objectid &&
5624 found_key.type < type)
5625 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005626 }
5627 return 1;
5628}