blob: c85cde7612485c208f4e878da0e883f5c414aad8 [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) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400954 int level = btrfs_header_level(buf);
955
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 ret = btrfs_set_disk_extent_flags(trans, root,
957 buf->start,
958 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400959 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700960 if (ret)
961 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 }
963 } else {
964 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
965 if (root->root_key.objectid ==
966 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200967 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200969 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200971 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100972 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 }
974 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400975 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976 }
977 return 0;
978}
979
980/*
Chris Masond3977122009-01-05 21:25:51 -0500981 * does the dirty work in cow of a single block. The parent block (if
982 * supplied) is updated to point to the new cow copy. The new buffer is marked
983 * dirty and returned locked. If you modify the block it needs to be marked
984 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400985 *
986 * search_start -- an allocation hint for the new block
987 *
Chris Masond3977122009-01-05 21:25:51 -0500988 * empty_size -- a hint that you plan on doing more cow. This is the size in
989 * bytes the allocator should try to find free next to the block it returns.
990 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400991 */
Chris Masond3977122009-01-05 21:25:51 -0500992static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400993 struct btrfs_root *root,
994 struct extent_buffer *buf,
995 struct extent_buffer *parent, int parent_slot,
996 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400997 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400998{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001001 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001002 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001003 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001005
Chris Mason925baed2008-06-25 16:01:30 -04001006 if (*cow_ret == buf)
1007 unlock_orig = 1;
1008
Chris Masonb9447ef82009-03-09 11:45:38 -04001009 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001010
Chris Mason7bb86312007-12-11 09:25:06 -05001011 WARN_ON(root->ref_cows && trans->transid !=
1012 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001013 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001014
Chris Mason7bb86312007-12-11 09:25:06 -05001015 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 if (level == 0)
1018 btrfs_item_key(buf, &disk_key, 0);
1019 else
1020 btrfs_node_key(buf, &disk_key, 0);
1021
1022 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1023 if (parent)
1024 parent_start = parent->start;
1025 else
1026 parent_start = 0;
1027 } else
1028 parent_start = 0;
1029
1030 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1031 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001032 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001033 if (IS_ERR(cow))
1034 return PTR_ERR(cow);
1035
Chris Masonb4ce94d2009-02-04 09:25:08 -05001036 /* cow is set to blocking by btrfs_init_new_buffer */
1037
Chris Mason5f39d392007-10-15 16:14:19 -04001038 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001039 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001040 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001041 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1042 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1043 BTRFS_HEADER_FLAG_RELOC);
1044 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1045 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1046 else
1047 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001048
Yan Zheng2b820322008-11-17 21:11:30 -05001049 write_extent_buffer(cow, root->fs_info->fsid,
1050 (unsigned long)btrfs_header_fsid(cow),
1051 BTRFS_FSID_SIZE);
1052
Mark Fashehbe1a5562011-08-08 13:20:18 -07001053 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001054 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001055 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001056 return ret;
1057 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001058
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001059 if (root->ref_cows)
1060 btrfs_reloc_cow_block(trans, root, buf, cow);
1061
Chris Mason6702ed42007-08-07 16:15:09 -04001062 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001063 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1065 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1066 parent_start = buf->start;
1067 else
1068 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001069
Chris Mason5f39d392007-10-15 16:14:19 -04001070 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001071 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001072 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001073
Yan, Zhengf0486c62010-05-16 10:46:25 -04001074 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001075 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001076 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001077 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001078 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001079 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1080 parent_start = parent->start;
1081 else
1082 parent_start = 0;
1083
1084 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001085 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1086 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001087 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001088 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001089 btrfs_set_node_ptr_generation(parent, parent_slot,
1090 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001091 btrfs_mark_buffer_dirty(parent);
Jan Schmidtd9abbf12013-03-20 13:49:48 +00001092 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001093 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001094 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001095 }
Chris Mason925baed2008-06-25 16:01:30 -04001096 if (unlock_orig)
1097 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001098 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001099 btrfs_mark_buffer_dirty(cow);
1100 *cow_ret = cow;
1101 return 0;
1102}
1103
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001104/*
1105 * returns the logical address of the oldest predecessor of the given root.
1106 * entries older than time_seq are ignored.
1107 */
1108static struct tree_mod_elem *
1109__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001110 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001111{
1112 struct tree_mod_elem *tm;
1113 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001114 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001115 int looped = 0;
1116
1117 if (!time_seq)
1118 return 0;
1119
1120 /*
1121 * the very last operation that's logged for a root is the replacement
1122 * operation (if it is replaced at all). this has the index of the *new*
1123 * root, making it the very first operation that's logged for this root.
1124 */
1125 while (1) {
1126 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1127 time_seq);
1128 if (!looped && !tm)
1129 return 0;
1130 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001131 * if there are no tree operation for the oldest root, we simply
1132 * return it. this should only happen if that (old) root is at
1133 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001134 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001135 if (!tm)
1136 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001137
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001138 /*
1139 * if there's an operation that's not a root replacement, we
1140 * found the oldest version of our root. normally, we'll find a
1141 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1142 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001143 if (tm->op != MOD_LOG_ROOT_REPLACE)
1144 break;
1145
1146 found = tm;
1147 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001148 looped = 1;
1149 }
1150
Jan Schmidta95236d2012-06-05 16:41:24 +02001151 /* if there's no old root to return, return what we found instead */
1152 if (!found)
1153 found = tm;
1154
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 return found;
1156}
1157
1158/*
1159 * tm is a pointer to the first operation to rewind within eb. then, all
1160 * previous operations will be rewinded (until we reach something older than
1161 * time_seq).
1162 */
1163static void
1164__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1165 struct tree_mod_elem *first_tm)
1166{
1167 u32 n;
1168 struct rb_node *next;
1169 struct tree_mod_elem *tm = first_tm;
1170 unsigned long o_dst;
1171 unsigned long o_src;
1172 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1173
1174 n = btrfs_header_nritems(eb);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001175 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001176 /*
1177 * all the operations are recorded with the operator used for
1178 * the modification. as we're going backwards, we do the
1179 * opposite of each operation here.
1180 */
1181 switch (tm->op) {
1182 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1183 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001184 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001185 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001186 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001187 btrfs_set_node_key(eb, &tm->key, tm->slot);
1188 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1189 btrfs_set_node_ptr_generation(eb, tm->slot,
1190 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001191 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001192 break;
1193 case MOD_LOG_KEY_REPLACE:
1194 BUG_ON(tm->slot >= n);
1195 btrfs_set_node_key(eb, &tm->key, tm->slot);
1196 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1197 btrfs_set_node_ptr_generation(eb, tm->slot,
1198 tm->generation);
1199 break;
1200 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001201 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001202 n--;
1203 break;
1204 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001205 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1206 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1207 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001208 tm->move.nr_items * p_size);
1209 break;
1210 case MOD_LOG_ROOT_REPLACE:
1211 /*
1212 * this operation is special. for roots, this must be
1213 * handled explicitly before rewinding.
1214 * for non-roots, this operation may exist if the node
1215 * was a root: root A -> child B; then A gets empty and
1216 * B is promoted to the new root. in the mod log, we'll
1217 * have a root-replace operation for B, a tree block
1218 * that is no root. we simply ignore that operation.
1219 */
1220 break;
1221 }
1222 next = rb_next(&tm->node);
1223 if (!next)
1224 break;
1225 tm = container_of(next, struct tree_mod_elem, node);
1226 if (tm->index != first_tm->index)
1227 break;
1228 }
1229 btrfs_set_header_nritems(eb, n);
1230}
1231
Jan Schmidt47fb0912013-04-13 13:19:55 +00001232/*
1233 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1234 * is returned. If rewind operations happen, a fresh buffer is returned. The
1235 * returned buffer is always read-locked. If the returned buffer is not the
1236 * input buffer, the lock on the input buffer is released and the input buffer
1237 * is freed (its refcount is decremented).
1238 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239static struct extent_buffer *
1240tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1241 u64 time_seq)
1242{
1243 struct extent_buffer *eb_rewin;
1244 struct tree_mod_elem *tm;
1245
1246 if (!time_seq)
1247 return eb;
1248
1249 if (btrfs_header_level(eb) == 0)
1250 return eb;
1251
1252 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1253 if (!tm)
1254 return eb;
1255
1256 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1257 BUG_ON(tm->slot != 0);
1258 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1259 fs_info->tree_root->nodesize);
1260 BUG_ON(!eb_rewin);
1261 btrfs_set_header_bytenr(eb_rewin, eb->start);
1262 btrfs_set_header_backref_rev(eb_rewin,
1263 btrfs_header_backref_rev(eb));
1264 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001265 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001266 } else {
1267 eb_rewin = btrfs_clone_extent_buffer(eb);
1268 BUG_ON(!eb_rewin);
1269 }
1270
1271 extent_buffer_get(eb_rewin);
Jan Schmidt47fb0912013-04-13 13:19:55 +00001272 btrfs_tree_read_unlock(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001273 free_extent_buffer(eb);
1274
Jan Schmidt47fb0912013-04-13 13:19:55 +00001275 extent_buffer_get(eb_rewin);
1276 btrfs_tree_read_lock(eb_rewin);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001277 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001278 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001279 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280
1281 return eb_rewin;
1282}
1283
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001284/*
1285 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1286 * value. If there are no changes, the current root->root_node is returned. If
1287 * anything changed in between, there's a fresh buffer allocated on which the
1288 * rewind operations are done. In any case, the returned buffer is read locked.
1289 * Returns NULL on error (with no locks held).
1290 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001291static inline struct extent_buffer *
1292get_old_root(struct btrfs_root *root, u64 time_seq)
1293{
1294 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001295 struct extent_buffer *eb = NULL;
1296 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001297 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001298 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001299 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001300 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001301 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001302
Jan Schmidt30b04632013-04-13 13:19:54 +00001303 eb_root = btrfs_read_lock_root_node(root);
1304 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001305 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001306 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307
Jan Schmidta95236d2012-06-05 16:41:24 +02001308 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1309 old_root = &tm->old_root;
1310 old_generation = tm->generation;
1311 logical = old_root->logical;
1312 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001313 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001314 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001315
Jan Schmidta95236d2012-06-05 16:41:24 +02001316 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001317 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001318 btrfs_tree_read_unlock(eb_root);
1319 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001320 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001321 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001322 if (!old || !extent_buffer_uptodate(old)) {
1323 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001324 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1325 logical);
1326 WARN_ON(1);
1327 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001328 eb = btrfs_clone_extent_buffer(old);
1329 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001330 }
1331 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001332 btrfs_tree_read_unlock(eb_root);
1333 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001334 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001335 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001336 eb = btrfs_clone_extent_buffer(eb_root);
1337 btrfs_tree_read_unlock(eb_root);
1338 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001339 }
1340
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001341 if (!eb)
1342 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001343 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001344 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001345 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001346 btrfs_set_header_bytenr(eb, eb->start);
1347 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001348 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001349 btrfs_set_header_level(eb, old_root->level);
1350 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001351 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001352 if (tm)
1353 __tree_mod_log_rewind(eb, time_seq, tm);
1354 else
1355 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001356 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001357
1358 return eb;
1359}
1360
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001361int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1362{
1363 struct tree_mod_elem *tm;
1364 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001365 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001366
Jan Schmidt30b04632013-04-13 13:19:54 +00001367 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001368 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1369 level = tm->old_root.level;
1370 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001371 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001372 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001373 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001374
1375 return level;
1376}
1377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378static inline int should_cow_block(struct btrfs_trans_handle *trans,
1379 struct btrfs_root *root,
1380 struct extent_buffer *buf)
1381{
Liu Bof1ebcc72011-11-14 20:48:06 -05001382 /* ensure we can see the force_cow */
1383 smp_rmb();
1384
1385 /*
1386 * We do not need to cow a block if
1387 * 1) this block is not created or changed in this transaction;
1388 * 2) this block does not belong to TREE_RELOC tree;
1389 * 3) the root is not forced COW.
1390 *
1391 * What is forced COW:
1392 * when we create snapshot during commiting the transaction,
1393 * after we've finished coping src root, we must COW the shared
1394 * block to ensure the metadata consistency.
1395 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 if (btrfs_header_generation(buf) == trans->transid &&
1397 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1398 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001399 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1400 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401 return 0;
1402 return 1;
1403}
1404
Chris Masond352ac62008-09-29 15:18:18 -04001405/*
1406 * cows a single block, see __btrfs_cow_block for the real work.
1407 * This version of it has extra checks so that a block isn't cow'd more than
1408 * once per transaction, as long as it hasn't been written yet
1409 */
Chris Masond3977122009-01-05 21:25:51 -05001410noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001411 struct btrfs_root *root, struct extent_buffer *buf,
1412 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001413 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001414{
Chris Mason6702ed42007-08-07 16:15:09 -04001415 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001416 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001417
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001418 if (trans->transaction != root->fs_info->running_transaction)
1419 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001420 (unsigned long long)trans->transid,
1421 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001422 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001423
1424 if (trans->transid != root->fs_info->generation)
1425 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001426 (unsigned long long)trans->transid,
1427 (unsigned long long)root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001430 *cow_ret = buf;
1431 return 0;
1432 }
Chris Masonc4876852009-02-04 09:24:25 -05001433
Chris Mason0b86a832008-03-24 15:01:56 -04001434 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435
1436 if (parent)
1437 btrfs_set_lock_blocking(parent);
1438 btrfs_set_lock_blocking(buf);
1439
Chris Masonf510cfe2007-10-15 16:14:48 -04001440 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001441 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001442
1443 trace_btrfs_cow_block(root, buf, *cow_ret);
1444
Chris Masonf510cfe2007-10-15 16:14:48 -04001445 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001446}
1447
Chris Masond352ac62008-09-29 15:18:18 -04001448/*
1449 * helper function for defrag to decide if two blocks pointed to by a
1450 * node are actually close by
1451 */
Chris Mason6b800532007-10-15 16:17:34 -04001452static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001453{
Chris Mason6b800532007-10-15 16:17:34 -04001454 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001455 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001456 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001457 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001458 return 0;
1459}
1460
Chris Mason081e9572007-11-06 10:26:24 -05001461/*
1462 * compare two keys in a memcmp fashion
1463 */
1464static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1465{
1466 struct btrfs_key k1;
1467
1468 btrfs_disk_key_to_cpu(&k1, disk);
1469
Diego Calleja20736ab2009-07-24 11:06:52 -04001470 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001471}
1472
Josef Bacikf3465ca2008-11-12 14:19:50 -05001473/*
1474 * same as comp_keys only with two btrfs_key's
1475 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001476int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001477{
1478 if (k1->objectid > k2->objectid)
1479 return 1;
1480 if (k1->objectid < k2->objectid)
1481 return -1;
1482 if (k1->type > k2->type)
1483 return 1;
1484 if (k1->type < k2->type)
1485 return -1;
1486 if (k1->offset > k2->offset)
1487 return 1;
1488 if (k1->offset < k2->offset)
1489 return -1;
1490 return 0;
1491}
Chris Mason081e9572007-11-06 10:26:24 -05001492
Chris Masond352ac62008-09-29 15:18:18 -04001493/*
1494 * this is used by the defrag code to go through all the
1495 * leaves pointed to by a node and reallocate them so that
1496 * disk order is close to key order
1497 */
Chris Mason6702ed42007-08-07 16:15:09 -04001498int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001499 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001500 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001501 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001502{
Chris Mason6b800532007-10-15 16:17:34 -04001503 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001504 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001505 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001506 u64 search_start = *last_ret;
1507 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001508 u64 other;
1509 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001510 int end_slot;
1511 int i;
1512 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001513 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001514 int uptodate;
1515 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001516 int progress_passed = 0;
1517 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001518
Chris Mason5708b952007-10-25 15:43:18 -04001519 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001520
Julia Lawall6c1500f2012-11-03 20:30:18 +00001521 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1522 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001523
Chris Mason6b800532007-10-15 16:17:34 -04001524 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001525 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001526 end_slot = parent_nritems;
1527
1528 if (parent_nritems == 1)
1529 return 0;
1530
Chris Masonb4ce94d2009-02-04 09:25:08 -05001531 btrfs_set_lock_blocking(parent);
1532
Chris Mason6702ed42007-08-07 16:15:09 -04001533 for (i = start_slot; i < end_slot; i++) {
1534 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001535
Chris Mason081e9572007-11-06 10:26:24 -05001536 btrfs_node_key(parent, &disk_key, i);
1537 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1538 continue;
1539
1540 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001541 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001542 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001543 if (last_block == 0)
1544 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001545
Chris Mason6702ed42007-08-07 16:15:09 -04001546 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001547 other = btrfs_node_blockptr(parent, i - 1);
1548 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001549 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001550 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001551 other = btrfs_node_blockptr(parent, i + 1);
1552 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001553 }
Chris Masone9d0b132007-08-10 14:06:19 -04001554 if (close) {
1555 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001556 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001557 }
Chris Mason6702ed42007-08-07 16:15:09 -04001558
Chris Mason6b800532007-10-15 16:17:34 -04001559 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1560 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001561 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001562 else
1563 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001564 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001565 if (!cur) {
1566 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001567 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001568 if (!cur || !extent_buffer_uptodate(cur)) {
1569 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001570 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001571 }
Chris Mason6b800532007-10-15 16:17:34 -04001572 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001573 err = btrfs_read_buffer(cur, gen);
1574 if (err) {
1575 free_extent_buffer(cur);
1576 return err;
1577 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001578 }
Chris Mason6702ed42007-08-07 16:15:09 -04001579 }
Chris Masone9d0b132007-08-10 14:06:19 -04001580 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001581 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001582
Chris Masone7a84562008-06-25 16:01:31 -04001583 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001584 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001585 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001586 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001587 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001588 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001589 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001590 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001591 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001592 break;
Yan252c38f2007-08-29 09:11:44 -04001593 }
Chris Masone7a84562008-06-25 16:01:31 -04001594 search_start = cur->start;
1595 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001596 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001597 btrfs_tree_unlock(cur);
1598 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001599 }
1600 return err;
1601}
1602
Chris Mason74123bd2007-02-02 11:05:29 -05001603/*
1604 * The leaf data grows from end-to-front in the node.
1605 * this returns the address of the start of the last item,
1606 * which is the stop of the leaf data stack
1607 */
Chris Mason123abc82007-03-14 14:14:43 -04001608static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001609 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001610{
Chris Mason5f39d392007-10-15 16:14:19 -04001611 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001613 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001614 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001615}
1616
Chris Masonaa5d6be2007-02-28 16:35:06 -05001617
Chris Mason74123bd2007-02-02 11:05:29 -05001618/*
Chris Mason5f39d392007-10-15 16:14:19 -04001619 * search for key in the extent_buffer. The items start at offset p,
1620 * and they are item_size apart. There are 'max' items in p.
1621 *
Chris Mason74123bd2007-02-02 11:05:29 -05001622 * the slot in the array is returned via slot, and it points to
1623 * the place where you would insert key if it is not found in
1624 * the array.
1625 *
1626 * slot may point to max if the key is bigger than all of the keys
1627 */
Chris Masone02119d2008-09-05 16:13:11 -04001628static noinline int generic_bin_search(struct extent_buffer *eb,
1629 unsigned long p,
1630 int item_size, struct btrfs_key *key,
1631 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001632{
1633 int low = 0;
1634 int high = max;
1635 int mid;
1636 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001637 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001638 struct btrfs_disk_key unaligned;
1639 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001640 char *kaddr = NULL;
1641 unsigned long map_start = 0;
1642 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001643 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001644
Chris Masond3977122009-01-05 21:25:51 -05001645 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001646 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001647 offset = p + mid * item_size;
1648
Chris Masona6591712011-07-19 12:04:14 -04001649 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001650 (offset + sizeof(struct btrfs_disk_key)) >
1651 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001652
1653 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001654 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001655 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001656
Chris Mason479965d2007-10-15 16:14:27 -04001657 if (!err) {
1658 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1659 map_start);
1660 } else {
1661 read_extent_buffer(eb, &unaligned,
1662 offset, sizeof(unaligned));
1663 tmp = &unaligned;
1664 }
1665
Chris Mason5f39d392007-10-15 16:14:19 -04001666 } else {
1667 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1668 map_start);
1669 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001670 ret = comp_keys(tmp, key);
1671
1672 if (ret < 0)
1673 low = mid + 1;
1674 else if (ret > 0)
1675 high = mid;
1676 else {
1677 *slot = mid;
1678 return 0;
1679 }
1680 }
1681 *slot = low;
1682 return 1;
1683}
1684
Chris Mason97571fd2007-02-24 13:39:08 -05001685/*
1686 * simple bin_search frontend that does the right thing for
1687 * leaves vs nodes
1688 */
Chris Mason5f39d392007-10-15 16:14:19 -04001689static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1690 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001691{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001692 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001693 return generic_bin_search(eb,
1694 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001695 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001696 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001697 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001698 else
Chris Mason5f39d392007-10-15 16:14:19 -04001699 return generic_bin_search(eb,
1700 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001701 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001702 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001703 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001704}
1705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1707 int level, int *slot)
1708{
1709 return bin_search(eb, key, level, slot);
1710}
1711
Yan, Zhengf0486c62010-05-16 10:46:25 -04001712static void root_add_used(struct btrfs_root *root, u32 size)
1713{
1714 spin_lock(&root->accounting_lock);
1715 btrfs_set_root_used(&root->root_item,
1716 btrfs_root_used(&root->root_item) + size);
1717 spin_unlock(&root->accounting_lock);
1718}
1719
1720static void root_sub_used(struct btrfs_root *root, u32 size)
1721{
1722 spin_lock(&root->accounting_lock);
1723 btrfs_set_root_used(&root->root_item,
1724 btrfs_root_used(&root->root_item) - size);
1725 spin_unlock(&root->accounting_lock);
1726}
1727
Chris Masond352ac62008-09-29 15:18:18 -04001728/* given a node and slot number, this reads the blocks it points to. The
1729 * extent buffer is returned with a reference taken (but unlocked).
1730 * NULL is returned on error.
1731 */
Chris Masone02119d2008-09-05 16:13:11 -04001732static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001733 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001734{
Chris Masonca7a79a2008-05-12 12:59:19 -04001735 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001736 struct extent_buffer *eb;
1737
Chris Masonbb803952007-03-01 12:04:21 -05001738 if (slot < 0)
1739 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001740 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001741 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001742
1743 BUG_ON(level == 0);
1744
Josef Bacik416bc652013-04-23 14:17:42 -04001745 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1746 btrfs_level_size(root, level - 1),
1747 btrfs_node_ptr_generation(parent, slot));
1748 if (eb && !extent_buffer_uptodate(eb)) {
1749 free_extent_buffer(eb);
1750 eb = NULL;
1751 }
1752
1753 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001754}
1755
Chris Masond352ac62008-09-29 15:18:18 -04001756/*
1757 * node level balancing, used to make sure nodes are in proper order for
1758 * item deletion. We balance from the top down, so we have to make sure
1759 * that a deletion won't leave an node completely empty later on.
1760 */
Chris Masone02119d2008-09-05 16:13:11 -04001761static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001762 struct btrfs_root *root,
1763 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001764{
Chris Mason5f39d392007-10-15 16:14:19 -04001765 struct extent_buffer *right = NULL;
1766 struct extent_buffer *mid;
1767 struct extent_buffer *left = NULL;
1768 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001769 int ret = 0;
1770 int wret;
1771 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001772 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001773 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001774
1775 if (level == 0)
1776 return 0;
1777
Chris Mason5f39d392007-10-15 16:14:19 -04001778 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001779
Chris Masonbd681512011-07-16 15:23:14 -04001780 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1781 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001782 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1783
Chris Mason1d4f8a02007-03-13 09:28:32 -04001784 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001785
Li Zefana05a9bb2011-09-06 16:55:34 +08001786 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001787 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001788 pslot = path->slots[level + 1];
1789 }
Chris Masonbb803952007-03-01 12:04:21 -05001790
Chris Mason40689472007-03-17 14:29:23 -04001791 /*
1792 * deal with the case where there is only one pointer in the root
1793 * by promoting the node below to a root
1794 */
Chris Mason5f39d392007-10-15 16:14:19 -04001795 if (!parent) {
1796 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001797
Chris Mason5f39d392007-10-15 16:14:19 -04001798 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001799 return 0;
1800
1801 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001802 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001803 if (!child) {
1804 ret = -EROFS;
1805 btrfs_std_error(root->fs_info, ret);
1806 goto enospc;
1807 }
1808
Chris Mason925baed2008-06-25 16:01:30 -04001809 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001810 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001811 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001812 if (ret) {
1813 btrfs_tree_unlock(child);
1814 free_extent_buffer(child);
1815 goto enospc;
1816 }
Yan2f375ab2008-02-01 14:58:07 -05001817
Jan Schmidt90f8d622013-04-13 13:19:53 +00001818 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001819 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001820
Chris Mason0b86a832008-03-24 15:01:56 -04001821 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001822 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001823
Chris Mason925baed2008-06-25 16:01:30 -04001824 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001825 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001826 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001827 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001828 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001829 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001830
1831 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001832 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001833 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001834 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001835 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001836 }
Chris Mason5f39d392007-10-15 16:14:19 -04001837 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001838 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001839 return 0;
1840
Chris Mason5f39d392007-10-15 16:14:19 -04001841 left = read_node_slot(root, parent, pslot - 1);
1842 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001843 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001844 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001845 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001846 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001847 if (wret) {
1848 ret = wret;
1849 goto enospc;
1850 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001851 }
Chris Mason5f39d392007-10-15 16:14:19 -04001852 right = read_node_slot(root, parent, pslot + 1);
1853 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001854 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001855 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001856 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001857 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001858 if (wret) {
1859 ret = wret;
1860 goto enospc;
1861 }
1862 }
1863
1864 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001865 if (left) {
1866 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001867 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001868 if (wret < 0)
1869 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001870 }
Chris Mason79f95c82007-03-01 15:16:26 -05001871
1872 /*
1873 * then try to empty the right most buffer into the middle
1874 */
Chris Mason5f39d392007-10-15 16:14:19 -04001875 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001876 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001877 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001878 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001879 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001880 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001881 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001882 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001883 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001884 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001885 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001886 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001887 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001888 struct btrfs_disk_key right_key;
1889 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001890 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001891 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001892 btrfs_set_node_key(parent, &right_key, pslot + 1);
1893 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001894 }
1895 }
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001897 /*
1898 * we're not allowed to leave a node with one item in the
1899 * tree during a delete. A deletion from lower in the tree
1900 * could try to delete the only pointer in this node.
1901 * So, pull some keys from the left.
1902 * There has to be a left pointer at this point because
1903 * otherwise we would have pulled some pointers from the
1904 * right
1905 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001906 if (!left) {
1907 ret = -EROFS;
1908 btrfs_std_error(root->fs_info, ret);
1909 goto enospc;
1910 }
Chris Mason5f39d392007-10-15 16:14:19 -04001911 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001912 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001913 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001914 goto enospc;
1915 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001916 if (wret == 1) {
1917 wret = push_node_left(trans, root, left, mid, 1);
1918 if (wret < 0)
1919 ret = wret;
1920 }
Chris Mason79f95c82007-03-01 15:16:26 -05001921 BUG_ON(wret == 1);
1922 }
Chris Mason5f39d392007-10-15 16:14:19 -04001923 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001924 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001925 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001926 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001927 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001928 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001929 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001930 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001931 } else {
1932 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001933 struct btrfs_disk_key mid_key;
1934 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001935 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001936 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001937 btrfs_set_node_key(parent, &mid_key, pslot);
1938 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001939 }
Chris Masonbb803952007-03-01 12:04:21 -05001940
Chris Mason79f95c82007-03-01 15:16:26 -05001941 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001942 if (left) {
1943 if (btrfs_header_nritems(left) > orig_slot) {
1944 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001945 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001946 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001947 path->slots[level + 1] -= 1;
1948 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001949 if (mid) {
1950 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001951 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001952 }
Chris Masonbb803952007-03-01 12:04:21 -05001953 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001954 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001955 path->slots[level] = orig_slot;
1956 }
1957 }
Chris Mason79f95c82007-03-01 15:16:26 -05001958 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001959 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001960 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001961 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001962enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001963 if (right) {
1964 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001965 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001966 }
1967 if (left) {
1968 if (path->nodes[level] != left)
1969 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001971 }
Chris Masonbb803952007-03-01 12:04:21 -05001972 return ret;
1973}
1974
Chris Masond352ac62008-09-29 15:18:18 -04001975/* Node balancing for insertion. Here we only split or push nodes around
1976 * when they are completely full. This is also done top down, so we
1977 * have to be pessimistic.
1978 */
Chris Masond3977122009-01-05 21:25:51 -05001979static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001980 struct btrfs_root *root,
1981 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001982{
Chris Mason5f39d392007-10-15 16:14:19 -04001983 struct extent_buffer *right = NULL;
1984 struct extent_buffer *mid;
1985 struct extent_buffer *left = NULL;
1986 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001987 int ret = 0;
1988 int wret;
1989 int pslot;
1990 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001991
1992 if (level == 0)
1993 return 1;
1994
Chris Mason5f39d392007-10-15 16:14:19 -04001995 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001996 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001997
Li Zefana05a9bb2011-09-06 16:55:34 +08001998 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001999 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002000 pslot = path->slots[level + 1];
2001 }
Chris Masone66f7092007-04-20 13:16:02 -04002002
Chris Mason5f39d392007-10-15 16:14:19 -04002003 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002004 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002005
Chris Mason5f39d392007-10-15 16:14:19 -04002006 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002007
2008 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002009 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002010 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002011
2012 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002013 btrfs_set_lock_blocking(left);
2014
Chris Mason5f39d392007-10-15 16:14:19 -04002015 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002016 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2017 wret = 1;
2018 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002019 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002020 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002021 if (ret)
2022 wret = 1;
2023 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002024 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002025 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002026 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002027 }
Chris Masone66f7092007-04-20 13:16:02 -04002028 if (wret < 0)
2029 ret = wret;
2030 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002031 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002032 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002034 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002035 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002036 btrfs_set_node_key(parent, &disk_key, pslot);
2037 btrfs_mark_buffer_dirty(parent);
2038 if (btrfs_header_nritems(left) > orig_slot) {
2039 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002040 path->slots[level + 1] -= 1;
2041 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002042 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002043 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002044 } else {
2045 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002047 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002048 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002049 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002050 }
Chris Masone66f7092007-04-20 13:16:02 -04002051 return 0;
2052 }
Chris Mason925baed2008-06-25 16:01:30 -04002053 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002054 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002055 }
Chris Mason925baed2008-06-25 16:01:30 -04002056 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002057
2058 /*
2059 * then try to empty the right most buffer into the middle
2060 */
Chris Mason5f39d392007-10-15 16:14:19 -04002061 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002062 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002063
Chris Mason925baed2008-06-25 16:01:30 -04002064 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002065 btrfs_set_lock_blocking(right);
2066
Chris Mason5f39d392007-10-15 16:14:19 -04002067 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002068 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2069 wret = 1;
2070 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002071 ret = btrfs_cow_block(trans, root, right,
2072 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002073 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002074 if (ret)
2075 wret = 1;
2076 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002077 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002078 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002079 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002080 }
Chris Masone66f7092007-04-20 13:16:02 -04002081 if (wret < 0)
2082 ret = wret;
2083 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002084 struct btrfs_disk_key disk_key;
2085
2086 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002087 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002088 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2090 btrfs_mark_buffer_dirty(parent);
2091
2092 if (btrfs_header_nritems(mid) <= orig_slot) {
2093 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002094 path->slots[level + 1] += 1;
2095 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002096 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002097 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002098 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002099 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002100 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002101 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002102 }
Chris Masone66f7092007-04-20 13:16:02 -04002103 return 0;
2104 }
Chris Mason925baed2008-06-25 16:01:30 -04002105 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002107 }
Chris Masone66f7092007-04-20 13:16:02 -04002108 return 1;
2109}
2110
Chris Mason74123bd2007-02-02 11:05:29 -05002111/*
Chris Masond352ac62008-09-29 15:18:18 -04002112 * readahead one full node of leaves, finding things that are close
2113 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002114 */
Chris Masonc8c42862009-04-03 10:14:18 -04002115static void reada_for_search(struct btrfs_root *root,
2116 struct btrfs_path *path,
2117 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002118{
Chris Mason5f39d392007-10-15 16:14:19 -04002119 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002120 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002121 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002122 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002123 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002124 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002125 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002126 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002127 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002128 u32 nr;
2129 u32 blocksize;
2130 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002131
Chris Masona6b6e752007-10-15 16:22:39 -04002132 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002133 return;
2134
Chris Mason6702ed42007-08-07 16:15:09 -04002135 if (!path->nodes[level])
2136 return;
2137
Chris Mason5f39d392007-10-15 16:14:19 -04002138 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002139
Chris Mason3c69fae2007-08-07 15:52:22 -04002140 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002141 blocksize = btrfs_level_size(root, level - 1);
2142 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 if (eb) {
2144 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002145 return;
2146 }
2147
Chris Masona7175312009-01-22 09:23:10 -05002148 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002149
Chris Mason5f39d392007-10-15 16:14:19 -04002150 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002151 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002152
Chris Masond3977122009-01-05 21:25:51 -05002153 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002154 if (direction < 0) {
2155 if (nr == 0)
2156 break;
2157 nr--;
2158 } else if (direction > 0) {
2159 nr++;
2160 if (nr >= nritems)
2161 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002162 }
Chris Mason01f46652007-12-21 16:24:26 -05002163 if (path->reada < 0 && objectid) {
2164 btrfs_node_key(node, &disk_key, nr);
2165 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2166 break;
2167 }
Chris Mason6b800532007-10-15 16:17:34 -04002168 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002169 if ((search <= target && target - search <= 65536) ||
2170 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002171 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002172 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002173 nread += blocksize;
2174 }
2175 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002176 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002177 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002178 }
2179}
Chris Mason925baed2008-06-25 16:01:30 -04002180
Chris Masond352ac62008-09-29 15:18:18 -04002181/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002182 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2183 * cache
2184 */
2185static noinline int reada_for_balance(struct btrfs_root *root,
2186 struct btrfs_path *path, int level)
2187{
2188 int slot;
2189 int nritems;
2190 struct extent_buffer *parent;
2191 struct extent_buffer *eb;
2192 u64 gen;
2193 u64 block1 = 0;
2194 u64 block2 = 0;
2195 int ret = 0;
2196 int blocksize;
2197
Chris Mason8c594ea2009-04-20 15:50:10 -04002198 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002199 if (!parent)
2200 return 0;
2201
2202 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002203 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002204 blocksize = btrfs_level_size(root, level);
2205
2206 if (slot > 0) {
2207 block1 = btrfs_node_blockptr(parent, slot - 1);
2208 gen = btrfs_node_ptr_generation(parent, slot - 1);
2209 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002210 /*
2211 * if we get -eagain from btrfs_buffer_uptodate, we
2212 * don't want to return eagain here. That will loop
2213 * forever
2214 */
2215 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002216 block1 = 0;
2217 free_extent_buffer(eb);
2218 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002219 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002220 block2 = btrfs_node_blockptr(parent, slot + 1);
2221 gen = btrfs_node_ptr_generation(parent, slot + 1);
2222 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002223 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002224 block2 = 0;
2225 free_extent_buffer(eb);
2226 }
2227 if (block1 || block2) {
2228 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002229
2230 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002231 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002232
2233 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002234 if (block1)
2235 readahead_tree_block(root, block1, blocksize, 0);
2236 if (block2)
2237 readahead_tree_block(root, block2, blocksize, 0);
2238
2239 if (block1) {
2240 eb = read_tree_block(root, block1, blocksize, 0);
2241 free_extent_buffer(eb);
2242 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002243 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002244 eb = read_tree_block(root, block2, blocksize, 0);
2245 free_extent_buffer(eb);
2246 }
2247 }
2248 return ret;
2249}
2250
2251
2252/*
Chris Masond3977122009-01-05 21:25:51 -05002253 * when we walk down the tree, it is usually safe to unlock the higher layers
2254 * in the tree. The exceptions are when our path goes through slot 0, because
2255 * operations on the tree might require changing key pointers higher up in the
2256 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002257 *
Chris Masond3977122009-01-05 21:25:51 -05002258 * callers might also have set path->keep_locks, which tells this code to keep
2259 * the lock if the path points to the last slot in the block. This is part of
2260 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002261 *
Chris Masond3977122009-01-05 21:25:51 -05002262 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2263 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002264 */
Chris Masone02119d2008-09-05 16:13:11 -04002265static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002266 int lowest_unlock, int min_write_lock_level,
2267 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002268{
2269 int i;
2270 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002271 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002272 struct extent_buffer *t;
2273
2274 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2275 if (!path->nodes[i])
2276 break;
2277 if (!path->locks[i])
2278 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002279 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002280 skip_level = i + 1;
2281 continue;
2282 }
Chris Mason051e1b92008-06-25 16:01:30 -04002283 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002284 u32 nritems;
2285 t = path->nodes[i];
2286 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002287 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002288 skip_level = i + 1;
2289 continue;
2290 }
2291 }
Chris Mason051e1b92008-06-25 16:01:30 -04002292 if (skip_level < i && i >= lowest_unlock)
2293 no_skips = 1;
2294
Chris Mason925baed2008-06-25 16:01:30 -04002295 t = path->nodes[i];
2296 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002297 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002298 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002299 if (write_lock_level &&
2300 i > min_write_lock_level &&
2301 i <= *write_lock_level) {
2302 *write_lock_level = i - 1;
2303 }
Chris Mason925baed2008-06-25 16:01:30 -04002304 }
2305 }
2306}
2307
Chris Mason3c69fae2007-08-07 15:52:22 -04002308/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002309 * This releases any locks held in the path starting at level and
2310 * going all the way up to the root.
2311 *
2312 * btrfs_search_slot will keep the lock held on higher nodes in a few
2313 * corner cases, such as COW of the block at slot zero in the node. This
2314 * ignores those rules, and it should only be called when there are no
2315 * more updates to be done higher up in the tree.
2316 */
2317noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2318{
2319 int i;
2320
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002321 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002322 return;
2323
2324 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2325 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002326 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002327 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002328 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002329 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002330 path->locks[i] = 0;
2331 }
2332}
2333
2334/*
Chris Masonc8c42862009-04-03 10:14:18 -04002335 * helper function for btrfs_search_slot. The goal is to find a block
2336 * in cache without setting the path to blocking. If we find the block
2337 * we return zero and the path is unchanged.
2338 *
2339 * If we can't find the block, we set the path blocking and do some
2340 * reada. -EAGAIN is returned and the search must be repeated.
2341 */
2342static int
2343read_block_for_search(struct btrfs_trans_handle *trans,
2344 struct btrfs_root *root, struct btrfs_path *p,
2345 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002346 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002347{
2348 u64 blocknr;
2349 u64 gen;
2350 u32 blocksize;
2351 struct extent_buffer *b = *eb_ret;
2352 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002353 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002354
2355 blocknr = btrfs_node_blockptr(b, slot);
2356 gen = btrfs_node_ptr_generation(b, slot);
2357 blocksize = btrfs_level_size(root, level - 1);
2358
2359 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002360 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002361 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002362 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2363 *eb_ret = tmp;
2364 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002365 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002366
2367 /* the pages were up to date, but we failed
2368 * the generation number check. Do a full
2369 * read for the generation number that is correct.
2370 * We must do this without dropping locks so
2371 * we can trust our generation number
2372 */
2373 btrfs_set_path_blocking(p);
2374
2375 /* now we're allowed to do a blocking uptodate check */
2376 ret = btrfs_read_buffer(tmp, gen);
2377 if (!ret) {
2378 *eb_ret = tmp;
2379 return 0;
2380 }
2381 free_extent_buffer(tmp);
2382 btrfs_release_path(p);
2383 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002384 }
2385
2386 /*
2387 * reduce lock contention at high levels
2388 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002389 * we read. Don't release the lock on the current
2390 * level because we need to walk this node to figure
2391 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002392 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002393 btrfs_unlock_up_safe(p, level + 1);
2394 btrfs_set_path_blocking(p);
2395
Chris Masoncb449212010-10-24 11:01:27 -04002396 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002397 if (p->reada)
2398 reada_for_search(root, p, level, slot, key->objectid);
2399
David Sterbab3b4aa72011-04-21 01:20:15 +02002400 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002401
2402 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002403 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002404 if (tmp) {
2405 /*
2406 * If the read above didn't mark this buffer up to date,
2407 * it will never end up being up to date. Set ret to EIO now
2408 * and give up so that our caller doesn't loop forever
2409 * on our EAGAINs.
2410 */
Chris Masonb9fab912012-05-06 07:23:47 -04002411 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002412 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002413 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002414 }
2415 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002416}
2417
2418/*
2419 * helper function for btrfs_search_slot. This does all of the checks
2420 * for node-level blocks and does any balancing required based on
2421 * the ins_len.
2422 *
2423 * If no extra work was required, zero is returned. If we had to
2424 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2425 * start over
2426 */
2427static int
2428setup_nodes_for_search(struct btrfs_trans_handle *trans,
2429 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002430 struct extent_buffer *b, int level, int ins_len,
2431 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002432{
2433 int ret;
2434 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2435 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2436 int sret;
2437
Chris Masonbd681512011-07-16 15:23:14 -04002438 if (*write_lock_level < level + 1) {
2439 *write_lock_level = level + 1;
2440 btrfs_release_path(p);
2441 goto again;
2442 }
2443
Chris Masonc8c42862009-04-03 10:14:18 -04002444 sret = reada_for_balance(root, p, level);
2445 if (sret)
2446 goto again;
2447
2448 btrfs_set_path_blocking(p);
2449 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002450 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002451
2452 BUG_ON(sret > 0);
2453 if (sret) {
2454 ret = sret;
2455 goto done;
2456 }
2457 b = p->nodes[level];
2458 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002459 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002460 int sret;
2461
Chris Masonbd681512011-07-16 15:23:14 -04002462 if (*write_lock_level < level + 1) {
2463 *write_lock_level = level + 1;
2464 btrfs_release_path(p);
2465 goto again;
2466 }
2467
Chris Masonc8c42862009-04-03 10:14:18 -04002468 sret = reada_for_balance(root, p, level);
2469 if (sret)
2470 goto again;
2471
2472 btrfs_set_path_blocking(p);
2473 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002474 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002475
2476 if (sret) {
2477 ret = sret;
2478 goto done;
2479 }
2480 b = p->nodes[level];
2481 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002482 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002483 goto again;
2484 }
2485 BUG_ON(btrfs_header_nritems(b) == 1);
2486 }
2487 return 0;
2488
2489again:
2490 ret = -EAGAIN;
2491done:
2492 return ret;
2493}
2494
2495/*
Chris Mason74123bd2007-02-02 11:05:29 -05002496 * look for key in the tree. path is filled in with nodes along the way
2497 * if key is found, we return zero and you can find the item in the leaf
2498 * level of the path (level 0)
2499 *
2500 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002501 * be inserted, and 1 is returned. If there are other errors during the
2502 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002503 *
2504 * if ins_len > 0, nodes and leaves will be split as we walk down the
2505 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2506 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002507 */
Chris Masone089f052007-03-16 16:20:31 -04002508int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2509 *root, struct btrfs_key *key, struct btrfs_path *p, int
2510 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002511{
Chris Mason5f39d392007-10-15 16:14:19 -04002512 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002513 int slot;
2514 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002515 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002516 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002517 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002518 int root_lock;
2519 /* everything at write_lock_level or lower must be write locked */
2520 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002521 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002522 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002523
Chris Mason6702ed42007-08-07 16:15:09 -04002524 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002525 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002526 WARN_ON(p->nodes[0] != NULL);
Josef Bacik251792012008-10-29 14:49:05 -04002527
Chris Masonbd681512011-07-16 15:23:14 -04002528 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002529 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002530
Chris Masonbd681512011-07-16 15:23:14 -04002531 /* when we are removing items, we might have to go up to level
2532 * two as we update tree pointers Make sure we keep write
2533 * for those levels as well
2534 */
2535 write_lock_level = 2;
2536 } else if (ins_len > 0) {
2537 /*
2538 * for inserting items, make sure we have a write lock on
2539 * level 1 so we can update keys
2540 */
2541 write_lock_level = 1;
2542 }
2543
2544 if (!cow)
2545 write_lock_level = -1;
2546
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002547 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002548 write_lock_level = BTRFS_MAX_LEVEL;
2549
Chris Masonf7c79f32012-03-19 15:54:38 -04002550 min_write_lock_level = write_lock_level;
2551
Chris Masonbb803952007-03-01 12:04:21 -05002552again:
Chris Masonbd681512011-07-16 15:23:14 -04002553 /*
2554 * we try very hard to do read locks on the root
2555 */
2556 root_lock = BTRFS_READ_LOCK;
2557 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002558 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002559 /*
2560 * the commit roots are read only
2561 * so we always do read locks
2562 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002563 b = root->commit_root;
2564 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002565 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002566 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002567 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002568 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002569 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002570 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002571 level = btrfs_header_level(b);
2572 } else {
2573 /* we don't know the level of the root node
2574 * until we actually have it read locked
2575 */
2576 b = btrfs_read_lock_root_node(root);
2577 level = btrfs_header_level(b);
2578 if (level <= write_lock_level) {
2579 /* whoops, must trade for write lock */
2580 btrfs_tree_read_unlock(b);
2581 free_extent_buffer(b);
2582 b = btrfs_lock_root_node(root);
2583 root_lock = BTRFS_WRITE_LOCK;
2584
2585 /* the level might have changed, check again */
2586 level = btrfs_header_level(b);
2587 }
2588 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002589 }
Chris Masonbd681512011-07-16 15:23:14 -04002590 p->nodes[level] = b;
2591 if (!p->skip_locking)
2592 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002593
Chris Masoneb60cea2007-02-02 09:18:22 -05002594 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002595 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002596
2597 /*
2598 * setup the path here so we can release it under lock
2599 * contention with the cow code
2600 */
Chris Mason02217ed2007-03-02 16:08:05 -05002601 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002602 /*
2603 * if we don't really need to cow this block
2604 * then we don't want to set the path blocking,
2605 * so we test it here
2606 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002607 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002608 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002609
Chris Masonb4ce94d2009-02-04 09:25:08 -05002610 btrfs_set_path_blocking(p);
2611
Chris Masonbd681512011-07-16 15:23:14 -04002612 /*
2613 * must have write locks on this node and the
2614 * parent
2615 */
Josef Bacik5124e002012-11-07 13:44:13 -05002616 if (level > write_lock_level ||
2617 (level + 1 > write_lock_level &&
2618 level + 1 < BTRFS_MAX_LEVEL &&
2619 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002620 write_lock_level = level + 1;
2621 btrfs_release_path(p);
2622 goto again;
2623 }
2624
Yan Zheng33c66f42009-07-22 09:59:00 -04002625 err = btrfs_cow_block(trans, root, b,
2626 p->nodes[level + 1],
2627 p->slots[level + 1], &b);
2628 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002629 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002630 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002631 }
Chris Mason02217ed2007-03-02 16:08:05 -05002632 }
Chris Mason65b51a02008-08-01 15:11:20 -04002633cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002634 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002635
Chris Masoneb60cea2007-02-02 09:18:22 -05002636 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002637 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002638
2639 /*
2640 * we have a lock on b and as long as we aren't changing
2641 * the tree, there is no way to for the items in b to change.
2642 * It is safe to drop the lock on our parent before we
2643 * go through the expensive btree search on b.
2644 *
2645 * If cow is true, then we might be changing slot zero,
2646 * which may require changing the parent. So, we can't
2647 * drop the lock until after we know which slot we're
2648 * operating on.
2649 */
2650 if (!cow)
2651 btrfs_unlock_up_safe(p, level + 1);
2652
Chris Mason5f39d392007-10-15 16:14:19 -04002653 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002654
Chris Mason5f39d392007-10-15 16:14:19 -04002655 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002656 int dec = 0;
2657 if (ret && slot > 0) {
2658 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002659 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002660 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002661 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002662 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002663 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002664 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002665 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002666 if (err) {
2667 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002668 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002669 }
Chris Masonc8c42862009-04-03 10:14:18 -04002670 b = p->nodes[level];
2671 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002672
Chris Masonbd681512011-07-16 15:23:14 -04002673 /*
2674 * slot 0 is special, if we change the key
2675 * we have to update the parent pointer
2676 * which means we must have a write lock
2677 * on the parent
2678 */
2679 if (slot == 0 && cow &&
2680 write_lock_level < level + 1) {
2681 write_lock_level = level + 1;
2682 btrfs_release_path(p);
2683 goto again;
2684 }
2685
Chris Masonf7c79f32012-03-19 15:54:38 -04002686 unlock_up(p, level, lowest_unlock,
2687 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002688
Chris Mason925baed2008-06-25 16:01:30 -04002689 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002690 if (dec)
2691 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002692 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002693 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002694
Yan Zheng33c66f42009-07-22 09:59:00 -04002695 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002696 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002697 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002698 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002699 if (err) {
2700 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002701 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002702 }
Chris Mason76a05b32009-05-14 13:24:30 -04002703
Chris Masonb4ce94d2009-02-04 09:25:08 -05002704 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002705 level = btrfs_header_level(b);
2706 if (level <= write_lock_level) {
2707 err = btrfs_try_tree_write_lock(b);
2708 if (!err) {
2709 btrfs_set_path_blocking(p);
2710 btrfs_tree_lock(b);
2711 btrfs_clear_path_blocking(p, b,
2712 BTRFS_WRITE_LOCK);
2713 }
2714 p->locks[level] = BTRFS_WRITE_LOCK;
2715 } else {
2716 err = btrfs_try_tree_read_lock(b);
2717 if (!err) {
2718 btrfs_set_path_blocking(p);
2719 btrfs_tree_read_lock(b);
2720 btrfs_clear_path_blocking(p, b,
2721 BTRFS_READ_LOCK);
2722 }
2723 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002724 }
Chris Masonbd681512011-07-16 15:23:14 -04002725 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002726 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002727 } else {
2728 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002729 if (ins_len > 0 &&
2730 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002731 if (write_lock_level < 1) {
2732 write_lock_level = 1;
2733 btrfs_release_path(p);
2734 goto again;
2735 }
2736
Chris Masonb4ce94d2009-02-04 09:25:08 -05002737 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002738 err = split_leaf(trans, root, key,
2739 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002740 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002741
Yan Zheng33c66f42009-07-22 09:59:00 -04002742 BUG_ON(err > 0);
2743 if (err) {
2744 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002745 goto done;
2746 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002747 }
Chris Mason459931e2008-12-10 09:10:46 -05002748 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002749 unlock_up(p, level, lowest_unlock,
2750 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002751 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002752 }
2753 }
Chris Mason65b51a02008-08-01 15:11:20 -04002754 ret = 1;
2755done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002756 /*
2757 * we don't really know what they plan on doing with the path
2758 * from here on, so for now just mark it as blocking
2759 */
Chris Masonb9473432009-03-13 11:00:37 -04002760 if (!p->leave_spinning)
2761 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002762 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002763 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002764 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002765}
2766
Chris Mason74123bd2007-02-02 11:05:29 -05002767/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002768 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2769 * current state of the tree together with the operations recorded in the tree
2770 * modification log to search for the key in a previous version of this tree, as
2771 * denoted by the time_seq parameter.
2772 *
2773 * Naturally, there is no support for insert, delete or cow operations.
2774 *
2775 * The resulting path and return value will be set up as if we called
2776 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2777 */
2778int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2779 struct btrfs_path *p, u64 time_seq)
2780{
2781 struct extent_buffer *b;
2782 int slot;
2783 int ret;
2784 int err;
2785 int level;
2786 int lowest_unlock = 1;
2787 u8 lowest_level = 0;
2788
2789 lowest_level = p->lowest_level;
2790 WARN_ON(p->nodes[0] != NULL);
2791
2792 if (p->search_commit_root) {
2793 BUG_ON(time_seq);
2794 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2795 }
2796
2797again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002798 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002799 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002800 p->locks[level] = BTRFS_READ_LOCK;
2801
2802 while (b) {
2803 level = btrfs_header_level(b);
2804 p->nodes[level] = b;
2805 btrfs_clear_path_blocking(p, NULL, 0);
2806
2807 /*
2808 * we have a lock on b and as long as we aren't changing
2809 * the tree, there is no way to for the items in b to change.
2810 * It is safe to drop the lock on our parent before we
2811 * go through the expensive btree search on b.
2812 */
2813 btrfs_unlock_up_safe(p, level + 1);
2814
2815 ret = bin_search(b, key, level, &slot);
2816
2817 if (level != 0) {
2818 int dec = 0;
2819 if (ret && slot > 0) {
2820 dec = 1;
2821 slot -= 1;
2822 }
2823 p->slots[level] = slot;
2824 unlock_up(p, level, lowest_unlock, 0, NULL);
2825
2826 if (level == lowest_level) {
2827 if (dec)
2828 p->slots[level]++;
2829 goto done;
2830 }
2831
2832 err = read_block_for_search(NULL, root, p, &b, level,
2833 slot, key, time_seq);
2834 if (err == -EAGAIN)
2835 goto again;
2836 if (err) {
2837 ret = err;
2838 goto done;
2839 }
2840
2841 level = btrfs_header_level(b);
2842 err = btrfs_try_tree_read_lock(b);
2843 if (!err) {
2844 btrfs_set_path_blocking(p);
2845 btrfs_tree_read_lock(b);
2846 btrfs_clear_path_blocking(p, b,
2847 BTRFS_READ_LOCK);
2848 }
Jan Schmidt47fb0912013-04-13 13:19:55 +00002849 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002850 p->locks[level] = BTRFS_READ_LOCK;
2851 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002852 } else {
2853 p->slots[level] = slot;
2854 unlock_up(p, level, lowest_unlock, 0, NULL);
2855 goto done;
2856 }
2857 }
2858 ret = 1;
2859done:
2860 if (!p->leave_spinning)
2861 btrfs_set_path_blocking(p);
2862 if (ret < 0)
2863 btrfs_release_path(p);
2864
2865 return ret;
2866}
2867
2868/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002869 * helper to use instead of search slot if no exact match is needed but
2870 * instead the next or previous item should be returned.
2871 * When find_higher is true, the next higher item is returned, the next lower
2872 * otherwise.
2873 * When return_any and find_higher are both true, and no higher item is found,
2874 * return the next lower instead.
2875 * When return_any is true and find_higher is false, and no lower item is found,
2876 * return the next higher instead.
2877 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2878 * < 0 on error
2879 */
2880int btrfs_search_slot_for_read(struct btrfs_root *root,
2881 struct btrfs_key *key, struct btrfs_path *p,
2882 int find_higher, int return_any)
2883{
2884 int ret;
2885 struct extent_buffer *leaf;
2886
2887again:
2888 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2889 if (ret <= 0)
2890 return ret;
2891 /*
2892 * a return value of 1 means the path is at the position where the
2893 * item should be inserted. Normally this is the next bigger item,
2894 * but in case the previous item is the last in a leaf, path points
2895 * to the first free slot in the previous leaf, i.e. at an invalid
2896 * item.
2897 */
2898 leaf = p->nodes[0];
2899
2900 if (find_higher) {
2901 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2902 ret = btrfs_next_leaf(root, p);
2903 if (ret <= 0)
2904 return ret;
2905 if (!return_any)
2906 return 1;
2907 /*
2908 * no higher item found, return the next
2909 * lower instead
2910 */
2911 return_any = 0;
2912 find_higher = 0;
2913 btrfs_release_path(p);
2914 goto again;
2915 }
2916 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002917 if (p->slots[0] == 0) {
2918 ret = btrfs_prev_leaf(root, p);
2919 if (ret < 0)
2920 return ret;
2921 if (!ret) {
2922 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2923 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002924 }
Arne Jansene6793762011-09-13 11:18:10 +02002925 if (!return_any)
2926 return 1;
2927 /*
2928 * no lower item found, return the next
2929 * higher instead
2930 */
2931 return_any = 0;
2932 find_higher = 1;
2933 btrfs_release_path(p);
2934 goto again;
2935 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002936 --p->slots[0];
2937 }
2938 }
2939 return 0;
2940}
2941
2942/*
Chris Mason74123bd2007-02-02 11:05:29 -05002943 * adjust the pointers going up the tree, starting at level
2944 * making sure the right key of each node is points to 'key'.
2945 * This is used after shifting pointers to the left, so it stops
2946 * fixing up pointers when a given leaf/node is not in slot 0 of the
2947 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002948 *
Chris Mason74123bd2007-02-02 11:05:29 -05002949 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002950static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002951 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002952{
2953 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002954 struct extent_buffer *t;
2955
Chris Mason234b63a2007-03-13 10:46:10 -04002956 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002957 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002958 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002959 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002960 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002961 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002962 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002963 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002964 if (tslot != 0)
2965 break;
2966 }
2967}
2968
Chris Mason74123bd2007-02-02 11:05:29 -05002969/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002970 * update item key.
2971 *
2972 * This function isn't completely safe. It's the caller's responsibility
2973 * that the new key won't break the order
2974 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002975void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002976 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002977{
2978 struct btrfs_disk_key disk_key;
2979 struct extent_buffer *eb;
2980 int slot;
2981
2982 eb = path->nodes[0];
2983 slot = path->slots[0];
2984 if (slot > 0) {
2985 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002986 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002987 }
2988 if (slot < btrfs_header_nritems(eb) - 1) {
2989 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002990 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002991 }
2992
2993 btrfs_cpu_key_to_disk(&disk_key, new_key);
2994 btrfs_set_item_key(eb, &disk_key, slot);
2995 btrfs_mark_buffer_dirty(eb);
2996 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002997 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002998}
2999
3000/*
Chris Mason74123bd2007-02-02 11:05:29 -05003001 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003002 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003003 *
3004 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3005 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003006 */
Chris Mason98ed5172008-01-03 10:01:48 -05003007static int push_node_left(struct btrfs_trans_handle *trans,
3008 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003009 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003010{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003012 int src_nritems;
3013 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003014 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003015
Chris Mason5f39d392007-10-15 16:14:19 -04003016 src_nritems = btrfs_header_nritems(src);
3017 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003018 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003019 WARN_ON(btrfs_header_generation(src) != trans->transid);
3020 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003021
Chris Masonbce4eae2008-04-24 14:42:46 -04003022 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003023 return 1;
3024
Chris Masond3977122009-01-05 21:25:51 -05003025 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003026 return 1;
3027
Chris Masonbce4eae2008-04-24 14:42:46 -04003028 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003029 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003030 if (push_items < src_nritems) {
3031 /* leave at least 8 pointers in the node if
3032 * we aren't going to empty it
3033 */
3034 if (src_nritems - push_items < 8) {
3035 if (push_items <= 8)
3036 return 1;
3037 push_items -= 8;
3038 }
3039 }
3040 } else
3041 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003042
Jan Schmidtf2304752012-05-26 11:43:17 +02003043 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003044 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003045 copy_extent_buffer(dst, src,
3046 btrfs_node_key_ptr_offset(dst_nritems),
3047 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003048 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003049
Chris Masonbb803952007-03-01 12:04:21 -05003050 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003051 /*
3052 * don't call tree_mod_log_eb_move here, key removal was already
3053 * fully logged by tree_mod_log_eb_copy above.
3054 */
Chris Mason5f39d392007-10-15 16:14:19 -04003055 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3056 btrfs_node_key_ptr_offset(push_items),
3057 (src_nritems - push_items) *
3058 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003059 }
Chris Mason5f39d392007-10-15 16:14:19 -04003060 btrfs_set_header_nritems(src, src_nritems - push_items);
3061 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3062 btrfs_mark_buffer_dirty(src);
3063 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003064
Chris Masonbb803952007-03-01 12:04:21 -05003065 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003066}
3067
Chris Mason97571fd2007-02-24 13:39:08 -05003068/*
Chris Mason79f95c82007-03-01 15:16:26 -05003069 * try to push data from one node into the next node right in the
3070 * tree.
3071 *
3072 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3073 * error, and > 0 if there was no room in the right hand block.
3074 *
3075 * this will only push up to 1/2 the contents of the left node over
3076 */
Chris Mason5f39d392007-10-15 16:14:19 -04003077static int balance_node_right(struct btrfs_trans_handle *trans,
3078 struct btrfs_root *root,
3079 struct extent_buffer *dst,
3080 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003081{
Chris Mason79f95c82007-03-01 15:16:26 -05003082 int push_items = 0;
3083 int max_push;
3084 int src_nritems;
3085 int dst_nritems;
3086 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003087
Chris Mason7bb86312007-12-11 09:25:06 -05003088 WARN_ON(btrfs_header_generation(src) != trans->transid);
3089 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3090
Chris Mason5f39d392007-10-15 16:14:19 -04003091 src_nritems = btrfs_header_nritems(src);
3092 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003093 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003094 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003095 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003096
Chris Masond3977122009-01-05 21:25:51 -05003097 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003098 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003099
3100 max_push = src_nritems / 2 + 1;
3101 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003102 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003103 return 1;
Yan252c38f2007-08-29 09:11:44 -04003104
Chris Mason79f95c82007-03-01 15:16:26 -05003105 if (max_push < push_items)
3106 push_items = max_push;
3107
Jan Schmidtf2304752012-05-26 11:43:17 +02003108 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003109 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3110 btrfs_node_key_ptr_offset(0),
3111 (dst_nritems) *
3112 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003113
Jan Schmidtf2304752012-05-26 11:43:17 +02003114 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003115 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003116 copy_extent_buffer(dst, src,
3117 btrfs_node_key_ptr_offset(0),
3118 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003119 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003120
Chris Mason5f39d392007-10-15 16:14:19 -04003121 btrfs_set_header_nritems(src, src_nritems - push_items);
3122 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003123
Chris Mason5f39d392007-10-15 16:14:19 -04003124 btrfs_mark_buffer_dirty(src);
3125 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003126
Chris Mason79f95c82007-03-01 15:16:26 -05003127 return ret;
3128}
3129
3130/*
Chris Mason97571fd2007-02-24 13:39:08 -05003131 * helper function to insert a new root level in the tree.
3132 * A new node is allocated, and a single item is inserted to
3133 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003134 *
3135 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003136 */
Chris Masond3977122009-01-05 21:25:51 -05003137static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003138 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003139 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003140{
Chris Mason7bb86312007-12-11 09:25:06 -05003141 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003142 struct extent_buffer *lower;
3143 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003144 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003145 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003146
3147 BUG_ON(path->nodes[level]);
3148 BUG_ON(path->nodes[level-1] != root->node);
3149
Chris Mason7bb86312007-12-11 09:25:06 -05003150 lower = path->nodes[level-1];
3151 if (level == 1)
3152 btrfs_item_key(lower, &lower_key, 0);
3153 else
3154 btrfs_node_key(lower, &lower_key, 0);
3155
Zheng Yan31840ae2008-09-23 13:14:14 -04003156 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003157 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003158 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003159 if (IS_ERR(c))
3160 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003161
Yan, Zhengf0486c62010-05-16 10:46:25 -04003162 root_add_used(root, root->nodesize);
3163
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003164 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003165 btrfs_set_header_nritems(c, 1);
3166 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003167 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003168 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003169 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003170 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003171
Chris Mason5f39d392007-10-15 16:14:19 -04003172 write_extent_buffer(c, root->fs_info->fsid,
3173 (unsigned long)btrfs_header_fsid(c),
3174 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003175
3176 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3177 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3178 BTRFS_UUID_SIZE);
3179
Chris Mason5f39d392007-10-15 16:14:19 -04003180 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003181 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003182 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003183 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003184
3185 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003186
3187 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003188
Chris Mason925baed2008-06-25 16:01:30 -04003189 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003190 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003191 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003192
3193 /* the super has an extra ref to root->node */
3194 free_extent_buffer(old);
3195
Chris Mason0b86a832008-03-24 15:01:56 -04003196 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003197 extent_buffer_get(c);
3198 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003199 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003200 path->slots[level] = 0;
3201 return 0;
3202}
3203
Chris Mason74123bd2007-02-02 11:05:29 -05003204/*
3205 * worker function to insert a single pointer in a node.
3206 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003207 *
Chris Mason74123bd2007-02-02 11:05:29 -05003208 * slot and level indicate where you want the key to go, and
3209 * blocknr is the block the key points to.
3210 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003211static void insert_ptr(struct btrfs_trans_handle *trans,
3212 struct btrfs_root *root, struct btrfs_path *path,
3213 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003214 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003215{
Chris Mason5f39d392007-10-15 16:14:19 -04003216 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003217 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003218 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003219
3220 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003221 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003222 lower = path->nodes[level];
3223 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003224 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003225 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003226 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003227 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003228 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3229 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003230 memmove_extent_buffer(lower,
3231 btrfs_node_key_ptr_offset(slot + 1),
3232 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003233 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003234 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003235 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003236 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3237 MOD_LOG_KEY_ADD);
3238 BUG_ON(ret < 0);
3239 }
Chris Mason5f39d392007-10-15 16:14:19 -04003240 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003241 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003242 WARN_ON(trans->transid == 0);
3243 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003244 btrfs_set_header_nritems(lower, nritems + 1);
3245 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003246}
3247
Chris Mason97571fd2007-02-24 13:39:08 -05003248/*
3249 * split the node at the specified level in path in two.
3250 * The path is corrected to point to the appropriate node after the split
3251 *
3252 * Before splitting this tries to make some room in the node by pushing
3253 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003254 *
3255 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003256 */
Chris Masone02119d2008-09-05 16:13:11 -04003257static noinline int split_node(struct btrfs_trans_handle *trans,
3258 struct btrfs_root *root,
3259 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003260{
Chris Mason5f39d392007-10-15 16:14:19 -04003261 struct extent_buffer *c;
3262 struct extent_buffer *split;
3263 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003265 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003266 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003267
Chris Mason5f39d392007-10-15 16:14:19 -04003268 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003269 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003270 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003271 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003272 * trying to split the root, lets make a new one
3273 *
Liu Bofdd99c72013-05-22 12:06:51 +00003274 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003275 * insert_new_root, because that root buffer will be kept as a
3276 * normal node. We are going to log removal of half of the
3277 * elements below with tree_mod_log_eb_copy. We're holding a
3278 * tree lock on the buffer, which is why we cannot race with
3279 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003280 */
Liu Bofdd99c72013-05-22 12:06:51 +00003281 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003282 if (ret)
3283 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003284 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003285 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003286 c = path->nodes[level];
3287 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003288 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003289 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003290 if (ret < 0)
3291 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003292 }
Chris Masone66f7092007-04-20 13:16:02 -04003293
Chris Mason5f39d392007-10-15 16:14:19 -04003294 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003295 mid = (c_nritems + 1) / 2;
3296 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003297
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003298 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003299 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003300 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003301 if (IS_ERR(split))
3302 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003303
Yan, Zhengf0486c62010-05-16 10:46:25 -04003304 root_add_used(root, root->nodesize);
3305
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003306 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003307 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003308 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003309 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003310 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003311 btrfs_set_header_owner(split, root->root_key.objectid);
3312 write_extent_buffer(split, root->fs_info->fsid,
3313 (unsigned long)btrfs_header_fsid(split),
3314 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003315 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3316 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3317 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003318
Jan Schmidt90f8d622013-04-13 13:19:53 +00003319 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003320 copy_extent_buffer(split, c,
3321 btrfs_node_key_ptr_offset(0),
3322 btrfs_node_key_ptr_offset(mid),
3323 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3324 btrfs_set_header_nritems(split, c_nritems - mid);
3325 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003326 ret = 0;
3327
Chris Mason5f39d392007-10-15 16:14:19 -04003328 btrfs_mark_buffer_dirty(c);
3329 btrfs_mark_buffer_dirty(split);
3330
Jeff Mahoney143bede2012-03-01 14:56:26 +01003331 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003332 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003333
Chris Mason5de08d72007-02-24 06:24:44 -05003334 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003335 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003336 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003337 free_extent_buffer(c);
3338 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003339 path->slots[level + 1] += 1;
3340 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003341 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003342 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003343 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003344 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003345}
3346
Chris Mason74123bd2007-02-02 11:05:29 -05003347/*
3348 * how many bytes are required to store the items in a leaf. start
3349 * and nr indicate which items in the leaf to check. This totals up the
3350 * space used both by the item structs and the item data
3351 */
Chris Mason5f39d392007-10-15 16:14:19 -04003352static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003353{
Josef Bacik41be1f32012-10-15 13:43:18 -04003354 struct btrfs_item *start_item;
3355 struct btrfs_item *end_item;
3356 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003357 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003358 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003359 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003360
3361 if (!nr)
3362 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003363 btrfs_init_map_token(&token);
3364 start_item = btrfs_item_nr(l, start);
3365 end_item = btrfs_item_nr(l, end);
3366 data_len = btrfs_token_item_offset(l, start_item, &token) +
3367 btrfs_token_item_size(l, start_item, &token);
3368 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003369 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003370 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003371 return data_len;
3372}
3373
Chris Mason74123bd2007-02-02 11:05:29 -05003374/*
Chris Masond4dbff92007-04-04 14:08:15 -04003375 * The space between the end of the leaf items and
3376 * the start of the leaf data. IOW, how much room
3377 * the leaf has left for both items and data
3378 */
Chris Masond3977122009-01-05 21:25:51 -05003379noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003380 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003381{
Chris Mason5f39d392007-10-15 16:14:19 -04003382 int nritems = btrfs_header_nritems(leaf);
3383 int ret;
3384 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3385 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003386 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3387 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003388 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003389 leaf_space_used(leaf, 0, nritems), nritems);
3390 }
3391 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003392}
3393
Chris Mason99d8f832010-07-07 10:51:48 -04003394/*
3395 * min slot controls the lowest index we're willing to push to the
3396 * right. We'll push up to and including min_slot, but no lower
3397 */
Chris Mason44871b12009-03-13 10:04:31 -04003398static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3399 struct btrfs_root *root,
3400 struct btrfs_path *path,
3401 int data_size, int empty,
3402 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003403 int free_space, u32 left_nritems,
3404 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003405{
Chris Mason5f39d392007-10-15 16:14:19 -04003406 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003407 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003408 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003409 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003410 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003411 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003412 int push_space = 0;
3413 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003414 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003415 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003416 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003417 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003418 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003419
Chris Masoncfed81a2012-03-03 07:40:03 -05003420 btrfs_init_map_token(&token);
3421
Chris Mason34a38212007-11-07 13:31:03 -05003422 if (empty)
3423 nr = 0;
3424 else
Chris Mason99d8f832010-07-07 10:51:48 -04003425 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003426
Zheng Yan31840ae2008-09-23 13:14:14 -04003427 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003428 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003429
Chris Mason44871b12009-03-13 10:04:31 -04003430 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003431 i = left_nritems - 1;
3432 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003433 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003434
Zheng Yan31840ae2008-09-23 13:14:14 -04003435 if (!empty && push_items > 0) {
3436 if (path->slots[0] > i)
3437 break;
3438 if (path->slots[0] == i) {
3439 int space = btrfs_leaf_free_space(root, left);
3440 if (space + push_space * 2 > free_space)
3441 break;
3442 }
3443 }
3444
Chris Mason00ec4c52007-02-24 12:47:20 -05003445 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003446 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003447
Chris Masondb945352007-10-15 16:15:53 -04003448 this_item_size = btrfs_item_size(left, item);
3449 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003450 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003451
Chris Mason00ec4c52007-02-24 12:47:20 -05003452 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003453 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003454 if (i == 0)
3455 break;
3456 i--;
Chris Masondb945352007-10-15 16:15:53 -04003457 }
Chris Mason5f39d392007-10-15 16:14:19 -04003458
Chris Mason925baed2008-06-25 16:01:30 -04003459 if (push_items == 0)
3460 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003461
Julia Lawall6c1500f2012-11-03 20:30:18 +00003462 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003463
Chris Mason00ec4c52007-02-24 12:47:20 -05003464 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003465 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003466
Chris Mason5f39d392007-10-15 16:14:19 -04003467 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003468 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003469
Chris Mason00ec4c52007-02-24 12:47:20 -05003470 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003471 data_end = leaf_data_end(root, right);
3472 memmove_extent_buffer(right,
3473 btrfs_leaf_data(right) + data_end - push_space,
3474 btrfs_leaf_data(right) + data_end,
3475 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3476
Chris Mason00ec4c52007-02-24 12:47:20 -05003477 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003478 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003479 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3480 btrfs_leaf_data(left) + leaf_data_end(root, left),
3481 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003482
3483 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3484 btrfs_item_nr_offset(0),
3485 right_nritems * sizeof(struct btrfs_item));
3486
Chris Mason00ec4c52007-02-24 12:47:20 -05003487 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003488 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3489 btrfs_item_nr_offset(left_nritems - push_items),
3490 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003491
3492 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003493 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003494 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003495 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003496 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003497 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003498 push_space -= btrfs_token_item_size(right, item, &token);
3499 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003500 }
3501
Chris Mason7518a232007-03-12 12:01:18 -04003502 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003503 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003504
Chris Mason34a38212007-11-07 13:31:03 -05003505 if (left_nritems)
3506 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003507 else
3508 clean_tree_block(trans, root, left);
3509
Chris Mason5f39d392007-10-15 16:14:19 -04003510 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003511
Chris Mason5f39d392007-10-15 16:14:19 -04003512 btrfs_item_key(right, &disk_key, 0);
3513 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003514 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003515
Chris Mason00ec4c52007-02-24 12:47:20 -05003516 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003517 if (path->slots[0] >= left_nritems) {
3518 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003519 if (btrfs_header_nritems(path->nodes[0]) == 0)
3520 clean_tree_block(trans, root, path->nodes[0]);
3521 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003522 free_extent_buffer(path->nodes[0]);
3523 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003524 path->slots[1] += 1;
3525 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003526 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003527 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003528 }
3529 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003530
3531out_unlock:
3532 btrfs_tree_unlock(right);
3533 free_extent_buffer(right);
3534 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003535}
Chris Mason925baed2008-06-25 16:01:30 -04003536
Chris Mason00ec4c52007-02-24 12:47:20 -05003537/*
Chris Mason44871b12009-03-13 10:04:31 -04003538 * push some data in the path leaf to the right, trying to free up at
3539 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3540 *
3541 * returns 1 if the push failed because the other node didn't have enough
3542 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003543 *
3544 * this will push starting from min_slot to the end of the leaf. It won't
3545 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003546 */
3547static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003548 *root, struct btrfs_path *path,
3549 int min_data_size, int data_size,
3550 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003551{
3552 struct extent_buffer *left = path->nodes[0];
3553 struct extent_buffer *right;
3554 struct extent_buffer *upper;
3555 int slot;
3556 int free_space;
3557 u32 left_nritems;
3558 int ret;
3559
3560 if (!path->nodes[1])
3561 return 1;
3562
3563 slot = path->slots[1];
3564 upper = path->nodes[1];
3565 if (slot >= btrfs_header_nritems(upper) - 1)
3566 return 1;
3567
3568 btrfs_assert_tree_locked(path->nodes[1]);
3569
3570 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003571 if (right == NULL)
3572 return 1;
3573
Chris Mason44871b12009-03-13 10:04:31 -04003574 btrfs_tree_lock(right);
3575 btrfs_set_lock_blocking(right);
3576
3577 free_space = btrfs_leaf_free_space(root, right);
3578 if (free_space < data_size)
3579 goto out_unlock;
3580
3581 /* cow and double check */
3582 ret = btrfs_cow_block(trans, root, right, upper,
3583 slot + 1, &right);
3584 if (ret)
3585 goto out_unlock;
3586
3587 free_space = btrfs_leaf_free_space(root, right);
3588 if (free_space < data_size)
3589 goto out_unlock;
3590
3591 left_nritems = btrfs_header_nritems(left);
3592 if (left_nritems == 0)
3593 goto out_unlock;
3594
Chris Mason99d8f832010-07-07 10:51:48 -04003595 return __push_leaf_right(trans, root, path, min_data_size, empty,
3596 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003597out_unlock:
3598 btrfs_tree_unlock(right);
3599 free_extent_buffer(right);
3600 return 1;
3601}
3602
3603/*
Chris Mason74123bd2007-02-02 11:05:29 -05003604 * push some data in the path leaf to the left, trying to free up at
3605 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003606 *
3607 * max_slot can put a limit on how far into the leaf we'll push items. The
3608 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3609 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003610 */
Chris Mason44871b12009-03-13 10:04:31 -04003611static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3612 struct btrfs_root *root,
3613 struct btrfs_path *path, int data_size,
3614 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003615 int free_space, u32 right_nritems,
3616 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003617{
Chris Mason5f39d392007-10-15 16:14:19 -04003618 struct btrfs_disk_key disk_key;
3619 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003620 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003621 int push_space = 0;
3622 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003623 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003624 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003625 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003626 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003627 u32 this_item_size;
3628 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003629 struct btrfs_map_token token;
3630
3631 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632
Chris Mason34a38212007-11-07 13:31:03 -05003633 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003634 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003635 else
Chris Mason99d8f832010-07-07 10:51:48 -04003636 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003637
3638 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003639 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003640
Zheng Yan31840ae2008-09-23 13:14:14 -04003641 if (!empty && push_items > 0) {
3642 if (path->slots[0] < i)
3643 break;
3644 if (path->slots[0] == i) {
3645 int space = btrfs_leaf_free_space(root, right);
3646 if (space + push_space * 2 > free_space)
3647 break;
3648 }
3649 }
3650
Chris Masonbe0e5c02007-01-26 15:51:26 -05003651 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003652 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003653
3654 this_item_size = btrfs_item_size(right, item);
3655 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003656 break;
Chris Masondb945352007-10-15 16:15:53 -04003657
Chris Masonbe0e5c02007-01-26 15:51:26 -05003658 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003659 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003660 }
Chris Masondb945352007-10-15 16:15:53 -04003661
Chris Masonbe0e5c02007-01-26 15:51:26 -05003662 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003663 ret = 1;
3664 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003665 }
Chris Mason34a38212007-11-07 13:31:03 -05003666 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003667 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003668
Chris Masonbe0e5c02007-01-26 15:51:26 -05003669 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003670 copy_extent_buffer(left, right,
3671 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3672 btrfs_item_nr_offset(0),
3673 push_items * sizeof(struct btrfs_item));
3674
Chris Mason123abc82007-03-14 14:14:43 -04003675 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003676 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003677
3678 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003679 leaf_data_end(root, left) - push_space,
3680 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003681 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003682 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003683 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003684 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003685
Chris Masondb945352007-10-15 16:15:53 -04003686 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003687 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003688 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003689
Chris Mason5f39d392007-10-15 16:14:19 -04003690 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003691
Chris Masoncfed81a2012-03-03 07:40:03 -05003692 ioff = btrfs_token_item_offset(left, item, &token);
3693 btrfs_set_token_item_offset(left, item,
3694 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3695 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003696 }
Chris Mason5f39d392007-10-15 16:14:19 -04003697 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003698
3699 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003700 if (push_items > right_nritems)
3701 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003702 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003703
Chris Mason34a38212007-11-07 13:31:03 -05003704 if (push_items < right_nritems) {
3705 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3706 leaf_data_end(root, right);
3707 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3708 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3709 btrfs_leaf_data(right) +
3710 leaf_data_end(root, right), push_space);
3711
3712 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003713 btrfs_item_nr_offset(push_items),
3714 (btrfs_header_nritems(right) - push_items) *
3715 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003716 }
Yaneef1c492007-11-26 10:58:13 -05003717 right_nritems -= push_items;
3718 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003719 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003720 for (i = 0; i < right_nritems; i++) {
3721 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003722
Chris Masoncfed81a2012-03-03 07:40:03 -05003723 push_space = push_space - btrfs_token_item_size(right,
3724 item, &token);
3725 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003726 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003727
Chris Mason5f39d392007-10-15 16:14:19 -04003728 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003729 if (right_nritems)
3730 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003731 else
3732 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003733
Chris Mason5f39d392007-10-15 16:14:19 -04003734 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003735 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003736
3737 /* then fixup the leaf pointer in the path */
3738 if (path->slots[0] < push_items) {
3739 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003740 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003741 free_extent_buffer(path->nodes[0]);
3742 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003743 path->slots[1] -= 1;
3744 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003745 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003746 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003747 path->slots[0] -= push_items;
3748 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003749 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003750 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003751out:
3752 btrfs_tree_unlock(left);
3753 free_extent_buffer(left);
3754 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003755}
3756
Chris Mason74123bd2007-02-02 11:05:29 -05003757/*
Chris Mason44871b12009-03-13 10:04:31 -04003758 * push some data in the path leaf to the left, trying to free up at
3759 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003760 *
3761 * max_slot can put a limit on how far into the leaf we'll push items. The
3762 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3763 * items
Chris Mason44871b12009-03-13 10:04:31 -04003764 */
3765static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003766 *root, struct btrfs_path *path, int min_data_size,
3767 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003768{
3769 struct extent_buffer *right = path->nodes[0];
3770 struct extent_buffer *left;
3771 int slot;
3772 int free_space;
3773 u32 right_nritems;
3774 int ret = 0;
3775
3776 slot = path->slots[1];
3777 if (slot == 0)
3778 return 1;
3779 if (!path->nodes[1])
3780 return 1;
3781
3782 right_nritems = btrfs_header_nritems(right);
3783 if (right_nritems == 0)
3784 return 1;
3785
3786 btrfs_assert_tree_locked(path->nodes[1]);
3787
3788 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003789 if (left == NULL)
3790 return 1;
3791
Chris Mason44871b12009-03-13 10:04:31 -04003792 btrfs_tree_lock(left);
3793 btrfs_set_lock_blocking(left);
3794
3795 free_space = btrfs_leaf_free_space(root, left);
3796 if (free_space < data_size) {
3797 ret = 1;
3798 goto out;
3799 }
3800
3801 /* cow and double check */
3802 ret = btrfs_cow_block(trans, root, left,
3803 path->nodes[1], slot - 1, &left);
3804 if (ret) {
3805 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003806 if (ret == -ENOSPC)
3807 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003808 goto out;
3809 }
3810
3811 free_space = btrfs_leaf_free_space(root, left);
3812 if (free_space < data_size) {
3813 ret = 1;
3814 goto out;
3815 }
3816
Chris Mason99d8f832010-07-07 10:51:48 -04003817 return __push_leaf_left(trans, root, path, min_data_size,
3818 empty, left, free_space, right_nritems,
3819 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003820out:
3821 btrfs_tree_unlock(left);
3822 free_extent_buffer(left);
3823 return ret;
3824}
3825
3826/*
Chris Mason74123bd2007-02-02 11:05:29 -05003827 * split the path's leaf in two, making sure there is at least data_size
3828 * available for the resulting leaf level of the path.
3829 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003830static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3831 struct btrfs_root *root,
3832 struct btrfs_path *path,
3833 struct extent_buffer *l,
3834 struct extent_buffer *right,
3835 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003836{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003837 int data_copy_size;
3838 int rt_data_off;
3839 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003840 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003841 struct btrfs_map_token token;
3842
3843 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 nritems = nritems - mid;
3846 btrfs_set_header_nritems(right, nritems);
3847 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3848
3849 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3850 btrfs_item_nr_offset(mid),
3851 nritems * sizeof(struct btrfs_item));
3852
3853 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003854 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3855 data_copy_size, btrfs_leaf_data(l) +
3856 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003857
Chris Mason5f39d392007-10-15 16:14:19 -04003858 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3859 btrfs_item_end_nr(l, mid);
3860
3861 for (i = 0; i < nritems; i++) {
3862 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003863 u32 ioff;
3864
Chris Masoncfed81a2012-03-03 07:40:03 -05003865 ioff = btrfs_token_item_offset(right, item, &token);
3866 btrfs_set_token_item_offset(right, item,
3867 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003868 }
Chris Mason74123bd2007-02-02 11:05:29 -05003869
Chris Mason5f39d392007-10-15 16:14:19 -04003870 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003871 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003872 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003873 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003874
3875 btrfs_mark_buffer_dirty(right);
3876 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003877 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003878
Chris Masonbe0e5c02007-01-26 15:51:26 -05003879 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003880 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003881 free_extent_buffer(path->nodes[0]);
3882 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883 path->slots[0] -= mid;
3884 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003885 } else {
3886 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003887 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003888 }
Chris Mason5f39d392007-10-15 16:14:19 -04003889
Chris Masoneb60cea2007-02-02 09:18:22 -05003890 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003891}
3892
3893/*
Chris Mason99d8f832010-07-07 10:51:48 -04003894 * double splits happen when we need to insert a big item in the middle
3895 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3896 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3897 * A B C
3898 *
3899 * We avoid this by trying to push the items on either side of our target
3900 * into the adjacent leaves. If all goes well we can avoid the double split
3901 * completely.
3902 */
3903static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3904 struct btrfs_root *root,
3905 struct btrfs_path *path,
3906 int data_size)
3907{
3908 int ret;
3909 int progress = 0;
3910 int slot;
3911 u32 nritems;
3912
3913 slot = path->slots[0];
3914
3915 /*
3916 * try to push all the items after our slot into the
3917 * right leaf
3918 */
3919 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3920 if (ret < 0)
3921 return ret;
3922
3923 if (ret == 0)
3924 progress++;
3925
3926 nritems = btrfs_header_nritems(path->nodes[0]);
3927 /*
3928 * our goal is to get our slot at the start or end of a leaf. If
3929 * we've done so we're done
3930 */
3931 if (path->slots[0] == 0 || path->slots[0] == nritems)
3932 return 0;
3933
3934 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3935 return 0;
3936
3937 /* try to push all the items before our slot into the next leaf */
3938 slot = path->slots[0];
3939 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3940 if (ret < 0)
3941 return ret;
3942
3943 if (ret == 0)
3944 progress++;
3945
3946 if (progress)
3947 return 0;
3948 return 1;
3949}
3950
3951/*
Chris Mason44871b12009-03-13 10:04:31 -04003952 * split the path's leaf in two, making sure there is at least data_size
3953 * available for the resulting leaf level of the path.
3954 *
3955 * returns 0 if all went well and < 0 on failure.
3956 */
3957static noinline int split_leaf(struct btrfs_trans_handle *trans,
3958 struct btrfs_root *root,
3959 struct btrfs_key *ins_key,
3960 struct btrfs_path *path, int data_size,
3961 int extend)
3962{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003963 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003964 struct extent_buffer *l;
3965 u32 nritems;
3966 int mid;
3967 int slot;
3968 struct extent_buffer *right;
3969 int ret = 0;
3970 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003971 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003972 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003973 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003974
Yan, Zhenga5719522009-09-24 09:17:31 -04003975 l = path->nodes[0];
3976 slot = path->slots[0];
3977 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3978 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3979 return -EOVERFLOW;
3980
Chris Mason44871b12009-03-13 10:04:31 -04003981 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003982 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003983 wret = push_leaf_right(trans, root, path, data_size,
3984 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003985 if (wret < 0)
3986 return wret;
3987 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003988 wret = push_leaf_left(trans, root, path, data_size,
3989 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003990 if (wret < 0)
3991 return wret;
3992 }
3993 l = path->nodes[0];
3994
3995 /* did the pushes work? */
3996 if (btrfs_leaf_free_space(root, l) >= data_size)
3997 return 0;
3998 }
3999
4000 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004001 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004002 if (ret)
4003 return ret;
4004 }
4005again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004006 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004007 l = path->nodes[0];
4008 slot = path->slots[0];
4009 nritems = btrfs_header_nritems(l);
4010 mid = (nritems + 1) / 2;
4011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012 if (mid <= slot) {
4013 if (nritems == 1 ||
4014 leaf_space_used(l, mid, nritems - mid) + data_size >
4015 BTRFS_LEAF_DATA_SIZE(root)) {
4016 if (slot >= nritems) {
4017 split = 0;
4018 } else {
4019 mid = slot;
4020 if (mid != nritems &&
4021 leaf_space_used(l, mid, nritems - mid) +
4022 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004023 if (data_size && !tried_avoid_double)
4024 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004025 split = 2;
4026 }
4027 }
4028 }
4029 } else {
4030 if (leaf_space_used(l, 0, mid) + data_size >
4031 BTRFS_LEAF_DATA_SIZE(root)) {
4032 if (!extend && data_size && slot == 0) {
4033 split = 0;
4034 } else if ((extend || !data_size) && slot == 0) {
4035 mid = 1;
4036 } else {
4037 mid = slot;
4038 if (mid != nritems &&
4039 leaf_space_used(l, mid, nritems - mid) +
4040 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004041 if (data_size && !tried_avoid_double)
4042 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 split = 2 ;
4044 }
4045 }
4046 }
4047 }
4048
4049 if (split == 0)
4050 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4051 else
4052 btrfs_item_key(l, &disk_key, mid);
4053
4054 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004055 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004056 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004057 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004058 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004059
4060 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004061
4062 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4063 btrfs_set_header_bytenr(right, right->start);
4064 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004066 btrfs_set_header_owner(right, root->root_key.objectid);
4067 btrfs_set_header_level(right, 0);
4068 write_extent_buffer(right, root->fs_info->fsid,
4069 (unsigned long)btrfs_header_fsid(right),
4070 BTRFS_FSID_SIZE);
4071
4072 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
4073 (unsigned long)btrfs_header_chunk_tree_uuid(right),
4074 BTRFS_UUID_SIZE);
4075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004076 if (split == 0) {
4077 if (mid <= slot) {
4078 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004079 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004080 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004081 btrfs_tree_unlock(path->nodes[0]);
4082 free_extent_buffer(path->nodes[0]);
4083 path->nodes[0] = right;
4084 path->slots[0] = 0;
4085 path->slots[1] += 1;
4086 } else {
4087 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004088 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004089 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004090 btrfs_tree_unlock(path->nodes[0]);
4091 free_extent_buffer(path->nodes[0]);
4092 path->nodes[0] = right;
4093 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004094 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004095 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004096 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004097 btrfs_mark_buffer_dirty(right);
4098 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004099 }
4100
Jeff Mahoney143bede2012-03-01 14:56:26 +01004101 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004103 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004104 BUG_ON(num_doubles != 0);
4105 num_doubles++;
4106 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004107 }
Chris Mason44871b12009-03-13 10:04:31 -04004108
Jeff Mahoney143bede2012-03-01 14:56:26 +01004109 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004110
4111push_for_double:
4112 push_for_double_split(trans, root, path, data_size);
4113 tried_avoid_double = 1;
4114 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4115 return 0;
4116 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004117}
4118
Yan, Zhengad48fd752009-11-12 09:33:58 +00004119static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4120 struct btrfs_root *root,
4121 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004122{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004123 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004124 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004125 struct btrfs_file_extent_item *fi;
4126 u64 extent_len = 0;
4127 u32 item_size;
4128 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004129
4130 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004131 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4132
4133 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4134 key.type != BTRFS_EXTENT_CSUM_KEY);
4135
4136 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4137 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004138
4139 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004140 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4141 fi = btrfs_item_ptr(leaf, path->slots[0],
4142 struct btrfs_file_extent_item);
4143 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4144 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004145 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004146
Chris Mason459931e2008-12-10 09:10:46 -05004147 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004148 path->search_for_split = 1;
4149 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004150 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004151 if (ret < 0)
4152 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004153
Yan, Zhengad48fd752009-11-12 09:33:58 +00004154 ret = -EAGAIN;
4155 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004156 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004157 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4158 goto err;
4159
Chris Mason109f6ae2010-04-02 09:20:18 -04004160 /* the leaf has changed, it now has room. return now */
4161 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4162 goto err;
4163
Yan, Zhengad48fd752009-11-12 09:33:58 +00004164 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4165 fi = btrfs_item_ptr(leaf, path->slots[0],
4166 struct btrfs_file_extent_item);
4167 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4168 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004169 }
4170
Chris Masonb9473432009-03-13 11:00:37 -04004171 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004172 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004173 if (ret)
4174 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004175
Yan, Zhengad48fd752009-11-12 09:33:58 +00004176 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004177 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004178 return 0;
4179err:
4180 path->keep_locks = 0;
4181 return ret;
4182}
4183
4184static noinline int split_item(struct btrfs_trans_handle *trans,
4185 struct btrfs_root *root,
4186 struct btrfs_path *path,
4187 struct btrfs_key *new_key,
4188 unsigned long split_offset)
4189{
4190 struct extent_buffer *leaf;
4191 struct btrfs_item *item;
4192 struct btrfs_item *new_item;
4193 int slot;
4194 char *buf;
4195 u32 nritems;
4196 u32 item_size;
4197 u32 orig_offset;
4198 struct btrfs_disk_key disk_key;
4199
Chris Masonb9473432009-03-13 11:00:37 -04004200 leaf = path->nodes[0];
4201 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4202
Chris Masonb4ce94d2009-02-04 09:25:08 -05004203 btrfs_set_path_blocking(path);
4204
Chris Mason459931e2008-12-10 09:10:46 -05004205 item = btrfs_item_nr(leaf, path->slots[0]);
4206 orig_offset = btrfs_item_offset(leaf, item);
4207 item_size = btrfs_item_size(leaf, item);
4208
Chris Mason459931e2008-12-10 09:10:46 -05004209 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004210 if (!buf)
4211 return -ENOMEM;
4212
Chris Mason459931e2008-12-10 09:10:46 -05004213 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4214 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004215
Chris Mason459931e2008-12-10 09:10:46 -05004216 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004217 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004218 if (slot != nritems) {
4219 /* shift the items */
4220 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004221 btrfs_item_nr_offset(slot),
4222 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004223 }
4224
4225 btrfs_cpu_key_to_disk(&disk_key, new_key);
4226 btrfs_set_item_key(leaf, &disk_key, slot);
4227
4228 new_item = btrfs_item_nr(leaf, slot);
4229
4230 btrfs_set_item_offset(leaf, new_item, orig_offset);
4231 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4232
4233 btrfs_set_item_offset(leaf, item,
4234 orig_offset + item_size - split_offset);
4235 btrfs_set_item_size(leaf, item, split_offset);
4236
4237 btrfs_set_header_nritems(leaf, nritems + 1);
4238
4239 /* write the data for the start of the original item */
4240 write_extent_buffer(leaf, buf,
4241 btrfs_item_ptr_offset(leaf, path->slots[0]),
4242 split_offset);
4243
4244 /* write the data for the new item */
4245 write_extent_buffer(leaf, buf + split_offset,
4246 btrfs_item_ptr_offset(leaf, slot),
4247 item_size - split_offset);
4248 btrfs_mark_buffer_dirty(leaf);
4249
Yan, Zhengad48fd752009-11-12 09:33:58 +00004250 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004251 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004252 return 0;
4253}
4254
4255/*
4256 * This function splits a single item into two items,
4257 * giving 'new_key' to the new item and splitting the
4258 * old one at split_offset (from the start of the item).
4259 *
4260 * The path may be released by this operation. After
4261 * the split, the path is pointing to the old item. The
4262 * new item is going to be in the same node as the old one.
4263 *
4264 * Note, the item being split must be smaller enough to live alone on
4265 * a tree block with room for one extra struct btrfs_item
4266 *
4267 * This allows us to split the item in place, keeping a lock on the
4268 * leaf the entire time.
4269 */
4270int btrfs_split_item(struct btrfs_trans_handle *trans,
4271 struct btrfs_root *root,
4272 struct btrfs_path *path,
4273 struct btrfs_key *new_key,
4274 unsigned long split_offset)
4275{
4276 int ret;
4277 ret = setup_leaf_for_split(trans, root, path,
4278 sizeof(struct btrfs_item));
4279 if (ret)
4280 return ret;
4281
4282 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004283 return ret;
4284}
4285
4286/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004287 * This function duplicate a item, giving 'new_key' to the new item.
4288 * It guarantees both items live in the same tree leaf and the new item
4289 * is contiguous with the original item.
4290 *
4291 * This allows us to split file extent in place, keeping a lock on the
4292 * leaf the entire time.
4293 */
4294int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4295 struct btrfs_root *root,
4296 struct btrfs_path *path,
4297 struct btrfs_key *new_key)
4298{
4299 struct extent_buffer *leaf;
4300 int ret;
4301 u32 item_size;
4302
4303 leaf = path->nodes[0];
4304 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4305 ret = setup_leaf_for_split(trans, root, path,
4306 item_size + sizeof(struct btrfs_item));
4307 if (ret)
4308 return ret;
4309
4310 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004311 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004312 item_size, item_size +
4313 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004314 leaf = path->nodes[0];
4315 memcpy_extent_buffer(leaf,
4316 btrfs_item_ptr_offset(leaf, path->slots[0]),
4317 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4318 item_size);
4319 return 0;
4320}
4321
4322/*
Chris Masond352ac62008-09-29 15:18:18 -04004323 * make the item pointed to by the path smaller. new_size indicates
4324 * how small to make it, and from_end tells us if we just chop bytes
4325 * off the end of the item or if we shift the item to chop bytes off
4326 * the front.
4327 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004328void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004329 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004330{
Chris Masonb18c6682007-04-17 13:26:50 -04004331 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004332 struct extent_buffer *leaf;
4333 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004334 u32 nritems;
4335 unsigned int data_end;
4336 unsigned int old_data_start;
4337 unsigned int old_size;
4338 unsigned int size_diff;
4339 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004340 struct btrfs_map_token token;
4341
4342 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004343
Chris Mason5f39d392007-10-15 16:14:19 -04004344 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004345 slot = path->slots[0];
4346
4347 old_size = btrfs_item_size_nr(leaf, slot);
4348 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004349 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004350
Chris Mason5f39d392007-10-15 16:14:19 -04004351 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004352 data_end = leaf_data_end(root, leaf);
4353
Chris Mason5f39d392007-10-15 16:14:19 -04004354 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004355
Chris Masonb18c6682007-04-17 13:26:50 -04004356 size_diff = old_size - new_size;
4357
4358 BUG_ON(slot < 0);
4359 BUG_ON(slot >= nritems);
4360
4361 /*
4362 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4363 */
4364 /* first correct the data pointers */
4365 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004366 u32 ioff;
4367 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004368
Chris Masoncfed81a2012-03-03 07:40:03 -05004369 ioff = btrfs_token_item_offset(leaf, item, &token);
4370 btrfs_set_token_item_offset(leaf, item,
4371 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004372 }
Chris Masondb945352007-10-15 16:15:53 -04004373
Chris Masonb18c6682007-04-17 13:26:50 -04004374 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004375 if (from_end) {
4376 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4377 data_end + size_diff, btrfs_leaf_data(leaf) +
4378 data_end, old_data_start + new_size - data_end);
4379 } else {
4380 struct btrfs_disk_key disk_key;
4381 u64 offset;
4382
4383 btrfs_item_key(leaf, &disk_key, slot);
4384
4385 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4386 unsigned long ptr;
4387 struct btrfs_file_extent_item *fi;
4388
4389 fi = btrfs_item_ptr(leaf, slot,
4390 struct btrfs_file_extent_item);
4391 fi = (struct btrfs_file_extent_item *)(
4392 (unsigned long)fi - size_diff);
4393
4394 if (btrfs_file_extent_type(leaf, fi) ==
4395 BTRFS_FILE_EXTENT_INLINE) {
4396 ptr = btrfs_item_ptr_offset(leaf, slot);
4397 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004398 (unsigned long)fi,
4399 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004400 disk_bytenr));
4401 }
4402 }
4403
4404 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4405 data_end + size_diff, btrfs_leaf_data(leaf) +
4406 data_end, old_data_start - data_end);
4407
4408 offset = btrfs_disk_key_offset(&disk_key);
4409 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4410 btrfs_set_item_key(leaf, &disk_key, slot);
4411 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004412 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004413 }
Chris Mason5f39d392007-10-15 16:14:19 -04004414
4415 item = btrfs_item_nr(leaf, slot);
4416 btrfs_set_item_size(leaf, item, new_size);
4417 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004418
Chris Mason5f39d392007-10-15 16:14:19 -04004419 if (btrfs_leaf_free_space(root, leaf) < 0) {
4420 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004421 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004422 }
Chris Masonb18c6682007-04-17 13:26:50 -04004423}
4424
Chris Masond352ac62008-09-29 15:18:18 -04004425/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004426 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004427 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004428void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004429 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004430{
Chris Mason6567e832007-04-16 09:22:45 -04004431 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004432 struct extent_buffer *leaf;
4433 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004434 u32 nritems;
4435 unsigned int data_end;
4436 unsigned int old_data;
4437 unsigned int old_size;
4438 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004439 struct btrfs_map_token token;
4440
4441 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004442
Chris Mason5f39d392007-10-15 16:14:19 -04004443 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004444
Chris Mason5f39d392007-10-15 16:14:19 -04004445 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004446 data_end = leaf_data_end(root, leaf);
4447
Chris Mason5f39d392007-10-15 16:14:19 -04004448 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4449 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004450 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004451 }
Chris Mason6567e832007-04-16 09:22:45 -04004452 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004453 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004454
4455 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004456 if (slot >= nritems) {
4457 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004458 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4459 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004460 BUG_ON(1);
4461 }
Chris Mason6567e832007-04-16 09:22:45 -04004462
4463 /*
4464 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4465 */
4466 /* first correct the data pointers */
4467 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004468 u32 ioff;
4469 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004470
Chris Masoncfed81a2012-03-03 07:40:03 -05004471 ioff = btrfs_token_item_offset(leaf, item, &token);
4472 btrfs_set_token_item_offset(leaf, item,
4473 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004474 }
Chris Mason5f39d392007-10-15 16:14:19 -04004475
Chris Mason6567e832007-04-16 09:22:45 -04004476 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004477 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004478 data_end - data_size, btrfs_leaf_data(leaf) +
4479 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004480
Chris Mason6567e832007-04-16 09:22:45 -04004481 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004482 old_size = btrfs_item_size_nr(leaf, slot);
4483 item = btrfs_item_nr(leaf, slot);
4484 btrfs_set_item_size(leaf, item, old_size + data_size);
4485 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004486
Chris Mason5f39d392007-10-15 16:14:19 -04004487 if (btrfs_leaf_free_space(root, leaf) < 0) {
4488 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004489 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004490 }
Chris Mason6567e832007-04-16 09:22:45 -04004491}
4492
Chris Mason74123bd2007-02-02 11:05:29 -05004493/*
Chris Mason44871b12009-03-13 10:04:31 -04004494 * this is a helper for btrfs_insert_empty_items, the main goal here is
4495 * to save stack depth by doing the bulk of the work in a function
4496 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004497 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004498void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004499 struct btrfs_key *cpu_key, u32 *data_size,
4500 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004501{
Chris Mason5f39d392007-10-15 16:14:19 -04004502 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004503 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004504 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004505 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004506 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004507 struct extent_buffer *leaf;
4508 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004509 struct btrfs_map_token token;
4510
4511 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004512
Chris Mason5f39d392007-10-15 16:14:19 -04004513 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004514 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004515
Chris Mason5f39d392007-10-15 16:14:19 -04004516 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004517 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004518
Chris Masonf25956c2008-09-12 15:32:53 -04004519 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004520 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004521 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004522 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004523 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004524 }
Chris Mason5f39d392007-10-15 16:14:19 -04004525
Chris Masonbe0e5c02007-01-26 15:51:26 -05004526 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004527 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004528
Chris Mason5f39d392007-10-15 16:14:19 -04004529 if (old_data < data_end) {
4530 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004531 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004532 slot, old_data, data_end);
4533 BUG_ON(1);
4534 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004535 /*
4536 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4537 */
4538 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004539 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004540 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004541
Chris Mason5f39d392007-10-15 16:14:19 -04004542 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004543 ioff = btrfs_token_item_offset(leaf, item, &token);
4544 btrfs_set_token_item_offset(leaf, item,
4545 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004546 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004547 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004548 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004549 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004550 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004551
4552 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004553 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004554 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004555 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004556 data_end = old_data;
4557 }
Chris Mason5f39d392007-10-15 16:14:19 -04004558
Chris Mason62e27492007-03-15 12:56:47 -04004559 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004560 for (i = 0; i < nr; i++) {
4561 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4562 btrfs_set_item_key(leaf, &disk_key, slot + i);
4563 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004564 btrfs_set_token_item_offset(leaf, item,
4565 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004566 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004567 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004568 }
Chris Mason44871b12009-03-13 10:04:31 -04004569
Chris Mason9c583092008-01-29 15:15:18 -05004570 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004571
Chris Mason5a01a2e2008-01-30 11:43:54 -05004572 if (slot == 0) {
4573 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004574 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004575 }
Chris Masonb9473432009-03-13 11:00:37 -04004576 btrfs_unlock_up_safe(path, 1);
4577 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004578
Chris Mason5f39d392007-10-15 16:14:19 -04004579 if (btrfs_leaf_free_space(root, leaf) < 0) {
4580 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004581 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004582 }
Chris Mason44871b12009-03-13 10:04:31 -04004583}
4584
4585/*
4586 * Given a key and some data, insert items into the tree.
4587 * This does all the path init required, making room in the tree if needed.
4588 */
4589int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4590 struct btrfs_root *root,
4591 struct btrfs_path *path,
4592 struct btrfs_key *cpu_key, u32 *data_size,
4593 int nr)
4594{
Chris Mason44871b12009-03-13 10:04:31 -04004595 int ret = 0;
4596 int slot;
4597 int i;
4598 u32 total_size = 0;
4599 u32 total_data = 0;
4600
4601 for (i = 0; i < nr; i++)
4602 total_data += data_size[i];
4603
4604 total_size = total_data + (nr * sizeof(struct btrfs_item));
4605 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4606 if (ret == 0)
4607 return -EEXIST;
4608 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004609 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004610
Chris Mason44871b12009-03-13 10:04:31 -04004611 slot = path->slots[0];
4612 BUG_ON(slot < 0);
4613
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004614 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004615 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004616 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004617}
4618
4619/*
4620 * Given a key and some data, insert an item into the tree.
4621 * This does all the path init required, making room in the tree if needed.
4622 */
Chris Masone089f052007-03-16 16:20:31 -04004623int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4624 *root, struct btrfs_key *cpu_key, void *data, u32
4625 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004626{
4627 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004628 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004629 struct extent_buffer *leaf;
4630 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004631
Chris Mason2c90e5d2007-04-02 10:50:19 -04004632 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004633 if (!path)
4634 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004635 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004636 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004637 leaf = path->nodes[0];
4638 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4639 write_extent_buffer(leaf, data, ptr, data_size);
4640 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004641 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004642 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004643 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004644}
4645
Chris Mason74123bd2007-02-02 11:05:29 -05004646/*
Chris Mason5de08d72007-02-24 06:24:44 -05004647 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004648 *
Chris Masond352ac62008-09-29 15:18:18 -04004649 * the tree should have been previously balanced so the deletion does not
4650 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004651 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004652static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4653 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004654{
Chris Mason5f39d392007-10-15 16:14:19 -04004655 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004656 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004657 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004658
Chris Mason5f39d392007-10-15 16:14:19 -04004659 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004660 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004661 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004662 tree_mod_log_eb_move(root->fs_info, parent, slot,
4663 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004664 memmove_extent_buffer(parent,
4665 btrfs_node_key_ptr_offset(slot),
4666 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004667 sizeof(struct btrfs_key_ptr) *
4668 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004669 } else if (level) {
4670 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4671 MOD_LOG_KEY_REMOVE);
4672 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004673 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004674
Chris Mason7518a232007-03-12 12:01:18 -04004675 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004676 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004677 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004678 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004679 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004680 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004681 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004682 struct btrfs_disk_key disk_key;
4683
4684 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004685 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004686 }
Chris Masond6025572007-03-30 14:27:56 -04004687 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004688}
4689
Chris Mason74123bd2007-02-02 11:05:29 -05004690/*
Chris Mason323ac952008-10-01 19:05:46 -04004691 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004692 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004693 *
4694 * This deletes the pointer in path->nodes[1] and frees the leaf
4695 * block extent. zero is returned if it all worked out, < 0 otherwise.
4696 *
4697 * The path must have already been setup for deleting the leaf, including
4698 * all the proper balancing. path->nodes[1] must be locked.
4699 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004700static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4701 struct btrfs_root *root,
4702 struct btrfs_path *path,
4703 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004704{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004705 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004706 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004707
Chris Mason4d081c42009-02-04 09:31:28 -05004708 /*
4709 * btrfs_free_extent is expensive, we want to make sure we
4710 * aren't holding any locks when we call it
4711 */
4712 btrfs_unlock_up_safe(path, 0);
4713
Yan, Zhengf0486c62010-05-16 10:46:25 -04004714 root_sub_used(root, leaf->len);
4715
Josef Bacik3083ee22012-03-09 16:01:49 -05004716 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004717 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004718 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004719}
4720/*
Chris Mason74123bd2007-02-02 11:05:29 -05004721 * delete the item at the leaf level in path. If that empties
4722 * the leaf, remove it from the tree
4723 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004724int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4725 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004726{
Chris Mason5f39d392007-10-15 16:14:19 -04004727 struct extent_buffer *leaf;
4728 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004729 int last_off;
4730 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004731 int ret = 0;
4732 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004733 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004734 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004735 struct btrfs_map_token token;
4736
4737 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004738
Chris Mason5f39d392007-10-15 16:14:19 -04004739 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004740 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4741
4742 for (i = 0; i < nr; i++)
4743 dsize += btrfs_item_size_nr(leaf, slot + i);
4744
Chris Mason5f39d392007-10-15 16:14:19 -04004745 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004746
Chris Mason85e21ba2008-01-29 15:11:36 -05004747 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004748 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004749
4750 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004751 data_end + dsize,
4752 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004753 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004754
Chris Mason85e21ba2008-01-29 15:11:36 -05004755 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004756 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004757
Chris Mason5f39d392007-10-15 16:14:19 -04004758 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004759 ioff = btrfs_token_item_offset(leaf, item, &token);
4760 btrfs_set_token_item_offset(leaf, item,
4761 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004762 }
Chris Masondb945352007-10-15 16:15:53 -04004763
Chris Mason5f39d392007-10-15 16:14:19 -04004764 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004765 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004766 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004767 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004768 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004769 btrfs_set_header_nritems(leaf, nritems - nr);
4770 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004771
Chris Mason74123bd2007-02-02 11:05:29 -05004772 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004773 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004774 if (leaf == root->node) {
4775 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004776 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004777 btrfs_set_path_blocking(path);
4778 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004779 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004780 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004781 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004782 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004783 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004784 struct btrfs_disk_key disk_key;
4785
4786 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004787 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004788 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004789
Chris Mason74123bd2007-02-02 11:05:29 -05004790 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004791 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004792 /* push_leaf_left fixes the path.
4793 * make sure the path still points to our leaf
4794 * for possible call to del_ptr below
4795 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004796 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004797 extent_buffer_get(leaf);
4798
Chris Masonb9473432009-03-13 11:00:37 -04004799 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004800 wret = push_leaf_left(trans, root, path, 1, 1,
4801 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004802 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004803 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004804
4805 if (path->nodes[0] == leaf &&
4806 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004807 wret = push_leaf_right(trans, root, path, 1,
4808 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004809 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004810 ret = wret;
4811 }
Chris Mason5f39d392007-10-15 16:14:19 -04004812
4813 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004814 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004815 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004816 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004817 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004818 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004819 /* if we're still in the path, make sure
4820 * we're dirty. Otherwise, one of the
4821 * push_leaf functions must have already
4822 * dirtied this buffer
4823 */
4824 if (path->nodes[0] == leaf)
4825 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004826 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004827 }
Chris Masond5719762007-03-23 10:01:08 -04004828 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004829 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004830 }
4831 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004832 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004833}
4834
Chris Mason97571fd2007-02-24 13:39:08 -05004835/*
Chris Mason925baed2008-06-25 16:01:30 -04004836 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004837 * returns 0 if it found something or 1 if there are no lesser leaves.
4838 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004839 *
4840 * This may release the path, and so you may lose any locks held at the
4841 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004842 */
4843int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4844{
Chris Mason925baed2008-06-25 16:01:30 -04004845 struct btrfs_key key;
4846 struct btrfs_disk_key found_key;
4847 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004848
Chris Mason925baed2008-06-25 16:01:30 -04004849 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004850
Chris Mason925baed2008-06-25 16:01:30 -04004851 if (key.offset > 0)
4852 key.offset--;
4853 else if (key.type > 0)
4854 key.type--;
4855 else if (key.objectid > 0)
4856 key.objectid--;
4857 else
4858 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004859
David Sterbab3b4aa72011-04-21 01:20:15 +02004860 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004861 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4862 if (ret < 0)
4863 return ret;
4864 btrfs_item_key(path->nodes[0], &found_key, 0);
4865 ret = comp_keys(&found_key, &key);
4866 if (ret < 0)
4867 return 0;
4868 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004869}
4870
Chris Mason3f157a22008-06-25 16:01:31 -04004871/*
4872 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004873 * for nodes or leaves that are have a minimum transaction id.
4874 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004875 *
4876 * This does not cow, but it does stuff the starting key it finds back
4877 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4878 * key and get a writable path.
4879 *
4880 * This does lock as it descends, and path->keep_locks should be set
4881 * to 1 by the caller.
4882 *
4883 * This honors path->lowest_level to prevent descent past a given level
4884 * of the tree.
4885 *
Chris Masond352ac62008-09-29 15:18:18 -04004886 * min_trans indicates the oldest transaction that you are interested
4887 * in walking through. Any nodes or leaves older than min_trans are
4888 * skipped over (without reading them).
4889 *
Chris Mason3f157a22008-06-25 16:01:31 -04004890 * returns zero if something useful was found, < 0 on error and 1 if there
4891 * was nothing in the tree that matched the search criteria.
4892 */
4893int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004894 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004895 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004896 u64 min_trans)
4897{
4898 struct extent_buffer *cur;
4899 struct btrfs_key found_key;
4900 int slot;
Yan96524802008-07-24 12:19:49 -04004901 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004902 u32 nritems;
4903 int level;
4904 int ret = 1;
4905
Chris Mason934d3752008-12-08 16:43:10 -05004906 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004907again:
Chris Masonbd681512011-07-16 15:23:14 -04004908 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004909 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004910 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004911 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004912 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004913
4914 if (btrfs_header_generation(cur) < min_trans) {
4915 ret = 1;
4916 goto out;
4917 }
Chris Masond3977122009-01-05 21:25:51 -05004918 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004919 nritems = btrfs_header_nritems(cur);
4920 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004921 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004922
Chris Mason323ac952008-10-01 19:05:46 -04004923 /* at the lowest level, we're done, setup the path and exit */
4924 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004925 if (slot >= nritems)
4926 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004927 ret = 0;
4928 path->slots[level] = slot;
4929 btrfs_item_key_to_cpu(cur, &found_key, slot);
4930 goto out;
4931 }
Yan96524802008-07-24 12:19:49 -04004932 if (sret && slot > 0)
4933 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004934 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004935 * check this node pointer against the min_trans parameters.
4936 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004937 */
Chris Masond3977122009-01-05 21:25:51 -05004938 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004939 u64 blockptr;
4940 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004941
Chris Mason3f157a22008-06-25 16:01:31 -04004942 blockptr = btrfs_node_blockptr(cur, slot);
4943 gen = btrfs_node_ptr_generation(cur, slot);
4944 if (gen < min_trans) {
4945 slot++;
4946 continue;
4947 }
Eric Sandeende78b512013-01-31 18:21:12 +00004948 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004949 }
Chris Masone02119d2008-09-05 16:13:11 -04004950find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004951 /*
4952 * we didn't find a candidate key in this node, walk forward
4953 * and find another one
4954 */
4955 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004956 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004957 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004958 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004959 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004960 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004961 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004962 goto again;
4963 } else {
4964 goto out;
4965 }
4966 }
4967 /* save our key for returning back */
4968 btrfs_node_key_to_cpu(cur, &found_key, slot);
4969 path->slots[level] = slot;
4970 if (level == path->lowest_level) {
4971 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004972 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004973 goto out;
4974 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004975 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004976 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004977 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004978
Chris Masonbd681512011-07-16 15:23:14 -04004979 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004980
Chris Masonbd681512011-07-16 15:23:14 -04004981 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004982 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004983 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004984 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004985 }
4986out:
4987 if (ret == 0)
4988 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004989 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004990 return ret;
4991}
4992
Alexander Block70698302012-06-05 21:07:48 +02004993static void tree_move_down(struct btrfs_root *root,
4994 struct btrfs_path *path,
4995 int *level, int root_level)
4996{
Chris Mason74dd17f2012-08-07 16:25:13 -04004997 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004998 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4999 path->slots[*level]);
5000 path->slots[*level - 1] = 0;
5001 (*level)--;
5002}
5003
5004static int tree_move_next_or_upnext(struct btrfs_root *root,
5005 struct btrfs_path *path,
5006 int *level, int root_level)
5007{
5008 int ret = 0;
5009 int nritems;
5010 nritems = btrfs_header_nritems(path->nodes[*level]);
5011
5012 path->slots[*level]++;
5013
Chris Mason74dd17f2012-08-07 16:25:13 -04005014 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005015 if (*level == root_level)
5016 return -1;
5017
5018 /* move upnext */
5019 path->slots[*level] = 0;
5020 free_extent_buffer(path->nodes[*level]);
5021 path->nodes[*level] = NULL;
5022 (*level)++;
5023 path->slots[*level]++;
5024
5025 nritems = btrfs_header_nritems(path->nodes[*level]);
5026 ret = 1;
5027 }
5028 return ret;
5029}
5030
5031/*
5032 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5033 * or down.
5034 */
5035static int tree_advance(struct btrfs_root *root,
5036 struct btrfs_path *path,
5037 int *level, int root_level,
5038 int allow_down,
5039 struct btrfs_key *key)
5040{
5041 int ret;
5042
5043 if (*level == 0 || !allow_down) {
5044 ret = tree_move_next_or_upnext(root, path, level, root_level);
5045 } else {
5046 tree_move_down(root, path, level, root_level);
5047 ret = 0;
5048 }
5049 if (ret >= 0) {
5050 if (*level == 0)
5051 btrfs_item_key_to_cpu(path->nodes[*level], key,
5052 path->slots[*level]);
5053 else
5054 btrfs_node_key_to_cpu(path->nodes[*level], key,
5055 path->slots[*level]);
5056 }
5057 return ret;
5058}
5059
5060static int tree_compare_item(struct btrfs_root *left_root,
5061 struct btrfs_path *left_path,
5062 struct btrfs_path *right_path,
5063 char *tmp_buf)
5064{
5065 int cmp;
5066 int len1, len2;
5067 unsigned long off1, off2;
5068
5069 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5070 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5071 if (len1 != len2)
5072 return 1;
5073
5074 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5075 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5076 right_path->slots[0]);
5077
5078 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5079
5080 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5081 if (cmp)
5082 return 1;
5083 return 0;
5084}
5085
5086#define ADVANCE 1
5087#define ADVANCE_ONLY_NEXT -1
5088
5089/*
5090 * This function compares two trees and calls the provided callback for
5091 * every changed/new/deleted item it finds.
5092 * If shared tree blocks are encountered, whole subtrees are skipped, making
5093 * the compare pretty fast on snapshotted subvolumes.
5094 *
5095 * This currently works on commit roots only. As commit roots are read only,
5096 * we don't do any locking. The commit roots are protected with transactions.
5097 * Transactions are ended and rejoined when a commit is tried in between.
5098 *
5099 * This function checks for modifications done to the trees while comparing.
5100 * If it detects a change, it aborts immediately.
5101 */
5102int btrfs_compare_trees(struct btrfs_root *left_root,
5103 struct btrfs_root *right_root,
5104 btrfs_changed_cb_t changed_cb, void *ctx)
5105{
5106 int ret;
5107 int cmp;
5108 struct btrfs_trans_handle *trans = NULL;
5109 struct btrfs_path *left_path = NULL;
5110 struct btrfs_path *right_path = NULL;
5111 struct btrfs_key left_key;
5112 struct btrfs_key right_key;
5113 char *tmp_buf = NULL;
5114 int left_root_level;
5115 int right_root_level;
5116 int left_level;
5117 int right_level;
5118 int left_end_reached;
5119 int right_end_reached;
5120 int advance_left;
5121 int advance_right;
5122 u64 left_blockptr;
5123 u64 right_blockptr;
5124 u64 left_start_ctransid;
5125 u64 right_start_ctransid;
5126 u64 ctransid;
5127
5128 left_path = btrfs_alloc_path();
5129 if (!left_path) {
5130 ret = -ENOMEM;
5131 goto out;
5132 }
5133 right_path = btrfs_alloc_path();
5134 if (!right_path) {
5135 ret = -ENOMEM;
5136 goto out;
5137 }
5138
5139 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5140 if (!tmp_buf) {
5141 ret = -ENOMEM;
5142 goto out;
5143 }
5144
5145 left_path->search_commit_root = 1;
5146 left_path->skip_locking = 1;
5147 right_path->search_commit_root = 1;
5148 right_path->skip_locking = 1;
5149
Anand Jain5f3ab902012-12-07 09:28:54 +00005150 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005151 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005152 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005153
Anand Jain5f3ab902012-12-07 09:28:54 +00005154 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005155 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005156 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005157
5158 trans = btrfs_join_transaction(left_root);
5159 if (IS_ERR(trans)) {
5160 ret = PTR_ERR(trans);
5161 trans = NULL;
5162 goto out;
5163 }
5164
5165 /*
5166 * Strategy: Go to the first items of both trees. Then do
5167 *
5168 * If both trees are at level 0
5169 * Compare keys of current items
5170 * If left < right treat left item as new, advance left tree
5171 * and repeat
5172 * If left > right treat right item as deleted, advance right tree
5173 * and repeat
5174 * If left == right do deep compare of items, treat as changed if
5175 * needed, advance both trees and repeat
5176 * If both trees are at the same level but not at level 0
5177 * Compare keys of current nodes/leafs
5178 * If left < right advance left tree and repeat
5179 * If left > right advance right tree and repeat
5180 * If left == right compare blockptrs of the next nodes/leafs
5181 * If they match advance both trees but stay at the same level
5182 * and repeat
5183 * If they don't match advance both trees while allowing to go
5184 * deeper and repeat
5185 * If tree levels are different
5186 * Advance the tree that needs it and repeat
5187 *
5188 * Advancing a tree means:
5189 * If we are at level 0, try to go to the next slot. If that's not
5190 * possible, go one level up and repeat. Stop when we found a level
5191 * where we could go to the next slot. We may at this point be on a
5192 * node or a leaf.
5193 *
5194 * If we are not at level 0 and not on shared tree blocks, go one
5195 * level deeper.
5196 *
5197 * If we are not at level 0 and on shared tree blocks, go one slot to
5198 * the right if possible or go up and right.
5199 */
5200
5201 left_level = btrfs_header_level(left_root->commit_root);
5202 left_root_level = left_level;
5203 left_path->nodes[left_level] = left_root->commit_root;
5204 extent_buffer_get(left_path->nodes[left_level]);
5205
5206 right_level = btrfs_header_level(right_root->commit_root);
5207 right_root_level = right_level;
5208 right_path->nodes[right_level] = right_root->commit_root;
5209 extent_buffer_get(right_path->nodes[right_level]);
5210
5211 if (left_level == 0)
5212 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5213 &left_key, left_path->slots[left_level]);
5214 else
5215 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5216 &left_key, left_path->slots[left_level]);
5217 if (right_level == 0)
5218 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5219 &right_key, right_path->slots[right_level]);
5220 else
5221 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5222 &right_key, right_path->slots[right_level]);
5223
5224 left_end_reached = right_end_reached = 0;
5225 advance_left = advance_right = 0;
5226
5227 while (1) {
5228 /*
5229 * We need to make sure the transaction does not get committed
5230 * while we do anything on commit roots. This means, we need to
5231 * join and leave transactions for every item that we process.
5232 */
5233 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5234 btrfs_release_path(left_path);
5235 btrfs_release_path(right_path);
5236
5237 ret = btrfs_end_transaction(trans, left_root);
5238 trans = NULL;
5239 if (ret < 0)
5240 goto out;
5241 }
5242 /* now rejoin the transaction */
5243 if (!trans) {
5244 trans = btrfs_join_transaction(left_root);
5245 if (IS_ERR(trans)) {
5246 ret = PTR_ERR(trans);
5247 trans = NULL;
5248 goto out;
5249 }
5250
Anand Jain5f3ab902012-12-07 09:28:54 +00005251 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005252 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005253 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005254 if (ctransid != left_start_ctransid)
5255 left_start_ctransid = 0;
5256
Anand Jain5f3ab902012-12-07 09:28:54 +00005257 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005258 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005259 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005260 if (ctransid != right_start_ctransid)
5261 right_start_ctransid = 0;
5262
5263 if (!left_start_ctransid || !right_start_ctransid) {
5264 WARN(1, KERN_WARNING
5265 "btrfs: btrfs_compare_tree detected "
5266 "a change in one of the trees while "
5267 "iterating. This is probably a "
5268 "bug.\n");
5269 ret = -EIO;
5270 goto out;
5271 }
5272
5273 /*
5274 * the commit root may have changed, so start again
5275 * where we stopped
5276 */
5277 left_path->lowest_level = left_level;
5278 right_path->lowest_level = right_level;
5279 ret = btrfs_search_slot(NULL, left_root,
5280 &left_key, left_path, 0, 0);
5281 if (ret < 0)
5282 goto out;
5283 ret = btrfs_search_slot(NULL, right_root,
5284 &right_key, right_path, 0, 0);
5285 if (ret < 0)
5286 goto out;
5287 }
5288
5289 if (advance_left && !left_end_reached) {
5290 ret = tree_advance(left_root, left_path, &left_level,
5291 left_root_level,
5292 advance_left != ADVANCE_ONLY_NEXT,
5293 &left_key);
5294 if (ret < 0)
5295 left_end_reached = ADVANCE;
5296 advance_left = 0;
5297 }
5298 if (advance_right && !right_end_reached) {
5299 ret = tree_advance(right_root, right_path, &right_level,
5300 right_root_level,
5301 advance_right != ADVANCE_ONLY_NEXT,
5302 &right_key);
5303 if (ret < 0)
5304 right_end_reached = ADVANCE;
5305 advance_right = 0;
5306 }
5307
5308 if (left_end_reached && right_end_reached) {
5309 ret = 0;
5310 goto out;
5311 } else if (left_end_reached) {
5312 if (right_level == 0) {
5313 ret = changed_cb(left_root, right_root,
5314 left_path, right_path,
5315 &right_key,
5316 BTRFS_COMPARE_TREE_DELETED,
5317 ctx);
5318 if (ret < 0)
5319 goto out;
5320 }
5321 advance_right = ADVANCE;
5322 continue;
5323 } else if (right_end_reached) {
5324 if (left_level == 0) {
5325 ret = changed_cb(left_root, right_root,
5326 left_path, right_path,
5327 &left_key,
5328 BTRFS_COMPARE_TREE_NEW,
5329 ctx);
5330 if (ret < 0)
5331 goto out;
5332 }
5333 advance_left = ADVANCE;
5334 continue;
5335 }
5336
5337 if (left_level == 0 && right_level == 0) {
5338 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5339 if (cmp < 0) {
5340 ret = changed_cb(left_root, right_root,
5341 left_path, right_path,
5342 &left_key,
5343 BTRFS_COMPARE_TREE_NEW,
5344 ctx);
5345 if (ret < 0)
5346 goto out;
5347 advance_left = ADVANCE;
5348 } else if (cmp > 0) {
5349 ret = changed_cb(left_root, right_root,
5350 left_path, right_path,
5351 &right_key,
5352 BTRFS_COMPARE_TREE_DELETED,
5353 ctx);
5354 if (ret < 0)
5355 goto out;
5356 advance_right = ADVANCE;
5357 } else {
Chris Mason74dd17f2012-08-07 16:25:13 -04005358 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005359 ret = tree_compare_item(left_root, left_path,
5360 right_path, tmp_buf);
5361 if (ret) {
Chris Mason74dd17f2012-08-07 16:25:13 -04005362 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005363 ret = changed_cb(left_root, right_root,
5364 left_path, right_path,
5365 &left_key,
5366 BTRFS_COMPARE_TREE_CHANGED,
5367 ctx);
5368 if (ret < 0)
5369 goto out;
5370 }
5371 advance_left = ADVANCE;
5372 advance_right = ADVANCE;
5373 }
5374 } else if (left_level == right_level) {
5375 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5376 if (cmp < 0) {
5377 advance_left = ADVANCE;
5378 } else if (cmp > 0) {
5379 advance_right = ADVANCE;
5380 } else {
5381 left_blockptr = btrfs_node_blockptr(
5382 left_path->nodes[left_level],
5383 left_path->slots[left_level]);
5384 right_blockptr = btrfs_node_blockptr(
5385 right_path->nodes[right_level],
5386 right_path->slots[right_level]);
5387 if (left_blockptr == right_blockptr) {
5388 /*
5389 * As we're on a shared block, don't
5390 * allow to go deeper.
5391 */
5392 advance_left = ADVANCE_ONLY_NEXT;
5393 advance_right = ADVANCE_ONLY_NEXT;
5394 } else {
5395 advance_left = ADVANCE;
5396 advance_right = ADVANCE;
5397 }
5398 }
5399 } else if (left_level < right_level) {
5400 advance_right = ADVANCE;
5401 } else {
5402 advance_left = ADVANCE;
5403 }
5404 }
5405
5406out:
5407 btrfs_free_path(left_path);
5408 btrfs_free_path(right_path);
5409 kfree(tmp_buf);
5410
5411 if (trans) {
5412 if (!ret)
5413 ret = btrfs_end_transaction(trans, left_root);
5414 else
5415 btrfs_end_transaction(trans, left_root);
5416 }
5417
5418 return ret;
5419}
5420
Chris Mason3f157a22008-06-25 16:01:31 -04005421/*
5422 * this is similar to btrfs_next_leaf, but does not try to preserve
5423 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005424 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005425 *
5426 * 0 is returned if another key is found, < 0 if there are any errors
5427 * and 1 is returned if there are no higher keys in the tree
5428 *
5429 * path->keep_locks should be set to 1 on the search made before
5430 * calling this function.
5431 */
Chris Masone7a84562008-06-25 16:01:31 -04005432int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005433 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005434{
Chris Masone7a84562008-06-25 16:01:31 -04005435 int slot;
5436 struct extent_buffer *c;
5437
Chris Mason934d3752008-12-08 16:43:10 -05005438 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005439 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005440 if (!path->nodes[level])
5441 return 1;
5442
5443 slot = path->slots[level] + 1;
5444 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005445next:
Chris Masone7a84562008-06-25 16:01:31 -04005446 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005447 int ret;
5448 int orig_lowest;
5449 struct btrfs_key cur_key;
5450 if (level + 1 >= BTRFS_MAX_LEVEL ||
5451 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005452 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005453
5454 if (path->locks[level + 1]) {
5455 level++;
5456 continue;
5457 }
5458
5459 slot = btrfs_header_nritems(c) - 1;
5460 if (level == 0)
5461 btrfs_item_key_to_cpu(c, &cur_key, slot);
5462 else
5463 btrfs_node_key_to_cpu(c, &cur_key, slot);
5464
5465 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005466 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005467 path->lowest_level = level;
5468 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5469 0, 0);
5470 path->lowest_level = orig_lowest;
5471 if (ret < 0)
5472 return ret;
5473
5474 c = path->nodes[level];
5475 slot = path->slots[level];
5476 if (ret == 0)
5477 slot++;
5478 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005479 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005480
Chris Masone7a84562008-06-25 16:01:31 -04005481 if (level == 0)
5482 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005483 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005484 u64 gen = btrfs_node_ptr_generation(c, slot);
5485
Chris Mason3f157a22008-06-25 16:01:31 -04005486 if (gen < min_trans) {
5487 slot++;
5488 goto next;
5489 }
Chris Masone7a84562008-06-25 16:01:31 -04005490 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005491 }
Chris Masone7a84562008-06-25 16:01:31 -04005492 return 0;
5493 }
5494 return 1;
5495}
5496
Chris Mason7bb86312007-12-11 09:25:06 -05005497/*
Chris Mason925baed2008-06-25 16:01:30 -04005498 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005499 * returns 0 if it found something or 1 if there are no greater leaves.
5500 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005501 */
Chris Mason234b63a2007-03-13 10:46:10 -04005502int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005503{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005504 return btrfs_next_old_leaf(root, path, 0);
5505}
5506
5507int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5508 u64 time_seq)
5509{
Chris Masond97e63b2007-02-20 16:40:44 -05005510 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005511 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005512 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005513 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005514 struct btrfs_key key;
5515 u32 nritems;
5516 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005517 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005518 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005519
5520 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005521 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005522 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005523
Chris Mason8e73f272009-04-03 10:14:18 -04005524 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5525again:
5526 level = 1;
5527 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005528 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005529 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005530
Chris Masona2135012008-06-25 16:01:30 -04005531 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005532 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005533
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005534 if (time_seq)
5535 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5536 else
5537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005538 path->keep_locks = 0;
5539
5540 if (ret < 0)
5541 return ret;
5542
Chris Masona2135012008-06-25 16:01:30 -04005543 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005544 /*
5545 * by releasing the path above we dropped all our locks. A balance
5546 * could have added more items next to the key that used to be
5547 * at the very end of the block. So, check again here and
5548 * advance the path if there are now more items available.
5549 */
Chris Masona2135012008-06-25 16:01:30 -04005550 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005551 if (ret == 0)
5552 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005553 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005554 goto done;
5555 }
Chris Masond97e63b2007-02-20 16:40:44 -05005556
Chris Masond3977122009-01-05 21:25:51 -05005557 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005558 if (!path->nodes[level]) {
5559 ret = 1;
5560 goto done;
5561 }
Chris Mason5f39d392007-10-15 16:14:19 -04005562
Chris Masond97e63b2007-02-20 16:40:44 -05005563 slot = path->slots[level] + 1;
5564 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005565 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005566 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005567 if (level == BTRFS_MAX_LEVEL) {
5568 ret = 1;
5569 goto done;
5570 }
Chris Masond97e63b2007-02-20 16:40:44 -05005571 continue;
5572 }
Chris Mason5f39d392007-10-15 16:14:19 -04005573
Chris Mason925baed2008-06-25 16:01:30 -04005574 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005575 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005576 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005577 }
Chris Mason5f39d392007-10-15 16:14:19 -04005578
Chris Mason8e73f272009-04-03 10:14:18 -04005579 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005580 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005581 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005582 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005583 if (ret == -EAGAIN)
5584 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005585
Chris Mason76a05b32009-05-14 13:24:30 -04005586 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005587 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005588 goto done;
5589 }
5590
Chris Mason5cd57b22008-06-25 16:01:30 -04005591 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005592 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005593 if (!ret && time_seq) {
5594 /*
5595 * If we don't get the lock, we may be racing
5596 * with push_leaf_left, holding that lock while
5597 * itself waiting for the leaf we've currently
5598 * locked. To solve this situation, we give up
5599 * on our lock and cycle.
5600 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005601 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005602 btrfs_release_path(path);
5603 cond_resched();
5604 goto again;
5605 }
Chris Mason8e73f272009-04-03 10:14:18 -04005606 if (!ret) {
5607 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005608 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005609 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005610 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005611 }
Chris Mason31533fb2011-07-26 16:01:59 -04005612 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005613 }
Chris Masond97e63b2007-02-20 16:40:44 -05005614 break;
5615 }
5616 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005617 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005618 level--;
5619 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005620 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005621 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005622
Chris Mason5f39d392007-10-15 16:14:19 -04005623 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005624 path->nodes[level] = next;
5625 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005626 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005627 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005628 if (!level)
5629 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005630
Chris Mason8e73f272009-04-03 10:14:18 -04005631 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005632 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005633 if (ret == -EAGAIN)
5634 goto again;
5635
Chris Mason76a05b32009-05-14 13:24:30 -04005636 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005637 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005638 goto done;
5639 }
5640
Chris Mason5cd57b22008-06-25 16:01:30 -04005641 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005642 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005643 if (!ret) {
5644 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005645 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005646 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005647 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005648 }
Chris Mason31533fb2011-07-26 16:01:59 -04005649 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005650 }
Chris Masond97e63b2007-02-20 16:40:44 -05005651 }
Chris Mason8e73f272009-04-03 10:14:18 -04005652 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005653done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005654 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005655 path->leave_spinning = old_spinning;
5656 if (!old_spinning)
5657 btrfs_set_path_blocking(path);
5658
5659 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005660}
Chris Mason0b86a832008-03-24 15:01:56 -04005661
Chris Mason3f157a22008-06-25 16:01:31 -04005662/*
5663 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5664 * searching until it gets past min_objectid or finds an item of 'type'
5665 *
5666 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5667 */
Chris Mason0b86a832008-03-24 15:01:56 -04005668int btrfs_previous_item(struct btrfs_root *root,
5669 struct btrfs_path *path, u64 min_objectid,
5670 int type)
5671{
5672 struct btrfs_key found_key;
5673 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005674 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005675 int ret;
5676
Chris Masond3977122009-01-05 21:25:51 -05005677 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005678 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005679 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005680 ret = btrfs_prev_leaf(root, path);
5681 if (ret != 0)
5682 return ret;
5683 } else {
5684 path->slots[0]--;
5685 }
5686 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005687 nritems = btrfs_header_nritems(leaf);
5688 if (nritems == 0)
5689 return 1;
5690 if (path->slots[0] == nritems)
5691 path->slots[0]--;
5692
Chris Mason0b86a832008-03-24 15:01:56 -04005693 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005694 if (found_key.objectid < min_objectid)
5695 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005696 if (found_key.type == type)
5697 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005698 if (found_key.objectid == min_objectid &&
5699 found_key.type < type)
5700 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005701 }
5702 return 1;
5703}