blob: de6de8e60b46019f528b93505d3848adb8699191 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000040static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
41 int level, int slot);
Jan Schmidtf2304752012-05-26 11:43:17 +020042static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
43 struct extent_buffer *eb);
Eric Sandeen48a3b632013-04-25 20:41:01 +000044static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Masond97e63b2007-02-20 16:40:44 -050045
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050049 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040050 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040051}
52
Chris Masonb4ce94d2009-02-04 09:25:08 -050053/*
54 * set all locked nodes in the path to blocking locks. This should
55 * be done before scheduling
56 */
57noinline void btrfs_set_path_blocking(struct btrfs_path *p)
58{
59 int i;
60 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040061 if (!p->nodes[i] || !p->locks[i])
62 continue;
63 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
64 if (p->locks[i] == BTRFS_READ_LOCK)
65 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
66 else if (p->locks[i] == BTRFS_WRITE_LOCK)
67 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050068 }
69}
70
71/*
72 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050073 *
74 * held is used to keep lockdep happy, when lockdep is enabled
75 * we set held to a blocking lock before we go around and
76 * retake all the spinlocks in the path. You can safely use NULL
77 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050078 */
Chris Mason4008c042009-02-12 14:09:45 -050079noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040080 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050081{
82 int i;
Chris Mason4008c042009-02-12 14:09:45 -050083
84#ifdef CONFIG_DEBUG_LOCK_ALLOC
85 /* lockdep really cares that we take all of these spinlocks
86 * in the right order. If any of the locks in the path are not
87 * currently blocking, it is going to complain. So, make really
88 * really sure by forcing the path to blocking before we clear
89 * the path blocking.
90 */
Chris Masonbd681512011-07-16 15:23:14 -040091 if (held) {
92 btrfs_set_lock_blocking_rw(held, held_rw);
93 if (held_rw == BTRFS_WRITE_LOCK)
94 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
95 else if (held_rw == BTRFS_READ_LOCK)
96 held_rw = BTRFS_READ_LOCK_BLOCKING;
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098 btrfs_set_path_blocking(p);
99#endif
100
101 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400102 if (p->nodes[i] && p->locks[i]) {
103 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
104 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
105 p->locks[i] = BTRFS_WRITE_LOCK;
106 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
107 p->locks[i] = BTRFS_READ_LOCK;
108 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500109 }
Chris Mason4008c042009-02-12 14:09:45 -0500110
111#ifdef CONFIG_DEBUG_LOCK_ALLOC
112 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400113 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500114#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400118void btrfs_free_path(struct btrfs_path *p)
119{
Jesper Juhlff175d52010-12-25 21:22:30 +0000120 if (!p)
121 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200122 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400123 kmem_cache_free(btrfs_path_cachep, p);
124}
125
Chris Masond352ac62008-09-29 15:18:18 -0400126/*
127 * path release drops references on the extent buffers in the path
128 * and it drops any locks held by this path
129 *
130 * It is safe to call this on paths that no locks or extent buffers held.
131 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200132noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500133{
134 int i;
Chris Masona2135012008-06-25 16:01:30 -0400135
Chris Mason234b63a2007-03-13 10:46:10 -0400136 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400137 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400139 continue;
140 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400141 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400142 p->locks[i] = 0;
143 }
Chris Mason5f39d392007-10-15 16:14:19 -0400144 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400145 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500146 }
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/*
150 * safely gets a reference on the root node of a tree. A lock
151 * is not taken, so a concurrent writer may put a different node
152 * at the root of the tree. See btrfs_lock_root_node for the
153 * looping required.
154 *
155 * The extent buffer returned by this has a reference taken, so
156 * it won't disappear. It may stop being the root of the tree
157 * at any time because there are no locks held.
158 */
Chris Mason925baed2008-06-25 16:01:30 -0400159struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
160{
161 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400162
Josef Bacik3083ee22012-03-09 16:01:49 -0500163 while (1) {
164 rcu_read_lock();
165 eb = rcu_dereference(root->node);
166
167 /*
168 * RCU really hurts here, we could free up the root node because
169 * it was cow'ed but we may not get the new root node yet so do
170 * the inc_not_zero dance and if it doesn't work then
171 * synchronize_rcu and try again.
172 */
173 if (atomic_inc_not_zero(&eb->refs)) {
174 rcu_read_unlock();
175 break;
176 }
177 rcu_read_unlock();
178 synchronize_rcu();
179 }
Chris Mason925baed2008-06-25 16:01:30 -0400180 return eb;
181}
182
Chris Masond352ac62008-09-29 15:18:18 -0400183/* loop around taking references on and locking the root node of the
184 * tree until you end up with a lock on the root. A locked buffer
185 * is returned, with a reference held.
186 */
Chris Mason925baed2008-06-25 16:01:30 -0400187struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
188{
189 struct extent_buffer *eb;
190
Chris Masond3977122009-01-05 21:25:51 -0500191 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400192 eb = btrfs_root_node(root);
193 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400194 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400195 break;
Chris Mason925baed2008-06-25 16:01:30 -0400196 btrfs_tree_unlock(eb);
197 free_extent_buffer(eb);
198 }
199 return eb;
200}
201
Chris Masonbd681512011-07-16 15:23:14 -0400202/* loop around taking references on and locking the root node of the
203 * tree until you end up with a lock on the root. A locked buffer
204 * is returned, with a reference held.
205 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000206static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400207{
208 struct extent_buffer *eb;
209
210 while (1) {
211 eb = btrfs_root_node(root);
212 btrfs_tree_read_lock(eb);
213 if (eb == root->node)
214 break;
215 btrfs_tree_read_unlock(eb);
216 free_extent_buffer(eb);
217 }
218 return eb;
219}
220
Chris Masond352ac62008-09-29 15:18:18 -0400221/* cowonly root (everything not a reference counted cow subvolume), just get
222 * put onto a simple dirty list. transaction.c walks this to make sure they
223 * get properly updated on disk.
224 */
Chris Mason0b86a832008-03-24 15:01:56 -0400225static void add_root_to_dirty_list(struct btrfs_root *root)
226{
Chris Masone5846fc2012-05-03 12:08:48 -0400227 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400228 if (root->track_dirty && list_empty(&root->dirty_list)) {
229 list_add(&root->dirty_list,
230 &root->fs_info->dirty_cowonly_roots);
231 }
Chris Masone5846fc2012-05-03 12:08:48 -0400232 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400233}
234
Chris Masond352ac62008-09-29 15:18:18 -0400235/*
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
239 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500240int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
244{
245 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 int ret = 0;
247 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249
250 WARN_ON(root->ref_cows && trans->transid !=
251 root->fs_info->running_transaction->transid);
252 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
253
254 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 if (level == 0)
256 btrfs_item_key(buf, &disk_key, 0);
257 else
258 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400259
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
261 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200262 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 return PTR_ERR(cow);
265
266 copy_extent_buffer(cow, buf, 0, 0, cow->len);
267 btrfs_set_header_bytenr(cow, cow->start);
268 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
270 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
271 BTRFS_HEADER_FLAG_RELOC);
272 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
273 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
274 else
275 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500276
Yan Zheng2b820322008-11-17 21:11:30 -0500277 write_extent_buffer(cow, root->fs_info->fsid,
278 (unsigned long)btrfs_header_fsid(cow),
279 BTRFS_FSID_SIZE);
280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200283 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200285 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 if (ret)
288 return ret;
289
290 btrfs_mark_buffer_dirty(cow);
291 *cow_ret = cow;
292 return 0;
293}
294
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200295enum mod_log_op {
296 MOD_LOG_KEY_REPLACE,
297 MOD_LOG_KEY_ADD,
298 MOD_LOG_KEY_REMOVE,
299 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
300 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
301 MOD_LOG_MOVE_KEYS,
302 MOD_LOG_ROOT_REPLACE,
303};
304
305struct tree_mod_move {
306 int dst_slot;
307 int nr_items;
308};
309
310struct tree_mod_root {
311 u64 logical;
312 u8 level;
313};
314
315struct tree_mod_elem {
316 struct rb_node node;
317 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200318 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200319 enum mod_log_op op;
320
321 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
322 int slot;
323
324 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
325 u64 generation;
326
327 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
328 struct btrfs_disk_key key;
329 u64 blockptr;
330
331 /* this is used for op == MOD_LOG_MOVE_KEYS */
332 struct tree_mod_move move;
333
334 /* this is used for op == MOD_LOG_ROOT_REPLACE */
335 struct tree_mod_root old_root;
336};
337
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200339{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200340 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200341}
342
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200344{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200345 read_unlock(&fs_info->tree_mod_log_lock);
346}
347
348static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
349{
350 write_lock(&fs_info->tree_mod_log_lock);
351}
352
353static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
354{
355 write_unlock(&fs_info->tree_mod_log_lock);
356}
357
358/*
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000359 * Increment the upper half of tree_mod_seq, set lower half zero.
360 *
361 * Must be called with fs_info->tree_mod_seq_lock held.
362 */
363static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
364{
365 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
366 seq &= 0xffffffff00000000ull;
367 seq += 1ull << 32;
368 atomic64_set(&fs_info->tree_mod_seq, seq);
369 return seq;
370}
371
372/*
373 * Increment the lower half of tree_mod_seq.
374 *
375 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
376 * are generated should not technically require a spin lock here. (Rationale:
377 * incrementing the minor while incrementing the major seq number is between its
378 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
379 * just returns a unique sequence number as usual.) We have decided to leave
380 * that requirement in here and rethink it once we notice it really imposes a
381 * problem on some workload.
382 */
383static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
384{
385 return atomic64_inc_return(&fs_info->tree_mod_seq);
386}
387
388/*
389 * return the last minor in the previous major tree_mod_seq number
390 */
391u64 btrfs_tree_mod_seq_prev(u64 seq)
392{
393 return (seq & 0xffffffff00000000ull) - 1ull;
394}
395
396/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397 * This adds a new blocker to the tree mod log's blocker list if the @elem
398 * passed does not already have a sequence number set. So when a caller expects
399 * to record tree modifications, it should ensure to set elem->seq to zero
400 * before calling btrfs_get_tree_mod_seq.
401 * Returns a fresh, unused tree log modification sequence number, even if no new
402 * blocker was added.
403 */
404u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
405 struct seq_list *elem)
406{
407 u64 seq;
408
409 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200411 if (!elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000412 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200413 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
414 }
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000415 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200417 tree_mod_log_write_unlock(fs_info);
418
419 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420}
421
422void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
423 struct seq_list *elem)
424{
425 struct rb_root *tm_root;
426 struct rb_node *node;
427 struct rb_node *next;
428 struct seq_list *cur_elem;
429 struct tree_mod_elem *tm;
430 u64 min_seq = (u64)-1;
431 u64 seq_putting = elem->seq;
432
433 if (!seq_putting)
434 return;
435
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436 spin_lock(&fs_info->tree_mod_seq_lock);
437 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200438 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200439
440 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200441 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442 if (seq_putting > cur_elem->seq) {
443 /*
444 * blocker with lower sequence number exists, we
445 * cannot remove anything from the log
446 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200447 spin_unlock(&fs_info->tree_mod_seq_lock);
448 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200449 }
450 min_seq = cur_elem->seq;
451 }
452 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200453 spin_unlock(&fs_info->tree_mod_seq_lock);
454
455 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456 * anything that's lower than the lowest existing (read: blocked)
457 * sequence number can be removed from the tree.
458 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200459 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460 tm_root = &fs_info->tree_mod_log;
461 for (node = rb_first(tm_root); node; node = next) {
462 next = rb_next(node);
463 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200464 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 continue;
466 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200467 kfree(tm);
468 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200469 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200470}
471
472/*
473 * key order of the log:
474 * index -> sequence
475 *
476 * the index is the shifted logical of the *new* root node for root replace
477 * operations, or the shifted logical of the affected block for all other
478 * operations.
479 */
480static noinline int
481__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
482{
483 struct rb_root *tm_root;
484 struct rb_node **new;
485 struct rb_node *parent = NULL;
486 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200487
Jan Schmidt097b8a72012-06-21 11:08:04 +0200488 BUG_ON(!tm || !tm->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200489
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200490 tm_root = &fs_info->tree_mod_log;
491 new = &tm_root->rb_node;
492 while (*new) {
493 cur = container_of(*new, struct tree_mod_elem, node);
494 parent = *new;
495 if (cur->index < tm->index)
496 new = &((*new)->rb_left);
497 else if (cur->index > tm->index)
498 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200499 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200500 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200501 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200502 new = &((*new)->rb_right);
503 else {
504 kfree(tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200505 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200506 }
507 }
508
509 rb_link_node(&tm->node, parent, new);
510 rb_insert_color(&tm->node, tm_root);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200511 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200512}
513
Jan Schmidt097b8a72012-06-21 11:08:04 +0200514/*
515 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
516 * returns zero with the tree_mod_log_lock acquired. The caller must hold
517 * this until all tree mod log insertions are recorded in the rb tree and then
518 * call tree_mod_log_write_unlock() to release.
519 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200520static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
521 struct extent_buffer *eb) {
522 smp_mb();
523 if (list_empty(&(fs_info)->tree_mod_seq_list))
524 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200525 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200526 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200527
528 tree_mod_log_write_lock(fs_info);
529 if (list_empty(&fs_info->tree_mod_seq_list)) {
530 /*
531 * someone emptied the list while we were waiting for the lock.
532 * we must not add to the list when no blocker exists.
533 */
534 tree_mod_log_write_unlock(fs_info);
535 return 1;
536 }
537
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200538 return 0;
539}
540
Jan Schmidt3310c362012-06-11 10:52:38 +0200541/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200542 * This allocates memory and gets a tree modification sequence number.
Jan Schmidt3310c362012-06-11 10:52:38 +0200543 *
Jan Schmidt097b8a72012-06-21 11:08:04 +0200544 * Returns <0 on error.
545 * Returns >0 (the added sequence number) on success.
Jan Schmidt3310c362012-06-11 10:52:38 +0200546 */
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200547static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
548 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200549{
550 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200551
Jan Schmidt097b8a72012-06-21 11:08:04 +0200552 /*
553 * once we switch from spin locks to something different, we should
554 * honor the flags parameter here.
555 */
556 tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200557 if (!tm)
558 return -ENOMEM;
559
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000560 spin_lock(&fs_info->tree_mod_seq_lock);
561 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
562 spin_unlock(&fs_info->tree_mod_seq_lock);
563
Jan Schmidt097b8a72012-06-21 11:08:04 +0200564 return tm->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200565}
566
Jan Schmidt097b8a72012-06-21 11:08:04 +0200567static inline int
568__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
569 struct extent_buffer *eb, int slot,
570 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200571{
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200572 int ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200573 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200574
575 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200576 if (ret < 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200577 return ret;
578
579 tm->index = eb->start >> PAGE_CACHE_SHIFT;
580 if (op != MOD_LOG_KEY_ADD) {
581 btrfs_node_key(eb, &tm->key, slot);
582 tm->blockptr = btrfs_node_blockptr(eb, slot);
583 }
584 tm->op = op;
585 tm->slot = slot;
586 tm->generation = btrfs_node_ptr_generation(eb, slot);
587
Jan Schmidt097b8a72012-06-21 11:08:04 +0200588 return __tree_mod_log_insert(fs_info, tm);
589}
590
591static noinline int
592tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
593 struct extent_buffer *eb, int slot,
594 enum mod_log_op op, gfp_t flags)
595{
596 int ret;
597
598 if (tree_mod_dont_log(fs_info, eb))
599 return 0;
600
601 ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
602
603 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200604 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200605}
606
607static noinline int
608tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
609 int slot, enum mod_log_op op)
610{
611 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
612}
613
614static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200615tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info,
616 struct extent_buffer *eb, int slot,
617 enum mod_log_op op)
618{
619 return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS);
620}
621
622static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200623tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
624 struct extent_buffer *eb, int dst_slot, int src_slot,
625 int nr_items, gfp_t flags)
626{
627 struct tree_mod_elem *tm;
628 int ret;
629 int i;
630
Jan Schmidtf3956942012-05-31 15:02:32 +0200631 if (tree_mod_dont_log(fs_info, eb))
632 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200633
Jan Schmidt01763a22012-10-23 15:02:12 +0200634 /*
635 * When we override something during the move, we log these removals.
636 * This can only happen when we move towards the beginning of the
637 * buffer, i.e. dst_slot < src_slot.
638 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200639 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200640 ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200641 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
642 BUG_ON(ret < 0);
643 }
644
Jan Schmidtf3956942012-05-31 15:02:32 +0200645 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200646 if (ret < 0)
647 goto out;
Jan Schmidtf3956942012-05-31 15:02:32 +0200648
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200649 tm->index = eb->start >> PAGE_CACHE_SHIFT;
650 tm->slot = src_slot;
651 tm->move.dst_slot = dst_slot;
652 tm->move.nr_items = nr_items;
653 tm->op = MOD_LOG_MOVE_KEYS;
654
Jan Schmidt3310c362012-06-11 10:52:38 +0200655 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200656out:
657 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200658 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200659}
660
Jan Schmidt097b8a72012-06-21 11:08:04 +0200661static inline void
662__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
663{
664 int i;
665 u32 nritems;
666 int ret;
667
Chris Masonb12a3b12012-08-07 15:34:49 -0400668 if (btrfs_header_level(eb) == 0)
669 return;
670
Jan Schmidt097b8a72012-06-21 11:08:04 +0200671 nritems = btrfs_header_nritems(eb);
672 for (i = nritems - 1; i >= 0; i--) {
673 ret = tree_mod_log_insert_key_locked(fs_info, eb, i,
674 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
675 BUG_ON(ret < 0);
676 }
677}
678
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200679static noinline int
680tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
681 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000682 struct extent_buffer *new_root, gfp_t flags,
683 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200684{
685 struct tree_mod_elem *tm;
686 int ret;
687
Jan Schmidt097b8a72012-06-21 11:08:04 +0200688 if (tree_mod_dont_log(fs_info, NULL))
689 return 0;
690
Jan Schmidt90f8d622013-04-13 13:19:53 +0000691 if (log_removal)
692 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000693
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200695 if (ret < 0)
696 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697
698 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
699 tm->old_root.logical = old_root->start;
700 tm->old_root.level = btrfs_header_level(old_root);
701 tm->generation = btrfs_header_generation(old_root);
702 tm->op = MOD_LOG_ROOT_REPLACE;
703
Jan Schmidt3310c362012-06-11 10:52:38 +0200704 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200705out:
706 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200707 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708}
709
710static struct tree_mod_elem *
711__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
712 int smallest)
713{
714 struct rb_root *tm_root;
715 struct rb_node *node;
716 struct tree_mod_elem *cur = NULL;
717 struct tree_mod_elem *found = NULL;
718 u64 index = start >> PAGE_CACHE_SHIFT;
719
Jan Schmidt097b8a72012-06-21 11:08:04 +0200720 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200721 tm_root = &fs_info->tree_mod_log;
722 node = tm_root->rb_node;
723 while (node) {
724 cur = container_of(node, struct tree_mod_elem, node);
725 if (cur->index < index) {
726 node = node->rb_left;
727 } else if (cur->index > index) {
728 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200729 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200730 node = node->rb_left;
731 } else if (!smallest) {
732 /* we want the node with the highest seq */
733 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200734 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735 found = cur;
736 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200737 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200738 /* we want the node with the smallest seq */
739 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200740 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200741 found = cur;
742 node = node->rb_right;
743 } else {
744 found = cur;
745 break;
746 }
747 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200748 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200749
750 return found;
751}
752
753/*
754 * this returns the element from the log with the smallest time sequence
755 * value that's in the log (the oldest log item). any element with a time
756 * sequence lower than min_seq will be ignored.
757 */
758static struct tree_mod_elem *
759tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
760 u64 min_seq)
761{
762 return __tree_mod_log_search(fs_info, start, min_seq, 1);
763}
764
765/*
766 * this returns the element from the log with the largest time sequence
767 * value that's in the log (the most recent log item). any element with
768 * a time sequence lower than min_seq will be ignored.
769 */
770static struct tree_mod_elem *
771tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
772{
773 return __tree_mod_log_search(fs_info, start, min_seq, 0);
774}
775
Jan Schmidt097b8a72012-06-21 11:08:04 +0200776static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200777tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
778 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000779 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200780{
781 int ret;
782 int i;
783
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200784 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200785 return;
786
Jan Schmidt097b8a72012-06-21 11:08:04 +0200787 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
788 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200790 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200791
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792 for (i = 0; i < nr_items; i++) {
Jan Schmidt90f8d622013-04-13 13:19:53 +0000793 ret = tree_mod_log_insert_key_locked(fs_info, src,
794 i + src_offset,
795 MOD_LOG_KEY_REMOVE);
796 BUG_ON(ret < 0);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200797 ret = tree_mod_log_insert_key_locked(fs_info, dst,
798 i + dst_offset,
799 MOD_LOG_KEY_ADD);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 BUG_ON(ret < 0);
801 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200802
803 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200804}
805
806static inline void
807tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
808 int dst_offset, int src_offset, int nr_items)
809{
810 int ret;
811 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
812 nr_items, GFP_NOFS);
813 BUG_ON(ret < 0);
814}
815
Jan Schmidt097b8a72012-06-21 11:08:04 +0200816static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200817tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000818 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200819{
820 int ret;
821
822 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
823 MOD_LOG_KEY_REPLACE,
824 atomic ? GFP_ATOMIC : GFP_NOFS);
825 BUG_ON(ret < 0);
826}
827
Jan Schmidt097b8a72012-06-21 11:08:04 +0200828static noinline void
829tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200830{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200831 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200832 return;
833
Jan Schmidt097b8a72012-06-21 11:08:04 +0200834 __tree_mod_log_free_eb(fs_info, eb);
835
836 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200837}
838
Jan Schmidt097b8a72012-06-21 11:08:04 +0200839static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200840tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000841 struct extent_buffer *new_root_node,
842 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200843{
844 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200845 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000846 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200847 BUG_ON(ret < 0);
848}
849
Chris Masond352ac62008-09-29 15:18:18 -0400850/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851 * check if the tree block can be shared by multiple trees
852 */
853int btrfs_block_can_be_shared(struct btrfs_root *root,
854 struct extent_buffer *buf)
855{
856 /*
857 * Tree blocks not in refernece counted trees and tree roots
858 * are never shared. If a block was allocated after the last
859 * snapshot and the block was not allocated by tree relocation,
860 * we know the block is not shared.
861 */
862 if (root->ref_cows &&
863 buf != root->node && buf != root->commit_root &&
864 (btrfs_header_generation(buf) <=
865 btrfs_root_last_snapshot(&root->root_item) ||
866 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
867 return 1;
868#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
869 if (root->ref_cows &&
870 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
871 return 1;
872#endif
873 return 0;
874}
875
876static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
877 struct btrfs_root *root,
878 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400879 struct extent_buffer *cow,
880 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881{
882 u64 refs;
883 u64 owner;
884 u64 flags;
885 u64 new_flags = 0;
886 int ret;
887
888 /*
889 * Backrefs update rules:
890 *
891 * Always use full backrefs for extent pointers in tree block
892 * allocated by tree relocation.
893 *
894 * If a shared tree block is no longer referenced by its owner
895 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
896 * use full backrefs for extent pointers in tree block.
897 *
898 * If a tree block is been relocating
899 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
900 * use full backrefs for extent pointers in tree block.
901 * The reason for this is some operations (such as drop tree)
902 * are only allowed for blocks use full backrefs.
903 */
904
905 if (btrfs_block_can_be_shared(root, buf)) {
906 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500907 btrfs_header_level(buf), 1,
908 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700909 if (ret)
910 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700911 if (refs == 0) {
912 ret = -EROFS;
913 btrfs_std_error(root->fs_info, ret);
914 return ret;
915 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 } else {
917 refs = 1;
918 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
919 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
920 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
921 else
922 flags = 0;
923 }
924
925 owner = btrfs_header_owner(buf);
926 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
927 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
928
929 if (refs > 1) {
930 if ((owner == root->root_key.objectid ||
931 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
932 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200933 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935
936 if (root->root_key.objectid ==
937 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200938 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100939 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200940 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100941 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942 }
943 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
944 } else {
945
946 if (root->root_key.objectid ==
947 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200948 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200950 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100951 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 }
953 if (new_flags != 0) {
954 ret = btrfs_set_disk_extent_flags(trans, root,
955 buf->start,
956 buf->len,
957 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700958 if (ret)
959 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400960 }
961 } else {
962 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
963 if (root->root_key.objectid ==
964 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200965 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400966 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200967 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100968 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200969 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971 }
972 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400973 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400974 }
975 return 0;
976}
977
978/*
Chris Masond3977122009-01-05 21:25:51 -0500979 * does the dirty work in cow of a single block. The parent block (if
980 * supplied) is updated to point to the new cow copy. The new buffer is marked
981 * dirty and returned locked. If you modify the block it needs to be marked
982 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400983 *
984 * search_start -- an allocation hint for the new block
985 *
Chris Masond3977122009-01-05 21:25:51 -0500986 * empty_size -- a hint that you plan on doing more cow. This is the size in
987 * bytes the allocator should try to find free next to the block it returns.
988 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400989 */
Chris Masond3977122009-01-05 21:25:51 -0500990static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400991 struct btrfs_root *root,
992 struct extent_buffer *buf,
993 struct extent_buffer *parent, int parent_slot,
994 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400995 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400996{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400998 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700999 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001000 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001001 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001003
Chris Mason925baed2008-06-25 16:01:30 -04001004 if (*cow_ret == buf)
1005 unlock_orig = 1;
1006
Chris Masonb9447ef2009-03-09 11:45:38 -04001007 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001008
Chris Mason7bb86312007-12-11 09:25:06 -05001009 WARN_ON(root->ref_cows && trans->transid !=
1010 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001011 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001012
Chris Mason7bb86312007-12-11 09:25:06 -05001013 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 if (level == 0)
1016 btrfs_item_key(buf, &disk_key, 0);
1017 else
1018 btrfs_node_key(buf, &disk_key, 0);
1019
1020 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1021 if (parent)
1022 parent_start = parent->start;
1023 else
1024 parent_start = 0;
1025 } else
1026 parent_start = 0;
1027
1028 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1029 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001030 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001031 if (IS_ERR(cow))
1032 return PTR_ERR(cow);
1033
Chris Masonb4ce94d2009-02-04 09:25:08 -05001034 /* cow is set to blocking by btrfs_init_new_buffer */
1035
Chris Mason5f39d392007-10-15 16:14:19 -04001036 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001037 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001038 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1040 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1041 BTRFS_HEADER_FLAG_RELOC);
1042 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1043 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1044 else
1045 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001046
Yan Zheng2b820322008-11-17 21:11:30 -05001047 write_extent_buffer(cow, root->fs_info->fsid,
1048 (unsigned long)btrfs_header_fsid(cow),
1049 BTRFS_FSID_SIZE);
1050
Mark Fashehbe1a5562011-08-08 13:20:18 -07001051 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001052 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001053 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001054 return ret;
1055 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001056
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001057 if (root->ref_cows)
1058 btrfs_reloc_cow_block(trans, root, buf, cow);
1059
Chris Mason6702ed42007-08-07 16:15:09 -04001060 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001061 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1063 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1064 parent_start = buf->start;
1065 else
1066 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001067
Chris Mason5f39d392007-10-15 16:14:19 -04001068 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001069 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001070 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001071
Yan, Zhengf0486c62010-05-16 10:46:25 -04001072 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001073 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001074 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001075 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001076 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001077 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1078 parent_start = parent->start;
1079 else
1080 parent_start = 0;
1081
1082 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001083 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1084 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001085 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001086 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001087 btrfs_set_node_ptr_generation(parent, parent_slot,
1088 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001089 btrfs_mark_buffer_dirty(parent);
Jan Schmidtd9abbf12013-03-20 13:49:48 +00001090 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001091 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001092 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001093 }
Chris Mason925baed2008-06-25 16:01:30 -04001094 if (unlock_orig)
1095 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001096 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001097 btrfs_mark_buffer_dirty(cow);
1098 *cow_ret = cow;
1099 return 0;
1100}
1101
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001102/*
1103 * returns the logical address of the oldest predecessor of the given root.
1104 * entries older than time_seq are ignored.
1105 */
1106static struct tree_mod_elem *
1107__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001108 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001109{
1110 struct tree_mod_elem *tm;
1111 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001112 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001113 int looped = 0;
1114
1115 if (!time_seq)
1116 return 0;
1117
1118 /*
1119 * the very last operation that's logged for a root is the replacement
1120 * operation (if it is replaced at all). this has the index of the *new*
1121 * root, making it the very first operation that's logged for this root.
1122 */
1123 while (1) {
1124 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1125 time_seq);
1126 if (!looped && !tm)
1127 return 0;
1128 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001129 * if there are no tree operation for the oldest root, we simply
1130 * return it. this should only happen if that (old) root is at
1131 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001132 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001133 if (!tm)
1134 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001135
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001136 /*
1137 * if there's an operation that's not a root replacement, we
1138 * found the oldest version of our root. normally, we'll find a
1139 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1140 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001141 if (tm->op != MOD_LOG_ROOT_REPLACE)
1142 break;
1143
1144 found = tm;
1145 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001146 looped = 1;
1147 }
1148
Jan Schmidta95236d2012-06-05 16:41:24 +02001149 /* if there's no old root to return, return what we found instead */
1150 if (!found)
1151 found = tm;
1152
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001153 return found;
1154}
1155
1156/*
1157 * tm is a pointer to the first operation to rewind within eb. then, all
1158 * previous operations will be rewinded (until we reach something older than
1159 * time_seq).
1160 */
1161static void
1162__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1163 struct tree_mod_elem *first_tm)
1164{
1165 u32 n;
1166 struct rb_node *next;
1167 struct tree_mod_elem *tm = first_tm;
1168 unsigned long o_dst;
1169 unsigned long o_src;
1170 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1171
1172 n = btrfs_header_nritems(eb);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001173 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001174 /*
1175 * all the operations are recorded with the operator used for
1176 * the modification. as we're going backwards, we do the
1177 * opposite of each operation here.
1178 */
1179 switch (tm->op) {
1180 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1181 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001182 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001183 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001184 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001185 btrfs_set_node_key(eb, &tm->key, tm->slot);
1186 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1187 btrfs_set_node_ptr_generation(eb, tm->slot,
1188 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001189 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001190 break;
1191 case MOD_LOG_KEY_REPLACE:
1192 BUG_ON(tm->slot >= n);
1193 btrfs_set_node_key(eb, &tm->key, tm->slot);
1194 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1195 btrfs_set_node_ptr_generation(eb, tm->slot,
1196 tm->generation);
1197 break;
1198 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001199 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001200 n--;
1201 break;
1202 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001203 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1204 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1205 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001206 tm->move.nr_items * p_size);
1207 break;
1208 case MOD_LOG_ROOT_REPLACE:
1209 /*
1210 * this operation is special. for roots, this must be
1211 * handled explicitly before rewinding.
1212 * for non-roots, this operation may exist if the node
1213 * was a root: root A -> child B; then A gets empty and
1214 * B is promoted to the new root. in the mod log, we'll
1215 * have a root-replace operation for B, a tree block
1216 * that is no root. we simply ignore that operation.
1217 */
1218 break;
1219 }
1220 next = rb_next(&tm->node);
1221 if (!next)
1222 break;
1223 tm = container_of(next, struct tree_mod_elem, node);
1224 if (tm->index != first_tm->index)
1225 break;
1226 }
1227 btrfs_set_header_nritems(eb, n);
1228}
1229
Jan Schmidt47fb0912013-04-13 13:19:55 +00001230/*
1231 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1232 * is returned. If rewind operations happen, a fresh buffer is returned. The
1233 * returned buffer is always read-locked. If the returned buffer is not the
1234 * input buffer, the lock on the input buffer is released and the input buffer
1235 * is freed (its refcount is decremented).
1236 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001237static struct extent_buffer *
1238tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1239 u64 time_seq)
1240{
1241 struct extent_buffer *eb_rewin;
1242 struct tree_mod_elem *tm;
1243
1244 if (!time_seq)
1245 return eb;
1246
1247 if (btrfs_header_level(eb) == 0)
1248 return eb;
1249
1250 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1251 if (!tm)
1252 return eb;
1253
1254 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1255 BUG_ON(tm->slot != 0);
1256 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1257 fs_info->tree_root->nodesize);
1258 BUG_ON(!eb_rewin);
1259 btrfs_set_header_bytenr(eb_rewin, eb->start);
1260 btrfs_set_header_backref_rev(eb_rewin,
1261 btrfs_header_backref_rev(eb));
1262 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001263 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001264 } else {
1265 eb_rewin = btrfs_clone_extent_buffer(eb);
1266 BUG_ON(!eb_rewin);
1267 }
1268
1269 extent_buffer_get(eb_rewin);
Jan Schmidt47fb0912013-04-13 13:19:55 +00001270 btrfs_tree_read_unlock(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001271 free_extent_buffer(eb);
1272
Jan Schmidt47fb0912013-04-13 13:19:55 +00001273 extent_buffer_get(eb_rewin);
1274 btrfs_tree_read_lock(eb_rewin);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001275 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001276 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001277 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001278
1279 return eb_rewin;
1280}
1281
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001282/*
1283 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1284 * value. If there are no changes, the current root->root_node is returned. If
1285 * anything changed in between, there's a fresh buffer allocated on which the
1286 * rewind operations are done. In any case, the returned buffer is read locked.
1287 * Returns NULL on error (with no locks held).
1288 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001289static inline struct extent_buffer *
1290get_old_root(struct btrfs_root *root, u64 time_seq)
1291{
1292 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001293 struct extent_buffer *eb = NULL;
1294 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001295 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001296 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001297 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001298 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001299 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001300
Jan Schmidt30b04632013-04-13 13:19:54 +00001301 eb_root = btrfs_read_lock_root_node(root);
1302 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001303 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001304 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001305
Jan Schmidta95236d2012-06-05 16:41:24 +02001306 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1307 old_root = &tm->old_root;
1308 old_generation = tm->generation;
1309 logical = old_root->logical;
1310 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001311 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001312 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001313
Jan Schmidta95236d2012-06-05 16:41:24 +02001314 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001315 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001316 btrfs_tree_read_unlock(eb_root);
1317 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001318 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001319 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001320 if (!old || !extent_buffer_uptodate(old)) {
1321 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001322 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1323 logical);
1324 WARN_ON(1);
1325 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001326 eb = btrfs_clone_extent_buffer(old);
1327 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001328 }
1329 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001330 btrfs_tree_read_unlock(eb_root);
1331 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001332 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001333 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001334 eb = btrfs_clone_extent_buffer(eb_root);
1335 btrfs_tree_read_unlock(eb_root);
1336 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001337 }
1338
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001339 if (!eb)
1340 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001341 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001342 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001343 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001344 btrfs_set_header_bytenr(eb, eb->start);
1345 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001346 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001347 btrfs_set_header_level(eb, old_root->level);
1348 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001349 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001350 if (tm)
1351 __tree_mod_log_rewind(eb, time_seq, tm);
1352 else
1353 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001354 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001355
1356 return eb;
1357}
1358
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001359int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1360{
1361 struct tree_mod_elem *tm;
1362 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001363 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001364
Jan Schmidt30b04632013-04-13 13:19:54 +00001365 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001366 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1367 level = tm->old_root.level;
1368 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001369 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001370 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001371 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001372
1373 return level;
1374}
1375
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001376static inline int should_cow_block(struct btrfs_trans_handle *trans,
1377 struct btrfs_root *root,
1378 struct extent_buffer *buf)
1379{
Liu Bof1ebcc72011-11-14 20:48:06 -05001380 /* ensure we can see the force_cow */
1381 smp_rmb();
1382
1383 /*
1384 * We do not need to cow a block if
1385 * 1) this block is not created or changed in this transaction;
1386 * 2) this block does not belong to TREE_RELOC tree;
1387 * 3) the root is not forced COW.
1388 *
1389 * What is forced COW:
1390 * when we create snapshot during commiting the transaction,
1391 * after we've finished coping src root, we must COW the shared
1392 * block to ensure the metadata consistency.
1393 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 if (btrfs_header_generation(buf) == trans->transid &&
1395 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1396 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001397 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1398 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001399 return 0;
1400 return 1;
1401}
1402
Chris Masond352ac62008-09-29 15:18:18 -04001403/*
1404 * cows a single block, see __btrfs_cow_block for the real work.
1405 * This version of it has extra checks so that a block isn't cow'd more than
1406 * once per transaction, as long as it hasn't been written yet
1407 */
Chris Masond3977122009-01-05 21:25:51 -05001408noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001409 struct btrfs_root *root, struct extent_buffer *buf,
1410 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001411 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001412{
Chris Mason6702ed42007-08-07 16:15:09 -04001413 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001414 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001415
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001416 if (trans->transaction != root->fs_info->running_transaction)
1417 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001418 (unsigned long long)trans->transid,
1419 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001420 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001421
1422 if (trans->transid != root->fs_info->generation)
1423 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001424 (unsigned long long)trans->transid,
1425 (unsigned long long)root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001427 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001428 *cow_ret = buf;
1429 return 0;
1430 }
Chris Masonc4876852009-02-04 09:24:25 -05001431
Chris Mason0b86a832008-03-24 15:01:56 -04001432 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001433
1434 if (parent)
1435 btrfs_set_lock_blocking(parent);
1436 btrfs_set_lock_blocking(buf);
1437
Chris Masonf510cfe2007-10-15 16:14:48 -04001438 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001439 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001440
1441 trace_btrfs_cow_block(root, buf, *cow_ret);
1442
Chris Masonf510cfe2007-10-15 16:14:48 -04001443 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001444}
1445
Chris Masond352ac62008-09-29 15:18:18 -04001446/*
1447 * helper function for defrag to decide if two blocks pointed to by a
1448 * node are actually close by
1449 */
Chris Mason6b800532007-10-15 16:17:34 -04001450static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001451{
Chris Mason6b800532007-10-15 16:17:34 -04001452 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001453 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001454 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001455 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001456 return 0;
1457}
1458
Chris Mason081e9572007-11-06 10:26:24 -05001459/*
1460 * compare two keys in a memcmp fashion
1461 */
1462static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1463{
1464 struct btrfs_key k1;
1465
1466 btrfs_disk_key_to_cpu(&k1, disk);
1467
Diego Calleja20736ab2009-07-24 11:06:52 -04001468 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001469}
1470
Josef Bacikf3465ca2008-11-12 14:19:50 -05001471/*
1472 * same as comp_keys only with two btrfs_key's
1473 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001475{
1476 if (k1->objectid > k2->objectid)
1477 return 1;
1478 if (k1->objectid < k2->objectid)
1479 return -1;
1480 if (k1->type > k2->type)
1481 return 1;
1482 if (k1->type < k2->type)
1483 return -1;
1484 if (k1->offset > k2->offset)
1485 return 1;
1486 if (k1->offset < k2->offset)
1487 return -1;
1488 return 0;
1489}
Chris Mason081e9572007-11-06 10:26:24 -05001490
Chris Masond352ac62008-09-29 15:18:18 -04001491/*
1492 * this is used by the defrag code to go through all the
1493 * leaves pointed to by a node and reallocate them so that
1494 * disk order is close to key order
1495 */
Chris Mason6702ed42007-08-07 16:15:09 -04001496int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001497 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001498 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001499 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001500{
Chris Mason6b800532007-10-15 16:17:34 -04001501 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001502 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001503 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001504 u64 search_start = *last_ret;
1505 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001506 u64 other;
1507 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001508 int end_slot;
1509 int i;
1510 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001511 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001512 int uptodate;
1513 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001514 int progress_passed = 0;
1515 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001516
Chris Mason5708b952007-10-25 15:43:18 -04001517 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001518
Julia Lawall6c1500f2012-11-03 20:30:18 +00001519 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1520 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001521
Chris Mason6b800532007-10-15 16:17:34 -04001522 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001523 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001524 end_slot = parent_nritems;
1525
1526 if (parent_nritems == 1)
1527 return 0;
1528
Chris Masonb4ce94d2009-02-04 09:25:08 -05001529 btrfs_set_lock_blocking(parent);
1530
Chris Mason6702ed42007-08-07 16:15:09 -04001531 for (i = start_slot; i < end_slot; i++) {
1532 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001533
Chris Mason081e9572007-11-06 10:26:24 -05001534 btrfs_node_key(parent, &disk_key, i);
1535 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1536 continue;
1537
1538 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001539 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001540 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001541 if (last_block == 0)
1542 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001543
Chris Mason6702ed42007-08-07 16:15:09 -04001544 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001545 other = btrfs_node_blockptr(parent, i - 1);
1546 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001547 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001548 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001549 other = btrfs_node_blockptr(parent, i + 1);
1550 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001551 }
Chris Masone9d0b132007-08-10 14:06:19 -04001552 if (close) {
1553 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001554 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001555 }
Chris Mason6702ed42007-08-07 16:15:09 -04001556
Chris Mason6b800532007-10-15 16:17:34 -04001557 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1558 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001559 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001560 else
1561 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001562 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001563 if (!cur) {
1564 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001565 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001566 if (!cur || !extent_buffer_uptodate(cur)) {
1567 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001568 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001569 }
Chris Mason6b800532007-10-15 16:17:34 -04001570 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001571 err = btrfs_read_buffer(cur, gen);
1572 if (err) {
1573 free_extent_buffer(cur);
1574 return err;
1575 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001576 }
Chris Mason6702ed42007-08-07 16:15:09 -04001577 }
Chris Masone9d0b132007-08-10 14:06:19 -04001578 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001579 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001580
Chris Masone7a84562008-06-25 16:01:31 -04001581 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001582 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001583 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001584 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001585 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001586 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001587 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001588 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001589 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001590 break;
Yan252c38f2007-08-29 09:11:44 -04001591 }
Chris Masone7a84562008-06-25 16:01:31 -04001592 search_start = cur->start;
1593 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001594 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001595 btrfs_tree_unlock(cur);
1596 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001597 }
1598 return err;
1599}
1600
Chris Mason74123bd2007-02-02 11:05:29 -05001601/*
1602 * The leaf data grows from end-to-front in the node.
1603 * this returns the address of the start of the last item,
1604 * which is the stop of the leaf data stack
1605 */
Chris Mason123abc82007-03-14 14:14:43 -04001606static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001607 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001608{
Chris Mason5f39d392007-10-15 16:14:19 -04001609 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001610 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001611 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001612 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001613}
1614
Chris Masonaa5d6be2007-02-28 16:35:06 -05001615
Chris Mason74123bd2007-02-02 11:05:29 -05001616/*
Chris Mason5f39d392007-10-15 16:14:19 -04001617 * search for key in the extent_buffer. The items start at offset p,
1618 * and they are item_size apart. There are 'max' items in p.
1619 *
Chris Mason74123bd2007-02-02 11:05:29 -05001620 * the slot in the array is returned via slot, and it points to
1621 * the place where you would insert key if it is not found in
1622 * the array.
1623 *
1624 * slot may point to max if the key is bigger than all of the keys
1625 */
Chris Masone02119d2008-09-05 16:13:11 -04001626static noinline int generic_bin_search(struct extent_buffer *eb,
1627 unsigned long p,
1628 int item_size, struct btrfs_key *key,
1629 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001630{
1631 int low = 0;
1632 int high = max;
1633 int mid;
1634 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001635 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001636 struct btrfs_disk_key unaligned;
1637 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001638 char *kaddr = NULL;
1639 unsigned long map_start = 0;
1640 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001641 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001642
Chris Masond3977122009-01-05 21:25:51 -05001643 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001644 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001645 offset = p + mid * item_size;
1646
Chris Masona6591712011-07-19 12:04:14 -04001647 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001648 (offset + sizeof(struct btrfs_disk_key)) >
1649 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001650
1651 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001652 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001653 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001654
Chris Mason479965d2007-10-15 16:14:27 -04001655 if (!err) {
1656 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1657 map_start);
1658 } else {
1659 read_extent_buffer(eb, &unaligned,
1660 offset, sizeof(unaligned));
1661 tmp = &unaligned;
1662 }
1663
Chris Mason5f39d392007-10-15 16:14:19 -04001664 } else {
1665 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1666 map_start);
1667 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001668 ret = comp_keys(tmp, key);
1669
1670 if (ret < 0)
1671 low = mid + 1;
1672 else if (ret > 0)
1673 high = mid;
1674 else {
1675 *slot = mid;
1676 return 0;
1677 }
1678 }
1679 *slot = low;
1680 return 1;
1681}
1682
Chris Mason97571fd2007-02-24 13:39:08 -05001683/*
1684 * simple bin_search frontend that does the right thing for
1685 * leaves vs nodes
1686 */
Chris Mason5f39d392007-10-15 16:14:19 -04001687static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1688 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001689{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001690 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001691 return generic_bin_search(eb,
1692 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001693 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001694 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001695 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001696 else
Chris Mason5f39d392007-10-15 16:14:19 -04001697 return generic_bin_search(eb,
1698 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001699 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001700 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001701 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001702}
1703
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1705 int level, int *slot)
1706{
1707 return bin_search(eb, key, level, slot);
1708}
1709
Yan, Zhengf0486c62010-05-16 10:46:25 -04001710static void root_add_used(struct btrfs_root *root, u32 size)
1711{
1712 spin_lock(&root->accounting_lock);
1713 btrfs_set_root_used(&root->root_item,
1714 btrfs_root_used(&root->root_item) + size);
1715 spin_unlock(&root->accounting_lock);
1716}
1717
1718static void root_sub_used(struct btrfs_root *root, u32 size)
1719{
1720 spin_lock(&root->accounting_lock);
1721 btrfs_set_root_used(&root->root_item,
1722 btrfs_root_used(&root->root_item) - size);
1723 spin_unlock(&root->accounting_lock);
1724}
1725
Chris Masond352ac62008-09-29 15:18:18 -04001726/* given a node and slot number, this reads the blocks it points to. The
1727 * extent buffer is returned with a reference taken (but unlocked).
1728 * NULL is returned on error.
1729 */
Chris Masone02119d2008-09-05 16:13:11 -04001730static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001731 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001732{
Chris Masonca7a79a2008-05-12 12:59:19 -04001733 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001734 struct extent_buffer *eb;
1735
Chris Masonbb803952007-03-01 12:04:21 -05001736 if (slot < 0)
1737 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001738 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001739 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001740
1741 BUG_ON(level == 0);
1742
Josef Bacik416bc652013-04-23 14:17:42 -04001743 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1744 btrfs_level_size(root, level - 1),
1745 btrfs_node_ptr_generation(parent, slot));
1746 if (eb && !extent_buffer_uptodate(eb)) {
1747 free_extent_buffer(eb);
1748 eb = NULL;
1749 }
1750
1751 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001752}
1753
Chris Masond352ac62008-09-29 15:18:18 -04001754/*
1755 * node level balancing, used to make sure nodes are in proper order for
1756 * item deletion. We balance from the top down, so we have to make sure
1757 * that a deletion won't leave an node completely empty later on.
1758 */
Chris Masone02119d2008-09-05 16:13:11 -04001759static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001760 struct btrfs_root *root,
1761 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001762{
Chris Mason5f39d392007-10-15 16:14:19 -04001763 struct extent_buffer *right = NULL;
1764 struct extent_buffer *mid;
1765 struct extent_buffer *left = NULL;
1766 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001767 int ret = 0;
1768 int wret;
1769 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001770 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001771 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001772
1773 if (level == 0)
1774 return 0;
1775
Chris Mason5f39d392007-10-15 16:14:19 -04001776 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001777
Chris Masonbd681512011-07-16 15:23:14 -04001778 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1779 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001780 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1781
Chris Mason1d4f8a02007-03-13 09:28:32 -04001782 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001783
Li Zefana05a9bb2011-09-06 16:55:34 +08001784 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001785 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001786 pslot = path->slots[level + 1];
1787 }
Chris Masonbb803952007-03-01 12:04:21 -05001788
Chris Mason40689472007-03-17 14:29:23 -04001789 /*
1790 * deal with the case where there is only one pointer in the root
1791 * by promoting the node below to a root
1792 */
Chris Mason5f39d392007-10-15 16:14:19 -04001793 if (!parent) {
1794 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001795
Chris Mason5f39d392007-10-15 16:14:19 -04001796 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001797 return 0;
1798
1799 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001800 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001801 if (!child) {
1802 ret = -EROFS;
1803 btrfs_std_error(root->fs_info, ret);
1804 goto enospc;
1805 }
1806
Chris Mason925baed2008-06-25 16:01:30 -04001807 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001808 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001809 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001810 if (ret) {
1811 btrfs_tree_unlock(child);
1812 free_extent_buffer(child);
1813 goto enospc;
1814 }
Yan2f375ab2008-02-01 14:58:07 -05001815
Jan Schmidt90f8d622013-04-13 13:19:53 +00001816 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001817 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001818
Chris Mason0b86a832008-03-24 15:01:56 -04001819 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001820 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001821
Chris Mason925baed2008-06-25 16:01:30 -04001822 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001823 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001824 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001825 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001826 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001827 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001828
1829 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001830 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001831 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001832 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001833 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001834 }
Chris Mason5f39d392007-10-15 16:14:19 -04001835 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001836 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001837 return 0;
1838
Chris Mason5f39d392007-10-15 16:14:19 -04001839 left = read_node_slot(root, parent, pslot - 1);
1840 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001841 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001842 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001843 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001844 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001845 if (wret) {
1846 ret = wret;
1847 goto enospc;
1848 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001849 }
Chris Mason5f39d392007-10-15 16:14:19 -04001850 right = read_node_slot(root, parent, pslot + 1);
1851 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001852 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001853 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001854 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001855 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001856 if (wret) {
1857 ret = wret;
1858 goto enospc;
1859 }
1860 }
1861
1862 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001863 if (left) {
1864 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001865 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001866 if (wret < 0)
1867 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001868 }
Chris Mason79f95c82007-03-01 15:16:26 -05001869
1870 /*
1871 * then try to empty the right most buffer into the middle
1872 */
Chris Mason5f39d392007-10-15 16:14:19 -04001873 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001874 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001875 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001876 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001877 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001878 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001879 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001880 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001881 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001882 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001883 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001884 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001885 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001886 struct btrfs_disk_key right_key;
1887 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001888 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001889 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001890 btrfs_set_node_key(parent, &right_key, pslot + 1);
1891 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001892 }
1893 }
Chris Mason5f39d392007-10-15 16:14:19 -04001894 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001895 /*
1896 * we're not allowed to leave a node with one item in the
1897 * tree during a delete. A deletion from lower in the tree
1898 * could try to delete the only pointer in this node.
1899 * So, pull some keys from the left.
1900 * There has to be a left pointer at this point because
1901 * otherwise we would have pulled some pointers from the
1902 * right
1903 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001904 if (!left) {
1905 ret = -EROFS;
1906 btrfs_std_error(root->fs_info, ret);
1907 goto enospc;
1908 }
Chris Mason5f39d392007-10-15 16:14:19 -04001909 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001910 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001911 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001912 goto enospc;
1913 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001914 if (wret == 1) {
1915 wret = push_node_left(trans, root, left, mid, 1);
1916 if (wret < 0)
1917 ret = wret;
1918 }
Chris Mason79f95c82007-03-01 15:16:26 -05001919 BUG_ON(wret == 1);
1920 }
Chris Mason5f39d392007-10-15 16:14:19 -04001921 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001922 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001923 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001924 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001925 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001926 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001927 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001928 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001929 } else {
1930 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001931 struct btrfs_disk_key mid_key;
1932 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001933 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001934 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001935 btrfs_set_node_key(parent, &mid_key, pslot);
1936 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001937 }
Chris Masonbb803952007-03-01 12:04:21 -05001938
Chris Mason79f95c82007-03-01 15:16:26 -05001939 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001940 if (left) {
1941 if (btrfs_header_nritems(left) > orig_slot) {
1942 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001943 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001944 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001945 path->slots[level + 1] -= 1;
1946 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001947 if (mid) {
1948 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001950 }
Chris Masonbb803952007-03-01 12:04:21 -05001951 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001952 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001953 path->slots[level] = orig_slot;
1954 }
1955 }
Chris Mason79f95c82007-03-01 15:16:26 -05001956 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001957 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001958 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001959 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001960enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001961 if (right) {
1962 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001963 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001964 }
1965 if (left) {
1966 if (path->nodes[level] != left)
1967 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001968 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001969 }
Chris Masonbb803952007-03-01 12:04:21 -05001970 return ret;
1971}
1972
Chris Masond352ac62008-09-29 15:18:18 -04001973/* Node balancing for insertion. Here we only split or push nodes around
1974 * when they are completely full. This is also done top down, so we
1975 * have to be pessimistic.
1976 */
Chris Masond3977122009-01-05 21:25:51 -05001977static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001978 struct btrfs_root *root,
1979 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001980{
Chris Mason5f39d392007-10-15 16:14:19 -04001981 struct extent_buffer *right = NULL;
1982 struct extent_buffer *mid;
1983 struct extent_buffer *left = NULL;
1984 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001985 int ret = 0;
1986 int wret;
1987 int pslot;
1988 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001989
1990 if (level == 0)
1991 return 1;
1992
Chris Mason5f39d392007-10-15 16:14:19 -04001993 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001994 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001995
Li Zefana05a9bb2011-09-06 16:55:34 +08001996 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001997 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001998 pslot = path->slots[level + 1];
1999 }
Chris Masone66f7092007-04-20 13:16:02 -04002000
Chris Mason5f39d392007-10-15 16:14:19 -04002001 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002002 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002003
Chris Mason5f39d392007-10-15 16:14:19 -04002004 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002005
2006 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002007 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002008 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002009
2010 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002011 btrfs_set_lock_blocking(left);
2012
Chris Mason5f39d392007-10-15 16:14:19 -04002013 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002014 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2015 wret = 1;
2016 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002017 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002018 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002019 if (ret)
2020 wret = 1;
2021 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002022 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002023 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002024 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002025 }
Chris Masone66f7092007-04-20 13:16:02 -04002026 if (wret < 0)
2027 ret = wret;
2028 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002029 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002030 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002031 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002032 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002033 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002034 btrfs_set_node_key(parent, &disk_key, pslot);
2035 btrfs_mark_buffer_dirty(parent);
2036 if (btrfs_header_nritems(left) > orig_slot) {
2037 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002038 path->slots[level + 1] -= 1;
2039 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002040 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002041 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002042 } else {
2043 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002044 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002045 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002046 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002047 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002048 }
Chris Masone66f7092007-04-20 13:16:02 -04002049 return 0;
2050 }
Chris Mason925baed2008-06-25 16:01:30 -04002051 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002053 }
Chris Mason925baed2008-06-25 16:01:30 -04002054 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002055
2056 /*
2057 * then try to empty the right most buffer into the middle
2058 */
Chris Mason5f39d392007-10-15 16:14:19 -04002059 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002060 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002061
Chris Mason925baed2008-06-25 16:01:30 -04002062 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002063 btrfs_set_lock_blocking(right);
2064
Chris Mason5f39d392007-10-15 16:14:19 -04002065 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002066 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2067 wret = 1;
2068 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002069 ret = btrfs_cow_block(trans, root, right,
2070 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002071 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002072 if (ret)
2073 wret = 1;
2074 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002075 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002076 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002077 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002078 }
Chris Masone66f7092007-04-20 13:16:02 -04002079 if (wret < 0)
2080 ret = wret;
2081 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002082 struct btrfs_disk_key disk_key;
2083
2084 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002085 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002086 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002087 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2088 btrfs_mark_buffer_dirty(parent);
2089
2090 if (btrfs_header_nritems(mid) <= orig_slot) {
2091 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002092 path->slots[level + 1] += 1;
2093 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002094 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002095 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002096 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002097 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002098 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002100 }
Chris Masone66f7092007-04-20 13:16:02 -04002101 return 0;
2102 }
Chris Mason925baed2008-06-25 16:01:30 -04002103 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002104 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002105 }
Chris Masone66f7092007-04-20 13:16:02 -04002106 return 1;
2107}
2108
Chris Mason74123bd2007-02-02 11:05:29 -05002109/*
Chris Masond352ac62008-09-29 15:18:18 -04002110 * readahead one full node of leaves, finding things that are close
2111 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002112 */
Chris Masonc8c42862009-04-03 10:14:18 -04002113static void reada_for_search(struct btrfs_root *root,
2114 struct btrfs_path *path,
2115 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002116{
Chris Mason5f39d392007-10-15 16:14:19 -04002117 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002118 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002119 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002120 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002121 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002122 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002123 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002124 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002125 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002126 u32 nr;
2127 u32 blocksize;
2128 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002129
Chris Masona6b6e752007-10-15 16:22:39 -04002130 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002131 return;
2132
Chris Mason6702ed42007-08-07 16:15:09 -04002133 if (!path->nodes[level])
2134 return;
2135
Chris Mason5f39d392007-10-15 16:14:19 -04002136 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002137
Chris Mason3c69fae2007-08-07 15:52:22 -04002138 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002139 blocksize = btrfs_level_size(root, level - 1);
2140 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002141 if (eb) {
2142 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002143 return;
2144 }
2145
Chris Masona7175312009-01-22 09:23:10 -05002146 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002147
Chris Mason5f39d392007-10-15 16:14:19 -04002148 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002149 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002150
Chris Masond3977122009-01-05 21:25:51 -05002151 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002152 if (direction < 0) {
2153 if (nr == 0)
2154 break;
2155 nr--;
2156 } else if (direction > 0) {
2157 nr++;
2158 if (nr >= nritems)
2159 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002160 }
Chris Mason01f46652007-12-21 16:24:26 -05002161 if (path->reada < 0 && objectid) {
2162 btrfs_node_key(node, &disk_key, nr);
2163 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2164 break;
2165 }
Chris Mason6b800532007-10-15 16:17:34 -04002166 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002167 if ((search <= target && target - search <= 65536) ||
2168 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002169 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002170 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002171 nread += blocksize;
2172 }
2173 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002174 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002175 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002176 }
2177}
Chris Mason925baed2008-06-25 16:01:30 -04002178
Chris Masond352ac62008-09-29 15:18:18 -04002179/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002180 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2181 * cache
2182 */
2183static noinline int reada_for_balance(struct btrfs_root *root,
2184 struct btrfs_path *path, int level)
2185{
2186 int slot;
2187 int nritems;
2188 struct extent_buffer *parent;
2189 struct extent_buffer *eb;
2190 u64 gen;
2191 u64 block1 = 0;
2192 u64 block2 = 0;
2193 int ret = 0;
2194 int blocksize;
2195
Chris Mason8c594ea2009-04-20 15:50:10 -04002196 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002197 if (!parent)
2198 return 0;
2199
2200 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002201 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002202 blocksize = btrfs_level_size(root, level);
2203
2204 if (slot > 0) {
2205 block1 = btrfs_node_blockptr(parent, slot - 1);
2206 gen = btrfs_node_ptr_generation(parent, slot - 1);
2207 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002208 /*
2209 * if we get -eagain from btrfs_buffer_uptodate, we
2210 * don't want to return eagain here. That will loop
2211 * forever
2212 */
2213 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002214 block1 = 0;
2215 free_extent_buffer(eb);
2216 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002217 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002218 block2 = btrfs_node_blockptr(parent, slot + 1);
2219 gen = btrfs_node_ptr_generation(parent, slot + 1);
2220 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002221 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002222 block2 = 0;
2223 free_extent_buffer(eb);
2224 }
2225 if (block1 || block2) {
2226 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002227
2228 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002229 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002230
2231 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002232 if (block1)
2233 readahead_tree_block(root, block1, blocksize, 0);
2234 if (block2)
2235 readahead_tree_block(root, block2, blocksize, 0);
2236
2237 if (block1) {
2238 eb = read_tree_block(root, block1, blocksize, 0);
2239 free_extent_buffer(eb);
2240 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002241 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002242 eb = read_tree_block(root, block2, blocksize, 0);
2243 free_extent_buffer(eb);
2244 }
2245 }
2246 return ret;
2247}
2248
2249
2250/*
Chris Masond3977122009-01-05 21:25:51 -05002251 * when we walk down the tree, it is usually safe to unlock the higher layers
2252 * in the tree. The exceptions are when our path goes through slot 0, because
2253 * operations on the tree might require changing key pointers higher up in the
2254 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002255 *
Chris Masond3977122009-01-05 21:25:51 -05002256 * callers might also have set path->keep_locks, which tells this code to keep
2257 * the lock if the path points to the last slot in the block. This is part of
2258 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002259 *
Chris Masond3977122009-01-05 21:25:51 -05002260 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2261 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002262 */
Chris Masone02119d2008-09-05 16:13:11 -04002263static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002264 int lowest_unlock, int min_write_lock_level,
2265 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002266{
2267 int i;
2268 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002269 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002270 struct extent_buffer *t;
2271
2272 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2273 if (!path->nodes[i])
2274 break;
2275 if (!path->locks[i])
2276 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002277 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002278 skip_level = i + 1;
2279 continue;
2280 }
Chris Mason051e1b92008-06-25 16:01:30 -04002281 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002282 u32 nritems;
2283 t = path->nodes[i];
2284 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002285 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002286 skip_level = i + 1;
2287 continue;
2288 }
2289 }
Chris Mason051e1b92008-06-25 16:01:30 -04002290 if (skip_level < i && i >= lowest_unlock)
2291 no_skips = 1;
2292
Chris Mason925baed2008-06-25 16:01:30 -04002293 t = path->nodes[i];
2294 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002295 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002296 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002297 if (write_lock_level &&
2298 i > min_write_lock_level &&
2299 i <= *write_lock_level) {
2300 *write_lock_level = i - 1;
2301 }
Chris Mason925baed2008-06-25 16:01:30 -04002302 }
2303 }
2304}
2305
Chris Mason3c69fae2007-08-07 15:52:22 -04002306/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002307 * This releases any locks held in the path starting at level and
2308 * going all the way up to the root.
2309 *
2310 * btrfs_search_slot will keep the lock held on higher nodes in a few
2311 * corner cases, such as COW of the block at slot zero in the node. This
2312 * ignores those rules, and it should only be called when there are no
2313 * more updates to be done higher up in the tree.
2314 */
2315noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2316{
2317 int i;
2318
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002319 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002320 return;
2321
2322 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2323 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002324 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002325 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002326 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002327 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002328 path->locks[i] = 0;
2329 }
2330}
2331
2332/*
Chris Masonc8c42862009-04-03 10:14:18 -04002333 * helper function for btrfs_search_slot. The goal is to find a block
2334 * in cache without setting the path to blocking. If we find the block
2335 * we return zero and the path is unchanged.
2336 *
2337 * If we can't find the block, we set the path blocking and do some
2338 * reada. -EAGAIN is returned and the search must be repeated.
2339 */
2340static int
2341read_block_for_search(struct btrfs_trans_handle *trans,
2342 struct btrfs_root *root, struct btrfs_path *p,
2343 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002344 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002345{
2346 u64 blocknr;
2347 u64 gen;
2348 u32 blocksize;
2349 struct extent_buffer *b = *eb_ret;
2350 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002351 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002352
2353 blocknr = btrfs_node_blockptr(b, slot);
2354 gen = btrfs_node_ptr_generation(b, slot);
2355 blocksize = btrfs_level_size(root, level - 1);
2356
2357 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002358 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002359 /* first we do an atomic uptodate check */
2360 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2361 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002362 /*
2363 * we found an up to date block without
2364 * sleeping, return
2365 * right away
2366 */
2367 *eb_ret = tmp;
2368 return 0;
2369 }
2370 /* the pages were up to date, but we failed
2371 * the generation number check. Do a full
2372 * read for the generation number that is correct.
2373 * We must do this without dropping locks so
2374 * we can trust our generation number
2375 */
2376 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002377 btrfs_set_path_blocking(p);
2378
Chris Masonb9fab912012-05-06 07:23:47 -04002379 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002380 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002381 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002382 *eb_ret = tmp;
2383 return 0;
2384 }
2385 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002386 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002387 return -EIO;
2388 }
Chris Masonc8c42862009-04-03 10:14:18 -04002389 }
2390
2391 /*
2392 * reduce lock contention at high levels
2393 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002394 * we read. Don't release the lock on the current
2395 * level because we need to walk this node to figure
2396 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002397 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002398 btrfs_unlock_up_safe(p, level + 1);
2399 btrfs_set_path_blocking(p);
2400
Chris Masoncb449212010-10-24 11:01:27 -04002401 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002402 if (p->reada)
2403 reada_for_search(root, p, level, slot, key->objectid);
2404
David Sterbab3b4aa72011-04-21 01:20:15 +02002405 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002406
2407 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002408 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002409 if (tmp) {
2410 /*
2411 * If the read above didn't mark this buffer up to date,
2412 * it will never end up being up to date. Set ret to EIO now
2413 * and give up so that our caller doesn't loop forever
2414 * on our EAGAINs.
2415 */
Chris Masonb9fab912012-05-06 07:23:47 -04002416 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002417 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002418 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002419 }
2420 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002421}
2422
2423/*
2424 * helper function for btrfs_search_slot. This does all of the checks
2425 * for node-level blocks and does any balancing required based on
2426 * the ins_len.
2427 *
2428 * If no extra work was required, zero is returned. If we had to
2429 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2430 * start over
2431 */
2432static int
2433setup_nodes_for_search(struct btrfs_trans_handle *trans,
2434 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002435 struct extent_buffer *b, int level, int ins_len,
2436 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002437{
2438 int ret;
2439 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2440 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2441 int sret;
2442
Chris Masonbd681512011-07-16 15:23:14 -04002443 if (*write_lock_level < level + 1) {
2444 *write_lock_level = level + 1;
2445 btrfs_release_path(p);
2446 goto again;
2447 }
2448
Chris Masonc8c42862009-04-03 10:14:18 -04002449 sret = reada_for_balance(root, p, level);
2450 if (sret)
2451 goto again;
2452
2453 btrfs_set_path_blocking(p);
2454 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002455 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002456
2457 BUG_ON(sret > 0);
2458 if (sret) {
2459 ret = sret;
2460 goto done;
2461 }
2462 b = p->nodes[level];
2463 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002464 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002465 int sret;
2466
Chris Masonbd681512011-07-16 15:23:14 -04002467 if (*write_lock_level < level + 1) {
2468 *write_lock_level = level + 1;
2469 btrfs_release_path(p);
2470 goto again;
2471 }
2472
Chris Masonc8c42862009-04-03 10:14:18 -04002473 sret = reada_for_balance(root, p, level);
2474 if (sret)
2475 goto again;
2476
2477 btrfs_set_path_blocking(p);
2478 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002479 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002480
2481 if (sret) {
2482 ret = sret;
2483 goto done;
2484 }
2485 b = p->nodes[level];
2486 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002487 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002488 goto again;
2489 }
2490 BUG_ON(btrfs_header_nritems(b) == 1);
2491 }
2492 return 0;
2493
2494again:
2495 ret = -EAGAIN;
2496done:
2497 return ret;
2498}
2499
2500/*
Chris Mason74123bd2007-02-02 11:05:29 -05002501 * look for key in the tree. path is filled in with nodes along the way
2502 * if key is found, we return zero and you can find the item in the leaf
2503 * level of the path (level 0)
2504 *
2505 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002506 * be inserted, and 1 is returned. If there are other errors during the
2507 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002508 *
2509 * if ins_len > 0, nodes and leaves will be split as we walk down the
2510 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2511 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002512 */
Chris Masone089f052007-03-16 16:20:31 -04002513int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2514 *root, struct btrfs_key *key, struct btrfs_path *p, int
2515 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002516{
Chris Mason5f39d392007-10-15 16:14:19 -04002517 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518 int slot;
2519 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002520 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002521 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002522 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002523 int root_lock;
2524 /* everything at write_lock_level or lower must be write locked */
2525 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002526 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002527 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002528
Chris Mason6702ed42007-08-07 16:15:09 -04002529 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002530 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002531 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002532
Chris Masonbd681512011-07-16 15:23:14 -04002533 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002534 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002535
Chris Masonbd681512011-07-16 15:23:14 -04002536 /* when we are removing items, we might have to go up to level
2537 * two as we update tree pointers Make sure we keep write
2538 * for those levels as well
2539 */
2540 write_lock_level = 2;
2541 } else if (ins_len > 0) {
2542 /*
2543 * for inserting items, make sure we have a write lock on
2544 * level 1 so we can update keys
2545 */
2546 write_lock_level = 1;
2547 }
2548
2549 if (!cow)
2550 write_lock_level = -1;
2551
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002552 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002553 write_lock_level = BTRFS_MAX_LEVEL;
2554
Chris Masonf7c79f32012-03-19 15:54:38 -04002555 min_write_lock_level = write_lock_level;
2556
Chris Masonbb803952007-03-01 12:04:21 -05002557again:
Chris Masonbd681512011-07-16 15:23:14 -04002558 /*
2559 * we try very hard to do read locks on the root
2560 */
2561 root_lock = BTRFS_READ_LOCK;
2562 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002563 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002564 /*
2565 * the commit roots are read only
2566 * so we always do read locks
2567 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002568 b = root->commit_root;
2569 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002570 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002571 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002572 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002573 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002574 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002575 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002576 level = btrfs_header_level(b);
2577 } else {
2578 /* we don't know the level of the root node
2579 * until we actually have it read locked
2580 */
2581 b = btrfs_read_lock_root_node(root);
2582 level = btrfs_header_level(b);
2583 if (level <= write_lock_level) {
2584 /* whoops, must trade for write lock */
2585 btrfs_tree_read_unlock(b);
2586 free_extent_buffer(b);
2587 b = btrfs_lock_root_node(root);
2588 root_lock = BTRFS_WRITE_LOCK;
2589
2590 /* the level might have changed, check again */
2591 level = btrfs_header_level(b);
2592 }
2593 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002594 }
Chris Masonbd681512011-07-16 15:23:14 -04002595 p->nodes[level] = b;
2596 if (!p->skip_locking)
2597 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002598
Chris Masoneb60cea2007-02-02 09:18:22 -05002599 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002600 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002601
2602 /*
2603 * setup the path here so we can release it under lock
2604 * contention with the cow code
2605 */
Chris Mason02217ed2007-03-02 16:08:05 -05002606 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002607 /*
2608 * if we don't really need to cow this block
2609 * then we don't want to set the path blocking,
2610 * so we test it here
2611 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002612 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002613 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002614
Chris Masonb4ce94d2009-02-04 09:25:08 -05002615 btrfs_set_path_blocking(p);
2616
Chris Masonbd681512011-07-16 15:23:14 -04002617 /*
2618 * must have write locks on this node and the
2619 * parent
2620 */
Josef Bacik5124e002012-11-07 13:44:13 -05002621 if (level > write_lock_level ||
2622 (level + 1 > write_lock_level &&
2623 level + 1 < BTRFS_MAX_LEVEL &&
2624 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002625 write_lock_level = level + 1;
2626 btrfs_release_path(p);
2627 goto again;
2628 }
2629
Yan Zheng33c66f42009-07-22 09:59:00 -04002630 err = btrfs_cow_block(trans, root, b,
2631 p->nodes[level + 1],
2632 p->slots[level + 1], &b);
2633 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002634 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002635 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002636 }
Chris Mason02217ed2007-03-02 16:08:05 -05002637 }
Chris Mason65b51a02008-08-01 15:11:20 -04002638cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002639 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002640
Chris Masoneb60cea2007-02-02 09:18:22 -05002641 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002642 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002643
2644 /*
2645 * we have a lock on b and as long as we aren't changing
2646 * the tree, there is no way to for the items in b to change.
2647 * It is safe to drop the lock on our parent before we
2648 * go through the expensive btree search on b.
2649 *
2650 * If cow is true, then we might be changing slot zero,
2651 * which may require changing the parent. So, we can't
2652 * drop the lock until after we know which slot we're
2653 * operating on.
2654 */
2655 if (!cow)
2656 btrfs_unlock_up_safe(p, level + 1);
2657
Chris Mason5f39d392007-10-15 16:14:19 -04002658 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002659
Chris Mason5f39d392007-10-15 16:14:19 -04002660 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002661 int dec = 0;
2662 if (ret && slot > 0) {
2663 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002664 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002665 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002666 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002667 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002668 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002669 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002670 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002671 if (err) {
2672 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002673 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002674 }
Chris Masonc8c42862009-04-03 10:14:18 -04002675 b = p->nodes[level];
2676 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002677
Chris Masonbd681512011-07-16 15:23:14 -04002678 /*
2679 * slot 0 is special, if we change the key
2680 * we have to update the parent pointer
2681 * which means we must have a write lock
2682 * on the parent
2683 */
2684 if (slot == 0 && cow &&
2685 write_lock_level < level + 1) {
2686 write_lock_level = level + 1;
2687 btrfs_release_path(p);
2688 goto again;
2689 }
2690
Chris Masonf7c79f32012-03-19 15:54:38 -04002691 unlock_up(p, level, lowest_unlock,
2692 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002693
Chris Mason925baed2008-06-25 16:01:30 -04002694 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002695 if (dec)
2696 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002697 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002698 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002699
Yan Zheng33c66f42009-07-22 09:59:00 -04002700 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002701 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002702 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002703 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002704 if (err) {
2705 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002706 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002707 }
Chris Mason76a05b32009-05-14 13:24:30 -04002708
Chris Masonb4ce94d2009-02-04 09:25:08 -05002709 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002710 level = btrfs_header_level(b);
2711 if (level <= write_lock_level) {
2712 err = btrfs_try_tree_write_lock(b);
2713 if (!err) {
2714 btrfs_set_path_blocking(p);
2715 btrfs_tree_lock(b);
2716 btrfs_clear_path_blocking(p, b,
2717 BTRFS_WRITE_LOCK);
2718 }
2719 p->locks[level] = BTRFS_WRITE_LOCK;
2720 } else {
2721 err = btrfs_try_tree_read_lock(b);
2722 if (!err) {
2723 btrfs_set_path_blocking(p);
2724 btrfs_tree_read_lock(b);
2725 btrfs_clear_path_blocking(p, b,
2726 BTRFS_READ_LOCK);
2727 }
2728 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002729 }
Chris Masonbd681512011-07-16 15:23:14 -04002730 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002731 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002732 } else {
2733 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002734 if (ins_len > 0 &&
2735 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002736 if (write_lock_level < 1) {
2737 write_lock_level = 1;
2738 btrfs_release_path(p);
2739 goto again;
2740 }
2741
Chris Masonb4ce94d2009-02-04 09:25:08 -05002742 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002743 err = split_leaf(trans, root, key,
2744 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002745 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002746
Yan Zheng33c66f42009-07-22 09:59:00 -04002747 BUG_ON(err > 0);
2748 if (err) {
2749 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002750 goto done;
2751 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002752 }
Chris Mason459931e2008-12-10 09:10:46 -05002753 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002754 unlock_up(p, level, lowest_unlock,
2755 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002756 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002757 }
2758 }
Chris Mason65b51a02008-08-01 15:11:20 -04002759 ret = 1;
2760done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002761 /*
2762 * we don't really know what they plan on doing with the path
2763 * from here on, so for now just mark it as blocking
2764 */
Chris Masonb9473432009-03-13 11:00:37 -04002765 if (!p->leave_spinning)
2766 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002767 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002768 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002769 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002770}
2771
Chris Mason74123bd2007-02-02 11:05:29 -05002772/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002773 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2774 * current state of the tree together with the operations recorded in the tree
2775 * modification log to search for the key in a previous version of this tree, as
2776 * denoted by the time_seq parameter.
2777 *
2778 * Naturally, there is no support for insert, delete or cow operations.
2779 *
2780 * The resulting path and return value will be set up as if we called
2781 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2782 */
2783int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2784 struct btrfs_path *p, u64 time_seq)
2785{
2786 struct extent_buffer *b;
2787 int slot;
2788 int ret;
2789 int err;
2790 int level;
2791 int lowest_unlock = 1;
2792 u8 lowest_level = 0;
2793
2794 lowest_level = p->lowest_level;
2795 WARN_ON(p->nodes[0] != NULL);
2796
2797 if (p->search_commit_root) {
2798 BUG_ON(time_seq);
2799 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2800 }
2801
2802again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002803 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002804 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002805 p->locks[level] = BTRFS_READ_LOCK;
2806
2807 while (b) {
2808 level = btrfs_header_level(b);
2809 p->nodes[level] = b;
2810 btrfs_clear_path_blocking(p, NULL, 0);
2811
2812 /*
2813 * we have a lock on b and as long as we aren't changing
2814 * the tree, there is no way to for the items in b to change.
2815 * It is safe to drop the lock on our parent before we
2816 * go through the expensive btree search on b.
2817 */
2818 btrfs_unlock_up_safe(p, level + 1);
2819
2820 ret = bin_search(b, key, level, &slot);
2821
2822 if (level != 0) {
2823 int dec = 0;
2824 if (ret && slot > 0) {
2825 dec = 1;
2826 slot -= 1;
2827 }
2828 p->slots[level] = slot;
2829 unlock_up(p, level, lowest_unlock, 0, NULL);
2830
2831 if (level == lowest_level) {
2832 if (dec)
2833 p->slots[level]++;
2834 goto done;
2835 }
2836
2837 err = read_block_for_search(NULL, root, p, &b, level,
2838 slot, key, time_seq);
2839 if (err == -EAGAIN)
2840 goto again;
2841 if (err) {
2842 ret = err;
2843 goto done;
2844 }
2845
2846 level = btrfs_header_level(b);
2847 err = btrfs_try_tree_read_lock(b);
2848 if (!err) {
2849 btrfs_set_path_blocking(p);
2850 btrfs_tree_read_lock(b);
2851 btrfs_clear_path_blocking(p, b,
2852 BTRFS_READ_LOCK);
2853 }
Jan Schmidt47fb0912013-04-13 13:19:55 +00002854 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002855 p->locks[level] = BTRFS_READ_LOCK;
2856 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002857 } else {
2858 p->slots[level] = slot;
2859 unlock_up(p, level, lowest_unlock, 0, NULL);
2860 goto done;
2861 }
2862 }
2863 ret = 1;
2864done:
2865 if (!p->leave_spinning)
2866 btrfs_set_path_blocking(p);
2867 if (ret < 0)
2868 btrfs_release_path(p);
2869
2870 return ret;
2871}
2872
2873/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002874 * helper to use instead of search slot if no exact match is needed but
2875 * instead the next or previous item should be returned.
2876 * When find_higher is true, the next higher item is returned, the next lower
2877 * otherwise.
2878 * When return_any and find_higher are both true, and no higher item is found,
2879 * return the next lower instead.
2880 * When return_any is true and find_higher is false, and no lower item is found,
2881 * return the next higher instead.
2882 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2883 * < 0 on error
2884 */
2885int btrfs_search_slot_for_read(struct btrfs_root *root,
2886 struct btrfs_key *key, struct btrfs_path *p,
2887 int find_higher, int return_any)
2888{
2889 int ret;
2890 struct extent_buffer *leaf;
2891
2892again:
2893 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2894 if (ret <= 0)
2895 return ret;
2896 /*
2897 * a return value of 1 means the path is at the position where the
2898 * item should be inserted. Normally this is the next bigger item,
2899 * but in case the previous item is the last in a leaf, path points
2900 * to the first free slot in the previous leaf, i.e. at an invalid
2901 * item.
2902 */
2903 leaf = p->nodes[0];
2904
2905 if (find_higher) {
2906 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2907 ret = btrfs_next_leaf(root, p);
2908 if (ret <= 0)
2909 return ret;
2910 if (!return_any)
2911 return 1;
2912 /*
2913 * no higher item found, return the next
2914 * lower instead
2915 */
2916 return_any = 0;
2917 find_higher = 0;
2918 btrfs_release_path(p);
2919 goto again;
2920 }
2921 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002922 if (p->slots[0] == 0) {
2923 ret = btrfs_prev_leaf(root, p);
2924 if (ret < 0)
2925 return ret;
2926 if (!ret) {
2927 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2928 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002929 }
Arne Jansene6793762011-09-13 11:18:10 +02002930 if (!return_any)
2931 return 1;
2932 /*
2933 * no lower item found, return the next
2934 * higher instead
2935 */
2936 return_any = 0;
2937 find_higher = 1;
2938 btrfs_release_path(p);
2939 goto again;
2940 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002941 --p->slots[0];
2942 }
2943 }
2944 return 0;
2945}
2946
2947/*
Chris Mason74123bd2007-02-02 11:05:29 -05002948 * adjust the pointers going up the tree, starting at level
2949 * making sure the right key of each node is points to 'key'.
2950 * This is used after shifting pointers to the left, so it stops
2951 * fixing up pointers when a given leaf/node is not in slot 0 of the
2952 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002953 *
Chris Mason74123bd2007-02-02 11:05:29 -05002954 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002955static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002956 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002957{
2958 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002959 struct extent_buffer *t;
2960
Chris Mason234b63a2007-03-13 10:46:10 -04002961 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002962 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002963 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002964 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002965 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002966 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002967 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002968 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002969 if (tslot != 0)
2970 break;
2971 }
2972}
2973
Chris Mason74123bd2007-02-02 11:05:29 -05002974/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002975 * update item key.
2976 *
2977 * This function isn't completely safe. It's the caller's responsibility
2978 * that the new key won't break the order
2979 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002980void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002981 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002982{
2983 struct btrfs_disk_key disk_key;
2984 struct extent_buffer *eb;
2985 int slot;
2986
2987 eb = path->nodes[0];
2988 slot = path->slots[0];
2989 if (slot > 0) {
2990 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002991 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002992 }
2993 if (slot < btrfs_header_nritems(eb) - 1) {
2994 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002995 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002996 }
2997
2998 btrfs_cpu_key_to_disk(&disk_key, new_key);
2999 btrfs_set_item_key(eb, &disk_key, slot);
3000 btrfs_mark_buffer_dirty(eb);
3001 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003002 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003003}
3004
3005/*
Chris Mason74123bd2007-02-02 11:05:29 -05003006 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003007 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003008 *
3009 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3010 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003011 */
Chris Mason98ed5172008-01-03 10:01:48 -05003012static int push_node_left(struct btrfs_trans_handle *trans,
3013 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003014 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003015{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003016 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003017 int src_nritems;
3018 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003019 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003020
Chris Mason5f39d392007-10-15 16:14:19 -04003021 src_nritems = btrfs_header_nritems(src);
3022 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003023 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003024 WARN_ON(btrfs_header_generation(src) != trans->transid);
3025 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003026
Chris Masonbce4eae2008-04-24 14:42:46 -04003027 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003028 return 1;
3029
Chris Masond3977122009-01-05 21:25:51 -05003030 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003031 return 1;
3032
Chris Masonbce4eae2008-04-24 14:42:46 -04003033 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003034 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003035 if (push_items < src_nritems) {
3036 /* leave at least 8 pointers in the node if
3037 * we aren't going to empty it
3038 */
3039 if (src_nritems - push_items < 8) {
3040 if (push_items <= 8)
3041 return 1;
3042 push_items -= 8;
3043 }
3044 }
3045 } else
3046 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003047
Jan Schmidtf2304752012-05-26 11:43:17 +02003048 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003049 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003050 copy_extent_buffer(dst, src,
3051 btrfs_node_key_ptr_offset(dst_nritems),
3052 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003053 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003054
Chris Masonbb803952007-03-01 12:04:21 -05003055 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003056 /*
3057 * don't call tree_mod_log_eb_move here, key removal was already
3058 * fully logged by tree_mod_log_eb_copy above.
3059 */
Chris Mason5f39d392007-10-15 16:14:19 -04003060 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3061 btrfs_node_key_ptr_offset(push_items),
3062 (src_nritems - push_items) *
3063 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003064 }
Chris Mason5f39d392007-10-15 16:14:19 -04003065 btrfs_set_header_nritems(src, src_nritems - push_items);
3066 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3067 btrfs_mark_buffer_dirty(src);
3068 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003069
Chris Masonbb803952007-03-01 12:04:21 -05003070 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003071}
3072
Chris Mason97571fd2007-02-24 13:39:08 -05003073/*
Chris Mason79f95c82007-03-01 15:16:26 -05003074 * try to push data from one node into the next node right in the
3075 * tree.
3076 *
3077 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3078 * error, and > 0 if there was no room in the right hand block.
3079 *
3080 * this will only push up to 1/2 the contents of the left node over
3081 */
Chris Mason5f39d392007-10-15 16:14:19 -04003082static int balance_node_right(struct btrfs_trans_handle *trans,
3083 struct btrfs_root *root,
3084 struct extent_buffer *dst,
3085 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003086{
Chris Mason79f95c82007-03-01 15:16:26 -05003087 int push_items = 0;
3088 int max_push;
3089 int src_nritems;
3090 int dst_nritems;
3091 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003092
Chris Mason7bb86312007-12-11 09:25:06 -05003093 WARN_ON(btrfs_header_generation(src) != trans->transid);
3094 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3095
Chris Mason5f39d392007-10-15 16:14:19 -04003096 src_nritems = btrfs_header_nritems(src);
3097 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003098 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003099 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003100 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003101
Chris Masond3977122009-01-05 21:25:51 -05003102 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003103 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003104
3105 max_push = src_nritems / 2 + 1;
3106 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003107 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003108 return 1;
Yan252c38f2007-08-29 09:11:44 -04003109
Chris Mason79f95c82007-03-01 15:16:26 -05003110 if (max_push < push_items)
3111 push_items = max_push;
3112
Jan Schmidtf2304752012-05-26 11:43:17 +02003113 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003114 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3115 btrfs_node_key_ptr_offset(0),
3116 (dst_nritems) *
3117 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003118
Jan Schmidtf2304752012-05-26 11:43:17 +02003119 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003120 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003121 copy_extent_buffer(dst, src,
3122 btrfs_node_key_ptr_offset(0),
3123 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003124 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003125
Chris Mason5f39d392007-10-15 16:14:19 -04003126 btrfs_set_header_nritems(src, src_nritems - push_items);
3127 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003128
Chris Mason5f39d392007-10-15 16:14:19 -04003129 btrfs_mark_buffer_dirty(src);
3130 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003131
Chris Mason79f95c82007-03-01 15:16:26 -05003132 return ret;
3133}
3134
3135/*
Chris Mason97571fd2007-02-24 13:39:08 -05003136 * helper function to insert a new root level in the tree.
3137 * A new node is allocated, and a single item is inserted to
3138 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003139 *
3140 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003141 */
Chris Masond3977122009-01-05 21:25:51 -05003142static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003143 struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003144 struct btrfs_path *path, int level, int log_removal)
Chris Mason5c680ed2007-02-22 11:39:13 -05003145{
Chris Mason7bb86312007-12-11 09:25:06 -05003146 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003147 struct extent_buffer *lower;
3148 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003149 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003150 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003151
3152 BUG_ON(path->nodes[level]);
3153 BUG_ON(path->nodes[level-1] != root->node);
3154
Chris Mason7bb86312007-12-11 09:25:06 -05003155 lower = path->nodes[level-1];
3156 if (level == 1)
3157 btrfs_item_key(lower, &lower_key, 0);
3158 else
3159 btrfs_node_key(lower, &lower_key, 0);
3160
Zheng Yan31840ae2008-09-23 13:14:14 -04003161 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003162 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003163 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003164 if (IS_ERR(c))
3165 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003166
Yan, Zhengf0486c62010-05-16 10:46:25 -04003167 root_add_used(root, root->nodesize);
3168
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003169 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003170 btrfs_set_header_nritems(c, 1);
3171 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003172 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003173 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003174 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003175 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003176
Chris Mason5f39d392007-10-15 16:14:19 -04003177 write_extent_buffer(c, root->fs_info->fsid,
3178 (unsigned long)btrfs_header_fsid(c),
3179 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003180
3181 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3182 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3183 BTRFS_UUID_SIZE);
3184
Chris Mason5f39d392007-10-15 16:14:19 -04003185 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003186 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003187 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003188 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003189
3190 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003191
3192 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003193
Chris Mason925baed2008-06-25 16:01:30 -04003194 old = root->node;
Jan Schmidt90f8d622013-04-13 13:19:53 +00003195 tree_mod_log_set_root_pointer(root, c, log_removal);
Chris Mason240f62c2011-03-23 14:54:42 -04003196 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003197
3198 /* the super has an extra ref to root->node */
3199 free_extent_buffer(old);
3200
Chris Mason0b86a832008-03-24 15:01:56 -04003201 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003202 extent_buffer_get(c);
3203 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003204 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003205 path->slots[level] = 0;
3206 return 0;
3207}
3208
Chris Mason74123bd2007-02-02 11:05:29 -05003209/*
3210 * worker function to insert a single pointer in a node.
3211 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003212 *
Chris Mason74123bd2007-02-02 11:05:29 -05003213 * slot and level indicate where you want the key to go, and
3214 * blocknr is the block the key points to.
3215 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003216static void insert_ptr(struct btrfs_trans_handle *trans,
3217 struct btrfs_root *root, struct btrfs_path *path,
3218 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003219 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003220{
Chris Mason5f39d392007-10-15 16:14:19 -04003221 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003222 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003223 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003224
3225 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003226 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003227 lower = path->nodes[level];
3228 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003229 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003230 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003231 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003232 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003233 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3234 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003235 memmove_extent_buffer(lower,
3236 btrfs_node_key_ptr_offset(slot + 1),
3237 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003238 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003239 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003240 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003241 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3242 MOD_LOG_KEY_ADD);
3243 BUG_ON(ret < 0);
3244 }
Chris Mason5f39d392007-10-15 16:14:19 -04003245 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003246 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003247 WARN_ON(trans->transid == 0);
3248 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003249 btrfs_set_header_nritems(lower, nritems + 1);
3250 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003251}
3252
Chris Mason97571fd2007-02-24 13:39:08 -05003253/*
3254 * split the node at the specified level in path in two.
3255 * The path is corrected to point to the appropriate node after the split
3256 *
3257 * Before splitting this tries to make some room in the node by pushing
3258 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003259 *
3260 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003261 */
Chris Masone02119d2008-09-05 16:13:11 -04003262static noinline int split_node(struct btrfs_trans_handle *trans,
3263 struct btrfs_root *root,
3264 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003265{
Chris Mason5f39d392007-10-15 16:14:19 -04003266 struct extent_buffer *c;
3267 struct extent_buffer *split;
3268 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003269 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003270 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003271 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003272
Chris Mason5f39d392007-10-15 16:14:19 -04003273 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003274 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003275 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003276 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003277 * trying to split the root, lets make a new one
3278 *
3279 * tree mod log: We pass 0 as log_removal parameter to
3280 * insert_new_root, because that root buffer will be kept as a
3281 * normal node. We are going to log removal of half of the
3282 * elements below with tree_mod_log_eb_copy. We're holding a
3283 * tree lock on the buffer, which is why we cannot race with
3284 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003285 */
Jan Schmidt90f8d622013-04-13 13:19:53 +00003286 ret = insert_new_root(trans, root, path, level + 1, 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05003287 if (ret)
3288 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003289 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003290 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003291 c = path->nodes[level];
3292 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003293 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003294 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003295 if (ret < 0)
3296 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003297 }
Chris Masone66f7092007-04-20 13:16:02 -04003298
Chris Mason5f39d392007-10-15 16:14:19 -04003299 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003300 mid = (c_nritems + 1) / 2;
3301 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003302
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003303 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003304 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003305 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003306 if (IS_ERR(split))
3307 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003308
Yan, Zhengf0486c62010-05-16 10:46:25 -04003309 root_add_used(root, root->nodesize);
3310
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003311 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003312 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003313 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003314 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003315 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003316 btrfs_set_header_owner(split, root->root_key.objectid);
3317 write_extent_buffer(split, root->fs_info->fsid,
3318 (unsigned long)btrfs_header_fsid(split),
3319 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003320 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3321 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3322 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003323
Jan Schmidt90f8d622013-04-13 13:19:53 +00003324 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003325 copy_extent_buffer(split, c,
3326 btrfs_node_key_ptr_offset(0),
3327 btrfs_node_key_ptr_offset(mid),
3328 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3329 btrfs_set_header_nritems(split, c_nritems - mid);
3330 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003331 ret = 0;
3332
Chris Mason5f39d392007-10-15 16:14:19 -04003333 btrfs_mark_buffer_dirty(c);
3334 btrfs_mark_buffer_dirty(split);
3335
Jeff Mahoney143bede2012-03-01 14:56:26 +01003336 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003337 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003338
Chris Mason5de08d72007-02-24 06:24:44 -05003339 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003340 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003341 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003342 free_extent_buffer(c);
3343 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003344 path->slots[level + 1] += 1;
3345 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003346 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003347 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003348 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003349 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003350}
3351
Chris Mason74123bd2007-02-02 11:05:29 -05003352/*
3353 * how many bytes are required to store the items in a leaf. start
3354 * and nr indicate which items in the leaf to check. This totals up the
3355 * space used both by the item structs and the item data
3356 */
Chris Mason5f39d392007-10-15 16:14:19 -04003357static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003358{
Josef Bacik41be1f32012-10-15 13:43:18 -04003359 struct btrfs_item *start_item;
3360 struct btrfs_item *end_item;
3361 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003362 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003363 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003364 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003365
3366 if (!nr)
3367 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003368 btrfs_init_map_token(&token);
3369 start_item = btrfs_item_nr(l, start);
3370 end_item = btrfs_item_nr(l, end);
3371 data_len = btrfs_token_item_offset(l, start_item, &token) +
3372 btrfs_token_item_size(l, start_item, &token);
3373 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003374 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003375 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003376 return data_len;
3377}
3378
Chris Mason74123bd2007-02-02 11:05:29 -05003379/*
Chris Masond4dbff92007-04-04 14:08:15 -04003380 * The space between the end of the leaf items and
3381 * the start of the leaf data. IOW, how much room
3382 * the leaf has left for both items and data
3383 */
Chris Masond3977122009-01-05 21:25:51 -05003384noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003385 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003386{
Chris Mason5f39d392007-10-15 16:14:19 -04003387 int nritems = btrfs_header_nritems(leaf);
3388 int ret;
3389 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3390 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003391 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3392 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003393 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003394 leaf_space_used(leaf, 0, nritems), nritems);
3395 }
3396 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003397}
3398
Chris Mason99d8f832010-07-07 10:51:48 -04003399/*
3400 * min slot controls the lowest index we're willing to push to the
3401 * right. We'll push up to and including min_slot, but no lower
3402 */
Chris Mason44871b12009-03-13 10:04:31 -04003403static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root,
3405 struct btrfs_path *path,
3406 int data_size, int empty,
3407 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003408 int free_space, u32 left_nritems,
3409 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003410{
Chris Mason5f39d392007-10-15 16:14:19 -04003411 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003412 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003413 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003414 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003415 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003416 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003417 int push_space = 0;
3418 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003419 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003420 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003421 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003422 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003423 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003424
Chris Masoncfed81a2012-03-03 07:40:03 -05003425 btrfs_init_map_token(&token);
3426
Chris Mason34a38212007-11-07 13:31:03 -05003427 if (empty)
3428 nr = 0;
3429 else
Chris Mason99d8f832010-07-07 10:51:48 -04003430 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003431
Zheng Yan31840ae2008-09-23 13:14:14 -04003432 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003433 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003434
Chris Mason44871b12009-03-13 10:04:31 -04003435 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003436 i = left_nritems - 1;
3437 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003438 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003439
Zheng Yan31840ae2008-09-23 13:14:14 -04003440 if (!empty && push_items > 0) {
3441 if (path->slots[0] > i)
3442 break;
3443 if (path->slots[0] == i) {
3444 int space = btrfs_leaf_free_space(root, left);
3445 if (space + push_space * 2 > free_space)
3446 break;
3447 }
3448 }
3449
Chris Mason00ec4c52007-02-24 12:47:20 -05003450 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003451 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003452
Chris Masondb945352007-10-15 16:15:53 -04003453 this_item_size = btrfs_item_size(left, item);
3454 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003455 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003456
Chris Mason00ec4c52007-02-24 12:47:20 -05003457 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003458 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003459 if (i == 0)
3460 break;
3461 i--;
Chris Masondb945352007-10-15 16:15:53 -04003462 }
Chris Mason5f39d392007-10-15 16:14:19 -04003463
Chris Mason925baed2008-06-25 16:01:30 -04003464 if (push_items == 0)
3465 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003466
Julia Lawall6c1500f2012-11-03 20:30:18 +00003467 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003468
Chris Mason00ec4c52007-02-24 12:47:20 -05003469 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003470 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003471
Chris Mason5f39d392007-10-15 16:14:19 -04003472 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003473 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003474
Chris Mason00ec4c52007-02-24 12:47:20 -05003475 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003476 data_end = leaf_data_end(root, right);
3477 memmove_extent_buffer(right,
3478 btrfs_leaf_data(right) + data_end - push_space,
3479 btrfs_leaf_data(right) + data_end,
3480 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3481
Chris Mason00ec4c52007-02-24 12:47:20 -05003482 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003483 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003484 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3485 btrfs_leaf_data(left) + leaf_data_end(root, left),
3486 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003487
3488 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3489 btrfs_item_nr_offset(0),
3490 right_nritems * sizeof(struct btrfs_item));
3491
Chris Mason00ec4c52007-02-24 12:47:20 -05003492 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003493 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3494 btrfs_item_nr_offset(left_nritems - push_items),
3495 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003496
3497 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003498 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003499 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003500 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003501 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003502 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003503 push_space -= btrfs_token_item_size(right, item, &token);
3504 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003505 }
3506
Chris Mason7518a232007-03-12 12:01:18 -04003507 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003508 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003509
Chris Mason34a38212007-11-07 13:31:03 -05003510 if (left_nritems)
3511 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003512 else
3513 clean_tree_block(trans, root, left);
3514
Chris Mason5f39d392007-10-15 16:14:19 -04003515 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003516
Chris Mason5f39d392007-10-15 16:14:19 -04003517 btrfs_item_key(right, &disk_key, 0);
3518 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003519 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003520
Chris Mason00ec4c52007-02-24 12:47:20 -05003521 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003522 if (path->slots[0] >= left_nritems) {
3523 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003524 if (btrfs_header_nritems(path->nodes[0]) == 0)
3525 clean_tree_block(trans, root, path->nodes[0]);
3526 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003527 free_extent_buffer(path->nodes[0]);
3528 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003529 path->slots[1] += 1;
3530 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003531 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003532 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003533 }
3534 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003535
3536out_unlock:
3537 btrfs_tree_unlock(right);
3538 free_extent_buffer(right);
3539 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003540}
Chris Mason925baed2008-06-25 16:01:30 -04003541
Chris Mason00ec4c52007-02-24 12:47:20 -05003542/*
Chris Mason44871b12009-03-13 10:04:31 -04003543 * push some data in the path leaf to the right, trying to free up at
3544 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3545 *
3546 * returns 1 if the push failed because the other node didn't have enough
3547 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003548 *
3549 * this will push starting from min_slot to the end of the leaf. It won't
3550 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003551 */
3552static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003553 *root, struct btrfs_path *path,
3554 int min_data_size, int data_size,
3555 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003556{
3557 struct extent_buffer *left = path->nodes[0];
3558 struct extent_buffer *right;
3559 struct extent_buffer *upper;
3560 int slot;
3561 int free_space;
3562 u32 left_nritems;
3563 int ret;
3564
3565 if (!path->nodes[1])
3566 return 1;
3567
3568 slot = path->slots[1];
3569 upper = path->nodes[1];
3570 if (slot >= btrfs_header_nritems(upper) - 1)
3571 return 1;
3572
3573 btrfs_assert_tree_locked(path->nodes[1]);
3574
3575 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003576 if (right == NULL)
3577 return 1;
3578
Chris Mason44871b12009-03-13 10:04:31 -04003579 btrfs_tree_lock(right);
3580 btrfs_set_lock_blocking(right);
3581
3582 free_space = btrfs_leaf_free_space(root, right);
3583 if (free_space < data_size)
3584 goto out_unlock;
3585
3586 /* cow and double check */
3587 ret = btrfs_cow_block(trans, root, right, upper,
3588 slot + 1, &right);
3589 if (ret)
3590 goto out_unlock;
3591
3592 free_space = btrfs_leaf_free_space(root, right);
3593 if (free_space < data_size)
3594 goto out_unlock;
3595
3596 left_nritems = btrfs_header_nritems(left);
3597 if (left_nritems == 0)
3598 goto out_unlock;
3599
Chris Mason99d8f832010-07-07 10:51:48 -04003600 return __push_leaf_right(trans, root, path, min_data_size, empty,
3601 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003602out_unlock:
3603 btrfs_tree_unlock(right);
3604 free_extent_buffer(right);
3605 return 1;
3606}
3607
3608/*
Chris Mason74123bd2007-02-02 11:05:29 -05003609 * push some data in the path leaf to the left, trying to free up at
3610 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003611 *
3612 * max_slot can put a limit on how far into the leaf we'll push items. The
3613 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3614 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003615 */
Chris Mason44871b12009-03-13 10:04:31 -04003616static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3617 struct btrfs_root *root,
3618 struct btrfs_path *path, int data_size,
3619 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003620 int free_space, u32 right_nritems,
3621 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003622{
Chris Mason5f39d392007-10-15 16:14:19 -04003623 struct btrfs_disk_key disk_key;
3624 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003625 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003626 int push_space = 0;
3627 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003628 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003629 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003630 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003631 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003632 u32 this_item_size;
3633 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003634 struct btrfs_map_token token;
3635
3636 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003637
Chris Mason34a38212007-11-07 13:31:03 -05003638 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003639 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003640 else
Chris Mason99d8f832010-07-07 10:51:48 -04003641 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003642
3643 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003644 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003645
Zheng Yan31840ae2008-09-23 13:14:14 -04003646 if (!empty && push_items > 0) {
3647 if (path->slots[0] < i)
3648 break;
3649 if (path->slots[0] == i) {
3650 int space = btrfs_leaf_free_space(root, right);
3651 if (space + push_space * 2 > free_space)
3652 break;
3653 }
3654 }
3655
Chris Masonbe0e5c02007-01-26 15:51:26 -05003656 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003657 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003658
3659 this_item_size = btrfs_item_size(right, item);
3660 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003661 break;
Chris Masondb945352007-10-15 16:15:53 -04003662
Chris Masonbe0e5c02007-01-26 15:51:26 -05003663 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003664 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003665 }
Chris Masondb945352007-10-15 16:15:53 -04003666
Chris Masonbe0e5c02007-01-26 15:51:26 -05003667 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003668 ret = 1;
3669 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670 }
Chris Mason34a38212007-11-07 13:31:03 -05003671 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003672 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003673
Chris Masonbe0e5c02007-01-26 15:51:26 -05003674 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003675 copy_extent_buffer(left, right,
3676 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3677 btrfs_item_nr_offset(0),
3678 push_items * sizeof(struct btrfs_item));
3679
Chris Mason123abc82007-03-14 14:14:43 -04003680 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003681 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003682
3683 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003684 leaf_data_end(root, left) - push_space,
3685 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003686 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003687 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003688 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003689 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003690
Chris Masondb945352007-10-15 16:15:53 -04003691 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003692 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003693 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003694
Chris Mason5f39d392007-10-15 16:14:19 -04003695 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003696
Chris Masoncfed81a2012-03-03 07:40:03 -05003697 ioff = btrfs_token_item_offset(left, item, &token);
3698 btrfs_set_token_item_offset(left, item,
3699 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3700 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003701 }
Chris Mason5f39d392007-10-15 16:14:19 -04003702 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003703
3704 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003705 if (push_items > right_nritems)
3706 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003707 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003708
Chris Mason34a38212007-11-07 13:31:03 -05003709 if (push_items < right_nritems) {
3710 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3711 leaf_data_end(root, right);
3712 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3713 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3714 btrfs_leaf_data(right) +
3715 leaf_data_end(root, right), push_space);
3716
3717 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003718 btrfs_item_nr_offset(push_items),
3719 (btrfs_header_nritems(right) - push_items) *
3720 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003721 }
Yaneef1c492007-11-26 10:58:13 -05003722 right_nritems -= push_items;
3723 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003724 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003725 for (i = 0; i < right_nritems; i++) {
3726 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003727
Chris Masoncfed81a2012-03-03 07:40:03 -05003728 push_space = push_space - btrfs_token_item_size(right,
3729 item, &token);
3730 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003731 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003732
Chris Mason5f39d392007-10-15 16:14:19 -04003733 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003734 if (right_nritems)
3735 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003736 else
3737 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003738
Chris Mason5f39d392007-10-15 16:14:19 -04003739 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003740 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003741
3742 /* then fixup the leaf pointer in the path */
3743 if (path->slots[0] < push_items) {
3744 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003745 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003746 free_extent_buffer(path->nodes[0]);
3747 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003748 path->slots[1] -= 1;
3749 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003750 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003751 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003752 path->slots[0] -= push_items;
3753 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003754 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003755 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003756out:
3757 btrfs_tree_unlock(left);
3758 free_extent_buffer(left);
3759 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003760}
3761
Chris Mason74123bd2007-02-02 11:05:29 -05003762/*
Chris Mason44871b12009-03-13 10:04:31 -04003763 * push some data in the path leaf to the left, trying to free up at
3764 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003765 *
3766 * max_slot can put a limit on how far into the leaf we'll push items. The
3767 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3768 * items
Chris Mason44871b12009-03-13 10:04:31 -04003769 */
3770static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003771 *root, struct btrfs_path *path, int min_data_size,
3772 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003773{
3774 struct extent_buffer *right = path->nodes[0];
3775 struct extent_buffer *left;
3776 int slot;
3777 int free_space;
3778 u32 right_nritems;
3779 int ret = 0;
3780
3781 slot = path->slots[1];
3782 if (slot == 0)
3783 return 1;
3784 if (!path->nodes[1])
3785 return 1;
3786
3787 right_nritems = btrfs_header_nritems(right);
3788 if (right_nritems == 0)
3789 return 1;
3790
3791 btrfs_assert_tree_locked(path->nodes[1]);
3792
3793 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003794 if (left == NULL)
3795 return 1;
3796
Chris Mason44871b12009-03-13 10:04:31 -04003797 btrfs_tree_lock(left);
3798 btrfs_set_lock_blocking(left);
3799
3800 free_space = btrfs_leaf_free_space(root, left);
3801 if (free_space < data_size) {
3802 ret = 1;
3803 goto out;
3804 }
3805
3806 /* cow and double check */
3807 ret = btrfs_cow_block(trans, root, left,
3808 path->nodes[1], slot - 1, &left);
3809 if (ret) {
3810 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003811 if (ret == -ENOSPC)
3812 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003813 goto out;
3814 }
3815
3816 free_space = btrfs_leaf_free_space(root, left);
3817 if (free_space < data_size) {
3818 ret = 1;
3819 goto out;
3820 }
3821
Chris Mason99d8f832010-07-07 10:51:48 -04003822 return __push_leaf_left(trans, root, path, min_data_size,
3823 empty, left, free_space, right_nritems,
3824 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003825out:
3826 btrfs_tree_unlock(left);
3827 free_extent_buffer(left);
3828 return ret;
3829}
3830
3831/*
Chris Mason74123bd2007-02-02 11:05:29 -05003832 * split the path's leaf in two, making sure there is at least data_size
3833 * available for the resulting leaf level of the path.
3834 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003835static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3836 struct btrfs_root *root,
3837 struct btrfs_path *path,
3838 struct extent_buffer *l,
3839 struct extent_buffer *right,
3840 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003841{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003842 int data_copy_size;
3843 int rt_data_off;
3844 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003845 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003846 struct btrfs_map_token token;
3847
3848 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003849
Chris Mason5f39d392007-10-15 16:14:19 -04003850 nritems = nritems - mid;
3851 btrfs_set_header_nritems(right, nritems);
3852 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3853
3854 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3855 btrfs_item_nr_offset(mid),
3856 nritems * sizeof(struct btrfs_item));
3857
3858 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003859 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3860 data_copy_size, btrfs_leaf_data(l) +
3861 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003862
Chris Mason5f39d392007-10-15 16:14:19 -04003863 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3864 btrfs_item_end_nr(l, mid);
3865
3866 for (i = 0; i < nritems; i++) {
3867 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003868 u32 ioff;
3869
Chris Masoncfed81a2012-03-03 07:40:03 -05003870 ioff = btrfs_token_item_offset(right, item, &token);
3871 btrfs_set_token_item_offset(right, item,
3872 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003873 }
Chris Mason74123bd2007-02-02 11:05:29 -05003874
Chris Mason5f39d392007-10-15 16:14:19 -04003875 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003876 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003877 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003878 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003879
3880 btrfs_mark_buffer_dirty(right);
3881 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003882 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003883
Chris Masonbe0e5c02007-01-26 15:51:26 -05003884 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003885 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003886 free_extent_buffer(path->nodes[0]);
3887 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003888 path->slots[0] -= mid;
3889 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003890 } else {
3891 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003892 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003893 }
Chris Mason5f39d392007-10-15 16:14:19 -04003894
Chris Masoneb60cea2007-02-02 09:18:22 -05003895 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003896}
3897
3898/*
Chris Mason99d8f832010-07-07 10:51:48 -04003899 * double splits happen when we need to insert a big item in the middle
3900 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3901 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3902 * A B C
3903 *
3904 * We avoid this by trying to push the items on either side of our target
3905 * into the adjacent leaves. If all goes well we can avoid the double split
3906 * completely.
3907 */
3908static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3909 struct btrfs_root *root,
3910 struct btrfs_path *path,
3911 int data_size)
3912{
3913 int ret;
3914 int progress = 0;
3915 int slot;
3916 u32 nritems;
3917
3918 slot = path->slots[0];
3919
3920 /*
3921 * try to push all the items after our slot into the
3922 * right leaf
3923 */
3924 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3925 if (ret < 0)
3926 return ret;
3927
3928 if (ret == 0)
3929 progress++;
3930
3931 nritems = btrfs_header_nritems(path->nodes[0]);
3932 /*
3933 * our goal is to get our slot at the start or end of a leaf. If
3934 * we've done so we're done
3935 */
3936 if (path->slots[0] == 0 || path->slots[0] == nritems)
3937 return 0;
3938
3939 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3940 return 0;
3941
3942 /* try to push all the items before our slot into the next leaf */
3943 slot = path->slots[0];
3944 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3945 if (ret < 0)
3946 return ret;
3947
3948 if (ret == 0)
3949 progress++;
3950
3951 if (progress)
3952 return 0;
3953 return 1;
3954}
3955
3956/*
Chris Mason44871b12009-03-13 10:04:31 -04003957 * split the path's leaf in two, making sure there is at least data_size
3958 * available for the resulting leaf level of the path.
3959 *
3960 * returns 0 if all went well and < 0 on failure.
3961 */
3962static noinline int split_leaf(struct btrfs_trans_handle *trans,
3963 struct btrfs_root *root,
3964 struct btrfs_key *ins_key,
3965 struct btrfs_path *path, int data_size,
3966 int extend)
3967{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003968 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003969 struct extent_buffer *l;
3970 u32 nritems;
3971 int mid;
3972 int slot;
3973 struct extent_buffer *right;
3974 int ret = 0;
3975 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003976 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003977 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003978 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003979
Yan, Zhenga5719522009-09-24 09:17:31 -04003980 l = path->nodes[0];
3981 slot = path->slots[0];
3982 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3983 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3984 return -EOVERFLOW;
3985
Chris Mason44871b12009-03-13 10:04:31 -04003986 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003987 if (data_size) {
3988 wret = push_leaf_right(trans, root, path, data_size,
3989 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003990 if (wret < 0)
3991 return wret;
3992 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003993 wret = push_leaf_left(trans, root, path, data_size,
3994 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003995 if (wret < 0)
3996 return wret;
3997 }
3998 l = path->nodes[0];
3999
4000 /* did the pushes work? */
4001 if (btrfs_leaf_free_space(root, l) >= data_size)
4002 return 0;
4003 }
4004
4005 if (!path->nodes[1]) {
Jan Schmidt90f8d622013-04-13 13:19:53 +00004006 ret = insert_new_root(trans, root, path, 1, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004007 if (ret)
4008 return ret;
4009 }
4010again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004011 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004012 l = path->nodes[0];
4013 slot = path->slots[0];
4014 nritems = btrfs_header_nritems(l);
4015 mid = (nritems + 1) / 2;
4016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004017 if (mid <= slot) {
4018 if (nritems == 1 ||
4019 leaf_space_used(l, mid, nritems - mid) + data_size >
4020 BTRFS_LEAF_DATA_SIZE(root)) {
4021 if (slot >= nritems) {
4022 split = 0;
4023 } else {
4024 mid = slot;
4025 if (mid != nritems &&
4026 leaf_space_used(l, mid, nritems - mid) +
4027 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004028 if (data_size && !tried_avoid_double)
4029 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004030 split = 2;
4031 }
4032 }
4033 }
4034 } else {
4035 if (leaf_space_used(l, 0, mid) + data_size >
4036 BTRFS_LEAF_DATA_SIZE(root)) {
4037 if (!extend && data_size && slot == 0) {
4038 split = 0;
4039 } else if ((extend || !data_size) && slot == 0) {
4040 mid = 1;
4041 } else {
4042 mid = slot;
4043 if (mid != nritems &&
4044 leaf_space_used(l, mid, nritems - mid) +
4045 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004046 if (data_size && !tried_avoid_double)
4047 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048 split = 2 ;
4049 }
4050 }
4051 }
4052 }
4053
4054 if (split == 0)
4055 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4056 else
4057 btrfs_item_key(l, &disk_key, mid);
4058
4059 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004060 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004061 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004062 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004063 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004064
4065 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004066
4067 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4068 btrfs_set_header_bytenr(right, right->start);
4069 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004070 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004071 btrfs_set_header_owner(right, root->root_key.objectid);
4072 btrfs_set_header_level(right, 0);
4073 write_extent_buffer(right, root->fs_info->fsid,
4074 (unsigned long)btrfs_header_fsid(right),
4075 BTRFS_FSID_SIZE);
4076
4077 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
4078 (unsigned long)btrfs_header_chunk_tree_uuid(right),
4079 BTRFS_UUID_SIZE);
4080
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 if (split == 0) {
4082 if (mid <= slot) {
4083 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004084 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004085 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086 btrfs_tree_unlock(path->nodes[0]);
4087 free_extent_buffer(path->nodes[0]);
4088 path->nodes[0] = right;
4089 path->slots[0] = 0;
4090 path->slots[1] += 1;
4091 } else {
4092 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004093 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004094 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004095 btrfs_tree_unlock(path->nodes[0]);
4096 free_extent_buffer(path->nodes[0]);
4097 path->nodes[0] = right;
4098 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004099 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004100 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004101 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004102 btrfs_mark_buffer_dirty(right);
4103 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004104 }
4105
Jeff Mahoney143bede2012-03-01 14:56:26 +01004106 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004109 BUG_ON(num_doubles != 0);
4110 num_doubles++;
4111 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004112 }
Chris Mason44871b12009-03-13 10:04:31 -04004113
Jeff Mahoney143bede2012-03-01 14:56:26 +01004114 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004115
4116push_for_double:
4117 push_for_double_split(trans, root, path, data_size);
4118 tried_avoid_double = 1;
4119 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4120 return 0;
4121 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004122}
4123
Yan, Zhengad48fd752009-11-12 09:33:58 +00004124static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4125 struct btrfs_root *root,
4126 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004127{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004128 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004129 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004130 struct btrfs_file_extent_item *fi;
4131 u64 extent_len = 0;
4132 u32 item_size;
4133 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004134
4135 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004136 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4137
4138 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4139 key.type != BTRFS_EXTENT_CSUM_KEY);
4140
4141 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4142 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004143
4144 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004145 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4146 fi = btrfs_item_ptr(leaf, path->slots[0],
4147 struct btrfs_file_extent_item);
4148 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4149 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004150 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004151
Chris Mason459931e2008-12-10 09:10:46 -05004152 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004153 path->search_for_split = 1;
4154 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004155 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004156 if (ret < 0)
4157 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004158
Yan, Zhengad48fd752009-11-12 09:33:58 +00004159 ret = -EAGAIN;
4160 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004161 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004162 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4163 goto err;
4164
Chris Mason109f6ae2010-04-02 09:20:18 -04004165 /* the leaf has changed, it now has room. return now */
4166 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4167 goto err;
4168
Yan, Zhengad48fd752009-11-12 09:33:58 +00004169 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4170 fi = btrfs_item_ptr(leaf, path->slots[0],
4171 struct btrfs_file_extent_item);
4172 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4173 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004174 }
4175
Chris Masonb9473432009-03-13 11:00:37 -04004176 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004177 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004178 if (ret)
4179 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004180
Yan, Zhengad48fd752009-11-12 09:33:58 +00004181 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004182 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004183 return 0;
4184err:
4185 path->keep_locks = 0;
4186 return ret;
4187}
4188
4189static noinline int split_item(struct btrfs_trans_handle *trans,
4190 struct btrfs_root *root,
4191 struct btrfs_path *path,
4192 struct btrfs_key *new_key,
4193 unsigned long split_offset)
4194{
4195 struct extent_buffer *leaf;
4196 struct btrfs_item *item;
4197 struct btrfs_item *new_item;
4198 int slot;
4199 char *buf;
4200 u32 nritems;
4201 u32 item_size;
4202 u32 orig_offset;
4203 struct btrfs_disk_key disk_key;
4204
Chris Masonb9473432009-03-13 11:00:37 -04004205 leaf = path->nodes[0];
4206 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4207
Chris Masonb4ce94d2009-02-04 09:25:08 -05004208 btrfs_set_path_blocking(path);
4209
Chris Mason459931e2008-12-10 09:10:46 -05004210 item = btrfs_item_nr(leaf, path->slots[0]);
4211 orig_offset = btrfs_item_offset(leaf, item);
4212 item_size = btrfs_item_size(leaf, item);
4213
Chris Mason459931e2008-12-10 09:10:46 -05004214 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004215 if (!buf)
4216 return -ENOMEM;
4217
Chris Mason459931e2008-12-10 09:10:46 -05004218 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4219 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004220
Chris Mason459931e2008-12-10 09:10:46 -05004221 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004222 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004223 if (slot != nritems) {
4224 /* shift the items */
4225 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004226 btrfs_item_nr_offset(slot),
4227 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004228 }
4229
4230 btrfs_cpu_key_to_disk(&disk_key, new_key);
4231 btrfs_set_item_key(leaf, &disk_key, slot);
4232
4233 new_item = btrfs_item_nr(leaf, slot);
4234
4235 btrfs_set_item_offset(leaf, new_item, orig_offset);
4236 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4237
4238 btrfs_set_item_offset(leaf, item,
4239 orig_offset + item_size - split_offset);
4240 btrfs_set_item_size(leaf, item, split_offset);
4241
4242 btrfs_set_header_nritems(leaf, nritems + 1);
4243
4244 /* write the data for the start of the original item */
4245 write_extent_buffer(leaf, buf,
4246 btrfs_item_ptr_offset(leaf, path->slots[0]),
4247 split_offset);
4248
4249 /* write the data for the new item */
4250 write_extent_buffer(leaf, buf + split_offset,
4251 btrfs_item_ptr_offset(leaf, slot),
4252 item_size - split_offset);
4253 btrfs_mark_buffer_dirty(leaf);
4254
Yan, Zhengad48fd752009-11-12 09:33:58 +00004255 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004256 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004257 return 0;
4258}
4259
4260/*
4261 * This function splits a single item into two items,
4262 * giving 'new_key' to the new item and splitting the
4263 * old one at split_offset (from the start of the item).
4264 *
4265 * The path may be released by this operation. After
4266 * the split, the path is pointing to the old item. The
4267 * new item is going to be in the same node as the old one.
4268 *
4269 * Note, the item being split must be smaller enough to live alone on
4270 * a tree block with room for one extra struct btrfs_item
4271 *
4272 * This allows us to split the item in place, keeping a lock on the
4273 * leaf the entire time.
4274 */
4275int btrfs_split_item(struct btrfs_trans_handle *trans,
4276 struct btrfs_root *root,
4277 struct btrfs_path *path,
4278 struct btrfs_key *new_key,
4279 unsigned long split_offset)
4280{
4281 int ret;
4282 ret = setup_leaf_for_split(trans, root, path,
4283 sizeof(struct btrfs_item));
4284 if (ret)
4285 return ret;
4286
4287 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004288 return ret;
4289}
4290
4291/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004292 * This function duplicate a item, giving 'new_key' to the new item.
4293 * It guarantees both items live in the same tree leaf and the new item
4294 * is contiguous with the original item.
4295 *
4296 * This allows us to split file extent in place, keeping a lock on the
4297 * leaf the entire time.
4298 */
4299int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4300 struct btrfs_root *root,
4301 struct btrfs_path *path,
4302 struct btrfs_key *new_key)
4303{
4304 struct extent_buffer *leaf;
4305 int ret;
4306 u32 item_size;
4307
4308 leaf = path->nodes[0];
4309 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4310 ret = setup_leaf_for_split(trans, root, path,
4311 item_size + sizeof(struct btrfs_item));
4312 if (ret)
4313 return ret;
4314
4315 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004316 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004317 item_size, item_size +
4318 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004319 leaf = path->nodes[0];
4320 memcpy_extent_buffer(leaf,
4321 btrfs_item_ptr_offset(leaf, path->slots[0]),
4322 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4323 item_size);
4324 return 0;
4325}
4326
4327/*
Chris Masond352ac62008-09-29 15:18:18 -04004328 * make the item pointed to by the path smaller. new_size indicates
4329 * how small to make it, and from_end tells us if we just chop bytes
4330 * off the end of the item or if we shift the item to chop bytes off
4331 * the front.
4332 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004333void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004334 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004335{
Chris Masonb18c6682007-04-17 13:26:50 -04004336 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004337 struct extent_buffer *leaf;
4338 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004339 u32 nritems;
4340 unsigned int data_end;
4341 unsigned int old_data_start;
4342 unsigned int old_size;
4343 unsigned int size_diff;
4344 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004345 struct btrfs_map_token token;
4346
4347 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004348
Chris Mason5f39d392007-10-15 16:14:19 -04004349 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004350 slot = path->slots[0];
4351
4352 old_size = btrfs_item_size_nr(leaf, slot);
4353 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004354 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004355
Chris Mason5f39d392007-10-15 16:14:19 -04004356 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004357 data_end = leaf_data_end(root, leaf);
4358
Chris Mason5f39d392007-10-15 16:14:19 -04004359 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004360
Chris Masonb18c6682007-04-17 13:26:50 -04004361 size_diff = old_size - new_size;
4362
4363 BUG_ON(slot < 0);
4364 BUG_ON(slot >= nritems);
4365
4366 /*
4367 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4368 */
4369 /* first correct the data pointers */
4370 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004371 u32 ioff;
4372 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004373
Chris Masoncfed81a2012-03-03 07:40:03 -05004374 ioff = btrfs_token_item_offset(leaf, item, &token);
4375 btrfs_set_token_item_offset(leaf, item,
4376 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004377 }
Chris Masondb945352007-10-15 16:15:53 -04004378
Chris Masonb18c6682007-04-17 13:26:50 -04004379 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004380 if (from_end) {
4381 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4382 data_end + size_diff, btrfs_leaf_data(leaf) +
4383 data_end, old_data_start + new_size - data_end);
4384 } else {
4385 struct btrfs_disk_key disk_key;
4386 u64 offset;
4387
4388 btrfs_item_key(leaf, &disk_key, slot);
4389
4390 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4391 unsigned long ptr;
4392 struct btrfs_file_extent_item *fi;
4393
4394 fi = btrfs_item_ptr(leaf, slot,
4395 struct btrfs_file_extent_item);
4396 fi = (struct btrfs_file_extent_item *)(
4397 (unsigned long)fi - size_diff);
4398
4399 if (btrfs_file_extent_type(leaf, fi) ==
4400 BTRFS_FILE_EXTENT_INLINE) {
4401 ptr = btrfs_item_ptr_offset(leaf, slot);
4402 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004403 (unsigned long)fi,
4404 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004405 disk_bytenr));
4406 }
4407 }
4408
4409 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4410 data_end + size_diff, btrfs_leaf_data(leaf) +
4411 data_end, old_data_start - data_end);
4412
4413 offset = btrfs_disk_key_offset(&disk_key);
4414 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4415 btrfs_set_item_key(leaf, &disk_key, slot);
4416 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004417 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004418 }
Chris Mason5f39d392007-10-15 16:14:19 -04004419
4420 item = btrfs_item_nr(leaf, slot);
4421 btrfs_set_item_size(leaf, item, new_size);
4422 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004423
Chris Mason5f39d392007-10-15 16:14:19 -04004424 if (btrfs_leaf_free_space(root, leaf) < 0) {
4425 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004426 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004427 }
Chris Masonb18c6682007-04-17 13:26:50 -04004428}
4429
Chris Masond352ac62008-09-29 15:18:18 -04004430/*
4431 * make the item pointed to by the path bigger, data_size is the new size.
4432 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004433void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004434 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004435{
Chris Mason6567e832007-04-16 09:22:45 -04004436 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004437 struct extent_buffer *leaf;
4438 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004439 u32 nritems;
4440 unsigned int data_end;
4441 unsigned int old_data;
4442 unsigned int old_size;
4443 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004444 struct btrfs_map_token token;
4445
4446 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004447
Chris Mason5f39d392007-10-15 16:14:19 -04004448 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004449
Chris Mason5f39d392007-10-15 16:14:19 -04004450 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004451 data_end = leaf_data_end(root, leaf);
4452
Chris Mason5f39d392007-10-15 16:14:19 -04004453 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4454 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004455 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004456 }
Chris Mason6567e832007-04-16 09:22:45 -04004457 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004458 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004459
4460 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004461 if (slot >= nritems) {
4462 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004463 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4464 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004465 BUG_ON(1);
4466 }
Chris Mason6567e832007-04-16 09:22:45 -04004467
4468 /*
4469 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4470 */
4471 /* first correct the data pointers */
4472 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004473 u32 ioff;
4474 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004475
Chris Masoncfed81a2012-03-03 07:40:03 -05004476 ioff = btrfs_token_item_offset(leaf, item, &token);
4477 btrfs_set_token_item_offset(leaf, item,
4478 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004479 }
Chris Mason5f39d392007-10-15 16:14:19 -04004480
Chris Mason6567e832007-04-16 09:22:45 -04004481 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004482 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004483 data_end - data_size, btrfs_leaf_data(leaf) +
4484 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004485
Chris Mason6567e832007-04-16 09:22:45 -04004486 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004487 old_size = btrfs_item_size_nr(leaf, slot);
4488 item = btrfs_item_nr(leaf, slot);
4489 btrfs_set_item_size(leaf, item, old_size + data_size);
4490 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004491
Chris Mason5f39d392007-10-15 16:14:19 -04004492 if (btrfs_leaf_free_space(root, leaf) < 0) {
4493 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004494 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004495 }
Chris Mason6567e832007-04-16 09:22:45 -04004496}
4497
Chris Mason74123bd2007-02-02 11:05:29 -05004498/*
Chris Mason44871b12009-03-13 10:04:31 -04004499 * this is a helper for btrfs_insert_empty_items, the main goal here is
4500 * to save stack depth by doing the bulk of the work in a function
4501 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004502 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004503void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004504 struct btrfs_key *cpu_key, u32 *data_size,
4505 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004506{
Chris Mason5f39d392007-10-15 16:14:19 -04004507 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004508 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004509 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004510 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004511 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004512 struct extent_buffer *leaf;
4513 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004514 struct btrfs_map_token token;
4515
4516 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004517
Chris Mason5f39d392007-10-15 16:14:19 -04004518 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004519 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004520
Chris Mason5f39d392007-10-15 16:14:19 -04004521 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004522 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004523
Chris Masonf25956c2008-09-12 15:32:53 -04004524 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004525 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004526 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004527 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004528 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004529 }
Chris Mason5f39d392007-10-15 16:14:19 -04004530
Chris Masonbe0e5c02007-01-26 15:51:26 -05004531 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004532 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004533
Chris Mason5f39d392007-10-15 16:14:19 -04004534 if (old_data < data_end) {
4535 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004536 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004537 slot, old_data, data_end);
4538 BUG_ON(1);
4539 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004540 /*
4541 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4542 */
4543 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004544 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004545 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004546
Chris Mason5f39d392007-10-15 16:14:19 -04004547 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004548 ioff = btrfs_token_item_offset(leaf, item, &token);
4549 btrfs_set_token_item_offset(leaf, item,
4550 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004551 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004552 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004553 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004554 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004555 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004556
4557 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004558 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004559 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004560 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004561 data_end = old_data;
4562 }
Chris Mason5f39d392007-10-15 16:14:19 -04004563
Chris Mason62e27492007-03-15 12:56:47 -04004564 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004565 for (i = 0; i < nr; i++) {
4566 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4567 btrfs_set_item_key(leaf, &disk_key, slot + i);
4568 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004569 btrfs_set_token_item_offset(leaf, item,
4570 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004571 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004572 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004573 }
Chris Mason44871b12009-03-13 10:04:31 -04004574
Chris Mason9c583092008-01-29 15:15:18 -05004575 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004576
Chris Mason5a01a2e2008-01-30 11:43:54 -05004577 if (slot == 0) {
4578 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004579 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004580 }
Chris Masonb9473432009-03-13 11:00:37 -04004581 btrfs_unlock_up_safe(path, 1);
4582 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004583
Chris Mason5f39d392007-10-15 16:14:19 -04004584 if (btrfs_leaf_free_space(root, leaf) < 0) {
4585 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004586 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004587 }
Chris Mason44871b12009-03-13 10:04:31 -04004588}
4589
4590/*
4591 * Given a key and some data, insert items into the tree.
4592 * This does all the path init required, making room in the tree if needed.
4593 */
4594int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4595 struct btrfs_root *root,
4596 struct btrfs_path *path,
4597 struct btrfs_key *cpu_key, u32 *data_size,
4598 int nr)
4599{
Chris Mason44871b12009-03-13 10:04:31 -04004600 int ret = 0;
4601 int slot;
4602 int i;
4603 u32 total_size = 0;
4604 u32 total_data = 0;
4605
4606 for (i = 0; i < nr; i++)
4607 total_data += data_size[i];
4608
4609 total_size = total_data + (nr * sizeof(struct btrfs_item));
4610 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4611 if (ret == 0)
4612 return -EEXIST;
4613 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004614 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004615
Chris Mason44871b12009-03-13 10:04:31 -04004616 slot = path->slots[0];
4617 BUG_ON(slot < 0);
4618
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004619 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004620 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004621 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004622}
4623
4624/*
4625 * Given a key and some data, insert an item into the tree.
4626 * This does all the path init required, making room in the tree if needed.
4627 */
Chris Masone089f052007-03-16 16:20:31 -04004628int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4629 *root, struct btrfs_key *cpu_key, void *data, u32
4630 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004631{
4632 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004633 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004634 struct extent_buffer *leaf;
4635 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004636
Chris Mason2c90e5d2007-04-02 10:50:19 -04004637 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004638 if (!path)
4639 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004640 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004641 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004642 leaf = path->nodes[0];
4643 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4644 write_extent_buffer(leaf, data, ptr, data_size);
4645 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004646 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004647 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004648 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004649}
4650
Chris Mason74123bd2007-02-02 11:05:29 -05004651/*
Chris Mason5de08d72007-02-24 06:24:44 -05004652 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004653 *
Chris Masond352ac62008-09-29 15:18:18 -04004654 * the tree should have been previously balanced so the deletion does not
4655 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004656 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004657static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4658 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004659{
Chris Mason5f39d392007-10-15 16:14:19 -04004660 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004661 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004662 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004663
Chris Mason5f39d392007-10-15 16:14:19 -04004664 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004665 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004666 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004667 tree_mod_log_eb_move(root->fs_info, parent, slot,
4668 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004669 memmove_extent_buffer(parent,
4670 btrfs_node_key_ptr_offset(slot),
4671 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004672 sizeof(struct btrfs_key_ptr) *
4673 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004674 } else if (level) {
4675 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4676 MOD_LOG_KEY_REMOVE);
4677 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004678 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004679
Chris Mason7518a232007-03-12 12:01:18 -04004680 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004681 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004682 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004683 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004684 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004685 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004686 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004687 struct btrfs_disk_key disk_key;
4688
4689 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004690 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004691 }
Chris Masond6025572007-03-30 14:27:56 -04004692 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004693}
4694
Chris Mason74123bd2007-02-02 11:05:29 -05004695/*
Chris Mason323ac952008-10-01 19:05:46 -04004696 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004697 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004698 *
4699 * This deletes the pointer in path->nodes[1] and frees the leaf
4700 * block extent. zero is returned if it all worked out, < 0 otherwise.
4701 *
4702 * The path must have already been setup for deleting the leaf, including
4703 * all the proper balancing. path->nodes[1] must be locked.
4704 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004705static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4706 struct btrfs_root *root,
4707 struct btrfs_path *path,
4708 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004709{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004710 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004711 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004712
Chris Mason4d081c42009-02-04 09:31:28 -05004713 /*
4714 * btrfs_free_extent is expensive, we want to make sure we
4715 * aren't holding any locks when we call it
4716 */
4717 btrfs_unlock_up_safe(path, 0);
4718
Yan, Zhengf0486c62010-05-16 10:46:25 -04004719 root_sub_used(root, leaf->len);
4720
Josef Bacik3083ee22012-03-09 16:01:49 -05004721 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004722 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004723 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004724}
4725/*
Chris Mason74123bd2007-02-02 11:05:29 -05004726 * delete the item at the leaf level in path. If that empties
4727 * the leaf, remove it from the tree
4728 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004729int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4730 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004731{
Chris Mason5f39d392007-10-15 16:14:19 -04004732 struct extent_buffer *leaf;
4733 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004734 int last_off;
4735 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004736 int ret = 0;
4737 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004738 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004739 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004740 struct btrfs_map_token token;
4741
4742 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004743
Chris Mason5f39d392007-10-15 16:14:19 -04004744 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004745 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4746
4747 for (i = 0; i < nr; i++)
4748 dsize += btrfs_item_size_nr(leaf, slot + i);
4749
Chris Mason5f39d392007-10-15 16:14:19 -04004750 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004751
Chris Mason85e21ba2008-01-29 15:11:36 -05004752 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004753 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004754
4755 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004756 data_end + dsize,
4757 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004758 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004759
Chris Mason85e21ba2008-01-29 15:11:36 -05004760 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004761 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004762
Chris Mason5f39d392007-10-15 16:14:19 -04004763 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004764 ioff = btrfs_token_item_offset(leaf, item, &token);
4765 btrfs_set_token_item_offset(leaf, item,
4766 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004767 }
Chris Masondb945352007-10-15 16:15:53 -04004768
Chris Mason5f39d392007-10-15 16:14:19 -04004769 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004770 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004771 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004772 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004773 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004774 btrfs_set_header_nritems(leaf, nritems - nr);
4775 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004776
Chris Mason74123bd2007-02-02 11:05:29 -05004777 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004778 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004779 if (leaf == root->node) {
4780 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004781 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004782 btrfs_set_path_blocking(path);
4783 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004784 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004785 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004786 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004787 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004788 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004789 struct btrfs_disk_key disk_key;
4790
4791 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004792 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004793 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004794
Chris Mason74123bd2007-02-02 11:05:29 -05004795 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004796 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004797 /* push_leaf_left fixes the path.
4798 * make sure the path still points to our leaf
4799 * for possible call to del_ptr below
4800 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004801 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004802 extent_buffer_get(leaf);
4803
Chris Masonb9473432009-03-13 11:00:37 -04004804 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004805 wret = push_leaf_left(trans, root, path, 1, 1,
4806 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004807 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004808 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004809
4810 if (path->nodes[0] == leaf &&
4811 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004812 wret = push_leaf_right(trans, root, path, 1,
4813 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004814 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004815 ret = wret;
4816 }
Chris Mason5f39d392007-10-15 16:14:19 -04004817
4818 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004819 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004820 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004821 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004822 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004823 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004824 /* if we're still in the path, make sure
4825 * we're dirty. Otherwise, one of the
4826 * push_leaf functions must have already
4827 * dirtied this buffer
4828 */
4829 if (path->nodes[0] == leaf)
4830 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004831 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004832 }
Chris Masond5719762007-03-23 10:01:08 -04004833 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004834 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004835 }
4836 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004837 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004838}
4839
Chris Mason97571fd2007-02-24 13:39:08 -05004840/*
Chris Mason925baed2008-06-25 16:01:30 -04004841 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004842 * returns 0 if it found something or 1 if there are no lesser leaves.
4843 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004844 *
4845 * This may release the path, and so you may lose any locks held at the
4846 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004847 */
4848int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4849{
Chris Mason925baed2008-06-25 16:01:30 -04004850 struct btrfs_key key;
4851 struct btrfs_disk_key found_key;
4852 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004853
Chris Mason925baed2008-06-25 16:01:30 -04004854 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004855
Chris Mason925baed2008-06-25 16:01:30 -04004856 if (key.offset > 0)
4857 key.offset--;
4858 else if (key.type > 0)
4859 key.type--;
4860 else if (key.objectid > 0)
4861 key.objectid--;
4862 else
4863 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004864
David Sterbab3b4aa72011-04-21 01:20:15 +02004865 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004866 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4867 if (ret < 0)
4868 return ret;
4869 btrfs_item_key(path->nodes[0], &found_key, 0);
4870 ret = comp_keys(&found_key, &key);
4871 if (ret < 0)
4872 return 0;
4873 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004874}
4875
Chris Mason3f157a22008-06-25 16:01:31 -04004876/*
4877 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004878 * for nodes or leaves that are have a minimum transaction id.
4879 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004880 *
4881 * This does not cow, but it does stuff the starting key it finds back
4882 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4883 * key and get a writable path.
4884 *
4885 * This does lock as it descends, and path->keep_locks should be set
4886 * to 1 by the caller.
4887 *
4888 * This honors path->lowest_level to prevent descent past a given level
4889 * of the tree.
4890 *
Chris Masond352ac62008-09-29 15:18:18 -04004891 * min_trans indicates the oldest transaction that you are interested
4892 * in walking through. Any nodes or leaves older than min_trans are
4893 * skipped over (without reading them).
4894 *
Chris Mason3f157a22008-06-25 16:01:31 -04004895 * returns zero if something useful was found, < 0 on error and 1 if there
4896 * was nothing in the tree that matched the search criteria.
4897 */
4898int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004899 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004900 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004901 u64 min_trans)
4902{
4903 struct extent_buffer *cur;
4904 struct btrfs_key found_key;
4905 int slot;
Yan96524802008-07-24 12:19:49 -04004906 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004907 u32 nritems;
4908 int level;
4909 int ret = 1;
4910
Chris Mason934d3752008-12-08 16:43:10 -05004911 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004912again:
Chris Masonbd681512011-07-16 15:23:14 -04004913 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004914 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004915 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004916 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004917 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004918
4919 if (btrfs_header_generation(cur) < min_trans) {
4920 ret = 1;
4921 goto out;
4922 }
Chris Masond3977122009-01-05 21:25:51 -05004923 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004924 nritems = btrfs_header_nritems(cur);
4925 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004926 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004927
Chris Mason323ac952008-10-01 19:05:46 -04004928 /* at the lowest level, we're done, setup the path and exit */
4929 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004930 if (slot >= nritems)
4931 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004932 ret = 0;
4933 path->slots[level] = slot;
4934 btrfs_item_key_to_cpu(cur, &found_key, slot);
4935 goto out;
4936 }
Yan96524802008-07-24 12:19:49 -04004937 if (sret && slot > 0)
4938 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004939 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004940 * check this node pointer against the min_trans parameters.
4941 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004942 */
Chris Masond3977122009-01-05 21:25:51 -05004943 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004944 u64 blockptr;
4945 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004946
Chris Mason3f157a22008-06-25 16:01:31 -04004947 blockptr = btrfs_node_blockptr(cur, slot);
4948 gen = btrfs_node_ptr_generation(cur, slot);
4949 if (gen < min_trans) {
4950 slot++;
4951 continue;
4952 }
Eric Sandeende78b512013-01-31 18:21:12 +00004953 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004954 }
Chris Masone02119d2008-09-05 16:13:11 -04004955find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004956 /*
4957 * we didn't find a candidate key in this node, walk forward
4958 * and find another one
4959 */
4960 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004961 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004962 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004963 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004964 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004965 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004966 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004967 goto again;
4968 } else {
4969 goto out;
4970 }
4971 }
4972 /* save our key for returning back */
4973 btrfs_node_key_to_cpu(cur, &found_key, slot);
4974 path->slots[level] = slot;
4975 if (level == path->lowest_level) {
4976 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004977 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004978 goto out;
4979 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004980 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004981 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004982 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004983
Chris Masonbd681512011-07-16 15:23:14 -04004984 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004985
Chris Masonbd681512011-07-16 15:23:14 -04004986 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004987 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004988 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004989 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004990 }
4991out:
4992 if (ret == 0)
4993 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004994 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004995 return ret;
4996}
4997
Alexander Block70698302012-06-05 21:07:48 +02004998static void tree_move_down(struct btrfs_root *root,
4999 struct btrfs_path *path,
5000 int *level, int root_level)
5001{
Chris Mason74dd17f2012-08-07 16:25:13 -04005002 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005003 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5004 path->slots[*level]);
5005 path->slots[*level - 1] = 0;
5006 (*level)--;
5007}
5008
5009static int tree_move_next_or_upnext(struct btrfs_root *root,
5010 struct btrfs_path *path,
5011 int *level, int root_level)
5012{
5013 int ret = 0;
5014 int nritems;
5015 nritems = btrfs_header_nritems(path->nodes[*level]);
5016
5017 path->slots[*level]++;
5018
Chris Mason74dd17f2012-08-07 16:25:13 -04005019 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005020 if (*level == root_level)
5021 return -1;
5022
5023 /* move upnext */
5024 path->slots[*level] = 0;
5025 free_extent_buffer(path->nodes[*level]);
5026 path->nodes[*level] = NULL;
5027 (*level)++;
5028 path->slots[*level]++;
5029
5030 nritems = btrfs_header_nritems(path->nodes[*level]);
5031 ret = 1;
5032 }
5033 return ret;
5034}
5035
5036/*
5037 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5038 * or down.
5039 */
5040static int tree_advance(struct btrfs_root *root,
5041 struct btrfs_path *path,
5042 int *level, int root_level,
5043 int allow_down,
5044 struct btrfs_key *key)
5045{
5046 int ret;
5047
5048 if (*level == 0 || !allow_down) {
5049 ret = tree_move_next_or_upnext(root, path, level, root_level);
5050 } else {
5051 tree_move_down(root, path, level, root_level);
5052 ret = 0;
5053 }
5054 if (ret >= 0) {
5055 if (*level == 0)
5056 btrfs_item_key_to_cpu(path->nodes[*level], key,
5057 path->slots[*level]);
5058 else
5059 btrfs_node_key_to_cpu(path->nodes[*level], key,
5060 path->slots[*level]);
5061 }
5062 return ret;
5063}
5064
5065static int tree_compare_item(struct btrfs_root *left_root,
5066 struct btrfs_path *left_path,
5067 struct btrfs_path *right_path,
5068 char *tmp_buf)
5069{
5070 int cmp;
5071 int len1, len2;
5072 unsigned long off1, off2;
5073
5074 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5075 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5076 if (len1 != len2)
5077 return 1;
5078
5079 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5080 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5081 right_path->slots[0]);
5082
5083 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5084
5085 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5086 if (cmp)
5087 return 1;
5088 return 0;
5089}
5090
5091#define ADVANCE 1
5092#define ADVANCE_ONLY_NEXT -1
5093
5094/*
5095 * This function compares two trees and calls the provided callback for
5096 * every changed/new/deleted item it finds.
5097 * If shared tree blocks are encountered, whole subtrees are skipped, making
5098 * the compare pretty fast on snapshotted subvolumes.
5099 *
5100 * This currently works on commit roots only. As commit roots are read only,
5101 * we don't do any locking. The commit roots are protected with transactions.
5102 * Transactions are ended and rejoined when a commit is tried in between.
5103 *
5104 * This function checks for modifications done to the trees while comparing.
5105 * If it detects a change, it aborts immediately.
5106 */
5107int btrfs_compare_trees(struct btrfs_root *left_root,
5108 struct btrfs_root *right_root,
5109 btrfs_changed_cb_t changed_cb, void *ctx)
5110{
5111 int ret;
5112 int cmp;
5113 struct btrfs_trans_handle *trans = NULL;
5114 struct btrfs_path *left_path = NULL;
5115 struct btrfs_path *right_path = NULL;
5116 struct btrfs_key left_key;
5117 struct btrfs_key right_key;
5118 char *tmp_buf = NULL;
5119 int left_root_level;
5120 int right_root_level;
5121 int left_level;
5122 int right_level;
5123 int left_end_reached;
5124 int right_end_reached;
5125 int advance_left;
5126 int advance_right;
5127 u64 left_blockptr;
5128 u64 right_blockptr;
5129 u64 left_start_ctransid;
5130 u64 right_start_ctransid;
5131 u64 ctransid;
5132
5133 left_path = btrfs_alloc_path();
5134 if (!left_path) {
5135 ret = -ENOMEM;
5136 goto out;
5137 }
5138 right_path = btrfs_alloc_path();
5139 if (!right_path) {
5140 ret = -ENOMEM;
5141 goto out;
5142 }
5143
5144 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5145 if (!tmp_buf) {
5146 ret = -ENOMEM;
5147 goto out;
5148 }
5149
5150 left_path->search_commit_root = 1;
5151 left_path->skip_locking = 1;
5152 right_path->search_commit_root = 1;
5153 right_path->skip_locking = 1;
5154
Anand Jain5f3ab902012-12-07 09:28:54 +00005155 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005156 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005157 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005158
Anand Jain5f3ab902012-12-07 09:28:54 +00005159 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005160 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005161 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005162
5163 trans = btrfs_join_transaction(left_root);
5164 if (IS_ERR(trans)) {
5165 ret = PTR_ERR(trans);
5166 trans = NULL;
5167 goto out;
5168 }
5169
5170 /*
5171 * Strategy: Go to the first items of both trees. Then do
5172 *
5173 * If both trees are at level 0
5174 * Compare keys of current items
5175 * If left < right treat left item as new, advance left tree
5176 * and repeat
5177 * If left > right treat right item as deleted, advance right tree
5178 * and repeat
5179 * If left == right do deep compare of items, treat as changed if
5180 * needed, advance both trees and repeat
5181 * If both trees are at the same level but not at level 0
5182 * Compare keys of current nodes/leafs
5183 * If left < right advance left tree and repeat
5184 * If left > right advance right tree and repeat
5185 * If left == right compare blockptrs of the next nodes/leafs
5186 * If they match advance both trees but stay at the same level
5187 * and repeat
5188 * If they don't match advance both trees while allowing to go
5189 * deeper and repeat
5190 * If tree levels are different
5191 * Advance the tree that needs it and repeat
5192 *
5193 * Advancing a tree means:
5194 * If we are at level 0, try to go to the next slot. If that's not
5195 * possible, go one level up and repeat. Stop when we found a level
5196 * where we could go to the next slot. We may at this point be on a
5197 * node or a leaf.
5198 *
5199 * If we are not at level 0 and not on shared tree blocks, go one
5200 * level deeper.
5201 *
5202 * If we are not at level 0 and on shared tree blocks, go one slot to
5203 * the right if possible or go up and right.
5204 */
5205
5206 left_level = btrfs_header_level(left_root->commit_root);
5207 left_root_level = left_level;
5208 left_path->nodes[left_level] = left_root->commit_root;
5209 extent_buffer_get(left_path->nodes[left_level]);
5210
5211 right_level = btrfs_header_level(right_root->commit_root);
5212 right_root_level = right_level;
5213 right_path->nodes[right_level] = right_root->commit_root;
5214 extent_buffer_get(right_path->nodes[right_level]);
5215
5216 if (left_level == 0)
5217 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5218 &left_key, left_path->slots[left_level]);
5219 else
5220 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5221 &left_key, left_path->slots[left_level]);
5222 if (right_level == 0)
5223 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5224 &right_key, right_path->slots[right_level]);
5225 else
5226 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5227 &right_key, right_path->slots[right_level]);
5228
5229 left_end_reached = right_end_reached = 0;
5230 advance_left = advance_right = 0;
5231
5232 while (1) {
5233 /*
5234 * We need to make sure the transaction does not get committed
5235 * while we do anything on commit roots. This means, we need to
5236 * join and leave transactions for every item that we process.
5237 */
5238 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5239 btrfs_release_path(left_path);
5240 btrfs_release_path(right_path);
5241
5242 ret = btrfs_end_transaction(trans, left_root);
5243 trans = NULL;
5244 if (ret < 0)
5245 goto out;
5246 }
5247 /* now rejoin the transaction */
5248 if (!trans) {
5249 trans = btrfs_join_transaction(left_root);
5250 if (IS_ERR(trans)) {
5251 ret = PTR_ERR(trans);
5252 trans = NULL;
5253 goto out;
5254 }
5255
Anand Jain5f3ab902012-12-07 09:28:54 +00005256 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005257 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005258 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005259 if (ctransid != left_start_ctransid)
5260 left_start_ctransid = 0;
5261
Anand Jain5f3ab902012-12-07 09:28:54 +00005262 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005263 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005264 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005265 if (ctransid != right_start_ctransid)
5266 right_start_ctransid = 0;
5267
5268 if (!left_start_ctransid || !right_start_ctransid) {
5269 WARN(1, KERN_WARNING
5270 "btrfs: btrfs_compare_tree detected "
5271 "a change in one of the trees while "
5272 "iterating. This is probably a "
5273 "bug.\n");
5274 ret = -EIO;
5275 goto out;
5276 }
5277
5278 /*
5279 * the commit root may have changed, so start again
5280 * where we stopped
5281 */
5282 left_path->lowest_level = left_level;
5283 right_path->lowest_level = right_level;
5284 ret = btrfs_search_slot(NULL, left_root,
5285 &left_key, left_path, 0, 0);
5286 if (ret < 0)
5287 goto out;
5288 ret = btrfs_search_slot(NULL, right_root,
5289 &right_key, right_path, 0, 0);
5290 if (ret < 0)
5291 goto out;
5292 }
5293
5294 if (advance_left && !left_end_reached) {
5295 ret = tree_advance(left_root, left_path, &left_level,
5296 left_root_level,
5297 advance_left != ADVANCE_ONLY_NEXT,
5298 &left_key);
5299 if (ret < 0)
5300 left_end_reached = ADVANCE;
5301 advance_left = 0;
5302 }
5303 if (advance_right && !right_end_reached) {
5304 ret = tree_advance(right_root, right_path, &right_level,
5305 right_root_level,
5306 advance_right != ADVANCE_ONLY_NEXT,
5307 &right_key);
5308 if (ret < 0)
5309 right_end_reached = ADVANCE;
5310 advance_right = 0;
5311 }
5312
5313 if (left_end_reached && right_end_reached) {
5314 ret = 0;
5315 goto out;
5316 } else if (left_end_reached) {
5317 if (right_level == 0) {
5318 ret = changed_cb(left_root, right_root,
5319 left_path, right_path,
5320 &right_key,
5321 BTRFS_COMPARE_TREE_DELETED,
5322 ctx);
5323 if (ret < 0)
5324 goto out;
5325 }
5326 advance_right = ADVANCE;
5327 continue;
5328 } else if (right_end_reached) {
5329 if (left_level == 0) {
5330 ret = changed_cb(left_root, right_root,
5331 left_path, right_path,
5332 &left_key,
5333 BTRFS_COMPARE_TREE_NEW,
5334 ctx);
5335 if (ret < 0)
5336 goto out;
5337 }
5338 advance_left = ADVANCE;
5339 continue;
5340 }
5341
5342 if (left_level == 0 && right_level == 0) {
5343 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5344 if (cmp < 0) {
5345 ret = changed_cb(left_root, right_root,
5346 left_path, right_path,
5347 &left_key,
5348 BTRFS_COMPARE_TREE_NEW,
5349 ctx);
5350 if (ret < 0)
5351 goto out;
5352 advance_left = ADVANCE;
5353 } else if (cmp > 0) {
5354 ret = changed_cb(left_root, right_root,
5355 left_path, right_path,
5356 &right_key,
5357 BTRFS_COMPARE_TREE_DELETED,
5358 ctx);
5359 if (ret < 0)
5360 goto out;
5361 advance_right = ADVANCE;
5362 } else {
Chris Mason74dd17f2012-08-07 16:25:13 -04005363 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005364 ret = tree_compare_item(left_root, left_path,
5365 right_path, tmp_buf);
5366 if (ret) {
Chris Mason74dd17f2012-08-07 16:25:13 -04005367 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005368 ret = changed_cb(left_root, right_root,
5369 left_path, right_path,
5370 &left_key,
5371 BTRFS_COMPARE_TREE_CHANGED,
5372 ctx);
5373 if (ret < 0)
5374 goto out;
5375 }
5376 advance_left = ADVANCE;
5377 advance_right = ADVANCE;
5378 }
5379 } else if (left_level == right_level) {
5380 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5381 if (cmp < 0) {
5382 advance_left = ADVANCE;
5383 } else if (cmp > 0) {
5384 advance_right = ADVANCE;
5385 } else {
5386 left_blockptr = btrfs_node_blockptr(
5387 left_path->nodes[left_level],
5388 left_path->slots[left_level]);
5389 right_blockptr = btrfs_node_blockptr(
5390 right_path->nodes[right_level],
5391 right_path->slots[right_level]);
5392 if (left_blockptr == right_blockptr) {
5393 /*
5394 * As we're on a shared block, don't
5395 * allow to go deeper.
5396 */
5397 advance_left = ADVANCE_ONLY_NEXT;
5398 advance_right = ADVANCE_ONLY_NEXT;
5399 } else {
5400 advance_left = ADVANCE;
5401 advance_right = ADVANCE;
5402 }
5403 }
5404 } else if (left_level < right_level) {
5405 advance_right = ADVANCE;
5406 } else {
5407 advance_left = ADVANCE;
5408 }
5409 }
5410
5411out:
5412 btrfs_free_path(left_path);
5413 btrfs_free_path(right_path);
5414 kfree(tmp_buf);
5415
5416 if (trans) {
5417 if (!ret)
5418 ret = btrfs_end_transaction(trans, left_root);
5419 else
5420 btrfs_end_transaction(trans, left_root);
5421 }
5422
5423 return ret;
5424}
5425
Chris Mason3f157a22008-06-25 16:01:31 -04005426/*
5427 * this is similar to btrfs_next_leaf, but does not try to preserve
5428 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005429 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005430 *
5431 * 0 is returned if another key is found, < 0 if there are any errors
5432 * and 1 is returned if there are no higher keys in the tree
5433 *
5434 * path->keep_locks should be set to 1 on the search made before
5435 * calling this function.
5436 */
Chris Masone7a84562008-06-25 16:01:31 -04005437int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005438 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005439{
Chris Masone7a84562008-06-25 16:01:31 -04005440 int slot;
5441 struct extent_buffer *c;
5442
Chris Mason934d3752008-12-08 16:43:10 -05005443 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005444 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005445 if (!path->nodes[level])
5446 return 1;
5447
5448 slot = path->slots[level] + 1;
5449 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005450next:
Chris Masone7a84562008-06-25 16:01:31 -04005451 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005452 int ret;
5453 int orig_lowest;
5454 struct btrfs_key cur_key;
5455 if (level + 1 >= BTRFS_MAX_LEVEL ||
5456 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005457 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005458
5459 if (path->locks[level + 1]) {
5460 level++;
5461 continue;
5462 }
5463
5464 slot = btrfs_header_nritems(c) - 1;
5465 if (level == 0)
5466 btrfs_item_key_to_cpu(c, &cur_key, slot);
5467 else
5468 btrfs_node_key_to_cpu(c, &cur_key, slot);
5469
5470 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005471 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005472 path->lowest_level = level;
5473 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5474 0, 0);
5475 path->lowest_level = orig_lowest;
5476 if (ret < 0)
5477 return ret;
5478
5479 c = path->nodes[level];
5480 slot = path->slots[level];
5481 if (ret == 0)
5482 slot++;
5483 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005484 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005485
Chris Masone7a84562008-06-25 16:01:31 -04005486 if (level == 0)
5487 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005488 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005489 u64 gen = btrfs_node_ptr_generation(c, slot);
5490
Chris Mason3f157a22008-06-25 16:01:31 -04005491 if (gen < min_trans) {
5492 slot++;
5493 goto next;
5494 }
Chris Masone7a84562008-06-25 16:01:31 -04005495 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005496 }
Chris Masone7a84562008-06-25 16:01:31 -04005497 return 0;
5498 }
5499 return 1;
5500}
5501
Chris Mason7bb86312007-12-11 09:25:06 -05005502/*
Chris Mason925baed2008-06-25 16:01:30 -04005503 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005504 * returns 0 if it found something or 1 if there are no greater leaves.
5505 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005506 */
Chris Mason234b63a2007-03-13 10:46:10 -04005507int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005508{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005509 return btrfs_next_old_leaf(root, path, 0);
5510}
5511
5512int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5513 u64 time_seq)
5514{
Chris Masond97e63b2007-02-20 16:40:44 -05005515 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005516 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005517 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005518 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005519 struct btrfs_key key;
5520 u32 nritems;
5521 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005522 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005523 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005524
5525 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005526 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005527 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005528
Chris Mason8e73f272009-04-03 10:14:18 -04005529 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5530again:
5531 level = 1;
5532 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005533 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005534 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005535
Chris Masona2135012008-06-25 16:01:30 -04005536 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005537 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005538
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005539 if (time_seq)
5540 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5541 else
5542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005543 path->keep_locks = 0;
5544
5545 if (ret < 0)
5546 return ret;
5547
Chris Masona2135012008-06-25 16:01:30 -04005548 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005549 /*
5550 * by releasing the path above we dropped all our locks. A balance
5551 * could have added more items next to the key that used to be
5552 * at the very end of the block. So, check again here and
5553 * advance the path if there are now more items available.
5554 */
Chris Masona2135012008-06-25 16:01:30 -04005555 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005556 if (ret == 0)
5557 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005558 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005559 goto done;
5560 }
Chris Masond97e63b2007-02-20 16:40:44 -05005561
Chris Masond3977122009-01-05 21:25:51 -05005562 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005563 if (!path->nodes[level]) {
5564 ret = 1;
5565 goto done;
5566 }
Chris Mason5f39d392007-10-15 16:14:19 -04005567
Chris Masond97e63b2007-02-20 16:40:44 -05005568 slot = path->slots[level] + 1;
5569 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005570 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005571 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005572 if (level == BTRFS_MAX_LEVEL) {
5573 ret = 1;
5574 goto done;
5575 }
Chris Masond97e63b2007-02-20 16:40:44 -05005576 continue;
5577 }
Chris Mason5f39d392007-10-15 16:14:19 -04005578
Chris Mason925baed2008-06-25 16:01:30 -04005579 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005580 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005581 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005582 }
Chris Mason5f39d392007-10-15 16:14:19 -04005583
Chris Mason8e73f272009-04-03 10:14:18 -04005584 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005585 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005586 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005587 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005588 if (ret == -EAGAIN)
5589 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005590
Chris Mason76a05b32009-05-14 13:24:30 -04005591 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005592 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005593 goto done;
5594 }
5595
Chris Mason5cd57b22008-06-25 16:01:30 -04005596 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005597 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005598 if (!ret && time_seq) {
5599 /*
5600 * If we don't get the lock, we may be racing
5601 * with push_leaf_left, holding that lock while
5602 * itself waiting for the leaf we've currently
5603 * locked. To solve this situation, we give up
5604 * on our lock and cycle.
5605 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005606 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005607 btrfs_release_path(path);
5608 cond_resched();
5609 goto again;
5610 }
Chris Mason8e73f272009-04-03 10:14:18 -04005611 if (!ret) {
5612 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005613 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005614 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005615 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005616 }
Chris Mason31533fb2011-07-26 16:01:59 -04005617 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005618 }
Chris Masond97e63b2007-02-20 16:40:44 -05005619 break;
5620 }
5621 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005622 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005623 level--;
5624 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005625 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005626 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005627
Chris Mason5f39d392007-10-15 16:14:19 -04005628 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005629 path->nodes[level] = next;
5630 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005631 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005632 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005633 if (!level)
5634 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005635
Chris Mason8e73f272009-04-03 10:14:18 -04005636 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005637 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005638 if (ret == -EAGAIN)
5639 goto again;
5640
Chris Mason76a05b32009-05-14 13:24:30 -04005641 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005642 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005643 goto done;
5644 }
5645
Chris Mason5cd57b22008-06-25 16:01:30 -04005646 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005647 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005648 if (!ret) {
5649 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005650 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005651 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005652 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005653 }
Chris Mason31533fb2011-07-26 16:01:59 -04005654 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005655 }
Chris Masond97e63b2007-02-20 16:40:44 -05005656 }
Chris Mason8e73f272009-04-03 10:14:18 -04005657 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005658done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005659 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005660 path->leave_spinning = old_spinning;
5661 if (!old_spinning)
5662 btrfs_set_path_blocking(path);
5663
5664 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005665}
Chris Mason0b86a832008-03-24 15:01:56 -04005666
Chris Mason3f157a22008-06-25 16:01:31 -04005667/*
5668 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5669 * searching until it gets past min_objectid or finds an item of 'type'
5670 *
5671 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5672 */
Chris Mason0b86a832008-03-24 15:01:56 -04005673int btrfs_previous_item(struct btrfs_root *root,
5674 struct btrfs_path *path, u64 min_objectid,
5675 int type)
5676{
5677 struct btrfs_key found_key;
5678 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005679 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005680 int ret;
5681
Chris Masond3977122009-01-05 21:25:51 -05005682 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005683 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005684 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005685 ret = btrfs_prev_leaf(root, path);
5686 if (ret != 0)
5687 return ret;
5688 } else {
5689 path->slots[0]--;
5690 }
5691 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005692 nritems = btrfs_header_nritems(leaf);
5693 if (nritems == 0)
5694 return 1;
5695 if (path->slots[0] == nritems)
5696 path->slots[0]--;
5697
Chris Mason0b86a832008-03-24 15:01:56 -04005698 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005699 if (found_key.objectid < min_objectid)
5700 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005701 if (found_key.type == type)
5702 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005703 if (found_key.objectid == min_objectid &&
5704 found_key.type < type)
5705 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005706 }
5707 return 1;
5708}