blob: 2f10e12ae94c9967390c35d3903d885a648d750e [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);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +000042static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
Jan Schmidtf2304752012-05-26 11:43:17 +020043 struct extent_buffer *eb);
Chris Masond97e63b2007-02-20 16:40:44 -050044
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050048 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040049 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040050}
51
Chris Masonb4ce94d2009-02-04 09:25:08 -050052/*
53 * set all locked nodes in the path to blocking locks. This should
54 * be done before scheduling
55 */
56noinline void btrfs_set_path_blocking(struct btrfs_path *p)
57{
58 int i;
59 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040060 if (!p->nodes[i] || !p->locks[i])
61 continue;
62 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
63 if (p->locks[i] == BTRFS_READ_LOCK)
64 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
65 else if (p->locks[i] == BTRFS_WRITE_LOCK)
66 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050067 }
68}
69
70/*
71 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050072 *
73 * held is used to keep lockdep happy, when lockdep is enabled
74 * we set held to a blocking lock before we go around and
75 * retake all the spinlocks in the path. You can safely use NULL
76 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050077 */
Chris Mason4008c042009-02-12 14:09:45 -050078noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040079 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050080{
81 int i;
Chris Mason4008c042009-02-12 14:09:45 -050082
83#ifdef CONFIG_DEBUG_LOCK_ALLOC
84 /* lockdep really cares that we take all of these spinlocks
85 * in the right order. If any of the locks in the path are not
86 * currently blocking, it is going to complain. So, make really
87 * really sure by forcing the path to blocking before we clear
88 * the path blocking.
89 */
Chris Masonbd681512011-07-16 15:23:14 -040090 if (held) {
91 btrfs_set_lock_blocking_rw(held, held_rw);
92 if (held_rw == BTRFS_WRITE_LOCK)
93 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
94 else if (held_rw == BTRFS_READ_LOCK)
95 held_rw = BTRFS_READ_LOCK_BLOCKING;
96 }
Chris Mason4008c042009-02-12 14:09:45 -050097 btrfs_set_path_blocking(p);
98#endif
99
100 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400101 if (p->nodes[i] && p->locks[i]) {
102 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
103 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
104 p->locks[i] = BTRFS_WRITE_LOCK;
105 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
106 p->locks[i] = BTRFS_READ_LOCK;
107 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500108 }
Chris Mason4008c042009-02-12 14:09:45 -0500109
110#ifdef CONFIG_DEBUG_LOCK_ALLOC
111 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400112 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500113#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400117void btrfs_free_path(struct btrfs_path *p)
118{
Jesper Juhlff175d52010-12-25 21:22:30 +0000119 if (!p)
120 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200121 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400122 kmem_cache_free(btrfs_path_cachep, p);
123}
124
Chris Masond352ac62008-09-29 15:18:18 -0400125/*
126 * path release drops references on the extent buffers in the path
127 * and it drops any locks held by this path
128 *
129 * It is safe to call this on paths that no locks or extent buffers held.
130 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200131noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500132{
133 int i;
Chris Masona2135012008-06-25 16:01:30 -0400134
Chris Mason234b63a2007-03-13 10:46:10 -0400135 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400136 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500137 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400138 continue;
139 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400140 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400141 p->locks[i] = 0;
142 }
Chris Mason5f39d392007-10-15 16:14:19 -0400143 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400144 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500145 }
146}
147
Chris Masond352ac62008-09-29 15:18:18 -0400148/*
149 * safely gets a reference on the root node of a tree. A lock
150 * is not taken, so a concurrent writer may put a different node
151 * at the root of the tree. See btrfs_lock_root_node for the
152 * looping required.
153 *
154 * The extent buffer returned by this has a reference taken, so
155 * it won't disappear. It may stop being the root of the tree
156 * at any time because there are no locks held.
157 */
Chris Mason925baed2008-06-25 16:01:30 -0400158struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
159{
160 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400161
Josef Bacik3083ee22012-03-09 16:01:49 -0500162 while (1) {
163 rcu_read_lock();
164 eb = rcu_dereference(root->node);
165
166 /*
167 * RCU really hurts here, we could free up the root node because
168 * it was cow'ed but we may not get the new root node yet so do
169 * the inc_not_zero dance and if it doesn't work then
170 * synchronize_rcu and try again.
171 */
172 if (atomic_inc_not_zero(&eb->refs)) {
173 rcu_read_unlock();
174 break;
175 }
176 rcu_read_unlock();
177 synchronize_rcu();
178 }
Chris Mason925baed2008-06-25 16:01:30 -0400179 return eb;
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/* loop around taking references on and locking the root node of the
183 * tree until you end up with a lock on the root. A locked buffer
184 * is returned, with a reference held.
185 */
Chris Mason925baed2008-06-25 16:01:30 -0400186struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
187{
188 struct extent_buffer *eb;
189
Chris Masond3977122009-01-05 21:25:51 -0500190 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400191 eb = btrfs_root_node(root);
192 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400193 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400194 break;
Chris Mason925baed2008-06-25 16:01:30 -0400195 btrfs_tree_unlock(eb);
196 free_extent_buffer(eb);
197 }
198 return eb;
199}
200
Chris Masonbd681512011-07-16 15:23:14 -0400201/* loop around taking references on and locking the root node of the
202 * tree until you end up with a lock on the root. A locked buffer
203 * is returned, with a reference held.
204 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000205static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400206{
207 struct extent_buffer *eb;
208
209 while (1) {
210 eb = btrfs_root_node(root);
211 btrfs_tree_read_lock(eb);
212 if (eb == root->node)
213 break;
214 btrfs_tree_read_unlock(eb);
215 free_extent_buffer(eb);
216 }
217 return eb;
218}
219
Chris Masond352ac62008-09-29 15:18:18 -0400220/* cowonly root (everything not a reference counted cow subvolume), just get
221 * put onto a simple dirty list. transaction.c walks this to make sure they
222 * get properly updated on disk.
223 */
Chris Mason0b86a832008-03-24 15:01:56 -0400224static void add_root_to_dirty_list(struct btrfs_root *root)
225{
Chris Masone5846fc2012-05-03 12:08:48 -0400226 spin_lock(&root->fs_info->trans_lock);
Miao Xie27cdeb72014-04-02 19:51:05 +0800227 if (test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state) &&
228 list_empty(&root->dirty_list)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400229 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
Miao Xie27cdeb72014-04-02 19:51:05 +0800250 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
251 trans->transid != root->fs_info->running_transaction->transid);
252 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
253 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500254
255 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400256 if (level == 0)
257 btrfs_item_key(buf, &disk_key, 0);
258 else
259 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400260
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
262 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200263 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400264 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 return PTR_ERR(cow);
266
267 copy_extent_buffer(cow, buf, 0, 0, cow->len);
268 btrfs_set_header_bytenr(cow, cow->start);
269 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400270 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
271 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
272 BTRFS_HEADER_FLAG_RELOC);
273 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
274 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
275 else
276 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500277
Ross Kirk0a4e5582013-09-24 10:12:38 +0100278 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500279 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.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000479 *
480 * Note: must be called with write lock (tree_mod_log_write_lock).
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200481 */
482static noinline int
483__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
484{
485 struct rb_root *tm_root;
486 struct rb_node **new;
487 struct rb_node *parent = NULL;
488 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200489
Josef Bacikc8cc6342013-07-01 16:18:19 -0400490 BUG_ON(!tm);
491
Josef Bacikc8cc6342013-07-01 16:18:19 -0400492 spin_lock(&fs_info->tree_mod_seq_lock);
493 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
494 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200495
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200496 tm_root = &fs_info->tree_mod_log;
497 new = &tm_root->rb_node;
498 while (*new) {
499 cur = container_of(*new, struct tree_mod_elem, node);
500 parent = *new;
501 if (cur->index < tm->index)
502 new = &((*new)->rb_left);
503 else if (cur->index > tm->index)
504 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200505 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200506 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200507 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200508 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000509 else
510 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200511 }
512
513 rb_link_node(&tm->node, parent, new);
514 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000515 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200516}
517
Jan Schmidt097b8a72012-06-21 11:08:04 +0200518/*
519 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
520 * returns zero with the tree_mod_log_lock acquired. The caller must hold
521 * this until all tree mod log insertions are recorded in the rb tree and then
522 * call tree_mod_log_write_unlock() to release.
523 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200524static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
525 struct extent_buffer *eb) {
526 smp_mb();
527 if (list_empty(&(fs_info)->tree_mod_seq_list))
528 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200529 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200530 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000531
532 tree_mod_log_write_lock(fs_info);
533 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
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
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000541/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
542static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
543 struct extent_buffer *eb)
544{
545 smp_mb();
546 if (list_empty(&(fs_info)->tree_mod_seq_list))
547 return 0;
548 if (eb && btrfs_header_level(eb) == 0)
549 return 0;
550
551 return 1;
552}
553
554static struct tree_mod_elem *
555alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
556 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200557{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200558 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200559
Josef Bacikc8cc6342013-07-01 16:18:19 -0400560 tm = kzalloc(sizeof(*tm), flags);
561 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000562 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200563
564 tm->index = eb->start >> PAGE_CACHE_SHIFT;
565 if (op != MOD_LOG_KEY_ADD) {
566 btrfs_node_key(eb, &tm->key, slot);
567 tm->blockptr = btrfs_node_blockptr(eb, slot);
568 }
569 tm->op = op;
570 tm->slot = slot;
571 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000572 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200573
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000574 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200575}
576
577static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400578tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
579 struct extent_buffer *eb, int slot,
580 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200581{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000582 struct tree_mod_elem *tm;
583 int ret;
584
585 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200586 return 0;
587
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000588 tm = alloc_tree_mod_elem(eb, slot, op, flags);
589 if (!tm)
590 return -ENOMEM;
591
592 if (tree_mod_dont_log(fs_info, eb)) {
593 kfree(tm);
594 return 0;
595 }
596
597 ret = __tree_mod_log_insert(fs_info, tm);
598 tree_mod_log_write_unlock(fs_info);
599 if (ret)
600 kfree(tm);
601
602 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200603}
604
605static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200606tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
607 struct extent_buffer *eb, int dst_slot, int src_slot,
608 int nr_items, gfp_t flags)
609{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000610 struct tree_mod_elem *tm = NULL;
611 struct tree_mod_elem **tm_list = NULL;
612 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200613 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000614 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200615
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000616 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200617 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200618
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000619 tm_list = kzalloc(nr_items * sizeof(struct tree_mod_elem *), flags);
620 if (!tm_list)
621 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200622
Josef Bacikc8cc6342013-07-01 16:18:19 -0400623 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000624 if (!tm) {
625 ret = -ENOMEM;
626 goto free_tms;
627 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200628
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200629 tm->index = eb->start >> PAGE_CACHE_SHIFT;
630 tm->slot = src_slot;
631 tm->move.dst_slot = dst_slot;
632 tm->move.nr_items = nr_items;
633 tm->op = MOD_LOG_MOVE_KEYS;
634
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000635 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
636 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
637 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
638 if (!tm_list[i]) {
639 ret = -ENOMEM;
640 goto free_tms;
641 }
642 }
643
644 if (tree_mod_dont_log(fs_info, eb))
645 goto free_tms;
646 locked = 1;
647
648 /*
649 * When we override something during the move, we log these removals.
650 * This can only happen when we move towards the beginning of the
651 * buffer, i.e. dst_slot < src_slot.
652 */
653 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
654 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
655 if (ret)
656 goto free_tms;
657 }
658
659 ret = __tree_mod_log_insert(fs_info, tm);
660 if (ret)
661 goto free_tms;
662 tree_mod_log_write_unlock(fs_info);
663 kfree(tm_list);
664
665 return 0;
666free_tms:
667 for (i = 0; i < nr_items; i++) {
668 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
669 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
670 kfree(tm_list[i]);
671 }
672 if (locked)
673 tree_mod_log_write_unlock(fs_info);
674 kfree(tm_list);
675 kfree(tm);
676
677 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200678}
679
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000680static inline int
681__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
682 struct tree_mod_elem **tm_list,
683 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200684{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000685 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200686 int ret;
687
Jan Schmidt097b8a72012-06-21 11:08:04 +0200688 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000689 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
690 if (ret) {
691 for (j = nritems - 1; j > i; j--)
692 rb_erase(&tm_list[j]->node,
693 &fs_info->tree_mod_log);
694 return ret;
695 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200696 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000697
698 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200699}
700
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200701static noinline int
702tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
703 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000704 struct extent_buffer *new_root, gfp_t flags,
705 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200706{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000707 struct tree_mod_elem *tm = NULL;
708 struct tree_mod_elem **tm_list = NULL;
709 int nritems = 0;
710 int ret = 0;
711 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200712
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000713 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200714 return 0;
715
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000716 if (log_removal && btrfs_header_level(old_root) > 0) {
717 nritems = btrfs_header_nritems(old_root);
718 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
719 flags);
720 if (!tm_list) {
721 ret = -ENOMEM;
722 goto free_tms;
723 }
724 for (i = 0; i < nritems; i++) {
725 tm_list[i] = alloc_tree_mod_elem(old_root, i,
726 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
727 if (!tm_list[i]) {
728 ret = -ENOMEM;
729 goto free_tms;
730 }
731 }
732 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000733
Josef Bacikc8cc6342013-07-01 16:18:19 -0400734 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000735 if (!tm) {
736 ret = -ENOMEM;
737 goto free_tms;
738 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200739
740 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
741 tm->old_root.logical = old_root->start;
742 tm->old_root.level = btrfs_header_level(old_root);
743 tm->generation = btrfs_header_generation(old_root);
744 tm->op = MOD_LOG_ROOT_REPLACE;
745
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000746 if (tree_mod_dont_log(fs_info, NULL))
747 goto free_tms;
748
749 if (tm_list)
750 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
751 if (!ret)
752 ret = __tree_mod_log_insert(fs_info, tm);
753
754 tree_mod_log_write_unlock(fs_info);
755 if (ret)
756 goto free_tms;
757 kfree(tm_list);
758
759 return ret;
760
761free_tms:
762 if (tm_list) {
763 for (i = 0; i < nritems; i++)
764 kfree(tm_list[i]);
765 kfree(tm_list);
766 }
767 kfree(tm);
768
769 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200770}
771
772static struct tree_mod_elem *
773__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
774 int smallest)
775{
776 struct rb_root *tm_root;
777 struct rb_node *node;
778 struct tree_mod_elem *cur = NULL;
779 struct tree_mod_elem *found = NULL;
780 u64 index = start >> PAGE_CACHE_SHIFT;
781
Jan Schmidt097b8a72012-06-21 11:08:04 +0200782 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200783 tm_root = &fs_info->tree_mod_log;
784 node = tm_root->rb_node;
785 while (node) {
786 cur = container_of(node, struct tree_mod_elem, node);
787 if (cur->index < index) {
788 node = node->rb_left;
789 } else if (cur->index > index) {
790 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200791 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792 node = node->rb_left;
793 } else if (!smallest) {
794 /* we want the node with the highest seq */
795 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200796 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200797 found = cur;
798 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200799 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 /* we want the node with the smallest seq */
801 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200802 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200803 found = cur;
804 node = node->rb_right;
805 } else {
806 found = cur;
807 break;
808 }
809 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200810 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200811
812 return found;
813}
814
815/*
816 * this returns the element from the log with the smallest time sequence
817 * value that's in the log (the oldest log item). any element with a time
818 * sequence lower than min_seq will be ignored.
819 */
820static struct tree_mod_elem *
821tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
822 u64 min_seq)
823{
824 return __tree_mod_log_search(fs_info, start, min_seq, 1);
825}
826
827/*
828 * this returns the element from the log with the largest time sequence
829 * value that's in the log (the most recent log item). any element with
830 * a time sequence lower than min_seq will be ignored.
831 */
832static struct tree_mod_elem *
833tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
834{
835 return __tree_mod_log_search(fs_info, start, min_seq, 0);
836}
837
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000838static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200839tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
840 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000841 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200842{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000843 int ret = 0;
844 struct tree_mod_elem **tm_list = NULL;
845 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200846 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000847 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200848
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000849 if (!tree_mod_need_log(fs_info, NULL))
850 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200851
Josef Bacikc8cc6342013-07-01 16:18:19 -0400852 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000853 return 0;
854
855 tm_list = kzalloc(nr_items * 2 * sizeof(struct tree_mod_elem *),
856 GFP_NOFS);
857 if (!tm_list)
858 return -ENOMEM;
859
860 tm_list_add = tm_list;
861 tm_list_rem = tm_list + nr_items;
862 for (i = 0; i < nr_items; i++) {
863 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
864 MOD_LOG_KEY_REMOVE, GFP_NOFS);
865 if (!tm_list_rem[i]) {
866 ret = -ENOMEM;
867 goto free_tms;
868 }
869
870 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
871 MOD_LOG_KEY_ADD, GFP_NOFS);
872 if (!tm_list_add[i]) {
873 ret = -ENOMEM;
874 goto free_tms;
875 }
876 }
877
878 if (tree_mod_dont_log(fs_info, NULL))
879 goto free_tms;
880 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200881
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200882 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000883 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
884 if (ret)
885 goto free_tms;
886 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
887 if (ret)
888 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200889 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000890
891 tree_mod_log_write_unlock(fs_info);
892 kfree(tm_list);
893
894 return 0;
895
896free_tms:
897 for (i = 0; i < nr_items * 2; i++) {
898 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
899 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
900 kfree(tm_list[i]);
901 }
902 if (locked)
903 tree_mod_log_write_unlock(fs_info);
904 kfree(tm_list);
905
906 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200907}
908
909static inline void
910tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
911 int dst_offset, int src_offset, int nr_items)
912{
913 int ret;
914 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
915 nr_items, GFP_NOFS);
916 BUG_ON(ret < 0);
917}
918
Jan Schmidt097b8a72012-06-21 11:08:04 +0200919static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200920tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000921 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200922{
923 int ret;
924
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000925 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400926 MOD_LOG_KEY_REPLACE,
927 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200928 BUG_ON(ret < 0);
929}
930
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000931static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200932tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200933{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000934 struct tree_mod_elem **tm_list = NULL;
935 int nritems = 0;
936 int i;
937 int ret = 0;
938
939 if (btrfs_header_level(eb) == 0)
940 return 0;
941
942 if (!tree_mod_need_log(fs_info, NULL))
943 return 0;
944
945 nritems = btrfs_header_nritems(eb);
946 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
947 GFP_NOFS);
948 if (!tm_list)
949 return -ENOMEM;
950
951 for (i = 0; i < nritems; i++) {
952 tm_list[i] = alloc_tree_mod_elem(eb, i,
953 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
954 if (!tm_list[i]) {
955 ret = -ENOMEM;
956 goto free_tms;
957 }
958 }
959
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200960 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000961 goto free_tms;
962
963 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
964 tree_mod_log_write_unlock(fs_info);
965 if (ret)
966 goto free_tms;
967 kfree(tm_list);
968
969 return 0;
970
971free_tms:
972 for (i = 0; i < nritems; i++)
973 kfree(tm_list[i]);
974 kfree(tm_list);
975
976 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200977}
978
Jan Schmidt097b8a72012-06-21 11:08:04 +0200979static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200980tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000981 struct extent_buffer *new_root_node,
982 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200983{
984 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200985 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000986 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200987 BUG_ON(ret < 0);
988}
989
Chris Masond352ac62008-09-29 15:18:18 -0400990/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 * check if the tree block can be shared by multiple trees
992 */
993int btrfs_block_can_be_shared(struct btrfs_root *root,
994 struct extent_buffer *buf)
995{
996 /*
997 * Tree blocks not in refernece counted trees and tree roots
998 * are never shared. If a block was allocated after the last
999 * snapshot and the block was not allocated by tree relocation,
1000 * we know the block is not shared.
1001 */
Miao Xie27cdeb72014-04-02 19:51:05 +08001002 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001003 buf != root->node && buf != root->commit_root &&
1004 (btrfs_header_generation(buf) <=
1005 btrfs_root_last_snapshot(&root->root_item) ||
1006 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
1007 return 1;
1008#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +08001009 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1011 return 1;
1012#endif
1013 return 0;
1014}
1015
1016static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
1017 struct btrfs_root *root,
1018 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -04001019 struct extent_buffer *cow,
1020 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021{
1022 u64 refs;
1023 u64 owner;
1024 u64 flags;
1025 u64 new_flags = 0;
1026 int ret;
1027
1028 /*
1029 * Backrefs update rules:
1030 *
1031 * Always use full backrefs for extent pointers in tree block
1032 * allocated by tree relocation.
1033 *
1034 * If a shared tree block is no longer referenced by its owner
1035 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
1036 * use full backrefs for extent pointers in tree block.
1037 *
1038 * If a tree block is been relocating
1039 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
1040 * use full backrefs for extent pointers in tree block.
1041 * The reason for this is some operations (such as drop tree)
1042 * are only allowed for blocks use full backrefs.
1043 */
1044
1045 if (btrfs_block_can_be_shared(root, buf)) {
1046 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -05001047 btrfs_header_level(buf), 1,
1048 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001049 if (ret)
1050 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -07001051 if (refs == 0) {
1052 ret = -EROFS;
1053 btrfs_std_error(root->fs_info, ret);
1054 return ret;
1055 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056 } else {
1057 refs = 1;
1058 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1059 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1060 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1061 else
1062 flags = 0;
1063 }
1064
1065 owner = btrfs_header_owner(buf);
1066 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1067 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1068
1069 if (refs > 1) {
1070 if ((owner == root->root_key.objectid ||
1071 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1072 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001073 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001074 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001075
1076 if (root->root_key.objectid ==
1077 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001078 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001079 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001080 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 }
1083 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1084 } else {
1085
1086 if (root->root_key.objectid ==
1087 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001088 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001089 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001090 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001091 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001092 }
1093 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001094 int level = btrfs_header_level(buf);
1095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096 ret = btrfs_set_disk_extent_flags(trans, root,
1097 buf->start,
1098 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001099 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001100 if (ret)
1101 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102 }
1103 } else {
1104 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1105 if (root->root_key.objectid ==
1106 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001107 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001109 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001110 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001111 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001112 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001113 }
1114 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001115 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001116 }
1117 return 0;
1118}
1119
1120/*
Chris Masond3977122009-01-05 21:25:51 -05001121 * does the dirty work in cow of a single block. The parent block (if
1122 * supplied) is updated to point to the new cow copy. The new buffer is marked
1123 * dirty and returned locked. If you modify the block it needs to be marked
1124 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001125 *
1126 * search_start -- an allocation hint for the new block
1127 *
Chris Masond3977122009-01-05 21:25:51 -05001128 * empty_size -- a hint that you plan on doing more cow. This is the size in
1129 * bytes the allocator should try to find free next to the block it returns.
1130 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001131 */
Chris Masond3977122009-01-05 21:25:51 -05001132static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001133 struct btrfs_root *root,
1134 struct extent_buffer *buf,
1135 struct extent_buffer *parent, int parent_slot,
1136 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001137 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001138{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001139 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001140 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001141 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001142 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001143 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001144 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001145
Chris Mason925baed2008-06-25 16:01:30 -04001146 if (*cow_ret == buf)
1147 unlock_orig = 1;
1148
Chris Masonb9447ef82009-03-09 11:45:38 -04001149 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001150
Miao Xie27cdeb72014-04-02 19:51:05 +08001151 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1152 trans->transid != root->fs_info->running_transaction->transid);
1153 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1154 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001155
Chris Mason7bb86312007-12-11 09:25:06 -05001156 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001157
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001158 if (level == 0)
1159 btrfs_item_key(buf, &disk_key, 0);
1160 else
1161 btrfs_node_key(buf, &disk_key, 0);
1162
1163 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1164 if (parent)
1165 parent_start = parent->start;
1166 else
1167 parent_start = 0;
1168 } else
1169 parent_start = 0;
1170
1171 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1172 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001173 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001174 if (IS_ERR(cow))
1175 return PTR_ERR(cow);
1176
Chris Masonb4ce94d2009-02-04 09:25:08 -05001177 /* cow is set to blocking by btrfs_init_new_buffer */
1178
Chris Mason5f39d392007-10-15 16:14:19 -04001179 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001180 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001181 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001182 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1183 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1184 BTRFS_HEADER_FLAG_RELOC);
1185 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1186 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1187 else
1188 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001189
Ross Kirk0a4e5582013-09-24 10:12:38 +01001190 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001191 BTRFS_FSID_SIZE);
1192
Mark Fashehbe1a5562011-08-08 13:20:18 -07001193 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001194 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001195 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001196 return ret;
1197 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001198
Miao Xie27cdeb72014-04-02 19:51:05 +08001199 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001200 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1201 if (ret)
1202 return ret;
1203 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001204
Chris Mason6702ed42007-08-07 16:15:09 -04001205 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001206 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1208 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1209 parent_start = buf->start;
1210 else
1211 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001212
Chris Mason5f39d392007-10-15 16:14:19 -04001213 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001214 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001215 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001216
Yan, Zhengf0486c62010-05-16 10:46:25 -04001217 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001218 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001219 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001220 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001221 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001222 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1223 parent_start = parent->start;
1224 else
1225 parent_start = 0;
1226
1227 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001228 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001229 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001230 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001231 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001232 btrfs_set_node_ptr_generation(parent, parent_slot,
1233 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001234 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001235 if (last_ref) {
1236 ret = tree_mod_log_free_eb(root->fs_info, buf);
1237 if (ret) {
1238 btrfs_abort_transaction(trans, root, ret);
1239 return ret;
1240 }
1241 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001242 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001243 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001244 }
Chris Mason925baed2008-06-25 16:01:30 -04001245 if (unlock_orig)
1246 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001247 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001248 btrfs_mark_buffer_dirty(cow);
1249 *cow_ret = cow;
1250 return 0;
1251}
1252
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001253/*
1254 * returns the logical address of the oldest predecessor of the given root.
1255 * entries older than time_seq are ignored.
1256 */
1257static struct tree_mod_elem *
1258__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001259 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001260{
1261 struct tree_mod_elem *tm;
1262 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001263 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001264 int looped = 0;
1265
1266 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001267 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268
1269 /*
1270 * the very last operation that's logged for a root is the replacement
1271 * operation (if it is replaced at all). this has the index of the *new*
1272 * root, making it the very first operation that's logged for this root.
1273 */
1274 while (1) {
1275 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1276 time_seq);
1277 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001278 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001279 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001280 * if there are no tree operation for the oldest root, we simply
1281 * return it. this should only happen if that (old) root is at
1282 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001283 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001284 if (!tm)
1285 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001286
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001287 /*
1288 * if there's an operation that's not a root replacement, we
1289 * found the oldest version of our root. normally, we'll find a
1290 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1291 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001292 if (tm->op != MOD_LOG_ROOT_REPLACE)
1293 break;
1294
1295 found = tm;
1296 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001297 looped = 1;
1298 }
1299
Jan Schmidta95236d2012-06-05 16:41:24 +02001300 /* if there's no old root to return, return what we found instead */
1301 if (!found)
1302 found = tm;
1303
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001304 return found;
1305}
1306
1307/*
1308 * tm is a pointer to the first operation to rewind within eb. then, all
1309 * previous operations will be rewinded (until we reach something older than
1310 * time_seq).
1311 */
1312static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001313__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1314 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001315{
1316 u32 n;
1317 struct rb_node *next;
1318 struct tree_mod_elem *tm = first_tm;
1319 unsigned long o_dst;
1320 unsigned long o_src;
1321 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1322
1323 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001324 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001325 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001326 /*
1327 * all the operations are recorded with the operator used for
1328 * the modification. as we're going backwards, we do the
1329 * opposite of each operation here.
1330 */
1331 switch (tm->op) {
1332 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1333 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001334 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001335 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001336 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001337 btrfs_set_node_key(eb, &tm->key, tm->slot);
1338 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1339 btrfs_set_node_ptr_generation(eb, tm->slot,
1340 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001341 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001342 break;
1343 case MOD_LOG_KEY_REPLACE:
1344 BUG_ON(tm->slot >= n);
1345 btrfs_set_node_key(eb, &tm->key, tm->slot);
1346 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1347 btrfs_set_node_ptr_generation(eb, tm->slot,
1348 tm->generation);
1349 break;
1350 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001351 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001352 n--;
1353 break;
1354 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001355 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1356 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1357 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001358 tm->move.nr_items * p_size);
1359 break;
1360 case MOD_LOG_ROOT_REPLACE:
1361 /*
1362 * this operation is special. for roots, this must be
1363 * handled explicitly before rewinding.
1364 * for non-roots, this operation may exist if the node
1365 * was a root: root A -> child B; then A gets empty and
1366 * B is promoted to the new root. in the mod log, we'll
1367 * have a root-replace operation for B, a tree block
1368 * that is no root. we simply ignore that operation.
1369 */
1370 break;
1371 }
1372 next = rb_next(&tm->node);
1373 if (!next)
1374 break;
1375 tm = container_of(next, struct tree_mod_elem, node);
1376 if (tm->index != first_tm->index)
1377 break;
1378 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001379 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001380 btrfs_set_header_nritems(eb, n);
1381}
1382
Jan Schmidt47fb0912013-04-13 13:19:55 +00001383/*
1384 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1385 * is returned. If rewind operations happen, a fresh buffer is returned. The
1386 * returned buffer is always read-locked. If the returned buffer is not the
1387 * input buffer, the lock on the input buffer is released and the input buffer
1388 * is freed (its refcount is decremented).
1389 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001390static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001391tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1392 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001393{
1394 struct extent_buffer *eb_rewin;
1395 struct tree_mod_elem *tm;
1396
1397 if (!time_seq)
1398 return eb;
1399
1400 if (btrfs_header_level(eb) == 0)
1401 return eb;
1402
1403 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1404 if (!tm)
1405 return eb;
1406
Josef Bacik9ec72672013-08-07 16:57:23 -04001407 btrfs_set_path_blocking(path);
1408 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1409
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001410 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1411 BUG_ON(tm->slot != 0);
1412 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1413 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001414 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001415 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001416 free_extent_buffer(eb);
1417 return NULL;
1418 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001419 btrfs_set_header_bytenr(eb_rewin, eb->start);
1420 btrfs_set_header_backref_rev(eb_rewin,
1421 btrfs_header_backref_rev(eb));
1422 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001423 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001424 } else {
1425 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001426 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001427 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001428 free_extent_buffer(eb);
1429 return NULL;
1430 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001431 }
1432
Josef Bacik9ec72672013-08-07 16:57:23 -04001433 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1434 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001435 free_extent_buffer(eb);
1436
Jan Schmidt47fb0912013-04-13 13:19:55 +00001437 extent_buffer_get(eb_rewin);
1438 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001439 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001440 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001441 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001442
1443 return eb_rewin;
1444}
1445
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001446/*
1447 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1448 * value. If there are no changes, the current root->root_node is returned. If
1449 * anything changed in between, there's a fresh buffer allocated on which the
1450 * rewind operations are done. In any case, the returned buffer is read locked.
1451 * Returns NULL on error (with no locks held).
1452 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001453static inline struct extent_buffer *
1454get_old_root(struct btrfs_root *root, u64 time_seq)
1455{
1456 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001457 struct extent_buffer *eb = NULL;
1458 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001459 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001460 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001461 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001462 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001463 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001464
Jan Schmidt30b04632013-04-13 13:19:54 +00001465 eb_root = btrfs_read_lock_root_node(root);
1466 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001467 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001468 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001469
Jan Schmidta95236d2012-06-05 16:41:24 +02001470 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1471 old_root = &tm->old_root;
1472 old_generation = tm->generation;
1473 logical = old_root->logical;
1474 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001475 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001476 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001477
Jan Schmidta95236d2012-06-05 16:41:24 +02001478 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001479 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001480 btrfs_tree_read_unlock(eb_root);
1481 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001482 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001483 old = read_tree_block(root, logical, blocksize, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301484 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001485 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001486 btrfs_warn(root->fs_info,
1487 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001488 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001489 eb = btrfs_clone_extent_buffer(old);
1490 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001491 }
1492 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001493 btrfs_tree_read_unlock(eb_root);
1494 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001495 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001496 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001497 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001498 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001499 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001500 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001501 }
1502
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001503 if (!eb)
1504 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001505 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001506 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001507 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001508 btrfs_set_header_bytenr(eb, eb->start);
1509 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001510 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001511 btrfs_set_header_level(eb, old_root->level);
1512 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001513 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001514 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001515 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001516 else
1517 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001518 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001519
1520 return eb;
1521}
1522
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001523int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1524{
1525 struct tree_mod_elem *tm;
1526 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001527 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001528
Jan Schmidt30b04632013-04-13 13:19:54 +00001529 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001530 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1531 level = tm->old_root.level;
1532 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001533 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001534 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001535 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001536
1537 return level;
1538}
1539
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001540static inline int should_cow_block(struct btrfs_trans_handle *trans,
1541 struct btrfs_root *root,
1542 struct extent_buffer *buf)
1543{
Liu Bof1ebcc72011-11-14 20:48:06 -05001544 /* ensure we can see the force_cow */
1545 smp_rmb();
1546
1547 /*
1548 * We do not need to cow a block if
1549 * 1) this block is not created or changed in this transaction;
1550 * 2) this block does not belong to TREE_RELOC tree;
1551 * 3) the root is not forced COW.
1552 *
1553 * What is forced COW:
1554 * when we create snapshot during commiting the transaction,
1555 * after we've finished coping src root, we must COW the shared
1556 * block to ensure the metadata consistency.
1557 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001558 if (btrfs_header_generation(buf) == trans->transid &&
1559 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1560 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001561 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001562 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001563 return 0;
1564 return 1;
1565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * cows a single block, see __btrfs_cow_block for the real work.
1569 * This version of it has extra checks so that a block isn't cow'd more than
1570 * once per transaction, as long as it hasn't been written yet
1571 */
Chris Masond3977122009-01-05 21:25:51 -05001572noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001573 struct btrfs_root *root, struct extent_buffer *buf,
1574 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001575 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001576{
Chris Mason6702ed42007-08-07 16:15:09 -04001577 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001578 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001579
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001580 if (trans->transaction != root->fs_info->running_transaction)
1581 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001582 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001583 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001584
1585 if (trans->transid != root->fs_info->generation)
1586 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001587 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001588
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001589 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001590 *cow_ret = buf;
1591 return 0;
1592 }
Chris Masonc4876852009-02-04 09:24:25 -05001593
Chris Mason0b86a832008-03-24 15:01:56 -04001594 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001595
1596 if (parent)
1597 btrfs_set_lock_blocking(parent);
1598 btrfs_set_lock_blocking(buf);
1599
Chris Masonf510cfe2007-10-15 16:14:48 -04001600 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001601 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001602
1603 trace_btrfs_cow_block(root, buf, *cow_ret);
1604
Chris Masonf510cfe2007-10-15 16:14:48 -04001605 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001606}
1607
Chris Masond352ac62008-09-29 15:18:18 -04001608/*
1609 * helper function for defrag to decide if two blocks pointed to by a
1610 * node are actually close by
1611 */
Chris Mason6b800532007-10-15 16:17:34 -04001612static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001613{
Chris Mason6b800532007-10-15 16:17:34 -04001614 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001615 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001616 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001617 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001618 return 0;
1619}
1620
Chris Mason081e9572007-11-06 10:26:24 -05001621/*
1622 * compare two keys in a memcmp fashion
1623 */
1624static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1625{
1626 struct btrfs_key k1;
1627
1628 btrfs_disk_key_to_cpu(&k1, disk);
1629
Diego Calleja20736ab2009-07-24 11:06:52 -04001630 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001631}
1632
Josef Bacikf3465ca2008-11-12 14:19:50 -05001633/*
1634 * same as comp_keys only with two btrfs_key's
1635 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001637{
1638 if (k1->objectid > k2->objectid)
1639 return 1;
1640 if (k1->objectid < k2->objectid)
1641 return -1;
1642 if (k1->type > k2->type)
1643 return 1;
1644 if (k1->type < k2->type)
1645 return -1;
1646 if (k1->offset > k2->offset)
1647 return 1;
1648 if (k1->offset < k2->offset)
1649 return -1;
1650 return 0;
1651}
Chris Mason081e9572007-11-06 10:26:24 -05001652
Chris Masond352ac62008-09-29 15:18:18 -04001653/*
1654 * this is used by the defrag code to go through all the
1655 * leaves pointed to by a node and reallocate them so that
1656 * disk order is close to key order
1657 */
Chris Mason6702ed42007-08-07 16:15:09 -04001658int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001659 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001660 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001661 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001662{
Chris Mason6b800532007-10-15 16:17:34 -04001663 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001664 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001665 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001666 u64 search_start = *last_ret;
1667 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001668 u64 other;
1669 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001670 int end_slot;
1671 int i;
1672 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001673 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001674 int uptodate;
1675 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001676 int progress_passed = 0;
1677 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001678
Chris Mason5708b952007-10-25 15:43:18 -04001679 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001680
Julia Lawall6c1500f2012-11-03 20:30:18 +00001681 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1682 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001683
Chris Mason6b800532007-10-15 16:17:34 -04001684 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001685 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001686 end_slot = parent_nritems;
1687
1688 if (parent_nritems == 1)
1689 return 0;
1690
Chris Masonb4ce94d2009-02-04 09:25:08 -05001691 btrfs_set_lock_blocking(parent);
1692
Chris Mason6702ed42007-08-07 16:15:09 -04001693 for (i = start_slot; i < end_slot; i++) {
1694 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001695
Chris Mason081e9572007-11-06 10:26:24 -05001696 btrfs_node_key(parent, &disk_key, i);
1697 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1698 continue;
1699
1700 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001701 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001702 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001703 if (last_block == 0)
1704 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001705
Chris Mason6702ed42007-08-07 16:15:09 -04001706 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001707 other = btrfs_node_blockptr(parent, i - 1);
1708 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001709 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001710 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001711 other = btrfs_node_blockptr(parent, i + 1);
1712 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001713 }
Chris Masone9d0b132007-08-10 14:06:19 -04001714 if (close) {
1715 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001716 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001717 }
Chris Mason6702ed42007-08-07 16:15:09 -04001718
Chris Mason6b800532007-10-15 16:17:34 -04001719 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1720 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001721 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001722 else
1723 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001724 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001725 if (!cur) {
1726 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001727 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001728 if (!cur || !extent_buffer_uptodate(cur)) {
1729 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001730 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001731 }
Chris Mason6b800532007-10-15 16:17:34 -04001732 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001733 err = btrfs_read_buffer(cur, gen);
1734 if (err) {
1735 free_extent_buffer(cur);
1736 return err;
1737 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001738 }
Chris Mason6702ed42007-08-07 16:15:09 -04001739 }
Chris Masone9d0b132007-08-10 14:06:19 -04001740 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001741 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001742
Chris Masone7a84562008-06-25 16:01:31 -04001743 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001744 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001745 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001746 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001747 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001748 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001749 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001750 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001751 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001752 break;
Yan252c38f2007-08-29 09:11:44 -04001753 }
Chris Masone7a84562008-06-25 16:01:31 -04001754 search_start = cur->start;
1755 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001756 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001757 btrfs_tree_unlock(cur);
1758 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001759 }
1760 return err;
1761}
1762
Chris Mason74123bd2007-02-02 11:05:29 -05001763/*
1764 * The leaf data grows from end-to-front in the node.
1765 * this returns the address of the start of the last item,
1766 * which is the stop of the leaf data stack
1767 */
Chris Mason123abc82007-03-14 14:14:43 -04001768static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001769 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001770{
Chris Mason5f39d392007-10-15 16:14:19 -04001771 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001772 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001773 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001774 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001775}
1776
Chris Masonaa5d6be2007-02-28 16:35:06 -05001777
Chris Mason74123bd2007-02-02 11:05:29 -05001778/*
Chris Mason5f39d392007-10-15 16:14:19 -04001779 * search for key in the extent_buffer. The items start at offset p,
1780 * and they are item_size apart. There are 'max' items in p.
1781 *
Chris Mason74123bd2007-02-02 11:05:29 -05001782 * the slot in the array is returned via slot, and it points to
1783 * the place where you would insert key if it is not found in
1784 * the array.
1785 *
1786 * slot may point to max if the key is bigger than all of the keys
1787 */
Chris Masone02119d2008-09-05 16:13:11 -04001788static noinline int generic_bin_search(struct extent_buffer *eb,
1789 unsigned long p,
1790 int item_size, struct btrfs_key *key,
1791 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001792{
1793 int low = 0;
1794 int high = max;
1795 int mid;
1796 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001797 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001798 struct btrfs_disk_key unaligned;
1799 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001800 char *kaddr = NULL;
1801 unsigned long map_start = 0;
1802 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001803 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001804
Chris Masond3977122009-01-05 21:25:51 -05001805 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001807 offset = p + mid * item_size;
1808
Chris Masona6591712011-07-19 12:04:14 -04001809 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001810 (offset + sizeof(struct btrfs_disk_key)) >
1811 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001812
1813 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001814 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001815 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001816
Chris Mason479965d2007-10-15 16:14:27 -04001817 if (!err) {
1818 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1819 map_start);
1820 } else {
1821 read_extent_buffer(eb, &unaligned,
1822 offset, sizeof(unaligned));
1823 tmp = &unaligned;
1824 }
1825
Chris Mason5f39d392007-10-15 16:14:19 -04001826 } else {
1827 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1828 map_start);
1829 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001830 ret = comp_keys(tmp, key);
1831
1832 if (ret < 0)
1833 low = mid + 1;
1834 else if (ret > 0)
1835 high = mid;
1836 else {
1837 *slot = mid;
1838 return 0;
1839 }
1840 }
1841 *slot = low;
1842 return 1;
1843}
1844
Chris Mason97571fd2007-02-24 13:39:08 -05001845/*
1846 * simple bin_search frontend that does the right thing for
1847 * leaves vs nodes
1848 */
Chris Mason5f39d392007-10-15 16:14:19 -04001849static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1850 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001851{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001852 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001853 return generic_bin_search(eb,
1854 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001855 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001856 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001857 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001858 else
Chris Mason5f39d392007-10-15 16:14:19 -04001859 return generic_bin_search(eb,
1860 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001861 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001862 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001863 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001864}
1865
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001866int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1867 int level, int *slot)
1868{
1869 return bin_search(eb, key, level, slot);
1870}
1871
Yan, Zhengf0486c62010-05-16 10:46:25 -04001872static void root_add_used(struct btrfs_root *root, u32 size)
1873{
1874 spin_lock(&root->accounting_lock);
1875 btrfs_set_root_used(&root->root_item,
1876 btrfs_root_used(&root->root_item) + size);
1877 spin_unlock(&root->accounting_lock);
1878}
1879
1880static void root_sub_used(struct btrfs_root *root, u32 size)
1881{
1882 spin_lock(&root->accounting_lock);
1883 btrfs_set_root_used(&root->root_item,
1884 btrfs_root_used(&root->root_item) - size);
1885 spin_unlock(&root->accounting_lock);
1886}
1887
Chris Masond352ac62008-09-29 15:18:18 -04001888/* given a node and slot number, this reads the blocks it points to. The
1889 * extent buffer is returned with a reference taken (but unlocked).
1890 * NULL is returned on error.
1891 */
Chris Masone02119d2008-09-05 16:13:11 -04001892static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001893 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001894{
Chris Masonca7a79a2008-05-12 12:59:19 -04001895 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001896 struct extent_buffer *eb;
1897
Chris Masonbb803952007-03-01 12:04:21 -05001898 if (slot < 0)
1899 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001900 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001901 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001902
1903 BUG_ON(level == 0);
1904
Josef Bacik416bc652013-04-23 14:17:42 -04001905 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1906 btrfs_level_size(root, level - 1),
1907 btrfs_node_ptr_generation(parent, slot));
1908 if (eb && !extent_buffer_uptodate(eb)) {
1909 free_extent_buffer(eb);
1910 eb = NULL;
1911 }
1912
1913 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001914}
1915
Chris Masond352ac62008-09-29 15:18:18 -04001916/*
1917 * node level balancing, used to make sure nodes are in proper order for
1918 * item deletion. We balance from the top down, so we have to make sure
1919 * that a deletion won't leave an node completely empty later on.
1920 */
Chris Masone02119d2008-09-05 16:13:11 -04001921static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001922 struct btrfs_root *root,
1923 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001924{
Chris Mason5f39d392007-10-15 16:14:19 -04001925 struct extent_buffer *right = NULL;
1926 struct extent_buffer *mid;
1927 struct extent_buffer *left = NULL;
1928 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001929 int ret = 0;
1930 int wret;
1931 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001932 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001933 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001934
1935 if (level == 0)
1936 return 0;
1937
Chris Mason5f39d392007-10-15 16:14:19 -04001938 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001939
Chris Masonbd681512011-07-16 15:23:14 -04001940 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1941 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001942 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1943
Chris Mason1d4f8a02007-03-13 09:28:32 -04001944 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001945
Li Zefana05a9bb2011-09-06 16:55:34 +08001946 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001947 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001948 pslot = path->slots[level + 1];
1949 }
Chris Masonbb803952007-03-01 12:04:21 -05001950
Chris Mason40689472007-03-17 14:29:23 -04001951 /*
1952 * deal with the case where there is only one pointer in the root
1953 * by promoting the node below to a root
1954 */
Chris Mason5f39d392007-10-15 16:14:19 -04001955 if (!parent) {
1956 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001957
Chris Mason5f39d392007-10-15 16:14:19 -04001958 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001959 return 0;
1960
1961 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001962 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001963 if (!child) {
1964 ret = -EROFS;
1965 btrfs_std_error(root->fs_info, ret);
1966 goto enospc;
1967 }
1968
Chris Mason925baed2008-06-25 16:01:30 -04001969 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001970 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001971 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001972 if (ret) {
1973 btrfs_tree_unlock(child);
1974 free_extent_buffer(child);
1975 goto enospc;
1976 }
Yan2f375ab2008-02-01 14:58:07 -05001977
Jan Schmidt90f8d622013-04-13 13:19:53 +00001978 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001979 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001980
Chris Mason0b86a832008-03-24 15:01:56 -04001981 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001982 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001983
Chris Mason925baed2008-06-25 16:01:30 -04001984 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001985 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001986 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001987 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001988 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001989 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001990
1991 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001992 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001993 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001994 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001995 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001996 }
Chris Mason5f39d392007-10-15 16:14:19 -04001997 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001998 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001999 return 0;
2000
Chris Mason5f39d392007-10-15 16:14:19 -04002001 left = read_node_slot(root, parent, pslot - 1);
2002 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04002003 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002004 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002005 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002006 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002007 if (wret) {
2008 ret = wret;
2009 goto enospc;
2010 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04002011 }
Chris Mason5f39d392007-10-15 16:14:19 -04002012 right = read_node_slot(root, parent, pslot + 1);
2013 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04002014 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002015 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002016 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002017 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04002018 if (wret) {
2019 ret = wret;
2020 goto enospc;
2021 }
2022 }
2023
2024 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002025 if (left) {
2026 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04002027 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05002028 if (wret < 0)
2029 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05002030 }
Chris Mason79f95c82007-03-01 15:16:26 -05002031
2032 /*
2033 * then try to empty the right most buffer into the middle
2034 */
Chris Mason5f39d392007-10-15 16:14:19 -04002035 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04002036 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002037 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05002038 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002039 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002040 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002041 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002042 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002043 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002044 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002045 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002046 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05002047 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002048 struct btrfs_disk_key right_key;
2049 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002050 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002051 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 btrfs_set_node_key(parent, &right_key, pslot + 1);
2053 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002054 }
2055 }
Chris Mason5f39d392007-10-15 16:14:19 -04002056 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002057 /*
2058 * we're not allowed to leave a node with one item in the
2059 * tree during a delete. A deletion from lower in the tree
2060 * could try to delete the only pointer in this node.
2061 * So, pull some keys from the left.
2062 * There has to be a left pointer at this point because
2063 * otherwise we would have pulled some pointers from the
2064 * right
2065 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002066 if (!left) {
2067 ret = -EROFS;
2068 btrfs_std_error(root->fs_info, ret);
2069 goto enospc;
2070 }
Chris Mason5f39d392007-10-15 16:14:19 -04002071 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002072 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002073 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002074 goto enospc;
2075 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002076 if (wret == 1) {
2077 wret = push_node_left(trans, root, left, mid, 1);
2078 if (wret < 0)
2079 ret = wret;
2080 }
Chris Mason79f95c82007-03-01 15:16:26 -05002081 BUG_ON(wret == 1);
2082 }
Chris Mason5f39d392007-10-15 16:14:19 -04002083 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002084 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002085 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002086 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002087 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002088 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002089 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002090 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002091 } else {
2092 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002093 struct btrfs_disk_key mid_key;
2094 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002095 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002096 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_node_key(parent, &mid_key, pslot);
2098 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002099 }
Chris Masonbb803952007-03-01 12:04:21 -05002100
Chris Mason79f95c82007-03-01 15:16:26 -05002101 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002102 if (left) {
2103 if (btrfs_header_nritems(left) > orig_slot) {
2104 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002105 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002106 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002107 path->slots[level + 1] -= 1;
2108 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002109 if (mid) {
2110 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002112 }
Chris Masonbb803952007-03-01 12:04:21 -05002113 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002114 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002115 path->slots[level] = orig_slot;
2116 }
2117 }
Chris Mason79f95c82007-03-01 15:16:26 -05002118 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002119 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002120 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002121 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002122enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002123 if (right) {
2124 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002125 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002126 }
2127 if (left) {
2128 if (path->nodes[level] != left)
2129 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002130 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002131 }
Chris Masonbb803952007-03-01 12:04:21 -05002132 return ret;
2133}
2134
Chris Masond352ac62008-09-29 15:18:18 -04002135/* Node balancing for insertion. Here we only split or push nodes around
2136 * when they are completely full. This is also done top down, so we
2137 * have to be pessimistic.
2138 */
Chris Masond3977122009-01-05 21:25:51 -05002139static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002140 struct btrfs_root *root,
2141 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002142{
Chris Mason5f39d392007-10-15 16:14:19 -04002143 struct extent_buffer *right = NULL;
2144 struct extent_buffer *mid;
2145 struct extent_buffer *left = NULL;
2146 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002147 int ret = 0;
2148 int wret;
2149 int pslot;
2150 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002151
2152 if (level == 0)
2153 return 1;
2154
Chris Mason5f39d392007-10-15 16:14:19 -04002155 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002156 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002157
Li Zefana05a9bb2011-09-06 16:55:34 +08002158 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002159 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002160 pslot = path->slots[level + 1];
2161 }
Chris Masone66f7092007-04-20 13:16:02 -04002162
Chris Mason5f39d392007-10-15 16:14:19 -04002163 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002164 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002165
Chris Mason5f39d392007-10-15 16:14:19 -04002166 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002167
2168 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002169 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002170 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002171
2172 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002173 btrfs_set_lock_blocking(left);
2174
Chris Mason5f39d392007-10-15 16:14:19 -04002175 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002176 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2177 wret = 1;
2178 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002179 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002180 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002181 if (ret)
2182 wret = 1;
2183 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002184 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002185 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002186 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002187 }
Chris Masone66f7092007-04-20 13:16:02 -04002188 if (wret < 0)
2189 ret = wret;
2190 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002191 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002192 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002193 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002194 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002195 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002196 btrfs_set_node_key(parent, &disk_key, pslot);
2197 btrfs_mark_buffer_dirty(parent);
2198 if (btrfs_header_nritems(left) > orig_slot) {
2199 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002200 path->slots[level + 1] -= 1;
2201 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002202 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002203 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002204 } else {
2205 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002206 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002207 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002208 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002209 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002210 }
Chris Masone66f7092007-04-20 13:16:02 -04002211 return 0;
2212 }
Chris Mason925baed2008-06-25 16:01:30 -04002213 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002215 }
Chris Mason925baed2008-06-25 16:01:30 -04002216 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002217
2218 /*
2219 * then try to empty the right most buffer into the middle
2220 */
Chris Mason5f39d392007-10-15 16:14:19 -04002221 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002222 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002223
Chris Mason925baed2008-06-25 16:01:30 -04002224 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002225 btrfs_set_lock_blocking(right);
2226
Chris Mason5f39d392007-10-15 16:14:19 -04002227 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002228 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2229 wret = 1;
2230 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002231 ret = btrfs_cow_block(trans, root, right,
2232 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002233 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002234 if (ret)
2235 wret = 1;
2236 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002237 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002238 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002239 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002240 }
Chris Masone66f7092007-04-20 13:16:02 -04002241 if (wret < 0)
2242 ret = wret;
2243 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002244 struct btrfs_disk_key disk_key;
2245
2246 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002247 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002248 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002249 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2250 btrfs_mark_buffer_dirty(parent);
2251
2252 if (btrfs_header_nritems(mid) <= orig_slot) {
2253 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002254 path->slots[level + 1] += 1;
2255 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002256 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002257 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002258 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002259 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002260 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002261 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002262 }
Chris Masone66f7092007-04-20 13:16:02 -04002263 return 0;
2264 }
Chris Mason925baed2008-06-25 16:01:30 -04002265 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002266 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002267 }
Chris Masone66f7092007-04-20 13:16:02 -04002268 return 1;
2269}
2270
Chris Mason74123bd2007-02-02 11:05:29 -05002271/*
Chris Masond352ac62008-09-29 15:18:18 -04002272 * readahead one full node of leaves, finding things that are close
2273 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002274 */
Chris Masonc8c42862009-04-03 10:14:18 -04002275static void reada_for_search(struct btrfs_root *root,
2276 struct btrfs_path *path,
2277 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002278{
Chris Mason5f39d392007-10-15 16:14:19 -04002279 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002280 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002281 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002282 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002283 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002284 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002285 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002286 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002287 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002288 u32 nr;
2289 u32 blocksize;
2290 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002291
Chris Masona6b6e752007-10-15 16:22:39 -04002292 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002293 return;
2294
Chris Mason6702ed42007-08-07 16:15:09 -04002295 if (!path->nodes[level])
2296 return;
2297
Chris Mason5f39d392007-10-15 16:14:19 -04002298 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002299
Chris Mason3c69fae2007-08-07 15:52:22 -04002300 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002301 blocksize = btrfs_level_size(root, level - 1);
2302 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002303 if (eb) {
2304 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002305 return;
2306 }
2307
Chris Masona7175312009-01-22 09:23:10 -05002308 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002309
Chris Mason5f39d392007-10-15 16:14:19 -04002310 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002311 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002312
Chris Masond3977122009-01-05 21:25:51 -05002313 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002314 if (direction < 0) {
2315 if (nr == 0)
2316 break;
2317 nr--;
2318 } else if (direction > 0) {
2319 nr++;
2320 if (nr >= nritems)
2321 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002322 }
Chris Mason01f46652007-12-21 16:24:26 -05002323 if (path->reada < 0 && objectid) {
2324 btrfs_node_key(node, &disk_key, nr);
2325 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2326 break;
2327 }
Chris Mason6b800532007-10-15 16:17:34 -04002328 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002329 if ((search <= target && target - search <= 65536) ||
2330 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002331 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002332 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002333 nread += blocksize;
2334 }
2335 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002336 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002337 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002338 }
2339}
Chris Mason925baed2008-06-25 16:01:30 -04002340
Josef Bacik0b088512013-06-17 14:23:02 -04002341static noinline void reada_for_balance(struct btrfs_root *root,
2342 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002343{
2344 int slot;
2345 int nritems;
2346 struct extent_buffer *parent;
2347 struct extent_buffer *eb;
2348 u64 gen;
2349 u64 block1 = 0;
2350 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002351 int blocksize;
2352
Chris Mason8c594ea2009-04-20 15:50:10 -04002353 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002354 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002355 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002356
2357 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002358 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002359 blocksize = btrfs_level_size(root, level);
2360
2361 if (slot > 0) {
2362 block1 = btrfs_node_blockptr(parent, slot - 1);
2363 gen = btrfs_node_ptr_generation(parent, slot - 1);
2364 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002365 /*
2366 * if we get -eagain from btrfs_buffer_uptodate, we
2367 * don't want to return eagain here. That will loop
2368 * forever
2369 */
2370 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002371 block1 = 0;
2372 free_extent_buffer(eb);
2373 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002374 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002375 block2 = btrfs_node_blockptr(parent, slot + 1);
2376 gen = btrfs_node_ptr_generation(parent, slot + 1);
2377 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002378 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002379 block2 = 0;
2380 free_extent_buffer(eb);
2381 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002382
Josef Bacik0b088512013-06-17 14:23:02 -04002383 if (block1)
2384 readahead_tree_block(root, block1, blocksize, 0);
2385 if (block2)
2386 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002387}
2388
2389
2390/*
Chris Masond3977122009-01-05 21:25:51 -05002391 * when we walk down the tree, it is usually safe to unlock the higher layers
2392 * in the tree. The exceptions are when our path goes through slot 0, because
2393 * operations on the tree might require changing key pointers higher up in the
2394 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002395 *
Chris Masond3977122009-01-05 21:25:51 -05002396 * callers might also have set path->keep_locks, which tells this code to keep
2397 * the lock if the path points to the last slot in the block. This is part of
2398 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002399 *
Chris Masond3977122009-01-05 21:25:51 -05002400 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2401 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002402 */
Chris Masone02119d2008-09-05 16:13:11 -04002403static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002404 int lowest_unlock, int min_write_lock_level,
2405 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002406{
2407 int i;
2408 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002409 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002410 struct extent_buffer *t;
2411
2412 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2413 if (!path->nodes[i])
2414 break;
2415 if (!path->locks[i])
2416 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002417 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002418 skip_level = i + 1;
2419 continue;
2420 }
Chris Mason051e1b92008-06-25 16:01:30 -04002421 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002422 u32 nritems;
2423 t = path->nodes[i];
2424 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002425 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002426 skip_level = i + 1;
2427 continue;
2428 }
2429 }
Chris Mason051e1b92008-06-25 16:01:30 -04002430 if (skip_level < i && i >= lowest_unlock)
2431 no_skips = 1;
2432
Chris Mason925baed2008-06-25 16:01:30 -04002433 t = path->nodes[i];
2434 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002435 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002436 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002437 if (write_lock_level &&
2438 i > min_write_lock_level &&
2439 i <= *write_lock_level) {
2440 *write_lock_level = i - 1;
2441 }
Chris Mason925baed2008-06-25 16:01:30 -04002442 }
2443 }
2444}
2445
Chris Mason3c69fae2007-08-07 15:52:22 -04002446/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002447 * This releases any locks held in the path starting at level and
2448 * going all the way up to the root.
2449 *
2450 * btrfs_search_slot will keep the lock held on higher nodes in a few
2451 * corner cases, such as COW of the block at slot zero in the node. This
2452 * ignores those rules, and it should only be called when there are no
2453 * more updates to be done higher up in the tree.
2454 */
2455noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2456{
2457 int i;
2458
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002459 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002460 return;
2461
2462 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2463 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002464 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002465 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002466 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002467 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002468 path->locks[i] = 0;
2469 }
2470}
2471
2472/*
Chris Masonc8c42862009-04-03 10:14:18 -04002473 * helper function for btrfs_search_slot. The goal is to find a block
2474 * in cache without setting the path to blocking. If we find the block
2475 * we return zero and the path is unchanged.
2476 *
2477 * If we can't find the block, we set the path blocking and do some
2478 * reada. -EAGAIN is returned and the search must be repeated.
2479 */
2480static int
2481read_block_for_search(struct btrfs_trans_handle *trans,
2482 struct btrfs_root *root, struct btrfs_path *p,
2483 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002484 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002485{
2486 u64 blocknr;
2487 u64 gen;
2488 u32 blocksize;
2489 struct extent_buffer *b = *eb_ret;
2490 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002491 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002492
2493 blocknr = btrfs_node_blockptr(b, slot);
2494 gen = btrfs_node_ptr_generation(b, slot);
2495 blocksize = btrfs_level_size(root, level - 1);
2496
2497 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002498 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002499 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002500 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2501 *eb_ret = tmp;
2502 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002503 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002504
2505 /* the pages were up to date, but we failed
2506 * the generation number check. Do a full
2507 * read for the generation number that is correct.
2508 * We must do this without dropping locks so
2509 * we can trust our generation number
2510 */
2511 btrfs_set_path_blocking(p);
2512
2513 /* now we're allowed to do a blocking uptodate check */
2514 ret = btrfs_read_buffer(tmp, gen);
2515 if (!ret) {
2516 *eb_ret = tmp;
2517 return 0;
2518 }
2519 free_extent_buffer(tmp);
2520 btrfs_release_path(p);
2521 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002522 }
2523
2524 /*
2525 * reduce lock contention at high levels
2526 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002527 * we read. Don't release the lock on the current
2528 * level because we need to walk this node to figure
2529 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002530 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002531 btrfs_unlock_up_safe(p, level + 1);
2532 btrfs_set_path_blocking(p);
2533
Chris Masoncb449212010-10-24 11:01:27 -04002534 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002535 if (p->reada)
2536 reada_for_search(root, p, level, slot, key->objectid);
2537
David Sterbab3b4aa72011-04-21 01:20:15 +02002538 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002539
2540 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002541 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002542 if (tmp) {
2543 /*
2544 * If the read above didn't mark this buffer up to date,
2545 * it will never end up being up to date. Set ret to EIO now
2546 * and give up so that our caller doesn't loop forever
2547 * on our EAGAINs.
2548 */
Chris Masonb9fab912012-05-06 07:23:47 -04002549 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002550 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002551 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002552 }
2553 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002554}
2555
2556/*
2557 * helper function for btrfs_search_slot. This does all of the checks
2558 * for node-level blocks and does any balancing required based on
2559 * the ins_len.
2560 *
2561 * If no extra work was required, zero is returned. If we had to
2562 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2563 * start over
2564 */
2565static int
2566setup_nodes_for_search(struct btrfs_trans_handle *trans,
2567 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002568 struct extent_buffer *b, int level, int ins_len,
2569 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002570{
2571 int ret;
2572 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2573 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2574 int sret;
2575
Chris Masonbd681512011-07-16 15:23:14 -04002576 if (*write_lock_level < level + 1) {
2577 *write_lock_level = level + 1;
2578 btrfs_release_path(p);
2579 goto again;
2580 }
2581
Chris Masonc8c42862009-04-03 10:14:18 -04002582 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002583 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002584 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002585 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002586
2587 BUG_ON(sret > 0);
2588 if (sret) {
2589 ret = sret;
2590 goto done;
2591 }
2592 b = p->nodes[level];
2593 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002594 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002595 int sret;
2596
Chris Masonbd681512011-07-16 15:23:14 -04002597 if (*write_lock_level < level + 1) {
2598 *write_lock_level = level + 1;
2599 btrfs_release_path(p);
2600 goto again;
2601 }
2602
Chris Masonc8c42862009-04-03 10:14:18 -04002603 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002604 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002605 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002606 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002607
2608 if (sret) {
2609 ret = sret;
2610 goto done;
2611 }
2612 b = p->nodes[level];
2613 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002614 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002615 goto again;
2616 }
2617 BUG_ON(btrfs_header_nritems(b) == 1);
2618 }
2619 return 0;
2620
2621again:
2622 ret = -EAGAIN;
2623done:
2624 return ret;
2625}
2626
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002627static void key_search_validate(struct extent_buffer *b,
2628 struct btrfs_key *key,
2629 int level)
2630{
2631#ifdef CONFIG_BTRFS_ASSERT
2632 struct btrfs_disk_key disk_key;
2633
2634 btrfs_cpu_key_to_disk(&disk_key, key);
2635
2636 if (level == 0)
2637 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2638 offsetof(struct btrfs_leaf, items[0].key),
2639 sizeof(disk_key)));
2640 else
2641 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2642 offsetof(struct btrfs_node, ptrs[0].key),
2643 sizeof(disk_key)));
2644#endif
2645}
2646
2647static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2648 int level, int *prev_cmp, int *slot)
2649{
2650 if (*prev_cmp != 0) {
2651 *prev_cmp = bin_search(b, key, level, slot);
2652 return *prev_cmp;
2653 }
2654
2655 key_search_validate(b, key, level);
2656 *slot = 0;
2657
2658 return 0;
2659}
2660
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002661int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002662 u64 iobjectid, u64 ioff, u8 key_type,
2663 struct btrfs_key *found_key)
2664{
2665 int ret;
2666 struct btrfs_key key;
2667 struct extent_buffer *eb;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002668 struct btrfs_path *path;
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002669
2670 key.type = key_type;
2671 key.objectid = iobjectid;
2672 key.offset = ioff;
2673
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002674 if (found_path == NULL) {
2675 path = btrfs_alloc_path();
2676 if (!path)
2677 return -ENOMEM;
2678 } else
2679 path = found_path;
2680
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002681 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002682 if ((ret < 0) || (found_key == NULL)) {
2683 if (path != found_path)
2684 btrfs_free_path(path);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002685 return ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002686 }
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002687
2688 eb = path->nodes[0];
2689 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2690 ret = btrfs_next_leaf(fs_root, path);
2691 if (ret)
2692 return ret;
2693 eb = path->nodes[0];
2694 }
2695
2696 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2697 if (found_key->type != key.type ||
2698 found_key->objectid != key.objectid)
2699 return 1;
2700
2701 return 0;
2702}
2703
Chris Masonc8c42862009-04-03 10:14:18 -04002704/*
Chris Mason74123bd2007-02-02 11:05:29 -05002705 * look for key in the tree. path is filled in with nodes along the way
2706 * if key is found, we return zero and you can find the item in the leaf
2707 * level of the path (level 0)
2708 *
2709 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002710 * be inserted, and 1 is returned. If there are other errors during the
2711 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002712 *
2713 * if ins_len > 0, nodes and leaves will be split as we walk down the
2714 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2715 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002716 */
Chris Masone089f052007-03-16 16:20:31 -04002717int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2718 *root, struct btrfs_key *key, struct btrfs_path *p, int
2719 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002720{
Chris Mason5f39d392007-10-15 16:14:19 -04002721 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002722 int slot;
2723 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002724 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002725 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002726 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002727 int root_lock;
2728 /* everything at write_lock_level or lower must be write locked */
2729 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002730 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002731 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002732 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002733
Chris Mason6702ed42007-08-07 16:15:09 -04002734 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002735 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002736 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002737 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002738
Chris Masonbd681512011-07-16 15:23:14 -04002739 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002740 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002741
Chris Masonbd681512011-07-16 15:23:14 -04002742 /* when we are removing items, we might have to go up to level
2743 * two as we update tree pointers Make sure we keep write
2744 * for those levels as well
2745 */
2746 write_lock_level = 2;
2747 } else if (ins_len > 0) {
2748 /*
2749 * for inserting items, make sure we have a write lock on
2750 * level 1 so we can update keys
2751 */
2752 write_lock_level = 1;
2753 }
2754
2755 if (!cow)
2756 write_lock_level = -1;
2757
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002758 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002759 write_lock_level = BTRFS_MAX_LEVEL;
2760
Chris Masonf7c79f32012-03-19 15:54:38 -04002761 min_write_lock_level = write_lock_level;
2762
Chris Masonbb803952007-03-01 12:04:21 -05002763again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002764 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002765 /*
2766 * we try very hard to do read locks on the root
2767 */
2768 root_lock = BTRFS_READ_LOCK;
2769 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002770 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002771 /*
2772 * the commit roots are read only
2773 * so we always do read locks
2774 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002775 if (p->need_commit_sem)
2776 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002777 b = root->commit_root;
2778 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002779 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002780 if (p->need_commit_sem)
2781 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002783 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002784 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002785 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002786 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002787 level = btrfs_header_level(b);
2788 } else {
2789 /* we don't know the level of the root node
2790 * until we actually have it read locked
2791 */
2792 b = btrfs_read_lock_root_node(root);
2793 level = btrfs_header_level(b);
2794 if (level <= write_lock_level) {
2795 /* whoops, must trade for write lock */
2796 btrfs_tree_read_unlock(b);
2797 free_extent_buffer(b);
2798 b = btrfs_lock_root_node(root);
2799 root_lock = BTRFS_WRITE_LOCK;
2800
2801 /* the level might have changed, check again */
2802 level = btrfs_header_level(b);
2803 }
2804 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002805 }
Chris Masonbd681512011-07-16 15:23:14 -04002806 p->nodes[level] = b;
2807 if (!p->skip_locking)
2808 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002809
Chris Masoneb60cea2007-02-02 09:18:22 -05002810 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002811 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002812
2813 /*
2814 * setup the path here so we can release it under lock
2815 * contention with the cow code
2816 */
Chris Mason02217ed2007-03-02 16:08:05 -05002817 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002818 /*
2819 * if we don't really need to cow this block
2820 * then we don't want to set the path blocking,
2821 * so we test it here
2822 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002823 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002824 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002825
Chris Masonb4ce94d2009-02-04 09:25:08 -05002826 btrfs_set_path_blocking(p);
2827
Chris Masonbd681512011-07-16 15:23:14 -04002828 /*
2829 * must have write locks on this node and the
2830 * parent
2831 */
Josef Bacik5124e002012-11-07 13:44:13 -05002832 if (level > write_lock_level ||
2833 (level + 1 > write_lock_level &&
2834 level + 1 < BTRFS_MAX_LEVEL &&
2835 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002836 write_lock_level = level + 1;
2837 btrfs_release_path(p);
2838 goto again;
2839 }
2840
Yan Zheng33c66f42009-07-22 09:59:00 -04002841 err = btrfs_cow_block(trans, root, b,
2842 p->nodes[level + 1],
2843 p->slots[level + 1], &b);
2844 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002845 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002846 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002847 }
Chris Mason02217ed2007-03-02 16:08:05 -05002848 }
Chris Mason65b51a02008-08-01 15:11:20 -04002849cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002850 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002851 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002852
2853 /*
2854 * we have a lock on b and as long as we aren't changing
2855 * the tree, there is no way to for the items in b to change.
2856 * It is safe to drop the lock on our parent before we
2857 * go through the expensive btree search on b.
2858 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002859 * If we're inserting or deleting (ins_len != 0), then we might
2860 * be changing slot zero, which may require changing the parent.
2861 * So, we can't drop the lock until after we know which slot
2862 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002863 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002864 if (!ins_len && !p->keep_locks) {
2865 int u = level + 1;
2866
2867 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2868 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2869 p->locks[u] = 0;
2870 }
2871 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002872
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002873 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002874
Chris Mason5f39d392007-10-15 16:14:19 -04002875 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002876 int dec = 0;
2877 if (ret && slot > 0) {
2878 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002879 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002880 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002881 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002882 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002883 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002884 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002885 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002886 if (err) {
2887 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002888 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002889 }
Chris Masonc8c42862009-04-03 10:14:18 -04002890 b = p->nodes[level];
2891 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002892
Chris Masonbd681512011-07-16 15:23:14 -04002893 /*
2894 * slot 0 is special, if we change the key
2895 * we have to update the parent pointer
2896 * which means we must have a write lock
2897 * on the parent
2898 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002899 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002900 write_lock_level < level + 1) {
2901 write_lock_level = level + 1;
2902 btrfs_release_path(p);
2903 goto again;
2904 }
2905
Chris Masonf7c79f32012-03-19 15:54:38 -04002906 unlock_up(p, level, lowest_unlock,
2907 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002908
Chris Mason925baed2008-06-25 16:01:30 -04002909 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002910 if (dec)
2911 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002912 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002913 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002914
Yan Zheng33c66f42009-07-22 09:59:00 -04002915 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002916 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002917 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002918 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002919 if (err) {
2920 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002921 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002922 }
Chris Mason76a05b32009-05-14 13:24:30 -04002923
Chris Masonb4ce94d2009-02-04 09:25:08 -05002924 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002925 level = btrfs_header_level(b);
2926 if (level <= write_lock_level) {
2927 err = btrfs_try_tree_write_lock(b);
2928 if (!err) {
2929 btrfs_set_path_blocking(p);
2930 btrfs_tree_lock(b);
2931 btrfs_clear_path_blocking(p, b,
2932 BTRFS_WRITE_LOCK);
2933 }
2934 p->locks[level] = BTRFS_WRITE_LOCK;
2935 } else {
2936 err = btrfs_try_tree_read_lock(b);
2937 if (!err) {
2938 btrfs_set_path_blocking(p);
2939 btrfs_tree_read_lock(b);
2940 btrfs_clear_path_blocking(p, b,
2941 BTRFS_READ_LOCK);
2942 }
2943 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002944 }
Chris Masonbd681512011-07-16 15:23:14 -04002945 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002946 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002947 } else {
2948 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002949 if (ins_len > 0 &&
2950 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002951 if (write_lock_level < 1) {
2952 write_lock_level = 1;
2953 btrfs_release_path(p);
2954 goto again;
2955 }
2956
Chris Masonb4ce94d2009-02-04 09:25:08 -05002957 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002958 err = split_leaf(trans, root, key,
2959 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002960 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002961
Yan Zheng33c66f42009-07-22 09:59:00 -04002962 BUG_ON(err > 0);
2963 if (err) {
2964 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002965 goto done;
2966 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002967 }
Chris Mason459931e2008-12-10 09:10:46 -05002968 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002969 unlock_up(p, level, lowest_unlock,
2970 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002971 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002972 }
2973 }
Chris Mason65b51a02008-08-01 15:11:20 -04002974 ret = 1;
2975done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002976 /*
2977 * we don't really know what they plan on doing with the path
2978 * from here on, so for now just mark it as blocking
2979 */
Chris Masonb9473432009-03-13 11:00:37 -04002980 if (!p->leave_spinning)
2981 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002982 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002983 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002984 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002985}
2986
Chris Mason74123bd2007-02-02 11:05:29 -05002987/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002988 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2989 * current state of the tree together with the operations recorded in the tree
2990 * modification log to search for the key in a previous version of this tree, as
2991 * denoted by the time_seq parameter.
2992 *
2993 * Naturally, there is no support for insert, delete or cow operations.
2994 *
2995 * The resulting path and return value will be set up as if we called
2996 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2997 */
2998int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2999 struct btrfs_path *p, u64 time_seq)
3000{
3001 struct extent_buffer *b;
3002 int slot;
3003 int ret;
3004 int err;
3005 int level;
3006 int lowest_unlock = 1;
3007 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04003008 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003009
3010 lowest_level = p->lowest_level;
3011 WARN_ON(p->nodes[0] != NULL);
3012
3013 if (p->search_commit_root) {
3014 BUG_ON(time_seq);
3015 return btrfs_search_slot(NULL, root, key, p, 0, 0);
3016 }
3017
3018again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003019 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003020 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003021 p->locks[level] = BTRFS_READ_LOCK;
3022
3023 while (b) {
3024 level = btrfs_header_level(b);
3025 p->nodes[level] = b;
3026 btrfs_clear_path_blocking(p, NULL, 0);
3027
3028 /*
3029 * we have a lock on b and as long as we aren't changing
3030 * the tree, there is no way to for the items in b to change.
3031 * It is safe to drop the lock on our parent before we
3032 * go through the expensive btree search on b.
3033 */
3034 btrfs_unlock_up_safe(p, level + 1);
3035
Josef Bacikd4b40872013-09-24 14:09:34 -04003036 /*
3037 * Since we can unwind eb's we want to do a real search every
3038 * time.
3039 */
3040 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01003041 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003042
3043 if (level != 0) {
3044 int dec = 0;
3045 if (ret && slot > 0) {
3046 dec = 1;
3047 slot -= 1;
3048 }
3049 p->slots[level] = slot;
3050 unlock_up(p, level, lowest_unlock, 0, NULL);
3051
3052 if (level == lowest_level) {
3053 if (dec)
3054 p->slots[level]++;
3055 goto done;
3056 }
3057
3058 err = read_block_for_search(NULL, root, p, &b, level,
3059 slot, key, time_seq);
3060 if (err == -EAGAIN)
3061 goto again;
3062 if (err) {
3063 ret = err;
3064 goto done;
3065 }
3066
3067 level = btrfs_header_level(b);
3068 err = btrfs_try_tree_read_lock(b);
3069 if (!err) {
3070 btrfs_set_path_blocking(p);
3071 btrfs_tree_read_lock(b);
3072 btrfs_clear_path_blocking(p, b,
3073 BTRFS_READ_LOCK);
3074 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003075 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003076 if (!b) {
3077 ret = -ENOMEM;
3078 goto done;
3079 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003080 p->locks[level] = BTRFS_READ_LOCK;
3081 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003082 } else {
3083 p->slots[level] = slot;
3084 unlock_up(p, level, lowest_unlock, 0, NULL);
3085 goto done;
3086 }
3087 }
3088 ret = 1;
3089done:
3090 if (!p->leave_spinning)
3091 btrfs_set_path_blocking(p);
3092 if (ret < 0)
3093 btrfs_release_path(p);
3094
3095 return ret;
3096}
3097
3098/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003099 * helper to use instead of search slot if no exact match is needed but
3100 * instead the next or previous item should be returned.
3101 * When find_higher is true, the next higher item is returned, the next lower
3102 * otherwise.
3103 * When return_any and find_higher are both true, and no higher item is found,
3104 * return the next lower instead.
3105 * When return_any is true and find_higher is false, and no lower item is found,
3106 * return the next higher instead.
3107 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3108 * < 0 on error
3109 */
3110int btrfs_search_slot_for_read(struct btrfs_root *root,
3111 struct btrfs_key *key, struct btrfs_path *p,
3112 int find_higher, int return_any)
3113{
3114 int ret;
3115 struct extent_buffer *leaf;
3116
3117again:
3118 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3119 if (ret <= 0)
3120 return ret;
3121 /*
3122 * a return value of 1 means the path is at the position where the
3123 * item should be inserted. Normally this is the next bigger item,
3124 * but in case the previous item is the last in a leaf, path points
3125 * to the first free slot in the previous leaf, i.e. at an invalid
3126 * item.
3127 */
3128 leaf = p->nodes[0];
3129
3130 if (find_higher) {
3131 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3132 ret = btrfs_next_leaf(root, p);
3133 if (ret <= 0)
3134 return ret;
3135 if (!return_any)
3136 return 1;
3137 /*
3138 * no higher item found, return the next
3139 * lower instead
3140 */
3141 return_any = 0;
3142 find_higher = 0;
3143 btrfs_release_path(p);
3144 goto again;
3145 }
3146 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003147 if (p->slots[0] == 0) {
3148 ret = btrfs_prev_leaf(root, p);
3149 if (ret < 0)
3150 return ret;
3151 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003152 leaf = p->nodes[0];
3153 if (p->slots[0] == btrfs_header_nritems(leaf))
3154 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003155 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003156 }
Arne Jansene6793762011-09-13 11:18:10 +02003157 if (!return_any)
3158 return 1;
3159 /*
3160 * no lower item found, return the next
3161 * higher instead
3162 */
3163 return_any = 0;
3164 find_higher = 1;
3165 btrfs_release_path(p);
3166 goto again;
3167 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003168 --p->slots[0];
3169 }
3170 }
3171 return 0;
3172}
3173
3174/*
Chris Mason74123bd2007-02-02 11:05:29 -05003175 * adjust the pointers going up the tree, starting at level
3176 * making sure the right key of each node is points to 'key'.
3177 * This is used after shifting pointers to the left, so it stops
3178 * fixing up pointers when a given leaf/node is not in slot 0 of the
3179 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003180 *
Chris Mason74123bd2007-02-02 11:05:29 -05003181 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003182static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003183 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003184{
3185 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003186 struct extent_buffer *t;
3187
Chris Mason234b63a2007-03-13 10:46:10 -04003188 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003189 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003190 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003191 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003192 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00003193 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003194 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003195 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003196 if (tslot != 0)
3197 break;
3198 }
3199}
3200
Chris Mason74123bd2007-02-02 11:05:29 -05003201/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003202 * update item key.
3203 *
3204 * This function isn't completely safe. It's the caller's responsibility
3205 * that the new key won't break the order
3206 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003207void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003208 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003209{
3210 struct btrfs_disk_key disk_key;
3211 struct extent_buffer *eb;
3212 int slot;
3213
3214 eb = path->nodes[0];
3215 slot = path->slots[0];
3216 if (slot > 0) {
3217 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003218 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003219 }
3220 if (slot < btrfs_header_nritems(eb) - 1) {
3221 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003222 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003223 }
3224
3225 btrfs_cpu_key_to_disk(&disk_key, new_key);
3226 btrfs_set_item_key(eb, &disk_key, slot);
3227 btrfs_mark_buffer_dirty(eb);
3228 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003229 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003230}
3231
3232/*
Chris Mason74123bd2007-02-02 11:05:29 -05003233 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003234 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003235 *
3236 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3237 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003238 */
Chris Mason98ed5172008-01-03 10:01:48 -05003239static int push_node_left(struct btrfs_trans_handle *trans,
3240 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003241 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003242{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003243 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003244 int src_nritems;
3245 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003246 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003247
Chris Mason5f39d392007-10-15 16:14:19 -04003248 src_nritems = btrfs_header_nritems(src);
3249 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003250 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003251 WARN_ON(btrfs_header_generation(src) != trans->transid);
3252 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003253
Chris Masonbce4eae2008-04-24 14:42:46 -04003254 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003255 return 1;
3256
Chris Masond3977122009-01-05 21:25:51 -05003257 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003258 return 1;
3259
Chris Masonbce4eae2008-04-24 14:42:46 -04003260 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003261 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003262 if (push_items < src_nritems) {
3263 /* leave at least 8 pointers in the node if
3264 * we aren't going to empty it
3265 */
3266 if (src_nritems - push_items < 8) {
3267 if (push_items <= 8)
3268 return 1;
3269 push_items -= 8;
3270 }
3271 }
3272 } else
3273 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003274
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003275 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3276 push_items);
3277 if (ret) {
3278 btrfs_abort_transaction(trans, root, ret);
3279 return ret;
3280 }
Chris Mason5f39d392007-10-15 16:14:19 -04003281 copy_extent_buffer(dst, src,
3282 btrfs_node_key_ptr_offset(dst_nritems),
3283 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003284 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003285
Chris Masonbb803952007-03-01 12:04:21 -05003286 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003287 /*
3288 * don't call tree_mod_log_eb_move here, key removal was already
3289 * fully logged by tree_mod_log_eb_copy above.
3290 */
Chris Mason5f39d392007-10-15 16:14:19 -04003291 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3292 btrfs_node_key_ptr_offset(push_items),
3293 (src_nritems - push_items) *
3294 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003295 }
Chris Mason5f39d392007-10-15 16:14:19 -04003296 btrfs_set_header_nritems(src, src_nritems - push_items);
3297 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3298 btrfs_mark_buffer_dirty(src);
3299 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003300
Chris Masonbb803952007-03-01 12:04:21 -05003301 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003302}
3303
Chris Mason97571fd2007-02-24 13:39:08 -05003304/*
Chris Mason79f95c82007-03-01 15:16:26 -05003305 * try to push data from one node into the next node right in the
3306 * tree.
3307 *
3308 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3309 * error, and > 0 if there was no room in the right hand block.
3310 *
3311 * this will only push up to 1/2 the contents of the left node over
3312 */
Chris Mason5f39d392007-10-15 16:14:19 -04003313static int balance_node_right(struct btrfs_trans_handle *trans,
3314 struct btrfs_root *root,
3315 struct extent_buffer *dst,
3316 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003317{
Chris Mason79f95c82007-03-01 15:16:26 -05003318 int push_items = 0;
3319 int max_push;
3320 int src_nritems;
3321 int dst_nritems;
3322 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003323
Chris Mason7bb86312007-12-11 09:25:06 -05003324 WARN_ON(btrfs_header_generation(src) != trans->transid);
3325 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3326
Chris Mason5f39d392007-10-15 16:14:19 -04003327 src_nritems = btrfs_header_nritems(src);
3328 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003329 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003330 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003331 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003332
Chris Masond3977122009-01-05 21:25:51 -05003333 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003334 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003335
3336 max_push = src_nritems / 2 + 1;
3337 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003338 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003339 return 1;
Yan252c38f2007-08-29 09:11:44 -04003340
Chris Mason79f95c82007-03-01 15:16:26 -05003341 if (max_push < push_items)
3342 push_items = max_push;
3343
Jan Schmidtf2304752012-05-26 11:43:17 +02003344 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003345 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3346 btrfs_node_key_ptr_offset(0),
3347 (dst_nritems) *
3348 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003349
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003350 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3351 src_nritems - push_items, push_items);
3352 if (ret) {
3353 btrfs_abort_transaction(trans, root, ret);
3354 return ret;
3355 }
Chris Mason5f39d392007-10-15 16:14:19 -04003356 copy_extent_buffer(dst, src,
3357 btrfs_node_key_ptr_offset(0),
3358 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003359 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003360
Chris Mason5f39d392007-10-15 16:14:19 -04003361 btrfs_set_header_nritems(src, src_nritems - push_items);
3362 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 btrfs_mark_buffer_dirty(src);
3365 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003366
Chris Mason79f95c82007-03-01 15:16:26 -05003367 return ret;
3368}
3369
3370/*
Chris Mason97571fd2007-02-24 13:39:08 -05003371 * helper function to insert a new root level in the tree.
3372 * A new node is allocated, and a single item is inserted to
3373 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003374 *
3375 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003376 */
Chris Masond3977122009-01-05 21:25:51 -05003377static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003378 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003379 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003380{
Chris Mason7bb86312007-12-11 09:25:06 -05003381 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003382 struct extent_buffer *lower;
3383 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003384 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003385 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003386
3387 BUG_ON(path->nodes[level]);
3388 BUG_ON(path->nodes[level-1] != root->node);
3389
Chris Mason7bb86312007-12-11 09:25:06 -05003390 lower = path->nodes[level-1];
3391 if (level == 1)
3392 btrfs_item_key(lower, &lower_key, 0);
3393 else
3394 btrfs_node_key(lower, &lower_key, 0);
3395
Zheng Yan31840ae2008-09-23 13:14:14 -04003396 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003397 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003398 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003399 if (IS_ERR(c))
3400 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003401
Yan, Zhengf0486c62010-05-16 10:46:25 -04003402 root_add_used(root, root->nodesize);
3403
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003404 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003405 btrfs_set_header_nritems(c, 1);
3406 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003407 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003408 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003409 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003410 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003411
Ross Kirk0a4e5582013-09-24 10:12:38 +01003412 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003413 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003414
3415 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003416 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003417
Chris Mason5f39d392007-10-15 16:14:19 -04003418 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003419 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003420 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003421 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003422
3423 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003424
3425 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003426
Chris Mason925baed2008-06-25 16:01:30 -04003427 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003428 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003429 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003430
3431 /* the super has an extra ref to root->node */
3432 free_extent_buffer(old);
3433
Chris Mason0b86a832008-03-24 15:01:56 -04003434 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003435 extent_buffer_get(c);
3436 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003437 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003438 path->slots[level] = 0;
3439 return 0;
3440}
3441
Chris Mason74123bd2007-02-02 11:05:29 -05003442/*
3443 * worker function to insert a single pointer in a node.
3444 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003445 *
Chris Mason74123bd2007-02-02 11:05:29 -05003446 * slot and level indicate where you want the key to go, and
3447 * blocknr is the block the key points to.
3448 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003449static void insert_ptr(struct btrfs_trans_handle *trans,
3450 struct btrfs_root *root, struct btrfs_path *path,
3451 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003452 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003453{
Chris Mason5f39d392007-10-15 16:14:19 -04003454 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003455 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003456 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003457
3458 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003459 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003460 lower = path->nodes[level];
3461 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003462 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003463 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003464 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003465 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003466 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3467 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003468 memmove_extent_buffer(lower,
3469 btrfs_node_key_ptr_offset(slot + 1),
3470 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003471 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003472 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003473 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003474 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003475 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003476 BUG_ON(ret < 0);
3477 }
Chris Mason5f39d392007-10-15 16:14:19 -04003478 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003479 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003480 WARN_ON(trans->transid == 0);
3481 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_set_header_nritems(lower, nritems + 1);
3483 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003484}
3485
Chris Mason97571fd2007-02-24 13:39:08 -05003486/*
3487 * split the node at the specified level in path in two.
3488 * The path is corrected to point to the appropriate node after the split
3489 *
3490 * Before splitting this tries to make some room in the node by pushing
3491 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003492 *
3493 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003494 */
Chris Masone02119d2008-09-05 16:13:11 -04003495static noinline int split_node(struct btrfs_trans_handle *trans,
3496 struct btrfs_root *root,
3497 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003498{
Chris Mason5f39d392007-10-15 16:14:19 -04003499 struct extent_buffer *c;
3500 struct extent_buffer *split;
3501 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003502 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003503 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003504 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003505
Chris Mason5f39d392007-10-15 16:14:19 -04003506 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003507 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003508 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003509 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003510 * trying to split the root, lets make a new one
3511 *
Liu Bofdd99c72013-05-22 12:06:51 +00003512 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003513 * insert_new_root, because that root buffer will be kept as a
3514 * normal node. We are going to log removal of half of the
3515 * elements below with tree_mod_log_eb_copy. We're holding a
3516 * tree lock on the buffer, which is why we cannot race with
3517 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003518 */
Liu Bofdd99c72013-05-22 12:06:51 +00003519 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003520 if (ret)
3521 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003522 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003523 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003524 c = path->nodes[level];
3525 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003526 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003527 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003528 if (ret < 0)
3529 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003530 }
Chris Masone66f7092007-04-20 13:16:02 -04003531
Chris Mason5f39d392007-10-15 16:14:19 -04003532 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003533 mid = (c_nritems + 1) / 2;
3534 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003535
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003536 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003537 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003538 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003539 if (IS_ERR(split))
3540 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003541
Yan, Zhengf0486c62010-05-16 10:46:25 -04003542 root_add_used(root, root->nodesize);
3543
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003544 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003545 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003546 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003547 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003548 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003549 btrfs_set_header_owner(split, root->root_key.objectid);
3550 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003551 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003552 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003553 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003554 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003555
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003556 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3557 mid, c_nritems - mid);
3558 if (ret) {
3559 btrfs_abort_transaction(trans, root, ret);
3560 return ret;
3561 }
Chris Mason5f39d392007-10-15 16:14:19 -04003562 copy_extent_buffer(split, c,
3563 btrfs_node_key_ptr_offset(0),
3564 btrfs_node_key_ptr_offset(mid),
3565 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3566 btrfs_set_header_nritems(split, c_nritems - mid);
3567 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003568 ret = 0;
3569
Chris Mason5f39d392007-10-15 16:14:19 -04003570 btrfs_mark_buffer_dirty(c);
3571 btrfs_mark_buffer_dirty(split);
3572
Jeff Mahoney143bede2012-03-01 14:56:26 +01003573 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003574 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003575
Chris Mason5de08d72007-02-24 06:24:44 -05003576 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003577 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003578 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003579 free_extent_buffer(c);
3580 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003581 path->slots[level + 1] += 1;
3582 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003583 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003584 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003585 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003586 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003587}
3588
Chris Mason74123bd2007-02-02 11:05:29 -05003589/*
3590 * how many bytes are required to store the items in a leaf. start
3591 * and nr indicate which items in the leaf to check. This totals up the
3592 * space used both by the item structs and the item data
3593 */
Chris Mason5f39d392007-10-15 16:14:19 -04003594static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003595{
Josef Bacik41be1f32012-10-15 13:43:18 -04003596 struct btrfs_item *start_item;
3597 struct btrfs_item *end_item;
3598 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003599 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003600 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003601 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003602
3603 if (!nr)
3604 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003605 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003606 start_item = btrfs_item_nr(start);
3607 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003608 data_len = btrfs_token_item_offset(l, start_item, &token) +
3609 btrfs_token_item_size(l, start_item, &token);
3610 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003611 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003612 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003613 return data_len;
3614}
3615
Chris Mason74123bd2007-02-02 11:05:29 -05003616/*
Chris Masond4dbff92007-04-04 14:08:15 -04003617 * The space between the end of the leaf items and
3618 * the start of the leaf data. IOW, how much room
3619 * the leaf has left for both items and data
3620 */
Chris Masond3977122009-01-05 21:25:51 -05003621noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003622 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003623{
Chris Mason5f39d392007-10-15 16:14:19 -04003624 int nritems = btrfs_header_nritems(leaf);
3625 int ret;
3626 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3627 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003628 btrfs_crit(root->fs_info,
3629 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003630 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003631 leaf_space_used(leaf, 0, nritems), nritems);
3632 }
3633 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003634}
3635
Chris Mason99d8f832010-07-07 10:51:48 -04003636/*
3637 * min slot controls the lowest index we're willing to push to the
3638 * right. We'll push up to and including min_slot, but no lower
3639 */
Chris Mason44871b12009-03-13 10:04:31 -04003640static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3641 struct btrfs_root *root,
3642 struct btrfs_path *path,
3643 int data_size, int empty,
3644 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003645 int free_space, u32 left_nritems,
3646 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003647{
Chris Mason5f39d392007-10-15 16:14:19 -04003648 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003649 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003650 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003651 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003652 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003653 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003654 int push_space = 0;
3655 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003656 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003657 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003658 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003659 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003660 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003661
Chris Masoncfed81a2012-03-03 07:40:03 -05003662 btrfs_init_map_token(&token);
3663
Chris Mason34a38212007-11-07 13:31:03 -05003664 if (empty)
3665 nr = 0;
3666 else
Chris Mason99d8f832010-07-07 10:51:48 -04003667 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003668
Zheng Yan31840ae2008-09-23 13:14:14 -04003669 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003670 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003671
Chris Mason44871b12009-03-13 10:04:31 -04003672 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003673 i = left_nritems - 1;
3674 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003675 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003676
Zheng Yan31840ae2008-09-23 13:14:14 -04003677 if (!empty && push_items > 0) {
3678 if (path->slots[0] > i)
3679 break;
3680 if (path->slots[0] == i) {
3681 int space = btrfs_leaf_free_space(root, left);
3682 if (space + push_space * 2 > free_space)
3683 break;
3684 }
3685 }
3686
Chris Mason00ec4c52007-02-24 12:47:20 -05003687 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003688 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003689
Chris Masondb945352007-10-15 16:15:53 -04003690 this_item_size = btrfs_item_size(left, item);
3691 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003692 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003693
Chris Mason00ec4c52007-02-24 12:47:20 -05003694 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003695 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003696 if (i == 0)
3697 break;
3698 i--;
Chris Masondb945352007-10-15 16:15:53 -04003699 }
Chris Mason5f39d392007-10-15 16:14:19 -04003700
Chris Mason925baed2008-06-25 16:01:30 -04003701 if (push_items == 0)
3702 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003703
Julia Lawall6c1500f2012-11-03 20:30:18 +00003704 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003705
Chris Mason00ec4c52007-02-24 12:47:20 -05003706 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003707 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003708
Chris Mason5f39d392007-10-15 16:14:19 -04003709 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003710 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003711
Chris Mason00ec4c52007-02-24 12:47:20 -05003712 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003713 data_end = leaf_data_end(root, right);
3714 memmove_extent_buffer(right,
3715 btrfs_leaf_data(right) + data_end - push_space,
3716 btrfs_leaf_data(right) + data_end,
3717 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3718
Chris Mason00ec4c52007-02-24 12:47:20 -05003719 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003720 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003721 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3722 btrfs_leaf_data(left) + leaf_data_end(root, left),
3723 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003724
3725 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3726 btrfs_item_nr_offset(0),
3727 right_nritems * sizeof(struct btrfs_item));
3728
Chris Mason00ec4c52007-02-24 12:47:20 -05003729 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003730 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3731 btrfs_item_nr_offset(left_nritems - push_items),
3732 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003733
3734 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003735 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003736 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003737 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003738 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003739 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003740 push_space -= btrfs_token_item_size(right, item, &token);
3741 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003742 }
3743
Chris Mason7518a232007-03-12 12:01:18 -04003744 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003745 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003746
Chris Mason34a38212007-11-07 13:31:03 -05003747 if (left_nritems)
3748 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003749 else
3750 clean_tree_block(trans, root, left);
3751
Chris Mason5f39d392007-10-15 16:14:19 -04003752 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003753
Chris Mason5f39d392007-10-15 16:14:19 -04003754 btrfs_item_key(right, &disk_key, 0);
3755 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003756 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003757
Chris Mason00ec4c52007-02-24 12:47:20 -05003758 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003759 if (path->slots[0] >= left_nritems) {
3760 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003761 if (btrfs_header_nritems(path->nodes[0]) == 0)
3762 clean_tree_block(trans, root, path->nodes[0]);
3763 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003764 free_extent_buffer(path->nodes[0]);
3765 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003766 path->slots[1] += 1;
3767 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003768 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003769 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003770 }
3771 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003772
3773out_unlock:
3774 btrfs_tree_unlock(right);
3775 free_extent_buffer(right);
3776 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003777}
Chris Mason925baed2008-06-25 16:01:30 -04003778
Chris Mason00ec4c52007-02-24 12:47:20 -05003779/*
Chris Mason44871b12009-03-13 10:04:31 -04003780 * push some data in the path leaf to the right, trying to free up at
3781 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3782 *
3783 * returns 1 if the push failed because the other node didn't have enough
3784 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003785 *
3786 * this will push starting from min_slot to the end of the leaf. It won't
3787 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003788 */
3789static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003790 *root, struct btrfs_path *path,
3791 int min_data_size, int data_size,
3792 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003793{
3794 struct extent_buffer *left = path->nodes[0];
3795 struct extent_buffer *right;
3796 struct extent_buffer *upper;
3797 int slot;
3798 int free_space;
3799 u32 left_nritems;
3800 int ret;
3801
3802 if (!path->nodes[1])
3803 return 1;
3804
3805 slot = path->slots[1];
3806 upper = path->nodes[1];
3807 if (slot >= btrfs_header_nritems(upper) - 1)
3808 return 1;
3809
3810 btrfs_assert_tree_locked(path->nodes[1]);
3811
3812 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003813 if (right == NULL)
3814 return 1;
3815
Chris Mason44871b12009-03-13 10:04:31 -04003816 btrfs_tree_lock(right);
3817 btrfs_set_lock_blocking(right);
3818
3819 free_space = btrfs_leaf_free_space(root, right);
3820 if (free_space < data_size)
3821 goto out_unlock;
3822
3823 /* cow and double check */
3824 ret = btrfs_cow_block(trans, root, right, upper,
3825 slot + 1, &right);
3826 if (ret)
3827 goto out_unlock;
3828
3829 free_space = btrfs_leaf_free_space(root, right);
3830 if (free_space < data_size)
3831 goto out_unlock;
3832
3833 left_nritems = btrfs_header_nritems(left);
3834 if (left_nritems == 0)
3835 goto out_unlock;
3836
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003837 if (path->slots[0] == left_nritems && !empty) {
3838 /* Key greater than all keys in the leaf, right neighbor has
3839 * enough room for it and we're not emptying our leaf to delete
3840 * it, therefore use right neighbor to insert the new item and
3841 * no need to touch/dirty our left leaft. */
3842 btrfs_tree_unlock(left);
3843 free_extent_buffer(left);
3844 path->nodes[0] = right;
3845 path->slots[0] = 0;
3846 path->slots[1]++;
3847 return 0;
3848 }
3849
Chris Mason99d8f832010-07-07 10:51:48 -04003850 return __push_leaf_right(trans, root, path, min_data_size, empty,
3851 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003852out_unlock:
3853 btrfs_tree_unlock(right);
3854 free_extent_buffer(right);
3855 return 1;
3856}
3857
3858/*
Chris Mason74123bd2007-02-02 11:05:29 -05003859 * push some data in the path leaf to the left, trying to free up at
3860 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003861 *
3862 * max_slot can put a limit on how far into the leaf we'll push items. The
3863 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3864 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003865 */
Chris Mason44871b12009-03-13 10:04:31 -04003866static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3867 struct btrfs_root *root,
3868 struct btrfs_path *path, int data_size,
3869 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003870 int free_space, u32 right_nritems,
3871 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003872{
Chris Mason5f39d392007-10-15 16:14:19 -04003873 struct btrfs_disk_key disk_key;
3874 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003875 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003876 int push_space = 0;
3877 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003878 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003879 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003880 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003881 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003882 u32 this_item_size;
3883 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003884 struct btrfs_map_token token;
3885
3886 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003887
Chris Mason34a38212007-11-07 13:31:03 -05003888 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003889 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003890 else
Chris Mason99d8f832010-07-07 10:51:48 -04003891 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003892
3893 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003894 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003895
Zheng Yan31840ae2008-09-23 13:14:14 -04003896 if (!empty && push_items > 0) {
3897 if (path->slots[0] < i)
3898 break;
3899 if (path->slots[0] == i) {
3900 int space = btrfs_leaf_free_space(root, right);
3901 if (space + push_space * 2 > free_space)
3902 break;
3903 }
3904 }
3905
Chris Masonbe0e5c02007-01-26 15:51:26 -05003906 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003907 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003908
3909 this_item_size = btrfs_item_size(right, item);
3910 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003911 break;
Chris Masondb945352007-10-15 16:15:53 -04003912
Chris Masonbe0e5c02007-01-26 15:51:26 -05003913 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003914 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003915 }
Chris Masondb945352007-10-15 16:15:53 -04003916
Chris Masonbe0e5c02007-01-26 15:51:26 -05003917 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003918 ret = 1;
3919 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003920 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303921 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003922
Chris Masonbe0e5c02007-01-26 15:51:26 -05003923 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003924 copy_extent_buffer(left, right,
3925 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3926 btrfs_item_nr_offset(0),
3927 push_items * sizeof(struct btrfs_item));
3928
Chris Mason123abc82007-03-14 14:14:43 -04003929 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003930 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003931
3932 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003933 leaf_data_end(root, left) - push_space,
3934 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003935 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003936 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003937 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003938 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003939
Chris Masondb945352007-10-15 16:15:53 -04003940 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003941 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003942 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003943
Ross Kirkdd3cc162013-09-16 15:58:09 +01003944 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003945
Chris Masoncfed81a2012-03-03 07:40:03 -05003946 ioff = btrfs_token_item_offset(left, item, &token);
3947 btrfs_set_token_item_offset(left, item,
3948 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3949 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003950 }
Chris Mason5f39d392007-10-15 16:14:19 -04003951 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003952
3953 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003954 if (push_items > right_nritems)
3955 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003956 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003957
Chris Mason34a38212007-11-07 13:31:03 -05003958 if (push_items < right_nritems) {
3959 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3960 leaf_data_end(root, right);
3961 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3962 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3963 btrfs_leaf_data(right) +
3964 leaf_data_end(root, right), push_space);
3965
3966 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003967 btrfs_item_nr_offset(push_items),
3968 (btrfs_header_nritems(right) - push_items) *
3969 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003970 }
Yaneef1c492007-11-26 10:58:13 -05003971 right_nritems -= push_items;
3972 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003973 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003974 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003975 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003976
Chris Masoncfed81a2012-03-03 07:40:03 -05003977 push_space = push_space - btrfs_token_item_size(right,
3978 item, &token);
3979 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003980 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003981
Chris Mason5f39d392007-10-15 16:14:19 -04003982 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003983 if (right_nritems)
3984 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003985 else
3986 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003987
Chris Mason5f39d392007-10-15 16:14:19 -04003988 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003989 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003990
3991 /* then fixup the leaf pointer in the path */
3992 if (path->slots[0] < push_items) {
3993 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003994 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003995 free_extent_buffer(path->nodes[0]);
3996 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003997 path->slots[1] -= 1;
3998 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003999 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04004000 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004001 path->slots[0] -= push_items;
4002 }
Chris Masoneb60cea2007-02-02 09:18:22 -05004003 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004004 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04004005out:
4006 btrfs_tree_unlock(left);
4007 free_extent_buffer(left);
4008 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004009}
4010
Chris Mason74123bd2007-02-02 11:05:29 -05004011/*
Chris Mason44871b12009-03-13 10:04:31 -04004012 * push some data in the path leaf to the left, trying to free up at
4013 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04004014 *
4015 * max_slot can put a limit on how far into the leaf we'll push items. The
4016 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
4017 * items
Chris Mason44871b12009-03-13 10:04:31 -04004018 */
4019static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04004020 *root, struct btrfs_path *path, int min_data_size,
4021 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04004022{
4023 struct extent_buffer *right = path->nodes[0];
4024 struct extent_buffer *left;
4025 int slot;
4026 int free_space;
4027 u32 right_nritems;
4028 int ret = 0;
4029
4030 slot = path->slots[1];
4031 if (slot == 0)
4032 return 1;
4033 if (!path->nodes[1])
4034 return 1;
4035
4036 right_nritems = btrfs_header_nritems(right);
4037 if (right_nritems == 0)
4038 return 1;
4039
4040 btrfs_assert_tree_locked(path->nodes[1]);
4041
4042 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004043 if (left == NULL)
4044 return 1;
4045
Chris Mason44871b12009-03-13 10:04:31 -04004046 btrfs_tree_lock(left);
4047 btrfs_set_lock_blocking(left);
4048
4049 free_space = btrfs_leaf_free_space(root, left);
4050 if (free_space < data_size) {
4051 ret = 1;
4052 goto out;
4053 }
4054
4055 /* cow and double check */
4056 ret = btrfs_cow_block(trans, root, left,
4057 path->nodes[1], slot - 1, &left);
4058 if (ret) {
4059 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004060 if (ret == -ENOSPC)
4061 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004062 goto out;
4063 }
4064
4065 free_space = btrfs_leaf_free_space(root, left);
4066 if (free_space < data_size) {
4067 ret = 1;
4068 goto out;
4069 }
4070
Chris Mason99d8f832010-07-07 10:51:48 -04004071 return __push_leaf_left(trans, root, path, min_data_size,
4072 empty, left, free_space, right_nritems,
4073 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004074out:
4075 btrfs_tree_unlock(left);
4076 free_extent_buffer(left);
4077 return ret;
4078}
4079
4080/*
Chris Mason74123bd2007-02-02 11:05:29 -05004081 * split the path's leaf in two, making sure there is at least data_size
4082 * available for the resulting leaf level of the path.
4083 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004084static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4085 struct btrfs_root *root,
4086 struct btrfs_path *path,
4087 struct extent_buffer *l,
4088 struct extent_buffer *right,
4089 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004090{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004091 int data_copy_size;
4092 int rt_data_off;
4093 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004094 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004095 struct btrfs_map_token token;
4096
4097 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004098
Chris Mason5f39d392007-10-15 16:14:19 -04004099 nritems = nritems - mid;
4100 btrfs_set_header_nritems(right, nritems);
4101 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4102
4103 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4104 btrfs_item_nr_offset(mid),
4105 nritems * sizeof(struct btrfs_item));
4106
4107 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004108 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4109 data_copy_size, btrfs_leaf_data(l) +
4110 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004111
Chris Mason5f39d392007-10-15 16:14:19 -04004112 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4113 btrfs_item_end_nr(l, mid);
4114
4115 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004116 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004117 u32 ioff;
4118
Chris Masoncfed81a2012-03-03 07:40:03 -05004119 ioff = btrfs_token_item_offset(right, item, &token);
4120 btrfs_set_token_item_offset(right, item,
4121 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004122 }
Chris Mason74123bd2007-02-02 11:05:29 -05004123
Chris Mason5f39d392007-10-15 16:14:19 -04004124 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004125 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004126 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004127 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004128
4129 btrfs_mark_buffer_dirty(right);
4130 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004131 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004132
Chris Masonbe0e5c02007-01-26 15:51:26 -05004133 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004134 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004135 free_extent_buffer(path->nodes[0]);
4136 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004137 path->slots[0] -= mid;
4138 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004139 } else {
4140 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004141 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004142 }
Chris Mason5f39d392007-10-15 16:14:19 -04004143
Chris Masoneb60cea2007-02-02 09:18:22 -05004144 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004145}
4146
4147/*
Chris Mason99d8f832010-07-07 10:51:48 -04004148 * double splits happen when we need to insert a big item in the middle
4149 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4150 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4151 * A B C
4152 *
4153 * We avoid this by trying to push the items on either side of our target
4154 * into the adjacent leaves. If all goes well we can avoid the double split
4155 * completely.
4156 */
4157static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4158 struct btrfs_root *root,
4159 struct btrfs_path *path,
4160 int data_size)
4161{
4162 int ret;
4163 int progress = 0;
4164 int slot;
4165 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004166 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004167
4168 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004169 if (slot < btrfs_header_nritems(path->nodes[0]))
4170 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004171
4172 /*
4173 * try to push all the items after our slot into the
4174 * right leaf
4175 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004176 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004177 if (ret < 0)
4178 return ret;
4179
4180 if (ret == 0)
4181 progress++;
4182
4183 nritems = btrfs_header_nritems(path->nodes[0]);
4184 /*
4185 * our goal is to get our slot at the start or end of a leaf. If
4186 * we've done so we're done
4187 */
4188 if (path->slots[0] == 0 || path->slots[0] == nritems)
4189 return 0;
4190
4191 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4192 return 0;
4193
4194 /* try to push all the items before our slot into the next leaf */
4195 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004196 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004197 if (ret < 0)
4198 return ret;
4199
4200 if (ret == 0)
4201 progress++;
4202
4203 if (progress)
4204 return 0;
4205 return 1;
4206}
4207
4208/*
Chris Mason44871b12009-03-13 10:04:31 -04004209 * split the path's leaf in two, making sure there is at least data_size
4210 * available for the resulting leaf level of the path.
4211 *
4212 * returns 0 if all went well and < 0 on failure.
4213 */
4214static noinline int split_leaf(struct btrfs_trans_handle *trans,
4215 struct btrfs_root *root,
4216 struct btrfs_key *ins_key,
4217 struct btrfs_path *path, int data_size,
4218 int extend)
4219{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004220 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004221 struct extent_buffer *l;
4222 u32 nritems;
4223 int mid;
4224 int slot;
4225 struct extent_buffer *right;
4226 int ret = 0;
4227 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004228 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004229 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004230 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004231
Yan, Zhenga5719522009-09-24 09:17:31 -04004232 l = path->nodes[0];
4233 slot = path->slots[0];
4234 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4235 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4236 return -EOVERFLOW;
4237
Chris Mason44871b12009-03-13 10:04:31 -04004238 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004239 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004240 int space_needed = data_size;
4241
4242 if (slot < btrfs_header_nritems(l))
4243 space_needed -= btrfs_leaf_free_space(root, l);
4244
4245 wret = push_leaf_right(trans, root, path, space_needed,
4246 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004247 if (wret < 0)
4248 return wret;
4249 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004250 wret = push_leaf_left(trans, root, path, space_needed,
4251 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004252 if (wret < 0)
4253 return wret;
4254 }
4255 l = path->nodes[0];
4256
4257 /* did the pushes work? */
4258 if (btrfs_leaf_free_space(root, l) >= data_size)
4259 return 0;
4260 }
4261
4262 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004263 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004264 if (ret)
4265 return ret;
4266 }
4267again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004268 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004269 l = path->nodes[0];
4270 slot = path->slots[0];
4271 nritems = btrfs_header_nritems(l);
4272 mid = (nritems + 1) / 2;
4273
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004274 if (mid <= slot) {
4275 if (nritems == 1 ||
4276 leaf_space_used(l, mid, nritems - mid) + data_size >
4277 BTRFS_LEAF_DATA_SIZE(root)) {
4278 if (slot >= nritems) {
4279 split = 0;
4280 } else {
4281 mid = slot;
4282 if (mid != nritems &&
4283 leaf_space_used(l, mid, nritems - mid) +
4284 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004285 if (data_size && !tried_avoid_double)
4286 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004287 split = 2;
4288 }
4289 }
4290 }
4291 } else {
4292 if (leaf_space_used(l, 0, mid) + data_size >
4293 BTRFS_LEAF_DATA_SIZE(root)) {
4294 if (!extend && data_size && slot == 0) {
4295 split = 0;
4296 } else if ((extend || !data_size) && slot == 0) {
4297 mid = 1;
4298 } else {
4299 mid = slot;
4300 if (mid != nritems &&
4301 leaf_space_used(l, mid, nritems - mid) +
4302 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004303 if (data_size && !tried_avoid_double)
4304 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304305 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004306 }
4307 }
4308 }
4309 }
4310
4311 if (split == 0)
4312 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4313 else
4314 btrfs_item_key(l, &disk_key, mid);
4315
4316 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004317 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004318 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004319 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004320 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004321
4322 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004323
4324 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4325 btrfs_set_header_bytenr(right, right->start);
4326 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004327 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004328 btrfs_set_header_owner(right, root->root_key.objectid);
4329 btrfs_set_header_level(right, 0);
4330 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004331 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004332
4333 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004334 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004335 BTRFS_UUID_SIZE);
4336
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004337 if (split == 0) {
4338 if (mid <= slot) {
4339 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004340 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004341 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004342 btrfs_tree_unlock(path->nodes[0]);
4343 free_extent_buffer(path->nodes[0]);
4344 path->nodes[0] = right;
4345 path->slots[0] = 0;
4346 path->slots[1] += 1;
4347 } else {
4348 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004349 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004350 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004351 btrfs_tree_unlock(path->nodes[0]);
4352 free_extent_buffer(path->nodes[0]);
4353 path->nodes[0] = right;
4354 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004355 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004356 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004357 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004358 btrfs_mark_buffer_dirty(right);
4359 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004360 }
4361
Jeff Mahoney143bede2012-03-01 14:56:26 +01004362 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004363
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004364 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004365 BUG_ON(num_doubles != 0);
4366 num_doubles++;
4367 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004368 }
Chris Mason44871b12009-03-13 10:04:31 -04004369
Jeff Mahoney143bede2012-03-01 14:56:26 +01004370 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004371
4372push_for_double:
4373 push_for_double_split(trans, root, path, data_size);
4374 tried_avoid_double = 1;
4375 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4376 return 0;
4377 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004378}
4379
Yan, Zhengad48fd752009-11-12 09:33:58 +00004380static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4381 struct btrfs_root *root,
4382 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004383{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004384 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004385 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004386 struct btrfs_file_extent_item *fi;
4387 u64 extent_len = 0;
4388 u32 item_size;
4389 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004390
4391 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004392 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4393
4394 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4395 key.type != BTRFS_EXTENT_CSUM_KEY);
4396
4397 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4398 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004399
4400 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004401 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4402 fi = btrfs_item_ptr(leaf, path->slots[0],
4403 struct btrfs_file_extent_item);
4404 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4405 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004406 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004407
Chris Mason459931e2008-12-10 09:10:46 -05004408 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004409 path->search_for_split = 1;
4410 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004411 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004412 if (ret < 0)
4413 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004414
Yan, Zhengad48fd752009-11-12 09:33:58 +00004415 ret = -EAGAIN;
4416 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004417 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004418 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4419 goto err;
4420
Chris Mason109f6ae2010-04-02 09:20:18 -04004421 /* the leaf has changed, it now has room. return now */
4422 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4423 goto err;
4424
Yan, Zhengad48fd752009-11-12 09:33:58 +00004425 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4426 fi = btrfs_item_ptr(leaf, path->slots[0],
4427 struct btrfs_file_extent_item);
4428 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4429 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004430 }
4431
Chris Masonb9473432009-03-13 11:00:37 -04004432 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004433 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004434 if (ret)
4435 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004436
Yan, Zhengad48fd752009-11-12 09:33:58 +00004437 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004438 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004439 return 0;
4440err:
4441 path->keep_locks = 0;
4442 return ret;
4443}
4444
4445static noinline int split_item(struct btrfs_trans_handle *trans,
4446 struct btrfs_root *root,
4447 struct btrfs_path *path,
4448 struct btrfs_key *new_key,
4449 unsigned long split_offset)
4450{
4451 struct extent_buffer *leaf;
4452 struct btrfs_item *item;
4453 struct btrfs_item *new_item;
4454 int slot;
4455 char *buf;
4456 u32 nritems;
4457 u32 item_size;
4458 u32 orig_offset;
4459 struct btrfs_disk_key disk_key;
4460
Chris Masonb9473432009-03-13 11:00:37 -04004461 leaf = path->nodes[0];
4462 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4463
Chris Masonb4ce94d2009-02-04 09:25:08 -05004464 btrfs_set_path_blocking(path);
4465
Ross Kirkdd3cc162013-09-16 15:58:09 +01004466 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004467 orig_offset = btrfs_item_offset(leaf, item);
4468 item_size = btrfs_item_size(leaf, item);
4469
Chris Mason459931e2008-12-10 09:10:46 -05004470 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004471 if (!buf)
4472 return -ENOMEM;
4473
Chris Mason459931e2008-12-10 09:10:46 -05004474 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4475 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004476
Chris Mason459931e2008-12-10 09:10:46 -05004477 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004478 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004479 if (slot != nritems) {
4480 /* shift the items */
4481 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004482 btrfs_item_nr_offset(slot),
4483 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004484 }
4485
4486 btrfs_cpu_key_to_disk(&disk_key, new_key);
4487 btrfs_set_item_key(leaf, &disk_key, slot);
4488
Ross Kirkdd3cc162013-09-16 15:58:09 +01004489 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004490
4491 btrfs_set_item_offset(leaf, new_item, orig_offset);
4492 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4493
4494 btrfs_set_item_offset(leaf, item,
4495 orig_offset + item_size - split_offset);
4496 btrfs_set_item_size(leaf, item, split_offset);
4497
4498 btrfs_set_header_nritems(leaf, nritems + 1);
4499
4500 /* write the data for the start of the original item */
4501 write_extent_buffer(leaf, buf,
4502 btrfs_item_ptr_offset(leaf, path->slots[0]),
4503 split_offset);
4504
4505 /* write the data for the new item */
4506 write_extent_buffer(leaf, buf + split_offset,
4507 btrfs_item_ptr_offset(leaf, slot),
4508 item_size - split_offset);
4509 btrfs_mark_buffer_dirty(leaf);
4510
Yan, Zhengad48fd752009-11-12 09:33:58 +00004511 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004512 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004513 return 0;
4514}
4515
4516/*
4517 * This function splits a single item into two items,
4518 * giving 'new_key' to the new item and splitting the
4519 * old one at split_offset (from the start of the item).
4520 *
4521 * The path may be released by this operation. After
4522 * the split, the path is pointing to the old item. The
4523 * new item is going to be in the same node as the old one.
4524 *
4525 * Note, the item being split must be smaller enough to live alone on
4526 * a tree block with room for one extra struct btrfs_item
4527 *
4528 * This allows us to split the item in place, keeping a lock on the
4529 * leaf the entire time.
4530 */
4531int btrfs_split_item(struct btrfs_trans_handle *trans,
4532 struct btrfs_root *root,
4533 struct btrfs_path *path,
4534 struct btrfs_key *new_key,
4535 unsigned long split_offset)
4536{
4537 int ret;
4538 ret = setup_leaf_for_split(trans, root, path,
4539 sizeof(struct btrfs_item));
4540 if (ret)
4541 return ret;
4542
4543 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004544 return ret;
4545}
4546
4547/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004548 * This function duplicate a item, giving 'new_key' to the new item.
4549 * It guarantees both items live in the same tree leaf and the new item
4550 * is contiguous with the original item.
4551 *
4552 * This allows us to split file extent in place, keeping a lock on the
4553 * leaf the entire time.
4554 */
4555int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4556 struct btrfs_root *root,
4557 struct btrfs_path *path,
4558 struct btrfs_key *new_key)
4559{
4560 struct extent_buffer *leaf;
4561 int ret;
4562 u32 item_size;
4563
4564 leaf = path->nodes[0];
4565 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4566 ret = setup_leaf_for_split(trans, root, path,
4567 item_size + sizeof(struct btrfs_item));
4568 if (ret)
4569 return ret;
4570
4571 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004572 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004573 item_size, item_size +
4574 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004575 leaf = path->nodes[0];
4576 memcpy_extent_buffer(leaf,
4577 btrfs_item_ptr_offset(leaf, path->slots[0]),
4578 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4579 item_size);
4580 return 0;
4581}
4582
4583/*
Chris Masond352ac62008-09-29 15:18:18 -04004584 * make the item pointed to by the path smaller. new_size indicates
4585 * how small to make it, and from_end tells us if we just chop bytes
4586 * off the end of the item or if we shift the item to chop bytes off
4587 * the front.
4588 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004589void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004590 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004591{
Chris Masonb18c6682007-04-17 13:26:50 -04004592 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004593 struct extent_buffer *leaf;
4594 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004595 u32 nritems;
4596 unsigned int data_end;
4597 unsigned int old_data_start;
4598 unsigned int old_size;
4599 unsigned int size_diff;
4600 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004601 struct btrfs_map_token token;
4602
4603 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004604
Chris Mason5f39d392007-10-15 16:14:19 -04004605 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004606 slot = path->slots[0];
4607
4608 old_size = btrfs_item_size_nr(leaf, slot);
4609 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004610 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004611
Chris Mason5f39d392007-10-15 16:14:19 -04004612 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004613 data_end = leaf_data_end(root, leaf);
4614
Chris Mason5f39d392007-10-15 16:14:19 -04004615 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004616
Chris Masonb18c6682007-04-17 13:26:50 -04004617 size_diff = old_size - new_size;
4618
4619 BUG_ON(slot < 0);
4620 BUG_ON(slot >= nritems);
4621
4622 /*
4623 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4624 */
4625 /* first correct the data pointers */
4626 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004627 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004628 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004629
Chris Masoncfed81a2012-03-03 07:40:03 -05004630 ioff = btrfs_token_item_offset(leaf, item, &token);
4631 btrfs_set_token_item_offset(leaf, item,
4632 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004633 }
Chris Masondb945352007-10-15 16:15:53 -04004634
Chris Masonb18c6682007-04-17 13:26:50 -04004635 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004636 if (from_end) {
4637 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4638 data_end + size_diff, btrfs_leaf_data(leaf) +
4639 data_end, old_data_start + new_size - data_end);
4640 } else {
4641 struct btrfs_disk_key disk_key;
4642 u64 offset;
4643
4644 btrfs_item_key(leaf, &disk_key, slot);
4645
4646 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4647 unsigned long ptr;
4648 struct btrfs_file_extent_item *fi;
4649
4650 fi = btrfs_item_ptr(leaf, slot,
4651 struct btrfs_file_extent_item);
4652 fi = (struct btrfs_file_extent_item *)(
4653 (unsigned long)fi - size_diff);
4654
4655 if (btrfs_file_extent_type(leaf, fi) ==
4656 BTRFS_FILE_EXTENT_INLINE) {
4657 ptr = btrfs_item_ptr_offset(leaf, slot);
4658 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004659 (unsigned long)fi,
4660 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004661 disk_bytenr));
4662 }
4663 }
4664
4665 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4666 data_end + size_diff, btrfs_leaf_data(leaf) +
4667 data_end, old_data_start - data_end);
4668
4669 offset = btrfs_disk_key_offset(&disk_key);
4670 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4671 btrfs_set_item_key(leaf, &disk_key, slot);
4672 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004673 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004674 }
Chris Mason5f39d392007-10-15 16:14:19 -04004675
Ross Kirkdd3cc162013-09-16 15:58:09 +01004676 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004677 btrfs_set_item_size(leaf, item, new_size);
4678 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004679
Chris Mason5f39d392007-10-15 16:14:19 -04004680 if (btrfs_leaf_free_space(root, leaf) < 0) {
4681 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004682 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004683 }
Chris Masonb18c6682007-04-17 13:26:50 -04004684}
4685
Chris Masond352ac62008-09-29 15:18:18 -04004686/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004687 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004688 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004689void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004690 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004691{
Chris Mason6567e832007-04-16 09:22:45 -04004692 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004693 struct extent_buffer *leaf;
4694 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004695 u32 nritems;
4696 unsigned int data_end;
4697 unsigned int old_data;
4698 unsigned int old_size;
4699 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004700 struct btrfs_map_token token;
4701
4702 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004703
Chris Mason5f39d392007-10-15 16:14:19 -04004704 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004705
Chris Mason5f39d392007-10-15 16:14:19 -04004706 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004707 data_end = leaf_data_end(root, leaf);
4708
Chris Mason5f39d392007-10-15 16:14:19 -04004709 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4710 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004711 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004712 }
Chris Mason6567e832007-04-16 09:22:45 -04004713 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004714 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004715
4716 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004717 if (slot >= nritems) {
4718 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004719 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004720 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004721 BUG_ON(1);
4722 }
Chris Mason6567e832007-04-16 09:22:45 -04004723
4724 /*
4725 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4726 */
4727 /* first correct the data pointers */
4728 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004729 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004730 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004731
Chris Masoncfed81a2012-03-03 07:40:03 -05004732 ioff = btrfs_token_item_offset(leaf, item, &token);
4733 btrfs_set_token_item_offset(leaf, item,
4734 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004735 }
Chris Mason5f39d392007-10-15 16:14:19 -04004736
Chris Mason6567e832007-04-16 09:22:45 -04004737 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004738 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004739 data_end - data_size, btrfs_leaf_data(leaf) +
4740 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004741
Chris Mason6567e832007-04-16 09:22:45 -04004742 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004743 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004744 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004745 btrfs_set_item_size(leaf, item, old_size + data_size);
4746 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004747
Chris Mason5f39d392007-10-15 16:14:19 -04004748 if (btrfs_leaf_free_space(root, leaf) < 0) {
4749 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004750 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004751 }
Chris Mason6567e832007-04-16 09:22:45 -04004752}
4753
Chris Mason74123bd2007-02-02 11:05:29 -05004754/*
Chris Mason44871b12009-03-13 10:04:31 -04004755 * this is a helper for btrfs_insert_empty_items, the main goal here is
4756 * to save stack depth by doing the bulk of the work in a function
4757 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004758 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004759void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004760 struct btrfs_key *cpu_key, u32 *data_size,
4761 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004762{
Chris Mason5f39d392007-10-15 16:14:19 -04004763 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004764 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004765 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004766 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004767 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004768 struct extent_buffer *leaf;
4769 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004770 struct btrfs_map_token token;
4771
4772 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004773
Chris Mason5f39d392007-10-15 16:14:19 -04004774 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004775 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004776
Chris Mason5f39d392007-10-15 16:14:19 -04004777 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004778 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004779
Chris Masonf25956c2008-09-12 15:32:53 -04004780 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004781 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004782 btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
Chris Mason9c583092008-01-29 15:15:18 -05004783 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004784 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004785 }
Chris Mason5f39d392007-10-15 16:14:19 -04004786
Chris Masonbe0e5c02007-01-26 15:51:26 -05004787 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004788 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004789
Chris Mason5f39d392007-10-15 16:14:19 -04004790 if (old_data < data_end) {
4791 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004792 btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
Chris Mason5f39d392007-10-15 16:14:19 -04004793 slot, old_data, data_end);
4794 BUG_ON(1);
4795 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004796 /*
4797 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4798 */
4799 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004800 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004801 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004802
Ross Kirkdd3cc162013-09-16 15:58:09 +01004803 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004804 ioff = btrfs_token_item_offset(leaf, item, &token);
4805 btrfs_set_token_item_offset(leaf, item,
4806 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004807 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004808 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004809 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004810 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004811 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004812
4813 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004814 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004815 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004816 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004817 data_end = old_data;
4818 }
Chris Mason5f39d392007-10-15 16:14:19 -04004819
Chris Mason62e27492007-03-15 12:56:47 -04004820 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004821 for (i = 0; i < nr; i++) {
4822 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4823 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004824 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004825 btrfs_set_token_item_offset(leaf, item,
4826 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004827 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004828 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004829 }
Chris Mason44871b12009-03-13 10:04:31 -04004830
Chris Mason9c583092008-01-29 15:15:18 -05004831 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004832
Chris Mason5a01a2e2008-01-30 11:43:54 -05004833 if (slot == 0) {
4834 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004835 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004836 }
Chris Masonb9473432009-03-13 11:00:37 -04004837 btrfs_unlock_up_safe(path, 1);
4838 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004839
Chris Mason5f39d392007-10-15 16:14:19 -04004840 if (btrfs_leaf_free_space(root, leaf) < 0) {
4841 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004842 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004843 }
Chris Mason44871b12009-03-13 10:04:31 -04004844}
4845
4846/*
4847 * Given a key and some data, insert items into the tree.
4848 * This does all the path init required, making room in the tree if needed.
4849 */
4850int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4851 struct btrfs_root *root,
4852 struct btrfs_path *path,
4853 struct btrfs_key *cpu_key, u32 *data_size,
4854 int nr)
4855{
Chris Mason44871b12009-03-13 10:04:31 -04004856 int ret = 0;
4857 int slot;
4858 int i;
4859 u32 total_size = 0;
4860 u32 total_data = 0;
4861
4862 for (i = 0; i < nr; i++)
4863 total_data += data_size[i];
4864
4865 total_size = total_data + (nr * sizeof(struct btrfs_item));
4866 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4867 if (ret == 0)
4868 return -EEXIST;
4869 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004870 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004871
Chris Mason44871b12009-03-13 10:04:31 -04004872 slot = path->slots[0];
4873 BUG_ON(slot < 0);
4874
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004875 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004876 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004877 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004878}
4879
4880/*
4881 * Given a key and some data, insert an item into the tree.
4882 * This does all the path init required, making room in the tree if needed.
4883 */
Chris Masone089f052007-03-16 16:20:31 -04004884int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4885 *root, struct btrfs_key *cpu_key, void *data, u32
4886 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004887{
4888 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004889 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004890 struct extent_buffer *leaf;
4891 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004892
Chris Mason2c90e5d2007-04-02 10:50:19 -04004893 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004894 if (!path)
4895 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004896 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004897 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004898 leaf = path->nodes[0];
4899 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4900 write_extent_buffer(leaf, data, ptr, data_size);
4901 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004902 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004903 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004904 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004905}
4906
Chris Mason74123bd2007-02-02 11:05:29 -05004907/*
Chris Mason5de08d72007-02-24 06:24:44 -05004908 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004909 *
Chris Masond352ac62008-09-29 15:18:18 -04004910 * the tree should have been previously balanced so the deletion does not
4911 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004912 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004913static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4914 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004915{
Chris Mason5f39d392007-10-15 16:14:19 -04004916 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004917 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004918 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004919
Chris Mason5f39d392007-10-15 16:14:19 -04004920 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004921 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004922 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004923 tree_mod_log_eb_move(root->fs_info, parent, slot,
4924 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004925 memmove_extent_buffer(parent,
4926 btrfs_node_key_ptr_offset(slot),
4927 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004928 sizeof(struct btrfs_key_ptr) *
4929 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004930 } else if (level) {
4931 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004932 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004933 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004934 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004935
Chris Mason7518a232007-03-12 12:01:18 -04004936 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004937 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004938 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004939 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004940 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004941 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004942 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004943 struct btrfs_disk_key disk_key;
4944
4945 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004946 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004947 }
Chris Masond6025572007-03-30 14:27:56 -04004948 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004949}
4950
Chris Mason74123bd2007-02-02 11:05:29 -05004951/*
Chris Mason323ac952008-10-01 19:05:46 -04004952 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004953 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004954 *
4955 * This deletes the pointer in path->nodes[1] and frees the leaf
4956 * block extent. zero is returned if it all worked out, < 0 otherwise.
4957 *
4958 * The path must have already been setup for deleting the leaf, including
4959 * all the proper balancing. path->nodes[1] must be locked.
4960 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004961static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4962 struct btrfs_root *root,
4963 struct btrfs_path *path,
4964 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004965{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004966 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004967 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004968
Chris Mason4d081c42009-02-04 09:31:28 -05004969 /*
4970 * btrfs_free_extent is expensive, we want to make sure we
4971 * aren't holding any locks when we call it
4972 */
4973 btrfs_unlock_up_safe(path, 0);
4974
Yan, Zhengf0486c62010-05-16 10:46:25 -04004975 root_sub_used(root, leaf->len);
4976
Josef Bacik3083ee22012-03-09 16:01:49 -05004977 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004978 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004979 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004980}
4981/*
Chris Mason74123bd2007-02-02 11:05:29 -05004982 * delete the item at the leaf level in path. If that empties
4983 * the leaf, remove it from the tree
4984 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004985int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4986 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004987{
Chris Mason5f39d392007-10-15 16:14:19 -04004988 struct extent_buffer *leaf;
4989 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004990 int last_off;
4991 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004992 int ret = 0;
4993 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004994 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004995 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004996 struct btrfs_map_token token;
4997
4998 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004999
Chris Mason5f39d392007-10-15 16:14:19 -04005000 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05005001 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
5002
5003 for (i = 0; i < nr; i++)
5004 dsize += btrfs_item_size_nr(leaf, slot + i);
5005
Chris Mason5f39d392007-10-15 16:14:19 -04005006 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005007
Chris Mason85e21ba2008-01-29 15:11:36 -05005008 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04005009 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005010
5011 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04005012 data_end + dsize,
5013 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05005014 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04005015
Chris Mason85e21ba2008-01-29 15:11:36 -05005016 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04005017 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04005018
Ross Kirkdd3cc162013-09-16 15:58:09 +01005019 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05005020 ioff = btrfs_token_item_offset(leaf, item, &token);
5021 btrfs_set_token_item_offset(leaf, item,
5022 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04005023 }
Chris Masondb945352007-10-15 16:15:53 -04005024
Chris Mason5f39d392007-10-15 16:14:19 -04005025 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05005026 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04005027 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05005028 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05005029 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005030 btrfs_set_header_nritems(leaf, nritems - nr);
5031 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04005032
Chris Mason74123bd2007-02-02 11:05:29 -05005033 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04005034 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005035 if (leaf == root->node) {
5036 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005037 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005038 btrfs_set_path_blocking(path);
5039 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005040 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005041 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005042 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005043 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005044 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005045 struct btrfs_disk_key disk_key;
5046
5047 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00005048 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005049 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005050
Chris Mason74123bd2007-02-02 11:05:29 -05005051 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04005052 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005053 /* push_leaf_left fixes the path.
5054 * make sure the path still points to our leaf
5055 * for possible call to del_ptr below
5056 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005057 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005058 extent_buffer_get(leaf);
5059
Chris Masonb9473432009-03-13 11:00:37 -04005060 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005061 wret = push_leaf_left(trans, root, path, 1, 1,
5062 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005063 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005064 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005065
5066 if (path->nodes[0] == leaf &&
5067 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005068 wret = push_leaf_right(trans, root, path, 1,
5069 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005070 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005071 ret = wret;
5072 }
Chris Mason5f39d392007-10-15 16:14:19 -04005073
5074 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005075 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005076 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005077 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005078 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005079 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005080 /* if we're still in the path, make sure
5081 * we're dirty. Otherwise, one of the
5082 * push_leaf functions must have already
5083 * dirtied this buffer
5084 */
5085 if (path->nodes[0] == leaf)
5086 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005087 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005088 }
Chris Masond5719762007-03-23 10:01:08 -04005089 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005090 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005091 }
5092 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005093 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005094}
5095
Chris Mason97571fd2007-02-24 13:39:08 -05005096/*
Chris Mason925baed2008-06-25 16:01:30 -04005097 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005098 * returns 0 if it found something or 1 if there are no lesser leaves.
5099 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005100 *
5101 * This may release the path, and so you may lose any locks held at the
5102 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005103 */
Josef Bacik16e75492013-10-22 12:18:51 -04005104int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005105{
Chris Mason925baed2008-06-25 16:01:30 -04005106 struct btrfs_key key;
5107 struct btrfs_disk_key found_key;
5108 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005109
Chris Mason925baed2008-06-25 16:01:30 -04005110 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005111
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005112 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005113 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005114 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005115 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005116 key.offset = (u64)-1;
5117 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005118 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005119 key.type = (u8)-1;
5120 key.offset = (u64)-1;
5121 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005122 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005123 }
Chris Mason7bb86312007-12-11 09:25:06 -05005124
David Sterbab3b4aa72011-04-21 01:20:15 +02005125 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005126 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5127 if (ret < 0)
5128 return ret;
5129 btrfs_item_key(path->nodes[0], &found_key, 0);
5130 ret = comp_keys(&found_key, &key);
5131 if (ret < 0)
5132 return 0;
5133 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005134}
5135
Chris Mason3f157a22008-06-25 16:01:31 -04005136/*
5137 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005138 * for nodes or leaves that are have a minimum transaction id.
5139 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005140 *
5141 * This does not cow, but it does stuff the starting key it finds back
5142 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5143 * key and get a writable path.
5144 *
5145 * This does lock as it descends, and path->keep_locks should be set
5146 * to 1 by the caller.
5147 *
5148 * This honors path->lowest_level to prevent descent past a given level
5149 * of the tree.
5150 *
Chris Masond352ac62008-09-29 15:18:18 -04005151 * min_trans indicates the oldest transaction that you are interested
5152 * in walking through. Any nodes or leaves older than min_trans are
5153 * skipped over (without reading them).
5154 *
Chris Mason3f157a22008-06-25 16:01:31 -04005155 * returns zero if something useful was found, < 0 on error and 1 if there
5156 * was nothing in the tree that matched the search criteria.
5157 */
5158int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005159 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005160 u64 min_trans)
5161{
5162 struct extent_buffer *cur;
5163 struct btrfs_key found_key;
5164 int slot;
Yan96524802008-07-24 12:19:49 -04005165 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005166 u32 nritems;
5167 int level;
5168 int ret = 1;
5169
Chris Mason934d3752008-12-08 16:43:10 -05005170 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04005171again:
Chris Masonbd681512011-07-16 15:23:14 -04005172 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005173 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005174 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005175 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005176 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005177
5178 if (btrfs_header_generation(cur) < min_trans) {
5179 ret = 1;
5180 goto out;
5181 }
Chris Masond3977122009-01-05 21:25:51 -05005182 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005183 nritems = btrfs_header_nritems(cur);
5184 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005185 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005186
Chris Mason323ac952008-10-01 19:05:46 -04005187 /* at the lowest level, we're done, setup the path and exit */
5188 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005189 if (slot >= nritems)
5190 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005191 ret = 0;
5192 path->slots[level] = slot;
5193 btrfs_item_key_to_cpu(cur, &found_key, slot);
5194 goto out;
5195 }
Yan96524802008-07-24 12:19:49 -04005196 if (sret && slot > 0)
5197 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005198 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005199 * check this node pointer against the min_trans parameters.
5200 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005201 */
Chris Masond3977122009-01-05 21:25:51 -05005202 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005203 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005204
Chris Mason3f157a22008-06-25 16:01:31 -04005205 gen = btrfs_node_ptr_generation(cur, slot);
5206 if (gen < min_trans) {
5207 slot++;
5208 continue;
5209 }
Eric Sandeende78b512013-01-31 18:21:12 +00005210 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005211 }
Chris Masone02119d2008-09-05 16:13:11 -04005212find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005213 /*
5214 * we didn't find a candidate key in this node, walk forward
5215 * and find another one
5216 */
5217 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005218 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005219 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005220 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005221 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005222 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005223 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005224 goto again;
5225 } else {
5226 goto out;
5227 }
5228 }
5229 /* save our key for returning back */
5230 btrfs_node_key_to_cpu(cur, &found_key, slot);
5231 path->slots[level] = slot;
5232 if (level == path->lowest_level) {
5233 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04005234 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005235 goto out;
5236 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005237 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005238 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005239 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005240
Chris Masonbd681512011-07-16 15:23:14 -04005241 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005242
Chris Masonbd681512011-07-16 15:23:14 -04005243 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005244 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005245 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005246 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005247 }
5248out:
5249 if (ret == 0)
5250 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05005251 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005252 return ret;
5253}
5254
Alexander Block70698302012-06-05 21:07:48 +02005255static void tree_move_down(struct btrfs_root *root,
5256 struct btrfs_path *path,
5257 int *level, int root_level)
5258{
Chris Mason74dd17f2012-08-07 16:25:13 -04005259 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005260 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5261 path->slots[*level]);
5262 path->slots[*level - 1] = 0;
5263 (*level)--;
5264}
5265
5266static int tree_move_next_or_upnext(struct btrfs_root *root,
5267 struct btrfs_path *path,
5268 int *level, int root_level)
5269{
5270 int ret = 0;
5271 int nritems;
5272 nritems = btrfs_header_nritems(path->nodes[*level]);
5273
5274 path->slots[*level]++;
5275
Chris Mason74dd17f2012-08-07 16:25:13 -04005276 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005277 if (*level == root_level)
5278 return -1;
5279
5280 /* move upnext */
5281 path->slots[*level] = 0;
5282 free_extent_buffer(path->nodes[*level]);
5283 path->nodes[*level] = NULL;
5284 (*level)++;
5285 path->slots[*level]++;
5286
5287 nritems = btrfs_header_nritems(path->nodes[*level]);
5288 ret = 1;
5289 }
5290 return ret;
5291}
5292
5293/*
5294 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5295 * or down.
5296 */
5297static int tree_advance(struct btrfs_root *root,
5298 struct btrfs_path *path,
5299 int *level, int root_level,
5300 int allow_down,
5301 struct btrfs_key *key)
5302{
5303 int ret;
5304
5305 if (*level == 0 || !allow_down) {
5306 ret = tree_move_next_or_upnext(root, path, level, root_level);
5307 } else {
5308 tree_move_down(root, path, level, root_level);
5309 ret = 0;
5310 }
5311 if (ret >= 0) {
5312 if (*level == 0)
5313 btrfs_item_key_to_cpu(path->nodes[*level], key,
5314 path->slots[*level]);
5315 else
5316 btrfs_node_key_to_cpu(path->nodes[*level], key,
5317 path->slots[*level]);
5318 }
5319 return ret;
5320}
5321
5322static int tree_compare_item(struct btrfs_root *left_root,
5323 struct btrfs_path *left_path,
5324 struct btrfs_path *right_path,
5325 char *tmp_buf)
5326{
5327 int cmp;
5328 int len1, len2;
5329 unsigned long off1, off2;
5330
5331 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5332 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5333 if (len1 != len2)
5334 return 1;
5335
5336 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5337 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5338 right_path->slots[0]);
5339
5340 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5341
5342 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5343 if (cmp)
5344 return 1;
5345 return 0;
5346}
5347
5348#define ADVANCE 1
5349#define ADVANCE_ONLY_NEXT -1
5350
5351/*
5352 * This function compares two trees and calls the provided callback for
5353 * every changed/new/deleted item it finds.
5354 * If shared tree blocks are encountered, whole subtrees are skipped, making
5355 * the compare pretty fast on snapshotted subvolumes.
5356 *
5357 * This currently works on commit roots only. As commit roots are read only,
5358 * we don't do any locking. The commit roots are protected with transactions.
5359 * Transactions are ended and rejoined when a commit is tried in between.
5360 *
5361 * This function checks for modifications done to the trees while comparing.
5362 * If it detects a change, it aborts immediately.
5363 */
5364int btrfs_compare_trees(struct btrfs_root *left_root,
5365 struct btrfs_root *right_root,
5366 btrfs_changed_cb_t changed_cb, void *ctx)
5367{
5368 int ret;
5369 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005370 struct btrfs_path *left_path = NULL;
5371 struct btrfs_path *right_path = NULL;
5372 struct btrfs_key left_key;
5373 struct btrfs_key right_key;
5374 char *tmp_buf = NULL;
5375 int left_root_level;
5376 int right_root_level;
5377 int left_level;
5378 int right_level;
5379 int left_end_reached;
5380 int right_end_reached;
5381 int advance_left;
5382 int advance_right;
5383 u64 left_blockptr;
5384 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005385 u64 left_gen;
5386 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005387
5388 left_path = btrfs_alloc_path();
5389 if (!left_path) {
5390 ret = -ENOMEM;
5391 goto out;
5392 }
5393 right_path = btrfs_alloc_path();
5394 if (!right_path) {
5395 ret = -ENOMEM;
5396 goto out;
5397 }
5398
5399 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5400 if (!tmp_buf) {
5401 ret = -ENOMEM;
5402 goto out;
5403 }
5404
5405 left_path->search_commit_root = 1;
5406 left_path->skip_locking = 1;
5407 right_path->search_commit_root = 1;
5408 right_path->skip_locking = 1;
5409
Alexander Block70698302012-06-05 21:07:48 +02005410 /*
5411 * Strategy: Go to the first items of both trees. Then do
5412 *
5413 * If both trees are at level 0
5414 * Compare keys of current items
5415 * If left < right treat left item as new, advance left tree
5416 * and repeat
5417 * If left > right treat right item as deleted, advance right tree
5418 * and repeat
5419 * If left == right do deep compare of items, treat as changed if
5420 * needed, advance both trees and repeat
5421 * If both trees are at the same level but not at level 0
5422 * Compare keys of current nodes/leafs
5423 * If left < right advance left tree and repeat
5424 * If left > right advance right tree and repeat
5425 * If left == right compare blockptrs of the next nodes/leafs
5426 * If they match advance both trees but stay at the same level
5427 * and repeat
5428 * If they don't match advance both trees while allowing to go
5429 * deeper and repeat
5430 * If tree levels are different
5431 * Advance the tree that needs it and repeat
5432 *
5433 * Advancing a tree means:
5434 * If we are at level 0, try to go to the next slot. If that's not
5435 * possible, go one level up and repeat. Stop when we found a level
5436 * where we could go to the next slot. We may at this point be on a
5437 * node or a leaf.
5438 *
5439 * If we are not at level 0 and not on shared tree blocks, go one
5440 * level deeper.
5441 *
5442 * If we are not at level 0 and on shared tree blocks, go one slot to
5443 * the right if possible or go up and right.
5444 */
5445
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005446 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005447 left_level = btrfs_header_level(left_root->commit_root);
5448 left_root_level = left_level;
5449 left_path->nodes[left_level] = left_root->commit_root;
5450 extent_buffer_get(left_path->nodes[left_level]);
5451
5452 right_level = btrfs_header_level(right_root->commit_root);
5453 right_root_level = right_level;
5454 right_path->nodes[right_level] = right_root->commit_root;
5455 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005456 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005457
5458 if (left_level == 0)
5459 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5460 &left_key, left_path->slots[left_level]);
5461 else
5462 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5463 &left_key, left_path->slots[left_level]);
5464 if (right_level == 0)
5465 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5466 &right_key, right_path->slots[right_level]);
5467 else
5468 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5469 &right_key, right_path->slots[right_level]);
5470
5471 left_end_reached = right_end_reached = 0;
5472 advance_left = advance_right = 0;
5473
5474 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005475 if (advance_left && !left_end_reached) {
5476 ret = tree_advance(left_root, left_path, &left_level,
5477 left_root_level,
5478 advance_left != ADVANCE_ONLY_NEXT,
5479 &left_key);
5480 if (ret < 0)
5481 left_end_reached = ADVANCE;
5482 advance_left = 0;
5483 }
5484 if (advance_right && !right_end_reached) {
5485 ret = tree_advance(right_root, right_path, &right_level,
5486 right_root_level,
5487 advance_right != ADVANCE_ONLY_NEXT,
5488 &right_key);
5489 if (ret < 0)
5490 right_end_reached = ADVANCE;
5491 advance_right = 0;
5492 }
5493
5494 if (left_end_reached && right_end_reached) {
5495 ret = 0;
5496 goto out;
5497 } else if (left_end_reached) {
5498 if (right_level == 0) {
5499 ret = changed_cb(left_root, right_root,
5500 left_path, right_path,
5501 &right_key,
5502 BTRFS_COMPARE_TREE_DELETED,
5503 ctx);
5504 if (ret < 0)
5505 goto out;
5506 }
5507 advance_right = ADVANCE;
5508 continue;
5509 } else if (right_end_reached) {
5510 if (left_level == 0) {
5511 ret = changed_cb(left_root, right_root,
5512 left_path, right_path,
5513 &left_key,
5514 BTRFS_COMPARE_TREE_NEW,
5515 ctx);
5516 if (ret < 0)
5517 goto out;
5518 }
5519 advance_left = ADVANCE;
5520 continue;
5521 }
5522
5523 if (left_level == 0 && right_level == 0) {
5524 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5525 if (cmp < 0) {
5526 ret = changed_cb(left_root, right_root,
5527 left_path, right_path,
5528 &left_key,
5529 BTRFS_COMPARE_TREE_NEW,
5530 ctx);
5531 if (ret < 0)
5532 goto out;
5533 advance_left = ADVANCE;
5534 } else if (cmp > 0) {
5535 ret = changed_cb(left_root, right_root,
5536 left_path, right_path,
5537 &right_key,
5538 BTRFS_COMPARE_TREE_DELETED,
5539 ctx);
5540 if (ret < 0)
5541 goto out;
5542 advance_right = ADVANCE;
5543 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005544 enum btrfs_compare_tree_result cmp;
5545
Chris Mason74dd17f2012-08-07 16:25:13 -04005546 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005547 ret = tree_compare_item(left_root, left_path,
5548 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005549 if (ret)
5550 cmp = BTRFS_COMPARE_TREE_CHANGED;
5551 else
5552 cmp = BTRFS_COMPARE_TREE_SAME;
5553 ret = changed_cb(left_root, right_root,
5554 left_path, right_path,
5555 &left_key, cmp, ctx);
5556 if (ret < 0)
5557 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005558 advance_left = ADVANCE;
5559 advance_right = ADVANCE;
5560 }
5561 } else if (left_level == right_level) {
5562 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5563 if (cmp < 0) {
5564 advance_left = ADVANCE;
5565 } else if (cmp > 0) {
5566 advance_right = ADVANCE;
5567 } else {
5568 left_blockptr = btrfs_node_blockptr(
5569 left_path->nodes[left_level],
5570 left_path->slots[left_level]);
5571 right_blockptr = btrfs_node_blockptr(
5572 right_path->nodes[right_level],
5573 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005574 left_gen = btrfs_node_ptr_generation(
5575 left_path->nodes[left_level],
5576 left_path->slots[left_level]);
5577 right_gen = btrfs_node_ptr_generation(
5578 right_path->nodes[right_level],
5579 right_path->slots[right_level]);
5580 if (left_blockptr == right_blockptr &&
5581 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005582 /*
5583 * As we're on a shared block, don't
5584 * allow to go deeper.
5585 */
5586 advance_left = ADVANCE_ONLY_NEXT;
5587 advance_right = ADVANCE_ONLY_NEXT;
5588 } else {
5589 advance_left = ADVANCE;
5590 advance_right = ADVANCE;
5591 }
5592 }
5593 } else if (left_level < right_level) {
5594 advance_right = ADVANCE;
5595 } else {
5596 advance_left = ADVANCE;
5597 }
5598 }
5599
5600out:
5601 btrfs_free_path(left_path);
5602 btrfs_free_path(right_path);
5603 kfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005604 return ret;
5605}
5606
Chris Mason3f157a22008-06-25 16:01:31 -04005607/*
5608 * this is similar to btrfs_next_leaf, but does not try to preserve
5609 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005610 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005611 *
5612 * 0 is returned if another key is found, < 0 if there are any errors
5613 * and 1 is returned if there are no higher keys in the tree
5614 *
5615 * path->keep_locks should be set to 1 on the search made before
5616 * calling this function.
5617 */
Chris Masone7a84562008-06-25 16:01:31 -04005618int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005619 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005620{
Chris Masone7a84562008-06-25 16:01:31 -04005621 int slot;
5622 struct extent_buffer *c;
5623
Chris Mason934d3752008-12-08 16:43:10 -05005624 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005625 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005626 if (!path->nodes[level])
5627 return 1;
5628
5629 slot = path->slots[level] + 1;
5630 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005631next:
Chris Masone7a84562008-06-25 16:01:31 -04005632 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005633 int ret;
5634 int orig_lowest;
5635 struct btrfs_key cur_key;
5636 if (level + 1 >= BTRFS_MAX_LEVEL ||
5637 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005638 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005639
5640 if (path->locks[level + 1]) {
5641 level++;
5642 continue;
5643 }
5644
5645 slot = btrfs_header_nritems(c) - 1;
5646 if (level == 0)
5647 btrfs_item_key_to_cpu(c, &cur_key, slot);
5648 else
5649 btrfs_node_key_to_cpu(c, &cur_key, slot);
5650
5651 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005652 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005653 path->lowest_level = level;
5654 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5655 0, 0);
5656 path->lowest_level = orig_lowest;
5657 if (ret < 0)
5658 return ret;
5659
5660 c = path->nodes[level];
5661 slot = path->slots[level];
5662 if (ret == 0)
5663 slot++;
5664 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005665 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005666
Chris Masone7a84562008-06-25 16:01:31 -04005667 if (level == 0)
5668 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005669 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005670 u64 gen = btrfs_node_ptr_generation(c, slot);
5671
Chris Mason3f157a22008-06-25 16:01:31 -04005672 if (gen < min_trans) {
5673 slot++;
5674 goto next;
5675 }
Chris Masone7a84562008-06-25 16:01:31 -04005676 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005677 }
Chris Masone7a84562008-06-25 16:01:31 -04005678 return 0;
5679 }
5680 return 1;
5681}
5682
Chris Mason7bb86312007-12-11 09:25:06 -05005683/*
Chris Mason925baed2008-06-25 16:01:30 -04005684 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005685 * returns 0 if it found something or 1 if there are no greater leaves.
5686 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005687 */
Chris Mason234b63a2007-03-13 10:46:10 -04005688int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005689{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005690 return btrfs_next_old_leaf(root, path, 0);
5691}
5692
5693int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5694 u64 time_seq)
5695{
Chris Masond97e63b2007-02-20 16:40:44 -05005696 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005697 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005698 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005699 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005700 struct btrfs_key key;
5701 u32 nritems;
5702 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005703 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005704 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005705
5706 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005707 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005708 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005709
Chris Mason8e73f272009-04-03 10:14:18 -04005710 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5711again:
5712 level = 1;
5713 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005714 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005715 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005716
Chris Masona2135012008-06-25 16:01:30 -04005717 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005718 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005719
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005720 if (time_seq)
5721 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5722 else
5723 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005724 path->keep_locks = 0;
5725
5726 if (ret < 0)
5727 return ret;
5728
Chris Masona2135012008-06-25 16:01:30 -04005729 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005730 /*
5731 * by releasing the path above we dropped all our locks. A balance
5732 * could have added more items next to the key that used to be
5733 * at the very end of the block. So, check again here and
5734 * advance the path if there are now more items available.
5735 */
Chris Masona2135012008-06-25 16:01:30 -04005736 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005737 if (ret == 0)
5738 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005739 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005740 goto done;
5741 }
Chris Masond97e63b2007-02-20 16:40:44 -05005742
Chris Masond3977122009-01-05 21:25:51 -05005743 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005744 if (!path->nodes[level]) {
5745 ret = 1;
5746 goto done;
5747 }
Chris Mason5f39d392007-10-15 16:14:19 -04005748
Chris Masond97e63b2007-02-20 16:40:44 -05005749 slot = path->slots[level] + 1;
5750 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005751 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005752 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005753 if (level == BTRFS_MAX_LEVEL) {
5754 ret = 1;
5755 goto done;
5756 }
Chris Masond97e63b2007-02-20 16:40:44 -05005757 continue;
5758 }
Chris Mason5f39d392007-10-15 16:14:19 -04005759
Chris Mason925baed2008-06-25 16:01:30 -04005760 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005761 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005762 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005763 }
Chris Mason5f39d392007-10-15 16:14:19 -04005764
Chris Mason8e73f272009-04-03 10:14:18 -04005765 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005766 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005767 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005768 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005769 if (ret == -EAGAIN)
5770 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005771
Chris Mason76a05b32009-05-14 13:24:30 -04005772 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005773 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005774 goto done;
5775 }
5776
Chris Mason5cd57b22008-06-25 16:01:30 -04005777 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005778 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005779 if (!ret && time_seq) {
5780 /*
5781 * If we don't get the lock, we may be racing
5782 * with push_leaf_left, holding that lock while
5783 * itself waiting for the leaf we've currently
5784 * locked. To solve this situation, we give up
5785 * on our lock and cycle.
5786 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005787 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005788 btrfs_release_path(path);
5789 cond_resched();
5790 goto again;
5791 }
Chris Mason8e73f272009-04-03 10:14:18 -04005792 if (!ret) {
5793 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005794 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005795 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005796 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005797 }
Chris Mason31533fb2011-07-26 16:01:59 -04005798 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005799 }
Chris Masond97e63b2007-02-20 16:40:44 -05005800 break;
5801 }
5802 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005803 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005804 level--;
5805 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005806 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005807 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005808
Chris Mason5f39d392007-10-15 16:14:19 -04005809 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005810 path->nodes[level] = next;
5811 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005812 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005813 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005814 if (!level)
5815 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005816
Chris Mason8e73f272009-04-03 10:14:18 -04005817 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005818 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005819 if (ret == -EAGAIN)
5820 goto again;
5821
Chris Mason76a05b32009-05-14 13:24:30 -04005822 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005823 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005824 goto done;
5825 }
5826
Chris Mason5cd57b22008-06-25 16:01:30 -04005827 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005828 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005829 if (!ret) {
5830 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005831 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005832 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005833 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005834 }
Chris Mason31533fb2011-07-26 16:01:59 -04005835 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005836 }
Chris Masond97e63b2007-02-20 16:40:44 -05005837 }
Chris Mason8e73f272009-04-03 10:14:18 -04005838 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005839done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005840 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005841 path->leave_spinning = old_spinning;
5842 if (!old_spinning)
5843 btrfs_set_path_blocking(path);
5844
5845 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005846}
Chris Mason0b86a832008-03-24 15:01:56 -04005847
Chris Mason3f157a22008-06-25 16:01:31 -04005848/*
5849 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5850 * searching until it gets past min_objectid or finds an item of 'type'
5851 *
5852 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5853 */
Chris Mason0b86a832008-03-24 15:01:56 -04005854int btrfs_previous_item(struct btrfs_root *root,
5855 struct btrfs_path *path, u64 min_objectid,
5856 int type)
5857{
5858 struct btrfs_key found_key;
5859 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005860 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005861 int ret;
5862
Chris Masond3977122009-01-05 21:25:51 -05005863 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005864 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005865 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005866 ret = btrfs_prev_leaf(root, path);
5867 if (ret != 0)
5868 return ret;
5869 } else {
5870 path->slots[0]--;
5871 }
5872 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005873 nritems = btrfs_header_nritems(leaf);
5874 if (nritems == 0)
5875 return 1;
5876 if (path->slots[0] == nritems)
5877 path->slots[0]--;
5878
Chris Mason0b86a832008-03-24 15:01:56 -04005879 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005880 if (found_key.objectid < min_objectid)
5881 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005882 if (found_key.type == type)
5883 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005884 if (found_key.objectid == min_objectid &&
5885 found_key.type < type)
5886 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005887 }
5888 return 1;
5889}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005890
5891/*
5892 * search in extent tree to find a previous Metadata/Data extent item with
5893 * min objecitd.
5894 *
5895 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5896 */
5897int btrfs_previous_extent_item(struct btrfs_root *root,
5898 struct btrfs_path *path, u64 min_objectid)
5899{
5900 struct btrfs_key found_key;
5901 struct extent_buffer *leaf;
5902 u32 nritems;
5903 int ret;
5904
5905 while (1) {
5906 if (path->slots[0] == 0) {
5907 btrfs_set_path_blocking(path);
5908 ret = btrfs_prev_leaf(root, path);
5909 if (ret != 0)
5910 return ret;
5911 } else {
5912 path->slots[0]--;
5913 }
5914 leaf = path->nodes[0];
5915 nritems = btrfs_header_nritems(leaf);
5916 if (nritems == 0)
5917 return 1;
5918 if (path->slots[0] == nritems)
5919 path->slots[0]--;
5920
5921 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5922 if (found_key.objectid < min_objectid)
5923 break;
5924 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5925 found_key.type == BTRFS_METADATA_ITEM_KEY)
5926 return 0;
5927 if (found_key.objectid == min_objectid &&
5928 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5929 break;
5930 }
5931 return 1;
5932}