blob: bbbe4f1c5086cd82e1fffd0e72f3acc2528928b2 [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/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700359 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000360 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700361static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000362{
363 return atomic64_inc_return(&fs_info->tree_mod_seq);
364}
365
366/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200367 * This adds a new blocker to the tree mod log's blocker list if the @elem
368 * passed does not already have a sequence number set. So when a caller expects
369 * to record tree modifications, it should ensure to set elem->seq to zero
370 * before calling btrfs_get_tree_mod_seq.
371 * Returns a fresh, unused tree log modification sequence number, even if no new
372 * blocker was added.
373 */
374u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
375 struct seq_list *elem)
376{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200377 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200379 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700380 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200381 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
382 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200383 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200384 tree_mod_log_write_unlock(fs_info);
385
Josef Bacikfcebe452014-05-13 17:30:47 -0700386 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200387}
388
389void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
390 struct seq_list *elem)
391{
392 struct rb_root *tm_root;
393 struct rb_node *node;
394 struct rb_node *next;
395 struct seq_list *cur_elem;
396 struct tree_mod_elem *tm;
397 u64 min_seq = (u64)-1;
398 u64 seq_putting = elem->seq;
399
400 if (!seq_putting)
401 return;
402
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200403 spin_lock(&fs_info->tree_mod_seq_lock);
404 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200405 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200406
407 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200408 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409 if (seq_putting > cur_elem->seq) {
410 /*
411 * blocker with lower sequence number exists, we
412 * cannot remove anything from the log
413 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200414 spin_unlock(&fs_info->tree_mod_seq_lock);
415 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 }
417 min_seq = cur_elem->seq;
418 }
419 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200420 spin_unlock(&fs_info->tree_mod_seq_lock);
421
422 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200423 * anything that's lower than the lowest existing (read: blocked)
424 * sequence number can be removed from the tree.
425 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200426 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200427 tm_root = &fs_info->tree_mod_log;
428 for (node = rb_first(tm_root); node; node = next) {
429 next = rb_next(node);
430 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200431 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200432 continue;
433 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200434 kfree(tm);
435 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200436 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200437}
438
439/*
440 * key order of the log:
441 * index -> sequence
442 *
443 * the index is the shifted logical of the *new* root node for root replace
444 * operations, or the shifted logical of the affected block for all other
445 * operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000446 *
447 * Note: must be called with write lock (tree_mod_log_write_lock).
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200448 */
449static noinline int
450__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
451{
452 struct rb_root *tm_root;
453 struct rb_node **new;
454 struct rb_node *parent = NULL;
455 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456
Josef Bacikc8cc6342013-07-01 16:18:19 -0400457 BUG_ON(!tm);
458
Josef Bacikfcebe452014-05-13 17:30:47 -0700459 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200461 tm_root = &fs_info->tree_mod_log;
462 new = &tm_root->rb_node;
463 while (*new) {
464 cur = container_of(*new, struct tree_mod_elem, node);
465 parent = *new;
466 if (cur->index < tm->index)
467 new = &((*new)->rb_left);
468 else if (cur->index > tm->index)
469 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200470 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200471 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200472 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200473 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000474 else
475 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200476 }
477
478 rb_link_node(&tm->node, parent, new);
479 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000480 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200481}
482
Jan Schmidt097b8a72012-06-21 11:08:04 +0200483/*
484 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
485 * returns zero with the tree_mod_log_lock acquired. The caller must hold
486 * this until all tree mod log insertions are recorded in the rb tree and then
487 * call tree_mod_log_write_unlock() to release.
488 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200489static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
490 struct extent_buffer *eb) {
491 smp_mb();
492 if (list_empty(&(fs_info)->tree_mod_seq_list))
493 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200494 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200495 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000496
497 tree_mod_log_write_lock(fs_info);
498 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
499 tree_mod_log_write_unlock(fs_info);
500 return 1;
501 }
502
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200503 return 0;
504}
505
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000506/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
507static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
508 struct extent_buffer *eb)
509{
510 smp_mb();
511 if (list_empty(&(fs_info)->tree_mod_seq_list))
512 return 0;
513 if (eb && btrfs_header_level(eb) == 0)
514 return 0;
515
516 return 1;
517}
518
519static struct tree_mod_elem *
520alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
521 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200522{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200523 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200524
Josef Bacikc8cc6342013-07-01 16:18:19 -0400525 tm = kzalloc(sizeof(*tm), flags);
526 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000527 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200528
529 tm->index = eb->start >> PAGE_CACHE_SHIFT;
530 if (op != MOD_LOG_KEY_ADD) {
531 btrfs_node_key(eb, &tm->key, slot);
532 tm->blockptr = btrfs_node_blockptr(eb, slot);
533 }
534 tm->op = op;
535 tm->slot = slot;
536 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000537 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200538
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000539 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200540}
541
542static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400543tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
544 struct extent_buffer *eb, int slot,
545 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200546{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000547 struct tree_mod_elem *tm;
548 int ret;
549
550 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200551 return 0;
552
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000553 tm = alloc_tree_mod_elem(eb, slot, op, flags);
554 if (!tm)
555 return -ENOMEM;
556
557 if (tree_mod_dont_log(fs_info, eb)) {
558 kfree(tm);
559 return 0;
560 }
561
562 ret = __tree_mod_log_insert(fs_info, tm);
563 tree_mod_log_write_unlock(fs_info);
564 if (ret)
565 kfree(tm);
566
567 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200568}
569
570static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200571tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
572 struct extent_buffer *eb, int dst_slot, int src_slot,
573 int nr_items, gfp_t flags)
574{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000575 struct tree_mod_elem *tm = NULL;
576 struct tree_mod_elem **tm_list = NULL;
577 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200578 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000579 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200580
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000581 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200582 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200583
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000584 tm_list = kzalloc(nr_items * sizeof(struct tree_mod_elem *), flags);
585 if (!tm_list)
586 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200587
Josef Bacikc8cc6342013-07-01 16:18:19 -0400588 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000589 if (!tm) {
590 ret = -ENOMEM;
591 goto free_tms;
592 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200593
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200594 tm->index = eb->start >> PAGE_CACHE_SHIFT;
595 tm->slot = src_slot;
596 tm->move.dst_slot = dst_slot;
597 tm->move.nr_items = nr_items;
598 tm->op = MOD_LOG_MOVE_KEYS;
599
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000600 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
601 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
602 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
603 if (!tm_list[i]) {
604 ret = -ENOMEM;
605 goto free_tms;
606 }
607 }
608
609 if (tree_mod_dont_log(fs_info, eb))
610 goto free_tms;
611 locked = 1;
612
613 /*
614 * When we override something during the move, we log these removals.
615 * This can only happen when we move towards the beginning of the
616 * buffer, i.e. dst_slot < src_slot.
617 */
618 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
619 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
620 if (ret)
621 goto free_tms;
622 }
623
624 ret = __tree_mod_log_insert(fs_info, tm);
625 if (ret)
626 goto free_tms;
627 tree_mod_log_write_unlock(fs_info);
628 kfree(tm_list);
629
630 return 0;
631free_tms:
632 for (i = 0; i < nr_items; i++) {
633 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
634 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
635 kfree(tm_list[i]);
636 }
637 if (locked)
638 tree_mod_log_write_unlock(fs_info);
639 kfree(tm_list);
640 kfree(tm);
641
642 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200643}
644
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000645static inline int
646__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
647 struct tree_mod_elem **tm_list,
648 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200649{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000650 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200651 int ret;
652
Jan Schmidt097b8a72012-06-21 11:08:04 +0200653 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000654 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
655 if (ret) {
656 for (j = nritems - 1; j > i; j--)
657 rb_erase(&tm_list[j]->node,
658 &fs_info->tree_mod_log);
659 return ret;
660 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200661 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000662
663 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200664}
665
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200666static noinline int
667tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
668 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000669 struct extent_buffer *new_root, gfp_t flags,
670 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200671{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000672 struct tree_mod_elem *tm = NULL;
673 struct tree_mod_elem **tm_list = NULL;
674 int nritems = 0;
675 int ret = 0;
676 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200677
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000678 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200679 return 0;
680
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000681 if (log_removal && btrfs_header_level(old_root) > 0) {
682 nritems = btrfs_header_nritems(old_root);
683 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
684 flags);
685 if (!tm_list) {
686 ret = -ENOMEM;
687 goto free_tms;
688 }
689 for (i = 0; i < nritems; i++) {
690 tm_list[i] = alloc_tree_mod_elem(old_root, i,
691 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
692 if (!tm_list[i]) {
693 ret = -ENOMEM;
694 goto free_tms;
695 }
696 }
697 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000698
Josef Bacikc8cc6342013-07-01 16:18:19 -0400699 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000700 if (!tm) {
701 ret = -ENOMEM;
702 goto free_tms;
703 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200704
705 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
706 tm->old_root.logical = old_root->start;
707 tm->old_root.level = btrfs_header_level(old_root);
708 tm->generation = btrfs_header_generation(old_root);
709 tm->op = MOD_LOG_ROOT_REPLACE;
710
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000711 if (tree_mod_dont_log(fs_info, NULL))
712 goto free_tms;
713
714 if (tm_list)
715 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
716 if (!ret)
717 ret = __tree_mod_log_insert(fs_info, tm);
718
719 tree_mod_log_write_unlock(fs_info);
720 if (ret)
721 goto free_tms;
722 kfree(tm_list);
723
724 return ret;
725
726free_tms:
727 if (tm_list) {
728 for (i = 0; i < nritems; i++)
729 kfree(tm_list[i]);
730 kfree(tm_list);
731 }
732 kfree(tm);
733
734 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735}
736
737static struct tree_mod_elem *
738__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
739 int smallest)
740{
741 struct rb_root *tm_root;
742 struct rb_node *node;
743 struct tree_mod_elem *cur = NULL;
744 struct tree_mod_elem *found = NULL;
745 u64 index = start >> PAGE_CACHE_SHIFT;
746
Jan Schmidt097b8a72012-06-21 11:08:04 +0200747 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200748 tm_root = &fs_info->tree_mod_log;
749 node = tm_root->rb_node;
750 while (node) {
751 cur = container_of(node, struct tree_mod_elem, node);
752 if (cur->index < index) {
753 node = node->rb_left;
754 } else if (cur->index > index) {
755 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200756 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200757 node = node->rb_left;
758 } else if (!smallest) {
759 /* we want the node with the highest seq */
760 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200761 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200762 found = cur;
763 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200764 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200765 /* we want the node with the smallest seq */
766 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200767 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200768 found = cur;
769 node = node->rb_right;
770 } else {
771 found = cur;
772 break;
773 }
774 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200775 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200776
777 return found;
778}
779
780/*
781 * this returns the element from the log with the smallest time sequence
782 * value that's in the log (the oldest log item). any element with a time
783 * sequence lower than min_seq will be ignored.
784 */
785static struct tree_mod_elem *
786tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
787 u64 min_seq)
788{
789 return __tree_mod_log_search(fs_info, start, min_seq, 1);
790}
791
792/*
793 * this returns the element from the log with the largest time sequence
794 * value that's in the log (the most recent log item). any element with
795 * a time sequence lower than min_seq will be ignored.
796 */
797static struct tree_mod_elem *
798tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
799{
800 return __tree_mod_log_search(fs_info, start, min_seq, 0);
801}
802
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000803static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200804tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
805 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000806 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200807{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000808 int ret = 0;
809 struct tree_mod_elem **tm_list = NULL;
810 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200811 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000812 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200813
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000814 if (!tree_mod_need_log(fs_info, NULL))
815 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200816
Josef Bacikc8cc6342013-07-01 16:18:19 -0400817 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000818 return 0;
819
820 tm_list = kzalloc(nr_items * 2 * sizeof(struct tree_mod_elem *),
821 GFP_NOFS);
822 if (!tm_list)
823 return -ENOMEM;
824
825 tm_list_add = tm_list;
826 tm_list_rem = tm_list + nr_items;
827 for (i = 0; i < nr_items; i++) {
828 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
829 MOD_LOG_KEY_REMOVE, GFP_NOFS);
830 if (!tm_list_rem[i]) {
831 ret = -ENOMEM;
832 goto free_tms;
833 }
834
835 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
836 MOD_LOG_KEY_ADD, GFP_NOFS);
837 if (!tm_list_add[i]) {
838 ret = -ENOMEM;
839 goto free_tms;
840 }
841 }
842
843 if (tree_mod_dont_log(fs_info, NULL))
844 goto free_tms;
845 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200846
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200847 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000848 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
849 if (ret)
850 goto free_tms;
851 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
852 if (ret)
853 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200854 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000855
856 tree_mod_log_write_unlock(fs_info);
857 kfree(tm_list);
858
859 return 0;
860
861free_tms:
862 for (i = 0; i < nr_items * 2; i++) {
863 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
864 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
865 kfree(tm_list[i]);
866 }
867 if (locked)
868 tree_mod_log_write_unlock(fs_info);
869 kfree(tm_list);
870
871 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200872}
873
874static inline void
875tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
876 int dst_offset, int src_offset, int nr_items)
877{
878 int ret;
879 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
880 nr_items, GFP_NOFS);
881 BUG_ON(ret < 0);
882}
883
Jan Schmidt097b8a72012-06-21 11:08:04 +0200884static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200885tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000886 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200887{
888 int ret;
889
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000890 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400891 MOD_LOG_KEY_REPLACE,
892 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200893 BUG_ON(ret < 0);
894}
895
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000896static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200897tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200898{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000899 struct tree_mod_elem **tm_list = NULL;
900 int nritems = 0;
901 int i;
902 int ret = 0;
903
904 if (btrfs_header_level(eb) == 0)
905 return 0;
906
907 if (!tree_mod_need_log(fs_info, NULL))
908 return 0;
909
910 nritems = btrfs_header_nritems(eb);
911 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
912 GFP_NOFS);
913 if (!tm_list)
914 return -ENOMEM;
915
916 for (i = 0; i < nritems; i++) {
917 tm_list[i] = alloc_tree_mod_elem(eb, i,
918 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
919 if (!tm_list[i]) {
920 ret = -ENOMEM;
921 goto free_tms;
922 }
923 }
924
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200925 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000926 goto free_tms;
927
928 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
929 tree_mod_log_write_unlock(fs_info);
930 if (ret)
931 goto free_tms;
932 kfree(tm_list);
933
934 return 0;
935
936free_tms:
937 for (i = 0; i < nritems; i++)
938 kfree(tm_list[i]);
939 kfree(tm_list);
940
941 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200942}
943
Jan Schmidt097b8a72012-06-21 11:08:04 +0200944static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200945tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000946 struct extent_buffer *new_root_node,
947 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200948{
949 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200950 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000951 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200952 BUG_ON(ret < 0);
953}
954
Chris Masond352ac62008-09-29 15:18:18 -0400955/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 * check if the tree block can be shared by multiple trees
957 */
958int btrfs_block_can_be_shared(struct btrfs_root *root,
959 struct extent_buffer *buf)
960{
961 /*
962 * Tree blocks not in refernece counted trees and tree roots
963 * are never shared. If a block was allocated after the last
964 * snapshot and the block was not allocated by tree relocation,
965 * we know the block is not shared.
966 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800967 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 buf != root->node && buf != root->commit_root &&
969 (btrfs_header_generation(buf) <=
970 btrfs_root_last_snapshot(&root->root_item) ||
971 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
972 return 1;
973#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800974 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
976 return 1;
977#endif
978 return 0;
979}
980
981static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
982 struct btrfs_root *root,
983 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400984 struct extent_buffer *cow,
985 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986{
987 u64 refs;
988 u64 owner;
989 u64 flags;
990 u64 new_flags = 0;
991 int ret;
992
993 /*
994 * Backrefs update rules:
995 *
996 * Always use full backrefs for extent pointers in tree block
997 * allocated by tree relocation.
998 *
999 * If a shared tree block is no longer referenced by its owner
1000 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
1001 * use full backrefs for extent pointers in tree block.
1002 *
1003 * If a tree block is been relocating
1004 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
1005 * use full backrefs for extent pointers in tree block.
1006 * The reason for this is some operations (such as drop tree)
1007 * are only allowed for blocks use full backrefs.
1008 */
1009
1010 if (btrfs_block_can_be_shared(root, buf)) {
1011 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -05001012 btrfs_header_level(buf), 1,
1013 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001014 if (ret)
1015 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -07001016 if (refs == 0) {
1017 ret = -EROFS;
1018 btrfs_std_error(root->fs_info, ret);
1019 return ret;
1020 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 } else {
1022 refs = 1;
1023 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1024 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1025 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1026 else
1027 flags = 0;
1028 }
1029
1030 owner = btrfs_header_owner(buf);
1031 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1032 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1033
1034 if (refs > 1) {
1035 if ((owner == root->root_key.objectid ||
1036 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1037 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001038 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040
1041 if (root->root_key.objectid ==
1042 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001043 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001044 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001045 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001046 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 }
1048 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1049 } else {
1050
1051 if (root->root_key.objectid ==
1052 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001053 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001055 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001057 }
1058 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001059 int level = btrfs_header_level(buf);
1060
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001061 ret = btrfs_set_disk_extent_flags(trans, root,
1062 buf->start,
1063 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001064 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001065 if (ret)
1066 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001067 }
1068 } else {
1069 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1070 if (root->root_key.objectid ==
1071 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001072 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001073 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001074 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001075 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001076 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 }
1079 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001080 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081 }
1082 return 0;
1083}
1084
1085/*
Chris Masond3977122009-01-05 21:25:51 -05001086 * does the dirty work in cow of a single block. The parent block (if
1087 * supplied) is updated to point to the new cow copy. The new buffer is marked
1088 * dirty and returned locked. If you modify the block it needs to be marked
1089 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001090 *
1091 * search_start -- an allocation hint for the new block
1092 *
Chris Masond3977122009-01-05 21:25:51 -05001093 * empty_size -- a hint that you plan on doing more cow. This is the size in
1094 * bytes the allocator should try to find free next to the block it returns.
1095 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001096 */
Chris Masond3977122009-01-05 21:25:51 -05001097static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001098 struct btrfs_root *root,
1099 struct extent_buffer *buf,
1100 struct extent_buffer *parent, int parent_slot,
1101 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001102 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001103{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001105 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001106 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001107 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001108 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001110
Chris Mason925baed2008-06-25 16:01:30 -04001111 if (*cow_ret == buf)
1112 unlock_orig = 1;
1113
Chris Masonb9447ef82009-03-09 11:45:38 -04001114 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001115
Miao Xie27cdeb72014-04-02 19:51:05 +08001116 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1117 trans->transid != root->fs_info->running_transaction->transid);
1118 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1119 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001120
Chris Mason7bb86312007-12-11 09:25:06 -05001121 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001122
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123 if (level == 0)
1124 btrfs_item_key(buf, &disk_key, 0);
1125 else
1126 btrfs_node_key(buf, &disk_key, 0);
1127
1128 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1129 if (parent)
1130 parent_start = parent->start;
1131 else
1132 parent_start = 0;
1133 } else
1134 parent_start = 0;
1135
1136 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1137 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001138 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001139 if (IS_ERR(cow))
1140 return PTR_ERR(cow);
1141
Chris Masonb4ce94d2009-02-04 09:25:08 -05001142 /* cow is set to blocking by btrfs_init_new_buffer */
1143
Chris Mason5f39d392007-10-15 16:14:19 -04001144 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001145 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001146 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1148 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1149 BTRFS_HEADER_FLAG_RELOC);
1150 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1151 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1152 else
1153 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001154
Ross Kirk0a4e5582013-09-24 10:12:38 +01001155 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001156 BTRFS_FSID_SIZE);
1157
Mark Fashehbe1a5562011-08-08 13:20:18 -07001158 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001159 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001160 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001161 return ret;
1162 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001163
Miao Xie27cdeb72014-04-02 19:51:05 +08001164 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001165 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1166 if (ret)
1167 return ret;
1168 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001169
Chris Mason6702ed42007-08-07 16:15:09 -04001170 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001171 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001172 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1173 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1174 parent_start = buf->start;
1175 else
1176 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001177
Chris Mason5f39d392007-10-15 16:14:19 -04001178 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001179 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001180 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001181
Yan, Zhengf0486c62010-05-16 10:46:25 -04001182 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001183 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001185 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001186 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1188 parent_start = parent->start;
1189 else
1190 parent_start = 0;
1191
1192 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001193 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001194 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001195 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001196 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001197 btrfs_set_node_ptr_generation(parent, parent_slot,
1198 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001199 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001200 if (last_ref) {
1201 ret = tree_mod_log_free_eb(root->fs_info, buf);
1202 if (ret) {
1203 btrfs_abort_transaction(trans, root, ret);
1204 return ret;
1205 }
1206 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001207 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001208 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001209 }
Chris Mason925baed2008-06-25 16:01:30 -04001210 if (unlock_orig)
1211 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001212 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001213 btrfs_mark_buffer_dirty(cow);
1214 *cow_ret = cow;
1215 return 0;
1216}
1217
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001218/*
1219 * returns the logical address of the oldest predecessor of the given root.
1220 * entries older than time_seq are ignored.
1221 */
1222static struct tree_mod_elem *
1223__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001224 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001225{
1226 struct tree_mod_elem *tm;
1227 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001228 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001229 int looped = 0;
1230
1231 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001232 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001233
1234 /*
1235 * the very last operation that's logged for a root is the replacement
1236 * operation (if it is replaced at all). this has the index of the *new*
1237 * root, making it the very first operation that's logged for this root.
1238 */
1239 while (1) {
1240 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1241 time_seq);
1242 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001243 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001244 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001245 * if there are no tree operation for the oldest root, we simply
1246 * return it. this should only happen if that (old) root is at
1247 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001248 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001249 if (!tm)
1250 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001251
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001252 /*
1253 * if there's an operation that's not a root replacement, we
1254 * found the oldest version of our root. normally, we'll find a
1255 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1256 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001257 if (tm->op != MOD_LOG_ROOT_REPLACE)
1258 break;
1259
1260 found = tm;
1261 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001262 looped = 1;
1263 }
1264
Jan Schmidta95236d2012-06-05 16:41:24 +02001265 /* if there's no old root to return, return what we found instead */
1266 if (!found)
1267 found = tm;
1268
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001269 return found;
1270}
1271
1272/*
1273 * tm is a pointer to the first operation to rewind within eb. then, all
1274 * previous operations will be rewinded (until we reach something older than
1275 * time_seq).
1276 */
1277static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001278__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1279 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280{
1281 u32 n;
1282 struct rb_node *next;
1283 struct tree_mod_elem *tm = first_tm;
1284 unsigned long o_dst;
1285 unsigned long o_src;
1286 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1287
1288 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001289 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001290 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001291 /*
1292 * all the operations are recorded with the operator used for
1293 * the modification. as we're going backwards, we do the
1294 * opposite of each operation here.
1295 */
1296 switch (tm->op) {
1297 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1298 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001299 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001300 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001301 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001302 btrfs_set_node_key(eb, &tm->key, tm->slot);
1303 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1304 btrfs_set_node_ptr_generation(eb, tm->slot,
1305 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001306 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307 break;
1308 case MOD_LOG_KEY_REPLACE:
1309 BUG_ON(tm->slot >= n);
1310 btrfs_set_node_key(eb, &tm->key, tm->slot);
1311 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1312 btrfs_set_node_ptr_generation(eb, tm->slot,
1313 tm->generation);
1314 break;
1315 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001316 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001317 n--;
1318 break;
1319 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001320 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1321 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1322 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001323 tm->move.nr_items * p_size);
1324 break;
1325 case MOD_LOG_ROOT_REPLACE:
1326 /*
1327 * this operation is special. for roots, this must be
1328 * handled explicitly before rewinding.
1329 * for non-roots, this operation may exist if the node
1330 * was a root: root A -> child B; then A gets empty and
1331 * B is promoted to the new root. in the mod log, we'll
1332 * have a root-replace operation for B, a tree block
1333 * that is no root. we simply ignore that operation.
1334 */
1335 break;
1336 }
1337 next = rb_next(&tm->node);
1338 if (!next)
1339 break;
1340 tm = container_of(next, struct tree_mod_elem, node);
1341 if (tm->index != first_tm->index)
1342 break;
1343 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001344 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001345 btrfs_set_header_nritems(eb, n);
1346}
1347
Jan Schmidt47fb0912013-04-13 13:19:55 +00001348/*
1349 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1350 * is returned. If rewind operations happen, a fresh buffer is returned. The
1351 * returned buffer is always read-locked. If the returned buffer is not the
1352 * input buffer, the lock on the input buffer is released and the input buffer
1353 * is freed (its refcount is decremented).
1354 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001355static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001356tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1357 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001358{
1359 struct extent_buffer *eb_rewin;
1360 struct tree_mod_elem *tm;
1361
1362 if (!time_seq)
1363 return eb;
1364
1365 if (btrfs_header_level(eb) == 0)
1366 return eb;
1367
1368 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1369 if (!tm)
1370 return eb;
1371
Josef Bacik9ec72672013-08-07 16:57:23 -04001372 btrfs_set_path_blocking(path);
1373 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1374
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001375 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1376 BUG_ON(tm->slot != 0);
1377 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1378 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001379 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001380 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001381 free_extent_buffer(eb);
1382 return NULL;
1383 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001384 btrfs_set_header_bytenr(eb_rewin, eb->start);
1385 btrfs_set_header_backref_rev(eb_rewin,
1386 btrfs_header_backref_rev(eb));
1387 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001388 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389 } else {
1390 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001391 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001392 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001393 free_extent_buffer(eb);
1394 return NULL;
1395 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001396 }
1397
Josef Bacik9ec72672013-08-07 16:57:23 -04001398 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1399 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001400 free_extent_buffer(eb);
1401
Jan Schmidt47fb0912013-04-13 13:19:55 +00001402 extent_buffer_get(eb_rewin);
1403 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001404 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001405 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001406 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001407
1408 return eb_rewin;
1409}
1410
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001411/*
1412 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1413 * value. If there are no changes, the current root->root_node is returned. If
1414 * anything changed in between, there's a fresh buffer allocated on which the
1415 * rewind operations are done. In any case, the returned buffer is read locked.
1416 * Returns NULL on error (with no locks held).
1417 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001418static inline struct extent_buffer *
1419get_old_root(struct btrfs_root *root, u64 time_seq)
1420{
1421 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001422 struct extent_buffer *eb = NULL;
1423 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001424 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001425 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001426 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001427 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001428 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001429
Jan Schmidt30b04632013-04-13 13:19:54 +00001430 eb_root = btrfs_read_lock_root_node(root);
1431 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001432 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001433 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001434
Jan Schmidta95236d2012-06-05 16:41:24 +02001435 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1436 old_root = &tm->old_root;
1437 old_generation = tm->generation;
1438 logical = old_root->logical;
1439 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001440 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001441 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001442
Jan Schmidta95236d2012-06-05 16:41:24 +02001443 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001444 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001445 btrfs_tree_read_unlock(eb_root);
1446 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001447 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001448 old = read_tree_block(root, logical, blocksize, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301449 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001450 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001451 btrfs_warn(root->fs_info,
1452 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001453 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001454 eb = btrfs_clone_extent_buffer(old);
1455 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001456 }
1457 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001458 btrfs_tree_read_unlock(eb_root);
1459 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001460 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001461 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001462 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001463 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001464 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001465 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001466 }
1467
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001468 if (!eb)
1469 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001470 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001471 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001472 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001473 btrfs_set_header_bytenr(eb, eb->start);
1474 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001475 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001476 btrfs_set_header_level(eb, old_root->level);
1477 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001478 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001479 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001480 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001481 else
1482 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001483 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001484
1485 return eb;
1486}
1487
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001488int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1489{
1490 struct tree_mod_elem *tm;
1491 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001492 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001493
Jan Schmidt30b04632013-04-13 13:19:54 +00001494 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001495 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1496 level = tm->old_root.level;
1497 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001498 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001499 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001500 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001501
1502 return level;
1503}
1504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001505static inline int should_cow_block(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct extent_buffer *buf)
1508{
Liu Bof1ebcc72011-11-14 20:48:06 -05001509 /* ensure we can see the force_cow */
1510 smp_rmb();
1511
1512 /*
1513 * We do not need to cow a block if
1514 * 1) this block is not created or changed in this transaction;
1515 * 2) this block does not belong to TREE_RELOC tree;
1516 * 3) the root is not forced COW.
1517 *
1518 * What is forced COW:
1519 * when we create snapshot during commiting the transaction,
1520 * after we've finished coping src root, we must COW the shared
1521 * block to ensure the metadata consistency.
1522 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001523 if (btrfs_header_generation(buf) == trans->transid &&
1524 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1525 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001526 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001527 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001528 return 0;
1529 return 1;
1530}
1531
Chris Masond352ac62008-09-29 15:18:18 -04001532/*
1533 * cows a single block, see __btrfs_cow_block for the real work.
1534 * This version of it has extra checks so that a block isn't cow'd more than
1535 * once per transaction, as long as it hasn't been written yet
1536 */
Chris Masond3977122009-01-05 21:25:51 -05001537noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001538 struct btrfs_root *root, struct extent_buffer *buf,
1539 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001540 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001541{
Chris Mason6702ed42007-08-07 16:15:09 -04001542 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001543 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001544
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001545 if (trans->transaction != root->fs_info->running_transaction)
1546 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001547 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001548 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001549
1550 if (trans->transid != root->fs_info->generation)
1551 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001552 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001553
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001554 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001555 *cow_ret = buf;
1556 return 0;
1557 }
Chris Masonc4876852009-02-04 09:24:25 -05001558
Chris Mason0b86a832008-03-24 15:01:56 -04001559 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001560
1561 if (parent)
1562 btrfs_set_lock_blocking(parent);
1563 btrfs_set_lock_blocking(buf);
1564
Chris Masonf510cfe2007-10-15 16:14:48 -04001565 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001566 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001567
1568 trace_btrfs_cow_block(root, buf, *cow_ret);
1569
Chris Masonf510cfe2007-10-15 16:14:48 -04001570 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * helper function for defrag to decide if two blocks pointed to by a
1575 * node are actually close by
1576 */
Chris Mason6b800532007-10-15 16:17:34 -04001577static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001578{
Chris Mason6b800532007-10-15 16:17:34 -04001579 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001580 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001581 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001582 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001583 return 0;
1584}
1585
Chris Mason081e9572007-11-06 10:26:24 -05001586/*
1587 * compare two keys in a memcmp fashion
1588 */
1589static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1590{
1591 struct btrfs_key k1;
1592
1593 btrfs_disk_key_to_cpu(&k1, disk);
1594
Diego Calleja20736ab2009-07-24 11:06:52 -04001595 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001596}
1597
Josef Bacikf3465ca2008-11-12 14:19:50 -05001598/*
1599 * same as comp_keys only with two btrfs_key's
1600 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001601int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001602{
1603 if (k1->objectid > k2->objectid)
1604 return 1;
1605 if (k1->objectid < k2->objectid)
1606 return -1;
1607 if (k1->type > k2->type)
1608 return 1;
1609 if (k1->type < k2->type)
1610 return -1;
1611 if (k1->offset > k2->offset)
1612 return 1;
1613 if (k1->offset < k2->offset)
1614 return -1;
1615 return 0;
1616}
Chris Mason081e9572007-11-06 10:26:24 -05001617
Chris Masond352ac62008-09-29 15:18:18 -04001618/*
1619 * this is used by the defrag code to go through all the
1620 * leaves pointed to by a node and reallocate them so that
1621 * disk order is close to key order
1622 */
Chris Mason6702ed42007-08-07 16:15:09 -04001623int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001624 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001625 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001626 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001627{
Chris Mason6b800532007-10-15 16:17:34 -04001628 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001629 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001630 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001631 u64 search_start = *last_ret;
1632 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001633 u64 other;
1634 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001635 int end_slot;
1636 int i;
1637 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001638 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001639 int uptodate;
1640 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001641 int progress_passed = 0;
1642 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001643
Chris Mason5708b952007-10-25 15:43:18 -04001644 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001645
Julia Lawall6c1500f2012-11-03 20:30:18 +00001646 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1647 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001648
Chris Mason6b800532007-10-15 16:17:34 -04001649 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001650 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001651 end_slot = parent_nritems;
1652
1653 if (parent_nritems == 1)
1654 return 0;
1655
Chris Masonb4ce94d2009-02-04 09:25:08 -05001656 btrfs_set_lock_blocking(parent);
1657
Chris Mason6702ed42007-08-07 16:15:09 -04001658 for (i = start_slot; i < end_slot; i++) {
1659 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001660
Chris Mason081e9572007-11-06 10:26:24 -05001661 btrfs_node_key(parent, &disk_key, i);
1662 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1663 continue;
1664
1665 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001666 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001667 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001668 if (last_block == 0)
1669 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001670
Chris Mason6702ed42007-08-07 16:15:09 -04001671 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001672 other = btrfs_node_blockptr(parent, i - 1);
1673 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001674 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001675 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001676 other = btrfs_node_blockptr(parent, i + 1);
1677 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001678 }
Chris Masone9d0b132007-08-10 14:06:19 -04001679 if (close) {
1680 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001681 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001682 }
Chris Mason6702ed42007-08-07 16:15:09 -04001683
Chris Mason6b800532007-10-15 16:17:34 -04001684 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1685 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001686 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001687 else
1688 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001689 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001690 if (!cur) {
1691 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001692 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001693 if (!cur || !extent_buffer_uptodate(cur)) {
1694 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001695 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001696 }
Chris Mason6b800532007-10-15 16:17:34 -04001697 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001698 err = btrfs_read_buffer(cur, gen);
1699 if (err) {
1700 free_extent_buffer(cur);
1701 return err;
1702 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001703 }
Chris Mason6702ed42007-08-07 16:15:09 -04001704 }
Chris Masone9d0b132007-08-10 14:06:19 -04001705 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001706 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001707
Chris Masone7a84562008-06-25 16:01:31 -04001708 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001709 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001710 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001711 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001712 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001713 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001714 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001715 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001716 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001717 break;
Yan252c38f2007-08-29 09:11:44 -04001718 }
Chris Masone7a84562008-06-25 16:01:31 -04001719 search_start = cur->start;
1720 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001721 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001722 btrfs_tree_unlock(cur);
1723 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001724 }
1725 return err;
1726}
1727
Chris Mason74123bd2007-02-02 11:05:29 -05001728/*
1729 * The leaf data grows from end-to-front in the node.
1730 * this returns the address of the start of the last item,
1731 * which is the stop of the leaf data stack
1732 */
Chris Mason123abc82007-03-14 14:14:43 -04001733static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001734 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001735{
Chris Mason5f39d392007-10-15 16:14:19 -04001736 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001737 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001738 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001739 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001740}
1741
Chris Masonaa5d6be2007-02-28 16:35:06 -05001742
Chris Mason74123bd2007-02-02 11:05:29 -05001743/*
Chris Mason5f39d392007-10-15 16:14:19 -04001744 * search for key in the extent_buffer. The items start at offset p,
1745 * and they are item_size apart. There are 'max' items in p.
1746 *
Chris Mason74123bd2007-02-02 11:05:29 -05001747 * the slot in the array is returned via slot, and it points to
1748 * the place where you would insert key if it is not found in
1749 * the array.
1750 *
1751 * slot may point to max if the key is bigger than all of the keys
1752 */
Chris Masone02119d2008-09-05 16:13:11 -04001753static noinline int generic_bin_search(struct extent_buffer *eb,
1754 unsigned long p,
1755 int item_size, struct btrfs_key *key,
1756 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757{
1758 int low = 0;
1759 int high = max;
1760 int mid;
1761 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001762 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001763 struct btrfs_disk_key unaligned;
1764 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001765 char *kaddr = NULL;
1766 unsigned long map_start = 0;
1767 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001768 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769
Chris Masond3977122009-01-05 21:25:51 -05001770 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001771 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001772 offset = p + mid * item_size;
1773
Chris Masona6591712011-07-19 12:04:14 -04001774 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001775 (offset + sizeof(struct btrfs_disk_key)) >
1776 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001777
1778 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001779 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001780 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001781
Chris Mason479965d2007-10-15 16:14:27 -04001782 if (!err) {
1783 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1784 map_start);
1785 } else {
1786 read_extent_buffer(eb, &unaligned,
1787 offset, sizeof(unaligned));
1788 tmp = &unaligned;
1789 }
1790
Chris Mason5f39d392007-10-15 16:14:19 -04001791 } else {
1792 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1793 map_start);
1794 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001795 ret = comp_keys(tmp, key);
1796
1797 if (ret < 0)
1798 low = mid + 1;
1799 else if (ret > 0)
1800 high = mid;
1801 else {
1802 *slot = mid;
1803 return 0;
1804 }
1805 }
1806 *slot = low;
1807 return 1;
1808}
1809
Chris Mason97571fd2007-02-24 13:39:08 -05001810/*
1811 * simple bin_search frontend that does the right thing for
1812 * leaves vs nodes
1813 */
Chris Mason5f39d392007-10-15 16:14:19 -04001814static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1815 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001816{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001817 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001818 return generic_bin_search(eb,
1819 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001820 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001821 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001822 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001823 else
Chris Mason5f39d392007-10-15 16:14:19 -04001824 return generic_bin_search(eb,
1825 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001826 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001827 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001828 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001829}
1830
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001831int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1832 int level, int *slot)
1833{
1834 return bin_search(eb, key, level, slot);
1835}
1836
Yan, Zhengf0486c62010-05-16 10:46:25 -04001837static void root_add_used(struct btrfs_root *root, u32 size)
1838{
1839 spin_lock(&root->accounting_lock);
1840 btrfs_set_root_used(&root->root_item,
1841 btrfs_root_used(&root->root_item) + size);
1842 spin_unlock(&root->accounting_lock);
1843}
1844
1845static void root_sub_used(struct btrfs_root *root, u32 size)
1846{
1847 spin_lock(&root->accounting_lock);
1848 btrfs_set_root_used(&root->root_item,
1849 btrfs_root_used(&root->root_item) - size);
1850 spin_unlock(&root->accounting_lock);
1851}
1852
Chris Masond352ac62008-09-29 15:18:18 -04001853/* given a node and slot number, this reads the blocks it points to. The
1854 * extent buffer is returned with a reference taken (but unlocked).
1855 * NULL is returned on error.
1856 */
Chris Masone02119d2008-09-05 16:13:11 -04001857static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001858 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001859{
Chris Masonca7a79a2008-05-12 12:59:19 -04001860 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001861 struct extent_buffer *eb;
1862
Chris Masonbb803952007-03-01 12:04:21 -05001863 if (slot < 0)
1864 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001865 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001866 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001867
1868 BUG_ON(level == 0);
1869
Josef Bacik416bc652013-04-23 14:17:42 -04001870 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1871 btrfs_level_size(root, level - 1),
1872 btrfs_node_ptr_generation(parent, slot));
1873 if (eb && !extent_buffer_uptodate(eb)) {
1874 free_extent_buffer(eb);
1875 eb = NULL;
1876 }
1877
1878 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001879}
1880
Chris Masond352ac62008-09-29 15:18:18 -04001881/*
1882 * node level balancing, used to make sure nodes are in proper order for
1883 * item deletion. We balance from the top down, so we have to make sure
1884 * that a deletion won't leave an node completely empty later on.
1885 */
Chris Masone02119d2008-09-05 16:13:11 -04001886static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001887 struct btrfs_root *root,
1888 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001889{
Chris Mason5f39d392007-10-15 16:14:19 -04001890 struct extent_buffer *right = NULL;
1891 struct extent_buffer *mid;
1892 struct extent_buffer *left = NULL;
1893 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001894 int ret = 0;
1895 int wret;
1896 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001897 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001898 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001899
1900 if (level == 0)
1901 return 0;
1902
Chris Mason5f39d392007-10-15 16:14:19 -04001903 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001904
Chris Masonbd681512011-07-16 15:23:14 -04001905 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1906 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001907 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1908
Chris Mason1d4f8a02007-03-13 09:28:32 -04001909 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001910
Li Zefana05a9bb2011-09-06 16:55:34 +08001911 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001912 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001913 pslot = path->slots[level + 1];
1914 }
Chris Masonbb803952007-03-01 12:04:21 -05001915
Chris Mason40689472007-03-17 14:29:23 -04001916 /*
1917 * deal with the case where there is only one pointer in the root
1918 * by promoting the node below to a root
1919 */
Chris Mason5f39d392007-10-15 16:14:19 -04001920 if (!parent) {
1921 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001922
Chris Mason5f39d392007-10-15 16:14:19 -04001923 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001924 return 0;
1925
1926 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001927 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001928 if (!child) {
1929 ret = -EROFS;
1930 btrfs_std_error(root->fs_info, ret);
1931 goto enospc;
1932 }
1933
Chris Mason925baed2008-06-25 16:01:30 -04001934 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001935 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001936 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001937 if (ret) {
1938 btrfs_tree_unlock(child);
1939 free_extent_buffer(child);
1940 goto enospc;
1941 }
Yan2f375ab2008-02-01 14:58:07 -05001942
Jan Schmidt90f8d622013-04-13 13:19:53 +00001943 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001944 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001945
Chris Mason0b86a832008-03-24 15:01:56 -04001946 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001947 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001948
Chris Mason925baed2008-06-25 16:01:30 -04001949 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001950 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001951 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001952 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001953 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001954 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001955
1956 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001957 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001958 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001959 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001960 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001961 }
Chris Mason5f39d392007-10-15 16:14:19 -04001962 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001963 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001964 return 0;
1965
Chris Mason5f39d392007-10-15 16:14:19 -04001966 left = read_node_slot(root, parent, pslot - 1);
1967 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001968 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001969 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001971 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001972 if (wret) {
1973 ret = wret;
1974 goto enospc;
1975 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001976 }
Chris Mason5f39d392007-10-15 16:14:19 -04001977 right = read_node_slot(root, parent, pslot + 1);
1978 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001979 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001980 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001981 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001982 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001983 if (wret) {
1984 ret = wret;
1985 goto enospc;
1986 }
1987 }
1988
1989 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001990 if (left) {
1991 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001992 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001993 if (wret < 0)
1994 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001995 }
Chris Mason79f95c82007-03-01 15:16:26 -05001996
1997 /*
1998 * then try to empty the right most buffer into the middle
1999 */
Chris Mason5f39d392007-10-15 16:14:19 -04002000 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04002001 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002002 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05002003 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002004 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002005 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002006 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002007 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002008 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002009 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002010 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002011 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05002012 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002013 struct btrfs_disk_key right_key;
2014 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002015 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002016 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002017 btrfs_set_node_key(parent, &right_key, pslot + 1);
2018 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002019 }
2020 }
Chris Mason5f39d392007-10-15 16:14:19 -04002021 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002022 /*
2023 * we're not allowed to leave a node with one item in the
2024 * tree during a delete. A deletion from lower in the tree
2025 * could try to delete the only pointer in this node.
2026 * So, pull some keys from the left.
2027 * There has to be a left pointer at this point because
2028 * otherwise we would have pulled some pointers from the
2029 * right
2030 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002031 if (!left) {
2032 ret = -EROFS;
2033 btrfs_std_error(root->fs_info, ret);
2034 goto enospc;
2035 }
Chris Mason5f39d392007-10-15 16:14:19 -04002036 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002037 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002038 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002039 goto enospc;
2040 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002041 if (wret == 1) {
2042 wret = push_node_left(trans, root, left, mid, 1);
2043 if (wret < 0)
2044 ret = wret;
2045 }
Chris Mason79f95c82007-03-01 15:16:26 -05002046 BUG_ON(wret == 1);
2047 }
Chris Mason5f39d392007-10-15 16:14:19 -04002048 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002049 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002050 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002051 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002052 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002053 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002054 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002055 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002056 } else {
2057 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002058 struct btrfs_disk_key mid_key;
2059 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002060 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002061 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002062 btrfs_set_node_key(parent, &mid_key, pslot);
2063 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002064 }
Chris Masonbb803952007-03-01 12:04:21 -05002065
Chris Mason79f95c82007-03-01 15:16:26 -05002066 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002067 if (left) {
2068 if (btrfs_header_nritems(left) > orig_slot) {
2069 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002070 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002071 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002072 path->slots[level + 1] -= 1;
2073 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002074 if (mid) {
2075 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002076 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002077 }
Chris Masonbb803952007-03-01 12:04:21 -05002078 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002079 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002080 path->slots[level] = orig_slot;
2081 }
2082 }
Chris Mason79f95c82007-03-01 15:16:26 -05002083 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002084 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002085 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002086 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002087enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002088 if (right) {
2089 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002091 }
2092 if (left) {
2093 if (path->nodes[level] != left)
2094 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002095 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002096 }
Chris Masonbb803952007-03-01 12:04:21 -05002097 return ret;
2098}
2099
Chris Masond352ac62008-09-29 15:18:18 -04002100/* Node balancing for insertion. Here we only split or push nodes around
2101 * when they are completely full. This is also done top down, so we
2102 * have to be pessimistic.
2103 */
Chris Masond3977122009-01-05 21:25:51 -05002104static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002105 struct btrfs_root *root,
2106 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002107{
Chris Mason5f39d392007-10-15 16:14:19 -04002108 struct extent_buffer *right = NULL;
2109 struct extent_buffer *mid;
2110 struct extent_buffer *left = NULL;
2111 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002112 int ret = 0;
2113 int wret;
2114 int pslot;
2115 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002116
2117 if (level == 0)
2118 return 1;
2119
Chris Mason5f39d392007-10-15 16:14:19 -04002120 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002121 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002122
Li Zefana05a9bb2011-09-06 16:55:34 +08002123 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002124 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002125 pslot = path->slots[level + 1];
2126 }
Chris Masone66f7092007-04-20 13:16:02 -04002127
Chris Mason5f39d392007-10-15 16:14:19 -04002128 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002129 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002130
Chris Mason5f39d392007-10-15 16:14:19 -04002131 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002132
2133 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002134 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002135 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002136
2137 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002138 btrfs_set_lock_blocking(left);
2139
Chris Mason5f39d392007-10-15 16:14:19 -04002140 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002141 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2142 wret = 1;
2143 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002144 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002145 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002146 if (ret)
2147 wret = 1;
2148 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002149 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002150 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002151 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002152 }
Chris Masone66f7092007-04-20 13:16:02 -04002153 if (wret < 0)
2154 ret = wret;
2155 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002156 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002157 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002158 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002159 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002160 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 btrfs_set_node_key(parent, &disk_key, pslot);
2162 btrfs_mark_buffer_dirty(parent);
2163 if (btrfs_header_nritems(left) > orig_slot) {
2164 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002165 path->slots[level + 1] -= 1;
2166 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002167 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002169 } else {
2170 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002171 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002172 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002173 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002174 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002175 }
Chris Masone66f7092007-04-20 13:16:02 -04002176 return 0;
2177 }
Chris Mason925baed2008-06-25 16:01:30 -04002178 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002179 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002180 }
Chris Mason925baed2008-06-25 16:01:30 -04002181 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002182
2183 /*
2184 * then try to empty the right most buffer into the middle
2185 */
Chris Mason5f39d392007-10-15 16:14:19 -04002186 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002187 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002188
Chris Mason925baed2008-06-25 16:01:30 -04002189 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002190 btrfs_set_lock_blocking(right);
2191
Chris Mason5f39d392007-10-15 16:14:19 -04002192 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002193 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2194 wret = 1;
2195 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002196 ret = btrfs_cow_block(trans, root, right,
2197 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002198 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002199 if (ret)
2200 wret = 1;
2201 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002202 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002203 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002204 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002205 }
Chris Masone66f7092007-04-20 13:16:02 -04002206 if (wret < 0)
2207 ret = wret;
2208 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002209 struct btrfs_disk_key disk_key;
2210
2211 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002212 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002213 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2215 btrfs_mark_buffer_dirty(parent);
2216
2217 if (btrfs_header_nritems(mid) <= orig_slot) {
2218 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002219 path->slots[level + 1] += 1;
2220 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002221 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002222 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002223 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002224 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002225 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002226 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002227 }
Chris Masone66f7092007-04-20 13:16:02 -04002228 return 0;
2229 }
Chris Mason925baed2008-06-25 16:01:30 -04002230 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002231 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002232 }
Chris Masone66f7092007-04-20 13:16:02 -04002233 return 1;
2234}
2235
Chris Mason74123bd2007-02-02 11:05:29 -05002236/*
Chris Masond352ac62008-09-29 15:18:18 -04002237 * readahead one full node of leaves, finding things that are close
2238 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002239 */
Chris Masonc8c42862009-04-03 10:14:18 -04002240static void reada_for_search(struct btrfs_root *root,
2241 struct btrfs_path *path,
2242 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002243{
Chris Mason5f39d392007-10-15 16:14:19 -04002244 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002245 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002246 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002247 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002248 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002249 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002250 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002251 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002252 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002253 u32 nr;
2254 u32 blocksize;
2255 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002256
Chris Masona6b6e752007-10-15 16:22:39 -04002257 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002258 return;
2259
Chris Mason6702ed42007-08-07 16:15:09 -04002260 if (!path->nodes[level])
2261 return;
2262
Chris Mason5f39d392007-10-15 16:14:19 -04002263 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002264
Chris Mason3c69fae2007-08-07 15:52:22 -04002265 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002266 blocksize = btrfs_level_size(root, level - 1);
2267 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002268 if (eb) {
2269 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002270 return;
2271 }
2272
Chris Masona7175312009-01-22 09:23:10 -05002273 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002274
Chris Mason5f39d392007-10-15 16:14:19 -04002275 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002276 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002277
Chris Masond3977122009-01-05 21:25:51 -05002278 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002279 if (direction < 0) {
2280 if (nr == 0)
2281 break;
2282 nr--;
2283 } else if (direction > 0) {
2284 nr++;
2285 if (nr >= nritems)
2286 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002287 }
Chris Mason01f46652007-12-21 16:24:26 -05002288 if (path->reada < 0 && objectid) {
2289 btrfs_node_key(node, &disk_key, nr);
2290 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2291 break;
2292 }
Chris Mason6b800532007-10-15 16:17:34 -04002293 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002294 if ((search <= target && target - search <= 65536) ||
2295 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002296 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002297 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002298 nread += blocksize;
2299 }
2300 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002301 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002302 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002303 }
2304}
Chris Mason925baed2008-06-25 16:01:30 -04002305
Josef Bacik0b088512013-06-17 14:23:02 -04002306static noinline void reada_for_balance(struct btrfs_root *root,
2307 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002308{
2309 int slot;
2310 int nritems;
2311 struct extent_buffer *parent;
2312 struct extent_buffer *eb;
2313 u64 gen;
2314 u64 block1 = 0;
2315 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002316 int blocksize;
2317
Chris Mason8c594ea2009-04-20 15:50:10 -04002318 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002319 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002320 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002321
2322 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002323 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002324 blocksize = btrfs_level_size(root, level);
2325
2326 if (slot > 0) {
2327 block1 = btrfs_node_blockptr(parent, slot - 1);
2328 gen = btrfs_node_ptr_generation(parent, slot - 1);
2329 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002330 /*
2331 * if we get -eagain from btrfs_buffer_uptodate, we
2332 * don't want to return eagain here. That will loop
2333 * forever
2334 */
2335 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002336 block1 = 0;
2337 free_extent_buffer(eb);
2338 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002339 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002340 block2 = btrfs_node_blockptr(parent, slot + 1);
2341 gen = btrfs_node_ptr_generation(parent, slot + 1);
2342 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002343 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002344 block2 = 0;
2345 free_extent_buffer(eb);
2346 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002347
Josef Bacik0b088512013-06-17 14:23:02 -04002348 if (block1)
2349 readahead_tree_block(root, block1, blocksize, 0);
2350 if (block2)
2351 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002352}
2353
2354
2355/*
Chris Masond3977122009-01-05 21:25:51 -05002356 * when we walk down the tree, it is usually safe to unlock the higher layers
2357 * in the tree. The exceptions are when our path goes through slot 0, because
2358 * operations on the tree might require changing key pointers higher up in the
2359 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002360 *
Chris Masond3977122009-01-05 21:25:51 -05002361 * callers might also have set path->keep_locks, which tells this code to keep
2362 * the lock if the path points to the last slot in the block. This is part of
2363 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002364 *
Chris Masond3977122009-01-05 21:25:51 -05002365 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2366 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002367 */
Chris Masone02119d2008-09-05 16:13:11 -04002368static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002369 int lowest_unlock, int min_write_lock_level,
2370 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002371{
2372 int i;
2373 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002374 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002375 struct extent_buffer *t;
2376
2377 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2378 if (!path->nodes[i])
2379 break;
2380 if (!path->locks[i])
2381 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002382 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002383 skip_level = i + 1;
2384 continue;
2385 }
Chris Mason051e1b92008-06-25 16:01:30 -04002386 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002387 u32 nritems;
2388 t = path->nodes[i];
2389 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002390 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002391 skip_level = i + 1;
2392 continue;
2393 }
2394 }
Chris Mason051e1b92008-06-25 16:01:30 -04002395 if (skip_level < i && i >= lowest_unlock)
2396 no_skips = 1;
2397
Chris Mason925baed2008-06-25 16:01:30 -04002398 t = path->nodes[i];
2399 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002400 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002401 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002402 if (write_lock_level &&
2403 i > min_write_lock_level &&
2404 i <= *write_lock_level) {
2405 *write_lock_level = i - 1;
2406 }
Chris Mason925baed2008-06-25 16:01:30 -04002407 }
2408 }
2409}
2410
Chris Mason3c69fae2007-08-07 15:52:22 -04002411/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002412 * This releases any locks held in the path starting at level and
2413 * going all the way up to the root.
2414 *
2415 * btrfs_search_slot will keep the lock held on higher nodes in a few
2416 * corner cases, such as COW of the block at slot zero in the node. This
2417 * ignores those rules, and it should only be called when there are no
2418 * more updates to be done higher up in the tree.
2419 */
2420noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2421{
2422 int i;
2423
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002424 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002425 return;
2426
2427 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2428 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002429 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002430 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002431 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002432 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002433 path->locks[i] = 0;
2434 }
2435}
2436
2437/*
Chris Masonc8c42862009-04-03 10:14:18 -04002438 * helper function for btrfs_search_slot. The goal is to find a block
2439 * in cache without setting the path to blocking. If we find the block
2440 * we return zero and the path is unchanged.
2441 *
2442 * If we can't find the block, we set the path blocking and do some
2443 * reada. -EAGAIN is returned and the search must be repeated.
2444 */
2445static int
2446read_block_for_search(struct btrfs_trans_handle *trans,
2447 struct btrfs_root *root, struct btrfs_path *p,
2448 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002449 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002450{
2451 u64 blocknr;
2452 u64 gen;
2453 u32 blocksize;
2454 struct extent_buffer *b = *eb_ret;
2455 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002456 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002457
2458 blocknr = btrfs_node_blockptr(b, slot);
2459 gen = btrfs_node_ptr_generation(b, slot);
2460 blocksize = btrfs_level_size(root, level - 1);
2461
2462 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002463 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002464 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002465 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2466 *eb_ret = tmp;
2467 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002468 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002469
2470 /* the pages were up to date, but we failed
2471 * the generation number check. Do a full
2472 * read for the generation number that is correct.
2473 * We must do this without dropping locks so
2474 * we can trust our generation number
2475 */
2476 btrfs_set_path_blocking(p);
2477
2478 /* now we're allowed to do a blocking uptodate check */
2479 ret = btrfs_read_buffer(tmp, gen);
2480 if (!ret) {
2481 *eb_ret = tmp;
2482 return 0;
2483 }
2484 free_extent_buffer(tmp);
2485 btrfs_release_path(p);
2486 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002487 }
2488
2489 /*
2490 * reduce lock contention at high levels
2491 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002492 * we read. Don't release the lock on the current
2493 * level because we need to walk this node to figure
2494 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002495 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002496 btrfs_unlock_up_safe(p, level + 1);
2497 btrfs_set_path_blocking(p);
2498
Chris Masoncb449212010-10-24 11:01:27 -04002499 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002500 if (p->reada)
2501 reada_for_search(root, p, level, slot, key->objectid);
2502
David Sterbab3b4aa72011-04-21 01:20:15 +02002503 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002504
2505 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002506 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002507 if (tmp) {
2508 /*
2509 * If the read above didn't mark this buffer up to date,
2510 * it will never end up being up to date. Set ret to EIO now
2511 * and give up so that our caller doesn't loop forever
2512 * on our EAGAINs.
2513 */
Chris Masonb9fab912012-05-06 07:23:47 -04002514 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002515 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002516 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002517 }
2518 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002519}
2520
2521/*
2522 * helper function for btrfs_search_slot. This does all of the checks
2523 * for node-level blocks and does any balancing required based on
2524 * the ins_len.
2525 *
2526 * If no extra work was required, zero is returned. If we had to
2527 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2528 * start over
2529 */
2530static int
2531setup_nodes_for_search(struct btrfs_trans_handle *trans,
2532 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002533 struct extent_buffer *b, int level, int ins_len,
2534 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002535{
2536 int ret;
2537 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2538 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2539 int sret;
2540
Chris Masonbd681512011-07-16 15:23:14 -04002541 if (*write_lock_level < level + 1) {
2542 *write_lock_level = level + 1;
2543 btrfs_release_path(p);
2544 goto again;
2545 }
2546
Chris Masonc8c42862009-04-03 10:14:18 -04002547 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002548 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002549 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002550 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002551
2552 BUG_ON(sret > 0);
2553 if (sret) {
2554 ret = sret;
2555 goto done;
2556 }
2557 b = p->nodes[level];
2558 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002559 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002560 int sret;
2561
Chris Masonbd681512011-07-16 15:23:14 -04002562 if (*write_lock_level < level + 1) {
2563 *write_lock_level = level + 1;
2564 btrfs_release_path(p);
2565 goto again;
2566 }
2567
Chris Masonc8c42862009-04-03 10:14:18 -04002568 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002569 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002570 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002571 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002572
2573 if (sret) {
2574 ret = sret;
2575 goto done;
2576 }
2577 b = p->nodes[level];
2578 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002579 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002580 goto again;
2581 }
2582 BUG_ON(btrfs_header_nritems(b) == 1);
2583 }
2584 return 0;
2585
2586again:
2587 ret = -EAGAIN;
2588done:
2589 return ret;
2590}
2591
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002592static void key_search_validate(struct extent_buffer *b,
2593 struct btrfs_key *key,
2594 int level)
2595{
2596#ifdef CONFIG_BTRFS_ASSERT
2597 struct btrfs_disk_key disk_key;
2598
2599 btrfs_cpu_key_to_disk(&disk_key, key);
2600
2601 if (level == 0)
2602 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2603 offsetof(struct btrfs_leaf, items[0].key),
2604 sizeof(disk_key)));
2605 else
2606 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2607 offsetof(struct btrfs_node, ptrs[0].key),
2608 sizeof(disk_key)));
2609#endif
2610}
2611
2612static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2613 int level, int *prev_cmp, int *slot)
2614{
2615 if (*prev_cmp != 0) {
2616 *prev_cmp = bin_search(b, key, level, slot);
2617 return *prev_cmp;
2618 }
2619
2620 key_search_validate(b, key, level);
2621 *slot = 0;
2622
2623 return 0;
2624}
2625
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002626int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002627 u64 iobjectid, u64 ioff, u8 key_type,
2628 struct btrfs_key *found_key)
2629{
2630 int ret;
2631 struct btrfs_key key;
2632 struct extent_buffer *eb;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002633 struct btrfs_path *path;
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002634
2635 key.type = key_type;
2636 key.objectid = iobjectid;
2637 key.offset = ioff;
2638
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002639 if (found_path == NULL) {
2640 path = btrfs_alloc_path();
2641 if (!path)
2642 return -ENOMEM;
2643 } else
2644 path = found_path;
2645
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002646 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002647 if ((ret < 0) || (found_key == NULL)) {
2648 if (path != found_path)
2649 btrfs_free_path(path);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002650 return ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002651 }
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002652
2653 eb = path->nodes[0];
2654 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2655 ret = btrfs_next_leaf(fs_root, path);
2656 if (ret)
2657 return ret;
2658 eb = path->nodes[0];
2659 }
2660
2661 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2662 if (found_key->type != key.type ||
2663 found_key->objectid != key.objectid)
2664 return 1;
2665
2666 return 0;
2667}
2668
Chris Masonc8c42862009-04-03 10:14:18 -04002669/*
Chris Mason74123bd2007-02-02 11:05:29 -05002670 * look for key in the tree. path is filled in with nodes along the way
2671 * if key is found, we return zero and you can find the item in the leaf
2672 * level of the path (level 0)
2673 *
2674 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002675 * be inserted, and 1 is returned. If there are other errors during the
2676 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002677 *
2678 * if ins_len > 0, nodes and leaves will be split as we walk down the
2679 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2680 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002681 */
Chris Masone089f052007-03-16 16:20:31 -04002682int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2683 *root, struct btrfs_key *key, struct btrfs_path *p, int
2684 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002685{
Chris Mason5f39d392007-10-15 16:14:19 -04002686 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002687 int slot;
2688 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002689 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002690 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002691 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002692 int root_lock;
2693 /* everything at write_lock_level or lower must be write locked */
2694 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002695 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002696 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002697 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002698
Chris Mason6702ed42007-08-07 16:15:09 -04002699 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002700 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002701 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002702 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002703
Chris Masonbd681512011-07-16 15:23:14 -04002704 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002705 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002706
Chris Masonbd681512011-07-16 15:23:14 -04002707 /* when we are removing items, we might have to go up to level
2708 * two as we update tree pointers Make sure we keep write
2709 * for those levels as well
2710 */
2711 write_lock_level = 2;
2712 } else if (ins_len > 0) {
2713 /*
2714 * for inserting items, make sure we have a write lock on
2715 * level 1 so we can update keys
2716 */
2717 write_lock_level = 1;
2718 }
2719
2720 if (!cow)
2721 write_lock_level = -1;
2722
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002723 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002724 write_lock_level = BTRFS_MAX_LEVEL;
2725
Chris Masonf7c79f32012-03-19 15:54:38 -04002726 min_write_lock_level = write_lock_level;
2727
Chris Masonbb803952007-03-01 12:04:21 -05002728again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002729 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002730 /*
2731 * we try very hard to do read locks on the root
2732 */
2733 root_lock = BTRFS_READ_LOCK;
2734 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002735 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002736 /*
2737 * the commit roots are read only
2738 * so we always do read locks
2739 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002740 if (p->need_commit_sem)
2741 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002742 b = root->commit_root;
2743 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002744 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002745 if (p->need_commit_sem)
2746 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002747 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002748 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002749 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002750 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002751 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002752 level = btrfs_header_level(b);
2753 } else {
2754 /* we don't know the level of the root node
2755 * until we actually have it read locked
2756 */
2757 b = btrfs_read_lock_root_node(root);
2758 level = btrfs_header_level(b);
2759 if (level <= write_lock_level) {
2760 /* whoops, must trade for write lock */
2761 btrfs_tree_read_unlock(b);
2762 free_extent_buffer(b);
2763 b = btrfs_lock_root_node(root);
2764 root_lock = BTRFS_WRITE_LOCK;
2765
2766 /* the level might have changed, check again */
2767 level = btrfs_header_level(b);
2768 }
2769 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002770 }
Chris Masonbd681512011-07-16 15:23:14 -04002771 p->nodes[level] = b;
2772 if (!p->skip_locking)
2773 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002774
Chris Masoneb60cea2007-02-02 09:18:22 -05002775 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002776 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002777
2778 /*
2779 * setup the path here so we can release it under lock
2780 * contention with the cow code
2781 */
Chris Mason02217ed2007-03-02 16:08:05 -05002782 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002783 /*
2784 * if we don't really need to cow this block
2785 * then we don't want to set the path blocking,
2786 * so we test it here
2787 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002788 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002789 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002790
Chris Masonb4ce94d2009-02-04 09:25:08 -05002791 btrfs_set_path_blocking(p);
2792
Chris Masonbd681512011-07-16 15:23:14 -04002793 /*
2794 * must have write locks on this node and the
2795 * parent
2796 */
Josef Bacik5124e002012-11-07 13:44:13 -05002797 if (level > write_lock_level ||
2798 (level + 1 > write_lock_level &&
2799 level + 1 < BTRFS_MAX_LEVEL &&
2800 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002801 write_lock_level = level + 1;
2802 btrfs_release_path(p);
2803 goto again;
2804 }
2805
Yan Zheng33c66f42009-07-22 09:59:00 -04002806 err = btrfs_cow_block(trans, root, b,
2807 p->nodes[level + 1],
2808 p->slots[level + 1], &b);
2809 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002810 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002811 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002812 }
Chris Mason02217ed2007-03-02 16:08:05 -05002813 }
Chris Mason65b51a02008-08-01 15:11:20 -04002814cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002815 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002816 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002817
2818 /*
2819 * we have a lock on b and as long as we aren't changing
2820 * the tree, there is no way to for the items in b to change.
2821 * It is safe to drop the lock on our parent before we
2822 * go through the expensive btree search on b.
2823 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002824 * If we're inserting or deleting (ins_len != 0), then we might
2825 * be changing slot zero, which may require changing the parent.
2826 * So, we can't drop the lock until after we know which slot
2827 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002828 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002829 if (!ins_len && !p->keep_locks) {
2830 int u = level + 1;
2831
2832 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2833 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2834 p->locks[u] = 0;
2835 }
2836 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002837
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002838 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002839
Chris Mason5f39d392007-10-15 16:14:19 -04002840 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002841 int dec = 0;
2842 if (ret && slot > 0) {
2843 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002844 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002845 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002846 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002847 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002848 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002849 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002850 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002851 if (err) {
2852 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002853 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002854 }
Chris Masonc8c42862009-04-03 10:14:18 -04002855 b = p->nodes[level];
2856 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002857
Chris Masonbd681512011-07-16 15:23:14 -04002858 /*
2859 * slot 0 is special, if we change the key
2860 * we have to update the parent pointer
2861 * which means we must have a write lock
2862 * on the parent
2863 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002864 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002865 write_lock_level < level + 1) {
2866 write_lock_level = level + 1;
2867 btrfs_release_path(p);
2868 goto again;
2869 }
2870
Chris Masonf7c79f32012-03-19 15:54:38 -04002871 unlock_up(p, level, lowest_unlock,
2872 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002873
Chris Mason925baed2008-06-25 16:01:30 -04002874 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002875 if (dec)
2876 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002877 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002878 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002879
Yan Zheng33c66f42009-07-22 09:59:00 -04002880 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002881 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002882 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002883 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002884 if (err) {
2885 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002886 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002887 }
Chris Mason76a05b32009-05-14 13:24:30 -04002888
Chris Masonb4ce94d2009-02-04 09:25:08 -05002889 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002890 level = btrfs_header_level(b);
2891 if (level <= write_lock_level) {
2892 err = btrfs_try_tree_write_lock(b);
2893 if (!err) {
2894 btrfs_set_path_blocking(p);
2895 btrfs_tree_lock(b);
2896 btrfs_clear_path_blocking(p, b,
2897 BTRFS_WRITE_LOCK);
2898 }
2899 p->locks[level] = BTRFS_WRITE_LOCK;
2900 } else {
2901 err = btrfs_try_tree_read_lock(b);
2902 if (!err) {
2903 btrfs_set_path_blocking(p);
2904 btrfs_tree_read_lock(b);
2905 btrfs_clear_path_blocking(p, b,
2906 BTRFS_READ_LOCK);
2907 }
2908 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002909 }
Chris Masonbd681512011-07-16 15:23:14 -04002910 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002911 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002912 } else {
2913 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002914 if (ins_len > 0 &&
2915 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002916 if (write_lock_level < 1) {
2917 write_lock_level = 1;
2918 btrfs_release_path(p);
2919 goto again;
2920 }
2921
Chris Masonb4ce94d2009-02-04 09:25:08 -05002922 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002923 err = split_leaf(trans, root, key,
2924 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002925 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002926
Yan Zheng33c66f42009-07-22 09:59:00 -04002927 BUG_ON(err > 0);
2928 if (err) {
2929 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002930 goto done;
2931 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002932 }
Chris Mason459931e2008-12-10 09:10:46 -05002933 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002934 unlock_up(p, level, lowest_unlock,
2935 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002936 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002937 }
2938 }
Chris Mason65b51a02008-08-01 15:11:20 -04002939 ret = 1;
2940done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002941 /*
2942 * we don't really know what they plan on doing with the path
2943 * from here on, so for now just mark it as blocking
2944 */
Chris Masonb9473432009-03-13 11:00:37 -04002945 if (!p->leave_spinning)
2946 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002947 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002948 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002949 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002950}
2951
Chris Mason74123bd2007-02-02 11:05:29 -05002952/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002953 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2954 * current state of the tree together with the operations recorded in the tree
2955 * modification log to search for the key in a previous version of this tree, as
2956 * denoted by the time_seq parameter.
2957 *
2958 * Naturally, there is no support for insert, delete or cow operations.
2959 *
2960 * The resulting path and return value will be set up as if we called
2961 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2962 */
2963int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2964 struct btrfs_path *p, u64 time_seq)
2965{
2966 struct extent_buffer *b;
2967 int slot;
2968 int ret;
2969 int err;
2970 int level;
2971 int lowest_unlock = 1;
2972 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002973 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002974
2975 lowest_level = p->lowest_level;
2976 WARN_ON(p->nodes[0] != NULL);
2977
2978 if (p->search_commit_root) {
2979 BUG_ON(time_seq);
2980 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2981 }
2982
2983again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002984 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002985 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002986 p->locks[level] = BTRFS_READ_LOCK;
2987
2988 while (b) {
2989 level = btrfs_header_level(b);
2990 p->nodes[level] = b;
2991 btrfs_clear_path_blocking(p, NULL, 0);
2992
2993 /*
2994 * we have a lock on b and as long as we aren't changing
2995 * the tree, there is no way to for the items in b to change.
2996 * It is safe to drop the lock on our parent before we
2997 * go through the expensive btree search on b.
2998 */
2999 btrfs_unlock_up_safe(p, level + 1);
3000
Josef Bacikd4b40872013-09-24 14:09:34 -04003001 /*
3002 * Since we can unwind eb's we want to do a real search every
3003 * time.
3004 */
3005 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01003006 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003007
3008 if (level != 0) {
3009 int dec = 0;
3010 if (ret && slot > 0) {
3011 dec = 1;
3012 slot -= 1;
3013 }
3014 p->slots[level] = slot;
3015 unlock_up(p, level, lowest_unlock, 0, NULL);
3016
3017 if (level == lowest_level) {
3018 if (dec)
3019 p->slots[level]++;
3020 goto done;
3021 }
3022
3023 err = read_block_for_search(NULL, root, p, &b, level,
3024 slot, key, time_seq);
3025 if (err == -EAGAIN)
3026 goto again;
3027 if (err) {
3028 ret = err;
3029 goto done;
3030 }
3031
3032 level = btrfs_header_level(b);
3033 err = btrfs_try_tree_read_lock(b);
3034 if (!err) {
3035 btrfs_set_path_blocking(p);
3036 btrfs_tree_read_lock(b);
3037 btrfs_clear_path_blocking(p, b,
3038 BTRFS_READ_LOCK);
3039 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003040 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003041 if (!b) {
3042 ret = -ENOMEM;
3043 goto done;
3044 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003045 p->locks[level] = BTRFS_READ_LOCK;
3046 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003047 } else {
3048 p->slots[level] = slot;
3049 unlock_up(p, level, lowest_unlock, 0, NULL);
3050 goto done;
3051 }
3052 }
3053 ret = 1;
3054done:
3055 if (!p->leave_spinning)
3056 btrfs_set_path_blocking(p);
3057 if (ret < 0)
3058 btrfs_release_path(p);
3059
3060 return ret;
3061}
3062
3063/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003064 * helper to use instead of search slot if no exact match is needed but
3065 * instead the next or previous item should be returned.
3066 * When find_higher is true, the next higher item is returned, the next lower
3067 * otherwise.
3068 * When return_any and find_higher are both true, and no higher item is found,
3069 * return the next lower instead.
3070 * When return_any is true and find_higher is false, and no lower item is found,
3071 * return the next higher instead.
3072 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3073 * < 0 on error
3074 */
3075int btrfs_search_slot_for_read(struct btrfs_root *root,
3076 struct btrfs_key *key, struct btrfs_path *p,
3077 int find_higher, int return_any)
3078{
3079 int ret;
3080 struct extent_buffer *leaf;
3081
3082again:
3083 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3084 if (ret <= 0)
3085 return ret;
3086 /*
3087 * a return value of 1 means the path is at the position where the
3088 * item should be inserted. Normally this is the next bigger item,
3089 * but in case the previous item is the last in a leaf, path points
3090 * to the first free slot in the previous leaf, i.e. at an invalid
3091 * item.
3092 */
3093 leaf = p->nodes[0];
3094
3095 if (find_higher) {
3096 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3097 ret = btrfs_next_leaf(root, p);
3098 if (ret <= 0)
3099 return ret;
3100 if (!return_any)
3101 return 1;
3102 /*
3103 * no higher item found, return the next
3104 * lower instead
3105 */
3106 return_any = 0;
3107 find_higher = 0;
3108 btrfs_release_path(p);
3109 goto again;
3110 }
3111 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003112 if (p->slots[0] == 0) {
3113 ret = btrfs_prev_leaf(root, p);
3114 if (ret < 0)
3115 return ret;
3116 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003117 leaf = p->nodes[0];
3118 if (p->slots[0] == btrfs_header_nritems(leaf))
3119 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003120 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003121 }
Arne Jansene6793762011-09-13 11:18:10 +02003122 if (!return_any)
3123 return 1;
3124 /*
3125 * no lower item found, return the next
3126 * higher instead
3127 */
3128 return_any = 0;
3129 find_higher = 1;
3130 btrfs_release_path(p);
3131 goto again;
3132 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003133 --p->slots[0];
3134 }
3135 }
3136 return 0;
3137}
3138
3139/*
Chris Mason74123bd2007-02-02 11:05:29 -05003140 * adjust the pointers going up the tree, starting at level
3141 * making sure the right key of each node is points to 'key'.
3142 * This is used after shifting pointers to the left, so it stops
3143 * fixing up pointers when a given leaf/node is not in slot 0 of the
3144 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003145 *
Chris Mason74123bd2007-02-02 11:05:29 -05003146 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003147static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003148 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003149{
3150 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003151 struct extent_buffer *t;
3152
Chris Mason234b63a2007-03-13 10:46:10 -04003153 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003154 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003155 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003156 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003157 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00003158 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003159 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003160 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003161 if (tslot != 0)
3162 break;
3163 }
3164}
3165
Chris Mason74123bd2007-02-02 11:05:29 -05003166/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003167 * update item key.
3168 *
3169 * This function isn't completely safe. It's the caller's responsibility
3170 * that the new key won't break the order
3171 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003172void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003173 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003174{
3175 struct btrfs_disk_key disk_key;
3176 struct extent_buffer *eb;
3177 int slot;
3178
3179 eb = path->nodes[0];
3180 slot = path->slots[0];
3181 if (slot > 0) {
3182 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003183 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003184 }
3185 if (slot < btrfs_header_nritems(eb) - 1) {
3186 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003187 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003188 }
3189
3190 btrfs_cpu_key_to_disk(&disk_key, new_key);
3191 btrfs_set_item_key(eb, &disk_key, slot);
3192 btrfs_mark_buffer_dirty(eb);
3193 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003194 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003195}
3196
3197/*
Chris Mason74123bd2007-02-02 11:05:29 -05003198 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003199 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003200 *
3201 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3202 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003203 */
Chris Mason98ed5172008-01-03 10:01:48 -05003204static int push_node_left(struct btrfs_trans_handle *trans,
3205 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003206 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003207{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003208 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003209 int src_nritems;
3210 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003211 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003212
Chris Mason5f39d392007-10-15 16:14:19 -04003213 src_nritems = btrfs_header_nritems(src);
3214 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003215 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003216 WARN_ON(btrfs_header_generation(src) != trans->transid);
3217 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003218
Chris Masonbce4eae2008-04-24 14:42:46 -04003219 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003220 return 1;
3221
Chris Masond3977122009-01-05 21:25:51 -05003222 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003223 return 1;
3224
Chris Masonbce4eae2008-04-24 14:42:46 -04003225 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003226 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003227 if (push_items < src_nritems) {
3228 /* leave at least 8 pointers in the node if
3229 * we aren't going to empty it
3230 */
3231 if (src_nritems - push_items < 8) {
3232 if (push_items <= 8)
3233 return 1;
3234 push_items -= 8;
3235 }
3236 }
3237 } else
3238 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003239
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003240 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3241 push_items);
3242 if (ret) {
3243 btrfs_abort_transaction(trans, root, ret);
3244 return ret;
3245 }
Chris Mason5f39d392007-10-15 16:14:19 -04003246 copy_extent_buffer(dst, src,
3247 btrfs_node_key_ptr_offset(dst_nritems),
3248 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003249 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003250
Chris Masonbb803952007-03-01 12:04:21 -05003251 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003252 /*
3253 * don't call tree_mod_log_eb_move here, key removal was already
3254 * fully logged by tree_mod_log_eb_copy above.
3255 */
Chris Mason5f39d392007-10-15 16:14:19 -04003256 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3257 btrfs_node_key_ptr_offset(push_items),
3258 (src_nritems - push_items) *
3259 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003260 }
Chris Mason5f39d392007-10-15 16:14:19 -04003261 btrfs_set_header_nritems(src, src_nritems - push_items);
3262 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3263 btrfs_mark_buffer_dirty(src);
3264 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003265
Chris Masonbb803952007-03-01 12:04:21 -05003266 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003267}
3268
Chris Mason97571fd2007-02-24 13:39:08 -05003269/*
Chris Mason79f95c82007-03-01 15:16:26 -05003270 * try to push data from one node into the next node right in the
3271 * tree.
3272 *
3273 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3274 * error, and > 0 if there was no room in the right hand block.
3275 *
3276 * this will only push up to 1/2 the contents of the left node over
3277 */
Chris Mason5f39d392007-10-15 16:14:19 -04003278static int balance_node_right(struct btrfs_trans_handle *trans,
3279 struct btrfs_root *root,
3280 struct extent_buffer *dst,
3281 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003282{
Chris Mason79f95c82007-03-01 15:16:26 -05003283 int push_items = 0;
3284 int max_push;
3285 int src_nritems;
3286 int dst_nritems;
3287 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003288
Chris Mason7bb86312007-12-11 09:25:06 -05003289 WARN_ON(btrfs_header_generation(src) != trans->transid);
3290 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3291
Chris Mason5f39d392007-10-15 16:14:19 -04003292 src_nritems = btrfs_header_nritems(src);
3293 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003294 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003295 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003296 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003297
Chris Masond3977122009-01-05 21:25:51 -05003298 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003299 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003300
3301 max_push = src_nritems / 2 + 1;
3302 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003303 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003304 return 1;
Yan252c38f2007-08-29 09:11:44 -04003305
Chris Mason79f95c82007-03-01 15:16:26 -05003306 if (max_push < push_items)
3307 push_items = max_push;
3308
Jan Schmidtf2304752012-05-26 11:43:17 +02003309 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003310 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3311 btrfs_node_key_ptr_offset(0),
3312 (dst_nritems) *
3313 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003314
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003315 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3316 src_nritems - push_items, push_items);
3317 if (ret) {
3318 btrfs_abort_transaction(trans, root, ret);
3319 return ret;
3320 }
Chris Mason5f39d392007-10-15 16:14:19 -04003321 copy_extent_buffer(dst, src,
3322 btrfs_node_key_ptr_offset(0),
3323 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003324 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003325
Chris Mason5f39d392007-10-15 16:14:19 -04003326 btrfs_set_header_nritems(src, src_nritems - push_items);
3327 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003328
Chris Mason5f39d392007-10-15 16:14:19 -04003329 btrfs_mark_buffer_dirty(src);
3330 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003331
Chris Mason79f95c82007-03-01 15:16:26 -05003332 return ret;
3333}
3334
3335/*
Chris Mason97571fd2007-02-24 13:39:08 -05003336 * helper function to insert a new root level in the tree.
3337 * A new node is allocated, and a single item is inserted to
3338 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003339 *
3340 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003341 */
Chris Masond3977122009-01-05 21:25:51 -05003342static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003343 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003344 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003345{
Chris Mason7bb86312007-12-11 09:25:06 -05003346 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003347 struct extent_buffer *lower;
3348 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003349 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003350 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003351
3352 BUG_ON(path->nodes[level]);
3353 BUG_ON(path->nodes[level-1] != root->node);
3354
Chris Mason7bb86312007-12-11 09:25:06 -05003355 lower = path->nodes[level-1];
3356 if (level == 1)
3357 btrfs_item_key(lower, &lower_key, 0);
3358 else
3359 btrfs_node_key(lower, &lower_key, 0);
3360
Zheng Yan31840ae2008-09-23 13:14:14 -04003361 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003363 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003364 if (IS_ERR(c))
3365 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003366
Yan, Zhengf0486c62010-05-16 10:46:25 -04003367 root_add_used(root, root->nodesize);
3368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003369 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003370 btrfs_set_header_nritems(c, 1);
3371 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003372 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003373 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003375 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003376
Ross Kirk0a4e5582013-09-24 10:12:38 +01003377 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003378 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003379
3380 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003381 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003382
Chris Mason5f39d392007-10-15 16:14:19 -04003383 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003384 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003385 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003386 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003387
3388 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003389
3390 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003391
Chris Mason925baed2008-06-25 16:01:30 -04003392 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003393 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003394 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003395
3396 /* the super has an extra ref to root->node */
3397 free_extent_buffer(old);
3398
Chris Mason0b86a832008-03-24 15:01:56 -04003399 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003400 extent_buffer_get(c);
3401 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003402 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003403 path->slots[level] = 0;
3404 return 0;
3405}
3406
Chris Mason74123bd2007-02-02 11:05:29 -05003407/*
3408 * worker function to insert a single pointer in a node.
3409 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003410 *
Chris Mason74123bd2007-02-02 11:05:29 -05003411 * slot and level indicate where you want the key to go, and
3412 * blocknr is the block the key points to.
3413 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003414static void insert_ptr(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *root, struct btrfs_path *path,
3416 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003417 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003418{
Chris Mason5f39d392007-10-15 16:14:19 -04003419 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003420 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003421 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003422
3423 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003424 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003425 lower = path->nodes[level];
3426 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003427 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003428 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003429 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003430 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003431 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3432 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003433 memmove_extent_buffer(lower,
3434 btrfs_node_key_ptr_offset(slot + 1),
3435 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003436 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003437 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003438 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003439 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003440 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003441 BUG_ON(ret < 0);
3442 }
Chris Mason5f39d392007-10-15 16:14:19 -04003443 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003444 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003445 WARN_ON(trans->transid == 0);
3446 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003447 btrfs_set_header_nritems(lower, nritems + 1);
3448 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003449}
3450
Chris Mason97571fd2007-02-24 13:39:08 -05003451/*
3452 * split the node at the specified level in path in two.
3453 * The path is corrected to point to the appropriate node after the split
3454 *
3455 * Before splitting this tries to make some room in the node by pushing
3456 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003457 *
3458 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003459 */
Chris Masone02119d2008-09-05 16:13:11 -04003460static noinline int split_node(struct btrfs_trans_handle *trans,
3461 struct btrfs_root *root,
3462 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003463{
Chris Mason5f39d392007-10-15 16:14:19 -04003464 struct extent_buffer *c;
3465 struct extent_buffer *split;
3466 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003467 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003468 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003469 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003470
Chris Mason5f39d392007-10-15 16:14:19 -04003471 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003472 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003473 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003474 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003475 * trying to split the root, lets make a new one
3476 *
Liu Bofdd99c72013-05-22 12:06:51 +00003477 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003478 * insert_new_root, because that root buffer will be kept as a
3479 * normal node. We are going to log removal of half of the
3480 * elements below with tree_mod_log_eb_copy. We're holding a
3481 * tree lock on the buffer, which is why we cannot race with
3482 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003483 */
Liu Bofdd99c72013-05-22 12:06:51 +00003484 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003485 if (ret)
3486 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003487 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003488 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003489 c = path->nodes[level];
3490 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003491 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003492 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003493 if (ret < 0)
3494 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003495 }
Chris Masone66f7092007-04-20 13:16:02 -04003496
Chris Mason5f39d392007-10-15 16:14:19 -04003497 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003498 mid = (c_nritems + 1) / 2;
3499 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003500
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003501 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003502 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003503 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003504 if (IS_ERR(split))
3505 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003506
Yan, Zhengf0486c62010-05-16 10:46:25 -04003507 root_add_used(root, root->nodesize);
3508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003509 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003510 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003511 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003512 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003513 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003514 btrfs_set_header_owner(split, root->root_key.objectid);
3515 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003516 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003517 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003518 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003519 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003520
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003521 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3522 mid, c_nritems - mid);
3523 if (ret) {
3524 btrfs_abort_transaction(trans, root, ret);
3525 return ret;
3526 }
Chris Mason5f39d392007-10-15 16:14:19 -04003527 copy_extent_buffer(split, c,
3528 btrfs_node_key_ptr_offset(0),
3529 btrfs_node_key_ptr_offset(mid),
3530 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3531 btrfs_set_header_nritems(split, c_nritems - mid);
3532 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003533 ret = 0;
3534
Chris Mason5f39d392007-10-15 16:14:19 -04003535 btrfs_mark_buffer_dirty(c);
3536 btrfs_mark_buffer_dirty(split);
3537
Jeff Mahoney143bede2012-03-01 14:56:26 +01003538 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003539 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003540
Chris Mason5de08d72007-02-24 06:24:44 -05003541 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003542 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003543 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003544 free_extent_buffer(c);
3545 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003546 path->slots[level + 1] += 1;
3547 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003548 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003549 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003550 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003551 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003552}
3553
Chris Mason74123bd2007-02-02 11:05:29 -05003554/*
3555 * how many bytes are required to store the items in a leaf. start
3556 * and nr indicate which items in the leaf to check. This totals up the
3557 * space used both by the item structs and the item data
3558 */
Chris Mason5f39d392007-10-15 16:14:19 -04003559static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003560{
Josef Bacik41be1f32012-10-15 13:43:18 -04003561 struct btrfs_item *start_item;
3562 struct btrfs_item *end_item;
3563 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003564 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003565 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003566 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567
3568 if (!nr)
3569 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003570 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003571 start_item = btrfs_item_nr(start);
3572 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003573 data_len = btrfs_token_item_offset(l, start_item, &token) +
3574 btrfs_token_item_size(l, start_item, &token);
3575 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003576 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003577 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003578 return data_len;
3579}
3580
Chris Mason74123bd2007-02-02 11:05:29 -05003581/*
Chris Masond4dbff92007-04-04 14:08:15 -04003582 * The space between the end of the leaf items and
3583 * the start of the leaf data. IOW, how much room
3584 * the leaf has left for both items and data
3585 */
Chris Masond3977122009-01-05 21:25:51 -05003586noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003587 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003588{
Chris Mason5f39d392007-10-15 16:14:19 -04003589 int nritems = btrfs_header_nritems(leaf);
3590 int ret;
3591 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3592 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003593 btrfs_crit(root->fs_info,
3594 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003595 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003596 leaf_space_used(leaf, 0, nritems), nritems);
3597 }
3598 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003599}
3600
Chris Mason99d8f832010-07-07 10:51:48 -04003601/*
3602 * min slot controls the lowest index we're willing to push to the
3603 * right. We'll push up to and including min_slot, but no lower
3604 */
Chris Mason44871b12009-03-13 10:04:31 -04003605static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3606 struct btrfs_root *root,
3607 struct btrfs_path *path,
3608 int data_size, int empty,
3609 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003610 int free_space, u32 left_nritems,
3611 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003612{
Chris Mason5f39d392007-10-15 16:14:19 -04003613 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003614 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003615 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003616 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003617 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003618 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003619 int push_space = 0;
3620 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003621 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003622 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003623 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003624 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003625 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003626
Chris Masoncfed81a2012-03-03 07:40:03 -05003627 btrfs_init_map_token(&token);
3628
Chris Mason34a38212007-11-07 13:31:03 -05003629 if (empty)
3630 nr = 0;
3631 else
Chris Mason99d8f832010-07-07 10:51:48 -04003632 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003633
Zheng Yan31840ae2008-09-23 13:14:14 -04003634 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003635 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003636
Chris Mason44871b12009-03-13 10:04:31 -04003637 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003638 i = left_nritems - 1;
3639 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003640 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003641
Zheng Yan31840ae2008-09-23 13:14:14 -04003642 if (!empty && push_items > 0) {
3643 if (path->slots[0] > i)
3644 break;
3645 if (path->slots[0] == i) {
3646 int space = btrfs_leaf_free_space(root, left);
3647 if (space + push_space * 2 > free_space)
3648 break;
3649 }
3650 }
3651
Chris Mason00ec4c52007-02-24 12:47:20 -05003652 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003653 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003654
Chris Masondb945352007-10-15 16:15:53 -04003655 this_item_size = btrfs_item_size(left, item);
3656 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003657 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003658
Chris Mason00ec4c52007-02-24 12:47:20 -05003659 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003660 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003661 if (i == 0)
3662 break;
3663 i--;
Chris Masondb945352007-10-15 16:15:53 -04003664 }
Chris Mason5f39d392007-10-15 16:14:19 -04003665
Chris Mason925baed2008-06-25 16:01:30 -04003666 if (push_items == 0)
3667 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003668
Julia Lawall6c1500f2012-11-03 20:30:18 +00003669 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003670
Chris Mason00ec4c52007-02-24 12:47:20 -05003671 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003672 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003673
Chris Mason5f39d392007-10-15 16:14:19 -04003674 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003675 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003676
Chris Mason00ec4c52007-02-24 12:47:20 -05003677 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003678 data_end = leaf_data_end(root, right);
3679 memmove_extent_buffer(right,
3680 btrfs_leaf_data(right) + data_end - push_space,
3681 btrfs_leaf_data(right) + data_end,
3682 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3683
Chris Mason00ec4c52007-02-24 12:47:20 -05003684 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003685 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003686 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3687 btrfs_leaf_data(left) + leaf_data_end(root, left),
3688 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003689
3690 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3691 btrfs_item_nr_offset(0),
3692 right_nritems * sizeof(struct btrfs_item));
3693
Chris Mason00ec4c52007-02-24 12:47:20 -05003694 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003695 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3696 btrfs_item_nr_offset(left_nritems - push_items),
3697 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003698
3699 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003700 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003701 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003702 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003703 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003704 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003705 push_space -= btrfs_token_item_size(right, item, &token);
3706 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003707 }
3708
Chris Mason7518a232007-03-12 12:01:18 -04003709 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003710 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003711
Chris Mason34a38212007-11-07 13:31:03 -05003712 if (left_nritems)
3713 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003714 else
3715 clean_tree_block(trans, root, left);
3716
Chris Mason5f39d392007-10-15 16:14:19 -04003717 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003718
Chris Mason5f39d392007-10-15 16:14:19 -04003719 btrfs_item_key(right, &disk_key, 0);
3720 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003721 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003722
Chris Mason00ec4c52007-02-24 12:47:20 -05003723 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003724 if (path->slots[0] >= left_nritems) {
3725 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003726 if (btrfs_header_nritems(path->nodes[0]) == 0)
3727 clean_tree_block(trans, root, path->nodes[0]);
3728 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003729 free_extent_buffer(path->nodes[0]);
3730 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003731 path->slots[1] += 1;
3732 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003733 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003734 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003735 }
3736 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003737
3738out_unlock:
3739 btrfs_tree_unlock(right);
3740 free_extent_buffer(right);
3741 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003742}
Chris Mason925baed2008-06-25 16:01:30 -04003743
Chris Mason00ec4c52007-02-24 12:47:20 -05003744/*
Chris Mason44871b12009-03-13 10:04:31 -04003745 * push some data in the path leaf to the right, trying to free up at
3746 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3747 *
3748 * returns 1 if the push failed because the other node didn't have enough
3749 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003750 *
3751 * this will push starting from min_slot to the end of the leaf. It won't
3752 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003753 */
3754static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003755 *root, struct btrfs_path *path,
3756 int min_data_size, int data_size,
3757 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003758{
3759 struct extent_buffer *left = path->nodes[0];
3760 struct extent_buffer *right;
3761 struct extent_buffer *upper;
3762 int slot;
3763 int free_space;
3764 u32 left_nritems;
3765 int ret;
3766
3767 if (!path->nodes[1])
3768 return 1;
3769
3770 slot = path->slots[1];
3771 upper = path->nodes[1];
3772 if (slot >= btrfs_header_nritems(upper) - 1)
3773 return 1;
3774
3775 btrfs_assert_tree_locked(path->nodes[1]);
3776
3777 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003778 if (right == NULL)
3779 return 1;
3780
Chris Mason44871b12009-03-13 10:04:31 -04003781 btrfs_tree_lock(right);
3782 btrfs_set_lock_blocking(right);
3783
3784 free_space = btrfs_leaf_free_space(root, right);
3785 if (free_space < data_size)
3786 goto out_unlock;
3787
3788 /* cow and double check */
3789 ret = btrfs_cow_block(trans, root, right, upper,
3790 slot + 1, &right);
3791 if (ret)
3792 goto out_unlock;
3793
3794 free_space = btrfs_leaf_free_space(root, right);
3795 if (free_space < data_size)
3796 goto out_unlock;
3797
3798 left_nritems = btrfs_header_nritems(left);
3799 if (left_nritems == 0)
3800 goto out_unlock;
3801
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003802 if (path->slots[0] == left_nritems && !empty) {
3803 /* Key greater than all keys in the leaf, right neighbor has
3804 * enough room for it and we're not emptying our leaf to delete
3805 * it, therefore use right neighbor to insert the new item and
3806 * no need to touch/dirty our left leaft. */
3807 btrfs_tree_unlock(left);
3808 free_extent_buffer(left);
3809 path->nodes[0] = right;
3810 path->slots[0] = 0;
3811 path->slots[1]++;
3812 return 0;
3813 }
3814
Chris Mason99d8f832010-07-07 10:51:48 -04003815 return __push_leaf_right(trans, root, path, min_data_size, empty,
3816 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003817out_unlock:
3818 btrfs_tree_unlock(right);
3819 free_extent_buffer(right);
3820 return 1;
3821}
3822
3823/*
Chris Mason74123bd2007-02-02 11:05:29 -05003824 * push some data in the path leaf to the left, trying to free up at
3825 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003826 *
3827 * max_slot can put a limit on how far into the leaf we'll push items. The
3828 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3829 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003830 */
Chris Mason44871b12009-03-13 10:04:31 -04003831static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3832 struct btrfs_root *root,
3833 struct btrfs_path *path, int data_size,
3834 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003835 int free_space, u32 right_nritems,
3836 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003837{
Chris Mason5f39d392007-10-15 16:14:19 -04003838 struct btrfs_disk_key disk_key;
3839 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003840 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003841 int push_space = 0;
3842 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003843 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003844 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003845 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003846 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003847 u32 this_item_size;
3848 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003849 struct btrfs_map_token token;
3850
3851 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003852
Chris Mason34a38212007-11-07 13:31:03 -05003853 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003854 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003855 else
Chris Mason99d8f832010-07-07 10:51:48 -04003856 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003857
3858 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003859 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003860
Zheng Yan31840ae2008-09-23 13:14:14 -04003861 if (!empty && push_items > 0) {
3862 if (path->slots[0] < i)
3863 break;
3864 if (path->slots[0] == i) {
3865 int space = btrfs_leaf_free_space(root, right);
3866 if (space + push_space * 2 > free_space)
3867 break;
3868 }
3869 }
3870
Chris Masonbe0e5c02007-01-26 15:51:26 -05003871 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003872 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003873
3874 this_item_size = btrfs_item_size(right, item);
3875 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003876 break;
Chris Masondb945352007-10-15 16:15:53 -04003877
Chris Masonbe0e5c02007-01-26 15:51:26 -05003878 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003879 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003880 }
Chris Masondb945352007-10-15 16:15:53 -04003881
Chris Masonbe0e5c02007-01-26 15:51:26 -05003882 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003883 ret = 1;
3884 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303886 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003887
Chris Masonbe0e5c02007-01-26 15:51:26 -05003888 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003889 copy_extent_buffer(left, right,
3890 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3891 btrfs_item_nr_offset(0),
3892 push_items * sizeof(struct btrfs_item));
3893
Chris Mason123abc82007-03-14 14:14:43 -04003894 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003895 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003896
3897 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003898 leaf_data_end(root, left) - push_space,
3899 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003900 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003901 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003902 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003903 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003904
Chris Masondb945352007-10-15 16:15:53 -04003905 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003906 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003907 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003908
Ross Kirkdd3cc162013-09-16 15:58:09 +01003909 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003910
Chris Masoncfed81a2012-03-03 07:40:03 -05003911 ioff = btrfs_token_item_offset(left, item, &token);
3912 btrfs_set_token_item_offset(left, item,
3913 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3914 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003915 }
Chris Mason5f39d392007-10-15 16:14:19 -04003916 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003917
3918 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003919 if (push_items > right_nritems)
3920 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003921 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003922
Chris Mason34a38212007-11-07 13:31:03 -05003923 if (push_items < right_nritems) {
3924 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3925 leaf_data_end(root, right);
3926 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3927 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3928 btrfs_leaf_data(right) +
3929 leaf_data_end(root, right), push_space);
3930
3931 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003932 btrfs_item_nr_offset(push_items),
3933 (btrfs_header_nritems(right) - push_items) *
3934 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003935 }
Yaneef1c492007-11-26 10:58:13 -05003936 right_nritems -= push_items;
3937 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003938 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003939 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003940 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003941
Chris Masoncfed81a2012-03-03 07:40:03 -05003942 push_space = push_space - btrfs_token_item_size(right,
3943 item, &token);
3944 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003945 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003946
Chris Mason5f39d392007-10-15 16:14:19 -04003947 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003948 if (right_nritems)
3949 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003950 else
3951 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003952
Chris Mason5f39d392007-10-15 16:14:19 -04003953 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003954 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003955
3956 /* then fixup the leaf pointer in the path */
3957 if (path->slots[0] < push_items) {
3958 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003959 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003960 free_extent_buffer(path->nodes[0]);
3961 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003962 path->slots[1] -= 1;
3963 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003964 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003965 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003966 path->slots[0] -= push_items;
3967 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003968 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003969 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003970out:
3971 btrfs_tree_unlock(left);
3972 free_extent_buffer(left);
3973 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003974}
3975
Chris Mason74123bd2007-02-02 11:05:29 -05003976/*
Chris Mason44871b12009-03-13 10:04:31 -04003977 * push some data in the path leaf to the left, trying to free up at
3978 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003979 *
3980 * max_slot can put a limit on how far into the leaf we'll push items. The
3981 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3982 * items
Chris Mason44871b12009-03-13 10:04:31 -04003983 */
3984static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003985 *root, struct btrfs_path *path, int min_data_size,
3986 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003987{
3988 struct extent_buffer *right = path->nodes[0];
3989 struct extent_buffer *left;
3990 int slot;
3991 int free_space;
3992 u32 right_nritems;
3993 int ret = 0;
3994
3995 slot = path->slots[1];
3996 if (slot == 0)
3997 return 1;
3998 if (!path->nodes[1])
3999 return 1;
4000
4001 right_nritems = btrfs_header_nritems(right);
4002 if (right_nritems == 0)
4003 return 1;
4004
4005 btrfs_assert_tree_locked(path->nodes[1]);
4006
4007 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004008 if (left == NULL)
4009 return 1;
4010
Chris Mason44871b12009-03-13 10:04:31 -04004011 btrfs_tree_lock(left);
4012 btrfs_set_lock_blocking(left);
4013
4014 free_space = btrfs_leaf_free_space(root, left);
4015 if (free_space < data_size) {
4016 ret = 1;
4017 goto out;
4018 }
4019
4020 /* cow and double check */
4021 ret = btrfs_cow_block(trans, root, left,
4022 path->nodes[1], slot - 1, &left);
4023 if (ret) {
4024 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004025 if (ret == -ENOSPC)
4026 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004027 goto out;
4028 }
4029
4030 free_space = btrfs_leaf_free_space(root, left);
4031 if (free_space < data_size) {
4032 ret = 1;
4033 goto out;
4034 }
4035
Chris Mason99d8f832010-07-07 10:51:48 -04004036 return __push_leaf_left(trans, root, path, min_data_size,
4037 empty, left, free_space, right_nritems,
4038 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004039out:
4040 btrfs_tree_unlock(left);
4041 free_extent_buffer(left);
4042 return ret;
4043}
4044
4045/*
Chris Mason74123bd2007-02-02 11:05:29 -05004046 * split the path's leaf in two, making sure there is at least data_size
4047 * available for the resulting leaf level of the path.
4048 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004049static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4050 struct btrfs_root *root,
4051 struct btrfs_path *path,
4052 struct extent_buffer *l,
4053 struct extent_buffer *right,
4054 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004055{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004056 int data_copy_size;
4057 int rt_data_off;
4058 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004059 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004060 struct btrfs_map_token token;
4061
4062 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004063
Chris Mason5f39d392007-10-15 16:14:19 -04004064 nritems = nritems - mid;
4065 btrfs_set_header_nritems(right, nritems);
4066 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4067
4068 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4069 btrfs_item_nr_offset(mid),
4070 nritems * sizeof(struct btrfs_item));
4071
4072 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004073 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4074 data_copy_size, btrfs_leaf_data(l) +
4075 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004076
Chris Mason5f39d392007-10-15 16:14:19 -04004077 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4078 btrfs_item_end_nr(l, mid);
4079
4080 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004081 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004082 u32 ioff;
4083
Chris Masoncfed81a2012-03-03 07:40:03 -05004084 ioff = btrfs_token_item_offset(right, item, &token);
4085 btrfs_set_token_item_offset(right, item,
4086 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004087 }
Chris Mason74123bd2007-02-02 11:05:29 -05004088
Chris Mason5f39d392007-10-15 16:14:19 -04004089 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004090 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004091 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004092 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004093
4094 btrfs_mark_buffer_dirty(right);
4095 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004096 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004097
Chris Masonbe0e5c02007-01-26 15:51:26 -05004098 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004099 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004100 free_extent_buffer(path->nodes[0]);
4101 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004102 path->slots[0] -= mid;
4103 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004104 } else {
4105 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004106 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004107 }
Chris Mason5f39d392007-10-15 16:14:19 -04004108
Chris Masoneb60cea2007-02-02 09:18:22 -05004109 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004110}
4111
4112/*
Chris Mason99d8f832010-07-07 10:51:48 -04004113 * double splits happen when we need to insert a big item in the middle
4114 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4115 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4116 * A B C
4117 *
4118 * We avoid this by trying to push the items on either side of our target
4119 * into the adjacent leaves. If all goes well we can avoid the double split
4120 * completely.
4121 */
4122static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4123 struct btrfs_root *root,
4124 struct btrfs_path *path,
4125 int data_size)
4126{
4127 int ret;
4128 int progress = 0;
4129 int slot;
4130 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004131 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004132
4133 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004134 if (slot < btrfs_header_nritems(path->nodes[0]))
4135 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004136
4137 /*
4138 * try to push all the items after our slot into the
4139 * right leaf
4140 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004141 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004142 if (ret < 0)
4143 return ret;
4144
4145 if (ret == 0)
4146 progress++;
4147
4148 nritems = btrfs_header_nritems(path->nodes[0]);
4149 /*
4150 * our goal is to get our slot at the start or end of a leaf. If
4151 * we've done so we're done
4152 */
4153 if (path->slots[0] == 0 || path->slots[0] == nritems)
4154 return 0;
4155
4156 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4157 return 0;
4158
4159 /* try to push all the items before our slot into the next leaf */
4160 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004161 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004162 if (ret < 0)
4163 return ret;
4164
4165 if (ret == 0)
4166 progress++;
4167
4168 if (progress)
4169 return 0;
4170 return 1;
4171}
4172
4173/*
Chris Mason44871b12009-03-13 10:04:31 -04004174 * split the path's leaf in two, making sure there is at least data_size
4175 * available for the resulting leaf level of the path.
4176 *
4177 * returns 0 if all went well and < 0 on failure.
4178 */
4179static noinline int split_leaf(struct btrfs_trans_handle *trans,
4180 struct btrfs_root *root,
4181 struct btrfs_key *ins_key,
4182 struct btrfs_path *path, int data_size,
4183 int extend)
4184{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004186 struct extent_buffer *l;
4187 u32 nritems;
4188 int mid;
4189 int slot;
4190 struct extent_buffer *right;
4191 int ret = 0;
4192 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004193 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004194 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004195 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004196
Yan, Zhenga5719522009-09-24 09:17:31 -04004197 l = path->nodes[0];
4198 slot = path->slots[0];
4199 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4200 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4201 return -EOVERFLOW;
4202
Chris Mason44871b12009-03-13 10:04:31 -04004203 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004204 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004205 int space_needed = data_size;
4206
4207 if (slot < btrfs_header_nritems(l))
4208 space_needed -= btrfs_leaf_free_space(root, l);
4209
4210 wret = push_leaf_right(trans, root, path, space_needed,
4211 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004212 if (wret < 0)
4213 return wret;
4214 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004215 wret = push_leaf_left(trans, root, path, space_needed,
4216 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004217 if (wret < 0)
4218 return wret;
4219 }
4220 l = path->nodes[0];
4221
4222 /* did the pushes work? */
4223 if (btrfs_leaf_free_space(root, l) >= data_size)
4224 return 0;
4225 }
4226
4227 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004228 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004229 if (ret)
4230 return ret;
4231 }
4232again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004233 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004234 l = path->nodes[0];
4235 slot = path->slots[0];
4236 nritems = btrfs_header_nritems(l);
4237 mid = (nritems + 1) / 2;
4238
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004239 if (mid <= slot) {
4240 if (nritems == 1 ||
4241 leaf_space_used(l, mid, nritems - mid) + data_size >
4242 BTRFS_LEAF_DATA_SIZE(root)) {
4243 if (slot >= nritems) {
4244 split = 0;
4245 } else {
4246 mid = slot;
4247 if (mid != nritems &&
4248 leaf_space_used(l, mid, nritems - mid) +
4249 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004250 if (data_size && !tried_avoid_double)
4251 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004252 split = 2;
4253 }
4254 }
4255 }
4256 } else {
4257 if (leaf_space_used(l, 0, mid) + data_size >
4258 BTRFS_LEAF_DATA_SIZE(root)) {
4259 if (!extend && data_size && slot == 0) {
4260 split = 0;
4261 } else if ((extend || !data_size) && slot == 0) {
4262 mid = 1;
4263 } else {
4264 mid = slot;
4265 if (mid != nritems &&
4266 leaf_space_used(l, mid, nritems - mid) +
4267 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004268 if (data_size && !tried_avoid_double)
4269 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304270 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004271 }
4272 }
4273 }
4274 }
4275
4276 if (split == 0)
4277 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4278 else
4279 btrfs_item_key(l, &disk_key, mid);
4280
4281 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004282 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004283 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004284 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004285 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004286
4287 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004288
4289 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4290 btrfs_set_header_bytenr(right, right->start);
4291 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004292 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004293 btrfs_set_header_owner(right, root->root_key.objectid);
4294 btrfs_set_header_level(right, 0);
4295 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004296 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004297
4298 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004299 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004300 BTRFS_UUID_SIZE);
4301
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004302 if (split == 0) {
4303 if (mid <= slot) {
4304 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004305 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004306 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004307 btrfs_tree_unlock(path->nodes[0]);
4308 free_extent_buffer(path->nodes[0]);
4309 path->nodes[0] = right;
4310 path->slots[0] = 0;
4311 path->slots[1] += 1;
4312 } else {
4313 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004314 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004315 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004316 btrfs_tree_unlock(path->nodes[0]);
4317 free_extent_buffer(path->nodes[0]);
4318 path->nodes[0] = right;
4319 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004320 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004321 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004322 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004323 btrfs_mark_buffer_dirty(right);
4324 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004325 }
4326
Jeff Mahoney143bede2012-03-01 14:56:26 +01004327 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004328
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004329 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004330 BUG_ON(num_doubles != 0);
4331 num_doubles++;
4332 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004333 }
Chris Mason44871b12009-03-13 10:04:31 -04004334
Jeff Mahoney143bede2012-03-01 14:56:26 +01004335 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004336
4337push_for_double:
4338 push_for_double_split(trans, root, path, data_size);
4339 tried_avoid_double = 1;
4340 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4341 return 0;
4342 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004343}
4344
Yan, Zhengad48fd752009-11-12 09:33:58 +00004345static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4346 struct btrfs_root *root,
4347 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004348{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004349 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004350 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004351 struct btrfs_file_extent_item *fi;
4352 u64 extent_len = 0;
4353 u32 item_size;
4354 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004355
4356 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004357 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4358
4359 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4360 key.type != BTRFS_EXTENT_CSUM_KEY);
4361
4362 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4363 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004364
4365 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004366 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4367 fi = btrfs_item_ptr(leaf, path->slots[0],
4368 struct btrfs_file_extent_item);
4369 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4370 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004371 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004372
Chris Mason459931e2008-12-10 09:10:46 -05004373 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004374 path->search_for_split = 1;
4375 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004376 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004377 if (ret < 0)
4378 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004379
Yan, Zhengad48fd752009-11-12 09:33:58 +00004380 ret = -EAGAIN;
4381 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004382 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004383 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4384 goto err;
4385
Chris Mason109f6ae2010-04-02 09:20:18 -04004386 /* the leaf has changed, it now has room. return now */
4387 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4388 goto err;
4389
Yan, Zhengad48fd752009-11-12 09:33:58 +00004390 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4391 fi = btrfs_item_ptr(leaf, path->slots[0],
4392 struct btrfs_file_extent_item);
4393 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4394 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004395 }
4396
Chris Masonb9473432009-03-13 11:00:37 -04004397 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004398 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004399 if (ret)
4400 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004401
Yan, Zhengad48fd752009-11-12 09:33:58 +00004402 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004403 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004404 return 0;
4405err:
4406 path->keep_locks = 0;
4407 return ret;
4408}
4409
4410static noinline int split_item(struct btrfs_trans_handle *trans,
4411 struct btrfs_root *root,
4412 struct btrfs_path *path,
4413 struct btrfs_key *new_key,
4414 unsigned long split_offset)
4415{
4416 struct extent_buffer *leaf;
4417 struct btrfs_item *item;
4418 struct btrfs_item *new_item;
4419 int slot;
4420 char *buf;
4421 u32 nritems;
4422 u32 item_size;
4423 u32 orig_offset;
4424 struct btrfs_disk_key disk_key;
4425
Chris Masonb9473432009-03-13 11:00:37 -04004426 leaf = path->nodes[0];
4427 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4428
Chris Masonb4ce94d2009-02-04 09:25:08 -05004429 btrfs_set_path_blocking(path);
4430
Ross Kirkdd3cc162013-09-16 15:58:09 +01004431 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004432 orig_offset = btrfs_item_offset(leaf, item);
4433 item_size = btrfs_item_size(leaf, item);
4434
Chris Mason459931e2008-12-10 09:10:46 -05004435 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004436 if (!buf)
4437 return -ENOMEM;
4438
Chris Mason459931e2008-12-10 09:10:46 -05004439 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4440 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004441
Chris Mason459931e2008-12-10 09:10:46 -05004442 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004443 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004444 if (slot != nritems) {
4445 /* shift the items */
4446 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004447 btrfs_item_nr_offset(slot),
4448 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004449 }
4450
4451 btrfs_cpu_key_to_disk(&disk_key, new_key);
4452 btrfs_set_item_key(leaf, &disk_key, slot);
4453
Ross Kirkdd3cc162013-09-16 15:58:09 +01004454 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004455
4456 btrfs_set_item_offset(leaf, new_item, orig_offset);
4457 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4458
4459 btrfs_set_item_offset(leaf, item,
4460 orig_offset + item_size - split_offset);
4461 btrfs_set_item_size(leaf, item, split_offset);
4462
4463 btrfs_set_header_nritems(leaf, nritems + 1);
4464
4465 /* write the data for the start of the original item */
4466 write_extent_buffer(leaf, buf,
4467 btrfs_item_ptr_offset(leaf, path->slots[0]),
4468 split_offset);
4469
4470 /* write the data for the new item */
4471 write_extent_buffer(leaf, buf + split_offset,
4472 btrfs_item_ptr_offset(leaf, slot),
4473 item_size - split_offset);
4474 btrfs_mark_buffer_dirty(leaf);
4475
Yan, Zhengad48fd752009-11-12 09:33:58 +00004476 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004477 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004478 return 0;
4479}
4480
4481/*
4482 * This function splits a single item into two items,
4483 * giving 'new_key' to the new item and splitting the
4484 * old one at split_offset (from the start of the item).
4485 *
4486 * The path may be released by this operation. After
4487 * the split, the path is pointing to the old item. The
4488 * new item is going to be in the same node as the old one.
4489 *
4490 * Note, the item being split must be smaller enough to live alone on
4491 * a tree block with room for one extra struct btrfs_item
4492 *
4493 * This allows us to split the item in place, keeping a lock on the
4494 * leaf the entire time.
4495 */
4496int btrfs_split_item(struct btrfs_trans_handle *trans,
4497 struct btrfs_root *root,
4498 struct btrfs_path *path,
4499 struct btrfs_key *new_key,
4500 unsigned long split_offset)
4501{
4502 int ret;
4503 ret = setup_leaf_for_split(trans, root, path,
4504 sizeof(struct btrfs_item));
4505 if (ret)
4506 return ret;
4507
4508 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004509 return ret;
4510}
4511
4512/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004513 * This function duplicate a item, giving 'new_key' to the new item.
4514 * It guarantees both items live in the same tree leaf and the new item
4515 * is contiguous with the original item.
4516 *
4517 * This allows us to split file extent in place, keeping a lock on the
4518 * leaf the entire time.
4519 */
4520int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4521 struct btrfs_root *root,
4522 struct btrfs_path *path,
4523 struct btrfs_key *new_key)
4524{
4525 struct extent_buffer *leaf;
4526 int ret;
4527 u32 item_size;
4528
4529 leaf = path->nodes[0];
4530 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4531 ret = setup_leaf_for_split(trans, root, path,
4532 item_size + sizeof(struct btrfs_item));
4533 if (ret)
4534 return ret;
4535
4536 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004537 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004538 item_size, item_size +
4539 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004540 leaf = path->nodes[0];
4541 memcpy_extent_buffer(leaf,
4542 btrfs_item_ptr_offset(leaf, path->slots[0]),
4543 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4544 item_size);
4545 return 0;
4546}
4547
4548/*
Chris Masond352ac62008-09-29 15:18:18 -04004549 * make the item pointed to by the path smaller. new_size indicates
4550 * how small to make it, and from_end tells us if we just chop bytes
4551 * off the end of the item or if we shift the item to chop bytes off
4552 * the front.
4553 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004554void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004555 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004556{
Chris Masonb18c6682007-04-17 13:26:50 -04004557 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004558 struct extent_buffer *leaf;
4559 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004560 u32 nritems;
4561 unsigned int data_end;
4562 unsigned int old_data_start;
4563 unsigned int old_size;
4564 unsigned int size_diff;
4565 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004566 struct btrfs_map_token token;
4567
4568 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004569
Chris Mason5f39d392007-10-15 16:14:19 -04004570 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004571 slot = path->slots[0];
4572
4573 old_size = btrfs_item_size_nr(leaf, slot);
4574 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004575 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004576
Chris Mason5f39d392007-10-15 16:14:19 -04004577 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004578 data_end = leaf_data_end(root, leaf);
4579
Chris Mason5f39d392007-10-15 16:14:19 -04004580 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004581
Chris Masonb18c6682007-04-17 13:26:50 -04004582 size_diff = old_size - new_size;
4583
4584 BUG_ON(slot < 0);
4585 BUG_ON(slot >= nritems);
4586
4587 /*
4588 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4589 */
4590 /* first correct the data pointers */
4591 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004592 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004593 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004594
Chris Masoncfed81a2012-03-03 07:40:03 -05004595 ioff = btrfs_token_item_offset(leaf, item, &token);
4596 btrfs_set_token_item_offset(leaf, item,
4597 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004598 }
Chris Masondb945352007-10-15 16:15:53 -04004599
Chris Masonb18c6682007-04-17 13:26:50 -04004600 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004601 if (from_end) {
4602 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4603 data_end + size_diff, btrfs_leaf_data(leaf) +
4604 data_end, old_data_start + new_size - data_end);
4605 } else {
4606 struct btrfs_disk_key disk_key;
4607 u64 offset;
4608
4609 btrfs_item_key(leaf, &disk_key, slot);
4610
4611 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4612 unsigned long ptr;
4613 struct btrfs_file_extent_item *fi;
4614
4615 fi = btrfs_item_ptr(leaf, slot,
4616 struct btrfs_file_extent_item);
4617 fi = (struct btrfs_file_extent_item *)(
4618 (unsigned long)fi - size_diff);
4619
4620 if (btrfs_file_extent_type(leaf, fi) ==
4621 BTRFS_FILE_EXTENT_INLINE) {
4622 ptr = btrfs_item_ptr_offset(leaf, slot);
4623 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004624 (unsigned long)fi,
4625 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004626 disk_bytenr));
4627 }
4628 }
4629
4630 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4631 data_end + size_diff, btrfs_leaf_data(leaf) +
4632 data_end, old_data_start - data_end);
4633
4634 offset = btrfs_disk_key_offset(&disk_key);
4635 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4636 btrfs_set_item_key(leaf, &disk_key, slot);
4637 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004638 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004639 }
Chris Mason5f39d392007-10-15 16:14:19 -04004640
Ross Kirkdd3cc162013-09-16 15:58:09 +01004641 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004642 btrfs_set_item_size(leaf, item, new_size);
4643 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004644
Chris Mason5f39d392007-10-15 16:14:19 -04004645 if (btrfs_leaf_free_space(root, leaf) < 0) {
4646 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004647 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004648 }
Chris Masonb18c6682007-04-17 13:26:50 -04004649}
4650
Chris Masond352ac62008-09-29 15:18:18 -04004651/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004652 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004653 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004654void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004655 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004656{
Chris Mason6567e832007-04-16 09:22:45 -04004657 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004658 struct extent_buffer *leaf;
4659 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004660 u32 nritems;
4661 unsigned int data_end;
4662 unsigned int old_data;
4663 unsigned int old_size;
4664 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004665 struct btrfs_map_token token;
4666
4667 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004668
Chris Mason5f39d392007-10-15 16:14:19 -04004669 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004670
Chris Mason5f39d392007-10-15 16:14:19 -04004671 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004672 data_end = leaf_data_end(root, leaf);
4673
Chris Mason5f39d392007-10-15 16:14:19 -04004674 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4675 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004676 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004677 }
Chris Mason6567e832007-04-16 09:22:45 -04004678 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004679 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004680
4681 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004682 if (slot >= nritems) {
4683 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004684 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004685 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004686 BUG_ON(1);
4687 }
Chris Mason6567e832007-04-16 09:22:45 -04004688
4689 /*
4690 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4691 */
4692 /* first correct the data pointers */
4693 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004694 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004695 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004696
Chris Masoncfed81a2012-03-03 07:40:03 -05004697 ioff = btrfs_token_item_offset(leaf, item, &token);
4698 btrfs_set_token_item_offset(leaf, item,
4699 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004700 }
Chris Mason5f39d392007-10-15 16:14:19 -04004701
Chris Mason6567e832007-04-16 09:22:45 -04004702 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004703 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004704 data_end - data_size, btrfs_leaf_data(leaf) +
4705 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004706
Chris Mason6567e832007-04-16 09:22:45 -04004707 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004708 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004709 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004710 btrfs_set_item_size(leaf, item, old_size + data_size);
4711 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004712
Chris Mason5f39d392007-10-15 16:14:19 -04004713 if (btrfs_leaf_free_space(root, leaf) < 0) {
4714 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004715 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004716 }
Chris Mason6567e832007-04-16 09:22:45 -04004717}
4718
Chris Mason74123bd2007-02-02 11:05:29 -05004719/*
Chris Mason44871b12009-03-13 10:04:31 -04004720 * this is a helper for btrfs_insert_empty_items, the main goal here is
4721 * to save stack depth by doing the bulk of the work in a function
4722 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004723 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004724void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004725 struct btrfs_key *cpu_key, u32 *data_size,
4726 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004727{
Chris Mason5f39d392007-10-15 16:14:19 -04004728 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004729 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004730 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004731 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004732 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004733 struct extent_buffer *leaf;
4734 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004735 struct btrfs_map_token token;
4736
4737 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004738
Chris Mason5f39d392007-10-15 16:14:19 -04004739 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004740 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004741
Chris Mason5f39d392007-10-15 16:14:19 -04004742 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004743 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004744
Chris Masonf25956c2008-09-12 15:32:53 -04004745 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004746 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004747 btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
Chris Mason9c583092008-01-29 15:15:18 -05004748 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004749 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004750 }
Chris Mason5f39d392007-10-15 16:14:19 -04004751
Chris Masonbe0e5c02007-01-26 15:51:26 -05004752 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004753 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004754
Chris Mason5f39d392007-10-15 16:14:19 -04004755 if (old_data < data_end) {
4756 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004757 btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
Chris Mason5f39d392007-10-15 16:14:19 -04004758 slot, old_data, data_end);
4759 BUG_ON(1);
4760 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004761 /*
4762 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4763 */
4764 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004765 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004766 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004767
Ross Kirkdd3cc162013-09-16 15:58:09 +01004768 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004769 ioff = btrfs_token_item_offset(leaf, item, &token);
4770 btrfs_set_token_item_offset(leaf, item,
4771 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004772 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004773 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004774 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004775 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004776 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004777
4778 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004779 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004780 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004781 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782 data_end = old_data;
4783 }
Chris Mason5f39d392007-10-15 16:14:19 -04004784
Chris Mason62e27492007-03-15 12:56:47 -04004785 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004786 for (i = 0; i < nr; i++) {
4787 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4788 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004789 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004790 btrfs_set_token_item_offset(leaf, item,
4791 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004792 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004793 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004794 }
Chris Mason44871b12009-03-13 10:04:31 -04004795
Chris Mason9c583092008-01-29 15:15:18 -05004796 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004797
Chris Mason5a01a2e2008-01-30 11:43:54 -05004798 if (slot == 0) {
4799 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004800 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004801 }
Chris Masonb9473432009-03-13 11:00:37 -04004802 btrfs_unlock_up_safe(path, 1);
4803 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004804
Chris Mason5f39d392007-10-15 16:14:19 -04004805 if (btrfs_leaf_free_space(root, leaf) < 0) {
4806 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004807 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004808 }
Chris Mason44871b12009-03-13 10:04:31 -04004809}
4810
4811/*
4812 * Given a key and some data, insert items into the tree.
4813 * This does all the path init required, making room in the tree if needed.
4814 */
4815int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4816 struct btrfs_root *root,
4817 struct btrfs_path *path,
4818 struct btrfs_key *cpu_key, u32 *data_size,
4819 int nr)
4820{
Chris Mason44871b12009-03-13 10:04:31 -04004821 int ret = 0;
4822 int slot;
4823 int i;
4824 u32 total_size = 0;
4825 u32 total_data = 0;
4826
4827 for (i = 0; i < nr; i++)
4828 total_data += data_size[i];
4829
4830 total_size = total_data + (nr * sizeof(struct btrfs_item));
4831 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4832 if (ret == 0)
4833 return -EEXIST;
4834 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004835 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004836
Chris Mason44871b12009-03-13 10:04:31 -04004837 slot = path->slots[0];
4838 BUG_ON(slot < 0);
4839
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004840 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004841 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004842 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004843}
4844
4845/*
4846 * Given a key and some data, insert an item into the tree.
4847 * This does all the path init required, making room in the tree if needed.
4848 */
Chris Masone089f052007-03-16 16:20:31 -04004849int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4850 *root, struct btrfs_key *cpu_key, void *data, u32
4851 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004852{
4853 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004854 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004855 struct extent_buffer *leaf;
4856 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004857
Chris Mason2c90e5d2007-04-02 10:50:19 -04004858 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004859 if (!path)
4860 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004861 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004862 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004863 leaf = path->nodes[0];
4864 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4865 write_extent_buffer(leaf, data, ptr, data_size);
4866 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004867 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004868 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004869 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004870}
4871
Chris Mason74123bd2007-02-02 11:05:29 -05004872/*
Chris Mason5de08d72007-02-24 06:24:44 -05004873 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004874 *
Chris Masond352ac62008-09-29 15:18:18 -04004875 * the tree should have been previously balanced so the deletion does not
4876 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004877 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004878static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4879 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004880{
Chris Mason5f39d392007-10-15 16:14:19 -04004881 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004882 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004883 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004884
Chris Mason5f39d392007-10-15 16:14:19 -04004885 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004886 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004887 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004888 tree_mod_log_eb_move(root->fs_info, parent, slot,
4889 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004890 memmove_extent_buffer(parent,
4891 btrfs_node_key_ptr_offset(slot),
4892 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004893 sizeof(struct btrfs_key_ptr) *
4894 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004895 } else if (level) {
4896 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004897 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004898 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004899 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004900
Chris Mason7518a232007-03-12 12:01:18 -04004901 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004902 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004903 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004904 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004905 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004906 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004907 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004908 struct btrfs_disk_key disk_key;
4909
4910 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004911 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004912 }
Chris Masond6025572007-03-30 14:27:56 -04004913 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004914}
4915
Chris Mason74123bd2007-02-02 11:05:29 -05004916/*
Chris Mason323ac952008-10-01 19:05:46 -04004917 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004918 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004919 *
4920 * This deletes the pointer in path->nodes[1] and frees the leaf
4921 * block extent. zero is returned if it all worked out, < 0 otherwise.
4922 *
4923 * The path must have already been setup for deleting the leaf, including
4924 * all the proper balancing. path->nodes[1] must be locked.
4925 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004926static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4927 struct btrfs_root *root,
4928 struct btrfs_path *path,
4929 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004930{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004931 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004932 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004933
Chris Mason4d081c42009-02-04 09:31:28 -05004934 /*
4935 * btrfs_free_extent is expensive, we want to make sure we
4936 * aren't holding any locks when we call it
4937 */
4938 btrfs_unlock_up_safe(path, 0);
4939
Yan, Zhengf0486c62010-05-16 10:46:25 -04004940 root_sub_used(root, leaf->len);
4941
Josef Bacik3083ee22012-03-09 16:01:49 -05004942 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004943 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004944 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004945}
4946/*
Chris Mason74123bd2007-02-02 11:05:29 -05004947 * delete the item at the leaf level in path. If that empties
4948 * the leaf, remove it from the tree
4949 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004950int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4951 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004952{
Chris Mason5f39d392007-10-15 16:14:19 -04004953 struct extent_buffer *leaf;
4954 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004955 int last_off;
4956 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004957 int ret = 0;
4958 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004959 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004960 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004961 struct btrfs_map_token token;
4962
4963 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004964
Chris Mason5f39d392007-10-15 16:14:19 -04004965 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004966 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4967
4968 for (i = 0; i < nr; i++)
4969 dsize += btrfs_item_size_nr(leaf, slot + i);
4970
Chris Mason5f39d392007-10-15 16:14:19 -04004971 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004972
Chris Mason85e21ba2008-01-29 15:11:36 -05004973 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004974 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004975
4976 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004977 data_end + dsize,
4978 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004979 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004980
Chris Mason85e21ba2008-01-29 15:11:36 -05004981 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004982 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004983
Ross Kirkdd3cc162013-09-16 15:58:09 +01004984 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004985 ioff = btrfs_token_item_offset(leaf, item, &token);
4986 btrfs_set_token_item_offset(leaf, item,
4987 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004988 }
Chris Masondb945352007-10-15 16:15:53 -04004989
Chris Mason5f39d392007-10-15 16:14:19 -04004990 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004991 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004992 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004993 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004994 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004995 btrfs_set_header_nritems(leaf, nritems - nr);
4996 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004997
Chris Mason74123bd2007-02-02 11:05:29 -05004998 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004999 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005000 if (leaf == root->node) {
5001 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005002 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005003 btrfs_set_path_blocking(path);
5004 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005005 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005006 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005007 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005008 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005009 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005010 struct btrfs_disk_key disk_key;
5011
5012 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00005013 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005014 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005015
Chris Mason74123bd2007-02-02 11:05:29 -05005016 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04005017 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005018 /* push_leaf_left fixes the path.
5019 * make sure the path still points to our leaf
5020 * for possible call to del_ptr below
5021 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005022 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005023 extent_buffer_get(leaf);
5024
Chris Masonb9473432009-03-13 11:00:37 -04005025 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005026 wret = push_leaf_left(trans, root, path, 1, 1,
5027 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005028 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005029 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005030
5031 if (path->nodes[0] == leaf &&
5032 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005033 wret = push_leaf_right(trans, root, path, 1,
5034 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005035 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005036 ret = wret;
5037 }
Chris Mason5f39d392007-10-15 16:14:19 -04005038
5039 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005040 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005041 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005042 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005043 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005044 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005045 /* if we're still in the path, make sure
5046 * we're dirty. Otherwise, one of the
5047 * push_leaf functions must have already
5048 * dirtied this buffer
5049 */
5050 if (path->nodes[0] == leaf)
5051 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005052 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005053 }
Chris Masond5719762007-03-23 10:01:08 -04005054 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005055 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005056 }
5057 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005058 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005059}
5060
Chris Mason97571fd2007-02-24 13:39:08 -05005061/*
Chris Mason925baed2008-06-25 16:01:30 -04005062 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005063 * returns 0 if it found something or 1 if there are no lesser leaves.
5064 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005065 *
5066 * This may release the path, and so you may lose any locks held at the
5067 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005068 */
Josef Bacik16e75492013-10-22 12:18:51 -04005069int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005070{
Chris Mason925baed2008-06-25 16:01:30 -04005071 struct btrfs_key key;
5072 struct btrfs_disk_key found_key;
5073 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005074
Chris Mason925baed2008-06-25 16:01:30 -04005075 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005076
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005077 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005078 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005079 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005080 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005081 key.offset = (u64)-1;
5082 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005083 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005084 key.type = (u8)-1;
5085 key.offset = (u64)-1;
5086 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005087 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005088 }
Chris Mason7bb86312007-12-11 09:25:06 -05005089
David Sterbab3b4aa72011-04-21 01:20:15 +02005090 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5092 if (ret < 0)
5093 return ret;
5094 btrfs_item_key(path->nodes[0], &found_key, 0);
5095 ret = comp_keys(&found_key, &key);
5096 if (ret < 0)
5097 return 0;
5098 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005099}
5100
Chris Mason3f157a22008-06-25 16:01:31 -04005101/*
5102 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005103 * for nodes or leaves that are have a minimum transaction id.
5104 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005105 *
5106 * This does not cow, but it does stuff the starting key it finds back
5107 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5108 * key and get a writable path.
5109 *
5110 * This does lock as it descends, and path->keep_locks should be set
5111 * to 1 by the caller.
5112 *
5113 * This honors path->lowest_level to prevent descent past a given level
5114 * of the tree.
5115 *
Chris Masond352ac62008-09-29 15:18:18 -04005116 * min_trans indicates the oldest transaction that you are interested
5117 * in walking through. Any nodes or leaves older than min_trans are
5118 * skipped over (without reading them).
5119 *
Chris Mason3f157a22008-06-25 16:01:31 -04005120 * returns zero if something useful was found, < 0 on error and 1 if there
5121 * was nothing in the tree that matched the search criteria.
5122 */
5123int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005124 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005125 u64 min_trans)
5126{
5127 struct extent_buffer *cur;
5128 struct btrfs_key found_key;
5129 int slot;
Yan96524802008-07-24 12:19:49 -04005130 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005131 u32 nritems;
5132 int level;
5133 int ret = 1;
5134
Chris Mason934d3752008-12-08 16:43:10 -05005135 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04005136again:
Chris Masonbd681512011-07-16 15:23:14 -04005137 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005138 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005139 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005140 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005141 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005142
5143 if (btrfs_header_generation(cur) < min_trans) {
5144 ret = 1;
5145 goto out;
5146 }
Chris Masond3977122009-01-05 21:25:51 -05005147 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005148 nritems = btrfs_header_nritems(cur);
5149 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005150 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005151
Chris Mason323ac952008-10-01 19:05:46 -04005152 /* at the lowest level, we're done, setup the path and exit */
5153 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005154 if (slot >= nritems)
5155 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005156 ret = 0;
5157 path->slots[level] = slot;
5158 btrfs_item_key_to_cpu(cur, &found_key, slot);
5159 goto out;
5160 }
Yan96524802008-07-24 12:19:49 -04005161 if (sret && slot > 0)
5162 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005163 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005164 * check this node pointer against the min_trans parameters.
5165 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005166 */
Chris Masond3977122009-01-05 21:25:51 -05005167 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005168 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005169
Chris Mason3f157a22008-06-25 16:01:31 -04005170 gen = btrfs_node_ptr_generation(cur, slot);
5171 if (gen < min_trans) {
5172 slot++;
5173 continue;
5174 }
Eric Sandeende78b512013-01-31 18:21:12 +00005175 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005176 }
Chris Masone02119d2008-09-05 16:13:11 -04005177find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005178 /*
5179 * we didn't find a candidate key in this node, walk forward
5180 * and find another one
5181 */
5182 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005183 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005184 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005185 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005186 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005187 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005188 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005189 goto again;
5190 } else {
5191 goto out;
5192 }
5193 }
5194 /* save our key for returning back */
5195 btrfs_node_key_to_cpu(cur, &found_key, slot);
5196 path->slots[level] = slot;
5197 if (level == path->lowest_level) {
5198 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04005199 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005200 goto out;
5201 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005202 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005203 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005204 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005205
Chris Masonbd681512011-07-16 15:23:14 -04005206 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005207
Chris Masonbd681512011-07-16 15:23:14 -04005208 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005209 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005210 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005211 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005212 }
5213out:
5214 if (ret == 0)
5215 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05005216 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005217 return ret;
5218}
5219
Alexander Block70698302012-06-05 21:07:48 +02005220static void tree_move_down(struct btrfs_root *root,
5221 struct btrfs_path *path,
5222 int *level, int root_level)
5223{
Chris Mason74dd17f2012-08-07 16:25:13 -04005224 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005225 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5226 path->slots[*level]);
5227 path->slots[*level - 1] = 0;
5228 (*level)--;
5229}
5230
5231static int tree_move_next_or_upnext(struct btrfs_root *root,
5232 struct btrfs_path *path,
5233 int *level, int root_level)
5234{
5235 int ret = 0;
5236 int nritems;
5237 nritems = btrfs_header_nritems(path->nodes[*level]);
5238
5239 path->slots[*level]++;
5240
Chris Mason74dd17f2012-08-07 16:25:13 -04005241 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005242 if (*level == root_level)
5243 return -1;
5244
5245 /* move upnext */
5246 path->slots[*level] = 0;
5247 free_extent_buffer(path->nodes[*level]);
5248 path->nodes[*level] = NULL;
5249 (*level)++;
5250 path->slots[*level]++;
5251
5252 nritems = btrfs_header_nritems(path->nodes[*level]);
5253 ret = 1;
5254 }
5255 return ret;
5256}
5257
5258/*
5259 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5260 * or down.
5261 */
5262static int tree_advance(struct btrfs_root *root,
5263 struct btrfs_path *path,
5264 int *level, int root_level,
5265 int allow_down,
5266 struct btrfs_key *key)
5267{
5268 int ret;
5269
5270 if (*level == 0 || !allow_down) {
5271 ret = tree_move_next_or_upnext(root, path, level, root_level);
5272 } else {
5273 tree_move_down(root, path, level, root_level);
5274 ret = 0;
5275 }
5276 if (ret >= 0) {
5277 if (*level == 0)
5278 btrfs_item_key_to_cpu(path->nodes[*level], key,
5279 path->slots[*level]);
5280 else
5281 btrfs_node_key_to_cpu(path->nodes[*level], key,
5282 path->slots[*level]);
5283 }
5284 return ret;
5285}
5286
5287static int tree_compare_item(struct btrfs_root *left_root,
5288 struct btrfs_path *left_path,
5289 struct btrfs_path *right_path,
5290 char *tmp_buf)
5291{
5292 int cmp;
5293 int len1, len2;
5294 unsigned long off1, off2;
5295
5296 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5297 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5298 if (len1 != len2)
5299 return 1;
5300
5301 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5302 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5303 right_path->slots[0]);
5304
5305 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5306
5307 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5308 if (cmp)
5309 return 1;
5310 return 0;
5311}
5312
5313#define ADVANCE 1
5314#define ADVANCE_ONLY_NEXT -1
5315
5316/*
5317 * This function compares two trees and calls the provided callback for
5318 * every changed/new/deleted item it finds.
5319 * If shared tree blocks are encountered, whole subtrees are skipped, making
5320 * the compare pretty fast on snapshotted subvolumes.
5321 *
5322 * This currently works on commit roots only. As commit roots are read only,
5323 * we don't do any locking. The commit roots are protected with transactions.
5324 * Transactions are ended and rejoined when a commit is tried in between.
5325 *
5326 * This function checks for modifications done to the trees while comparing.
5327 * If it detects a change, it aborts immediately.
5328 */
5329int btrfs_compare_trees(struct btrfs_root *left_root,
5330 struct btrfs_root *right_root,
5331 btrfs_changed_cb_t changed_cb, void *ctx)
5332{
5333 int ret;
5334 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005335 struct btrfs_path *left_path = NULL;
5336 struct btrfs_path *right_path = NULL;
5337 struct btrfs_key left_key;
5338 struct btrfs_key right_key;
5339 char *tmp_buf = NULL;
5340 int left_root_level;
5341 int right_root_level;
5342 int left_level;
5343 int right_level;
5344 int left_end_reached;
5345 int right_end_reached;
5346 int advance_left;
5347 int advance_right;
5348 u64 left_blockptr;
5349 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005350 u64 left_gen;
5351 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005352
5353 left_path = btrfs_alloc_path();
5354 if (!left_path) {
5355 ret = -ENOMEM;
5356 goto out;
5357 }
5358 right_path = btrfs_alloc_path();
5359 if (!right_path) {
5360 ret = -ENOMEM;
5361 goto out;
5362 }
5363
5364 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5365 if (!tmp_buf) {
5366 ret = -ENOMEM;
5367 goto out;
5368 }
5369
5370 left_path->search_commit_root = 1;
5371 left_path->skip_locking = 1;
5372 right_path->search_commit_root = 1;
5373 right_path->skip_locking = 1;
5374
Alexander Block70698302012-06-05 21:07:48 +02005375 /*
5376 * Strategy: Go to the first items of both trees. Then do
5377 *
5378 * If both trees are at level 0
5379 * Compare keys of current items
5380 * If left < right treat left item as new, advance left tree
5381 * and repeat
5382 * If left > right treat right item as deleted, advance right tree
5383 * and repeat
5384 * If left == right do deep compare of items, treat as changed if
5385 * needed, advance both trees and repeat
5386 * If both trees are at the same level but not at level 0
5387 * Compare keys of current nodes/leafs
5388 * If left < right advance left tree and repeat
5389 * If left > right advance right tree and repeat
5390 * If left == right compare blockptrs of the next nodes/leafs
5391 * If they match advance both trees but stay at the same level
5392 * and repeat
5393 * If they don't match advance both trees while allowing to go
5394 * deeper and repeat
5395 * If tree levels are different
5396 * Advance the tree that needs it and repeat
5397 *
5398 * Advancing a tree means:
5399 * If we are at level 0, try to go to the next slot. If that's not
5400 * possible, go one level up and repeat. Stop when we found a level
5401 * where we could go to the next slot. We may at this point be on a
5402 * node or a leaf.
5403 *
5404 * If we are not at level 0 and not on shared tree blocks, go one
5405 * level deeper.
5406 *
5407 * If we are not at level 0 and on shared tree blocks, go one slot to
5408 * the right if possible or go up and right.
5409 */
5410
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005411 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005412 left_level = btrfs_header_level(left_root->commit_root);
5413 left_root_level = left_level;
5414 left_path->nodes[left_level] = left_root->commit_root;
5415 extent_buffer_get(left_path->nodes[left_level]);
5416
5417 right_level = btrfs_header_level(right_root->commit_root);
5418 right_root_level = right_level;
5419 right_path->nodes[right_level] = right_root->commit_root;
5420 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005421 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005422
5423 if (left_level == 0)
5424 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5425 &left_key, left_path->slots[left_level]);
5426 else
5427 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5428 &left_key, left_path->slots[left_level]);
5429 if (right_level == 0)
5430 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5431 &right_key, right_path->slots[right_level]);
5432 else
5433 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5434 &right_key, right_path->slots[right_level]);
5435
5436 left_end_reached = right_end_reached = 0;
5437 advance_left = advance_right = 0;
5438
5439 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005440 if (advance_left && !left_end_reached) {
5441 ret = tree_advance(left_root, left_path, &left_level,
5442 left_root_level,
5443 advance_left != ADVANCE_ONLY_NEXT,
5444 &left_key);
5445 if (ret < 0)
5446 left_end_reached = ADVANCE;
5447 advance_left = 0;
5448 }
5449 if (advance_right && !right_end_reached) {
5450 ret = tree_advance(right_root, right_path, &right_level,
5451 right_root_level,
5452 advance_right != ADVANCE_ONLY_NEXT,
5453 &right_key);
5454 if (ret < 0)
5455 right_end_reached = ADVANCE;
5456 advance_right = 0;
5457 }
5458
5459 if (left_end_reached && right_end_reached) {
5460 ret = 0;
5461 goto out;
5462 } else if (left_end_reached) {
5463 if (right_level == 0) {
5464 ret = changed_cb(left_root, right_root,
5465 left_path, right_path,
5466 &right_key,
5467 BTRFS_COMPARE_TREE_DELETED,
5468 ctx);
5469 if (ret < 0)
5470 goto out;
5471 }
5472 advance_right = ADVANCE;
5473 continue;
5474 } else if (right_end_reached) {
5475 if (left_level == 0) {
5476 ret = changed_cb(left_root, right_root,
5477 left_path, right_path,
5478 &left_key,
5479 BTRFS_COMPARE_TREE_NEW,
5480 ctx);
5481 if (ret < 0)
5482 goto out;
5483 }
5484 advance_left = ADVANCE;
5485 continue;
5486 }
5487
5488 if (left_level == 0 && right_level == 0) {
5489 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5490 if (cmp < 0) {
5491 ret = changed_cb(left_root, right_root,
5492 left_path, right_path,
5493 &left_key,
5494 BTRFS_COMPARE_TREE_NEW,
5495 ctx);
5496 if (ret < 0)
5497 goto out;
5498 advance_left = ADVANCE;
5499 } else if (cmp > 0) {
5500 ret = changed_cb(left_root, right_root,
5501 left_path, right_path,
5502 &right_key,
5503 BTRFS_COMPARE_TREE_DELETED,
5504 ctx);
5505 if (ret < 0)
5506 goto out;
5507 advance_right = ADVANCE;
5508 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005509 enum btrfs_compare_tree_result cmp;
5510
Chris Mason74dd17f2012-08-07 16:25:13 -04005511 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005512 ret = tree_compare_item(left_root, left_path,
5513 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005514 if (ret)
5515 cmp = BTRFS_COMPARE_TREE_CHANGED;
5516 else
5517 cmp = BTRFS_COMPARE_TREE_SAME;
5518 ret = changed_cb(left_root, right_root,
5519 left_path, right_path,
5520 &left_key, cmp, ctx);
5521 if (ret < 0)
5522 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005523 advance_left = ADVANCE;
5524 advance_right = ADVANCE;
5525 }
5526 } else if (left_level == right_level) {
5527 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5528 if (cmp < 0) {
5529 advance_left = ADVANCE;
5530 } else if (cmp > 0) {
5531 advance_right = ADVANCE;
5532 } else {
5533 left_blockptr = btrfs_node_blockptr(
5534 left_path->nodes[left_level],
5535 left_path->slots[left_level]);
5536 right_blockptr = btrfs_node_blockptr(
5537 right_path->nodes[right_level],
5538 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005539 left_gen = btrfs_node_ptr_generation(
5540 left_path->nodes[left_level],
5541 left_path->slots[left_level]);
5542 right_gen = btrfs_node_ptr_generation(
5543 right_path->nodes[right_level],
5544 right_path->slots[right_level]);
5545 if (left_blockptr == right_blockptr &&
5546 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005547 /*
5548 * As we're on a shared block, don't
5549 * allow to go deeper.
5550 */
5551 advance_left = ADVANCE_ONLY_NEXT;
5552 advance_right = ADVANCE_ONLY_NEXT;
5553 } else {
5554 advance_left = ADVANCE;
5555 advance_right = ADVANCE;
5556 }
5557 }
5558 } else if (left_level < right_level) {
5559 advance_right = ADVANCE;
5560 } else {
5561 advance_left = ADVANCE;
5562 }
5563 }
5564
5565out:
5566 btrfs_free_path(left_path);
5567 btrfs_free_path(right_path);
5568 kfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005569 return ret;
5570}
5571
Chris Mason3f157a22008-06-25 16:01:31 -04005572/*
5573 * this is similar to btrfs_next_leaf, but does not try to preserve
5574 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005575 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005576 *
5577 * 0 is returned if another key is found, < 0 if there are any errors
5578 * and 1 is returned if there are no higher keys in the tree
5579 *
5580 * path->keep_locks should be set to 1 on the search made before
5581 * calling this function.
5582 */
Chris Masone7a84562008-06-25 16:01:31 -04005583int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005584 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005585{
Chris Masone7a84562008-06-25 16:01:31 -04005586 int slot;
5587 struct extent_buffer *c;
5588
Chris Mason934d3752008-12-08 16:43:10 -05005589 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005590 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005591 if (!path->nodes[level])
5592 return 1;
5593
5594 slot = path->slots[level] + 1;
5595 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005596next:
Chris Masone7a84562008-06-25 16:01:31 -04005597 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005598 int ret;
5599 int orig_lowest;
5600 struct btrfs_key cur_key;
5601 if (level + 1 >= BTRFS_MAX_LEVEL ||
5602 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005603 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005604
5605 if (path->locks[level + 1]) {
5606 level++;
5607 continue;
5608 }
5609
5610 slot = btrfs_header_nritems(c) - 1;
5611 if (level == 0)
5612 btrfs_item_key_to_cpu(c, &cur_key, slot);
5613 else
5614 btrfs_node_key_to_cpu(c, &cur_key, slot);
5615
5616 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005617 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005618 path->lowest_level = level;
5619 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5620 0, 0);
5621 path->lowest_level = orig_lowest;
5622 if (ret < 0)
5623 return ret;
5624
5625 c = path->nodes[level];
5626 slot = path->slots[level];
5627 if (ret == 0)
5628 slot++;
5629 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005630 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005631
Chris Masone7a84562008-06-25 16:01:31 -04005632 if (level == 0)
5633 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005634 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005635 u64 gen = btrfs_node_ptr_generation(c, slot);
5636
Chris Mason3f157a22008-06-25 16:01:31 -04005637 if (gen < min_trans) {
5638 slot++;
5639 goto next;
5640 }
Chris Masone7a84562008-06-25 16:01:31 -04005641 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005642 }
Chris Masone7a84562008-06-25 16:01:31 -04005643 return 0;
5644 }
5645 return 1;
5646}
5647
Chris Mason7bb86312007-12-11 09:25:06 -05005648/*
Chris Mason925baed2008-06-25 16:01:30 -04005649 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005650 * returns 0 if it found something or 1 if there are no greater leaves.
5651 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005652 */
Chris Mason234b63a2007-03-13 10:46:10 -04005653int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005654{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005655 return btrfs_next_old_leaf(root, path, 0);
5656}
5657
5658int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5659 u64 time_seq)
5660{
Chris Masond97e63b2007-02-20 16:40:44 -05005661 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005662 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005663 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005664 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005665 struct btrfs_key key;
5666 u32 nritems;
5667 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005668 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005669 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005670
5671 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005672 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005673 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005674
Chris Mason8e73f272009-04-03 10:14:18 -04005675 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5676again:
5677 level = 1;
5678 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005679 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005680 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005681
Chris Masona2135012008-06-25 16:01:30 -04005682 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005683 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005684
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005685 if (time_seq)
5686 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5687 else
5688 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005689 path->keep_locks = 0;
5690
5691 if (ret < 0)
5692 return ret;
5693
Chris Masona2135012008-06-25 16:01:30 -04005694 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005695 /*
5696 * by releasing the path above we dropped all our locks. A balance
5697 * could have added more items next to the key that used to be
5698 * at the very end of the block. So, check again here and
5699 * advance the path if there are now more items available.
5700 */
Chris Masona2135012008-06-25 16:01:30 -04005701 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005702 if (ret == 0)
5703 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005704 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005705 goto done;
5706 }
Chris Masond97e63b2007-02-20 16:40:44 -05005707
Chris Masond3977122009-01-05 21:25:51 -05005708 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005709 if (!path->nodes[level]) {
5710 ret = 1;
5711 goto done;
5712 }
Chris Mason5f39d392007-10-15 16:14:19 -04005713
Chris Masond97e63b2007-02-20 16:40:44 -05005714 slot = path->slots[level] + 1;
5715 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005716 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005717 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005718 if (level == BTRFS_MAX_LEVEL) {
5719 ret = 1;
5720 goto done;
5721 }
Chris Masond97e63b2007-02-20 16:40:44 -05005722 continue;
5723 }
Chris Mason5f39d392007-10-15 16:14:19 -04005724
Chris Mason925baed2008-06-25 16:01:30 -04005725 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005726 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005727 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005728 }
Chris Mason5f39d392007-10-15 16:14:19 -04005729
Chris Mason8e73f272009-04-03 10:14:18 -04005730 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005731 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005732 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005733 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005734 if (ret == -EAGAIN)
5735 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005736
Chris Mason76a05b32009-05-14 13:24:30 -04005737 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005738 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005739 goto done;
5740 }
5741
Chris Mason5cd57b22008-06-25 16:01:30 -04005742 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005743 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005744 if (!ret && time_seq) {
5745 /*
5746 * If we don't get the lock, we may be racing
5747 * with push_leaf_left, holding that lock while
5748 * itself waiting for the leaf we've currently
5749 * locked. To solve this situation, we give up
5750 * on our lock and cycle.
5751 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005752 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005753 btrfs_release_path(path);
5754 cond_resched();
5755 goto again;
5756 }
Chris Mason8e73f272009-04-03 10:14:18 -04005757 if (!ret) {
5758 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005759 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005760 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005761 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005762 }
Chris Mason31533fb2011-07-26 16:01:59 -04005763 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005764 }
Chris Masond97e63b2007-02-20 16:40:44 -05005765 break;
5766 }
5767 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005768 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005769 level--;
5770 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005771 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005772 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005773
Chris Mason5f39d392007-10-15 16:14:19 -04005774 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005775 path->nodes[level] = next;
5776 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005777 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005778 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005779 if (!level)
5780 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005781
Chris Mason8e73f272009-04-03 10:14:18 -04005782 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005783 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005784 if (ret == -EAGAIN)
5785 goto again;
5786
Chris Mason76a05b32009-05-14 13:24:30 -04005787 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005788 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005789 goto done;
5790 }
5791
Chris Mason5cd57b22008-06-25 16:01:30 -04005792 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005793 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005794 if (!ret) {
5795 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005796 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005797 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005798 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005799 }
Chris Mason31533fb2011-07-26 16:01:59 -04005800 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005801 }
Chris Masond97e63b2007-02-20 16:40:44 -05005802 }
Chris Mason8e73f272009-04-03 10:14:18 -04005803 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005804done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005805 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005806 path->leave_spinning = old_spinning;
5807 if (!old_spinning)
5808 btrfs_set_path_blocking(path);
5809
5810 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005811}
Chris Mason0b86a832008-03-24 15:01:56 -04005812
Chris Mason3f157a22008-06-25 16:01:31 -04005813/*
5814 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5815 * searching until it gets past min_objectid or finds an item of 'type'
5816 *
5817 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5818 */
Chris Mason0b86a832008-03-24 15:01:56 -04005819int btrfs_previous_item(struct btrfs_root *root,
5820 struct btrfs_path *path, u64 min_objectid,
5821 int type)
5822{
5823 struct btrfs_key found_key;
5824 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005825 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005826 int ret;
5827
Chris Masond3977122009-01-05 21:25:51 -05005828 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005829 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005830 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005831 ret = btrfs_prev_leaf(root, path);
5832 if (ret != 0)
5833 return ret;
5834 } else {
5835 path->slots[0]--;
5836 }
5837 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005838 nritems = btrfs_header_nritems(leaf);
5839 if (nritems == 0)
5840 return 1;
5841 if (path->slots[0] == nritems)
5842 path->slots[0]--;
5843
Chris Mason0b86a832008-03-24 15:01:56 -04005844 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005845 if (found_key.objectid < min_objectid)
5846 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005847 if (found_key.type == type)
5848 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005849 if (found_key.objectid == min_objectid &&
5850 found_key.type < type)
5851 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005852 }
5853 return 1;
5854}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005855
5856/*
5857 * search in extent tree to find a previous Metadata/Data extent item with
5858 * min objecitd.
5859 *
5860 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5861 */
5862int btrfs_previous_extent_item(struct btrfs_root *root,
5863 struct btrfs_path *path, u64 min_objectid)
5864{
5865 struct btrfs_key found_key;
5866 struct extent_buffer *leaf;
5867 u32 nritems;
5868 int ret;
5869
5870 while (1) {
5871 if (path->slots[0] == 0) {
5872 btrfs_set_path_blocking(path);
5873 ret = btrfs_prev_leaf(root, path);
5874 if (ret != 0)
5875 return ret;
5876 } else {
5877 path->slots[0]--;
5878 }
5879 leaf = path->nodes[0];
5880 nritems = btrfs_header_nritems(leaf);
5881 if (nritems == 0)
5882 return 1;
5883 if (path->slots[0] == nritems)
5884 path->slots[0]--;
5885
5886 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5887 if (found_key.objectid < min_objectid)
5888 break;
5889 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5890 found_key.type == BTRFS_METADATA_ITEM_KEY)
5891 return 0;
5892 if (found_key.objectid == min_objectid &&
5893 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5894 break;
5895 }
5896 return 1;
5897}