blob: 39c68ef10808fa96d25d9b3df2ffc273386c21b9 [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
Chris Masonbd681512011-07-16 15:23:14 -040083 if (held) {
84 btrfs_set_lock_blocking_rw(held, held_rw);
85 if (held_rw == BTRFS_WRITE_LOCK)
86 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
87 else if (held_rw == BTRFS_READ_LOCK)
88 held_rw = BTRFS_READ_LOCK_BLOCKING;
89 }
Chris Mason4008c042009-02-12 14:09:45 -050090 btrfs_set_path_blocking(p);
Chris Mason4008c042009-02-12 14:09:45 -050091
92 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040093 if (p->nodes[i] && p->locks[i]) {
94 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
95 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
96 p->locks[i] = BTRFS_WRITE_LOCK;
97 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
98 p->locks[i] = BTRFS_READ_LOCK;
99 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500100 }
Chris Mason4008c042009-02-12 14:09:45 -0500101
Chris Mason4008c042009-02-12 14:09:45 -0500102 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400103 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500104}
105
Chris Masond352ac62008-09-29 15:18:18 -0400106/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400107void btrfs_free_path(struct btrfs_path *p)
108{
Jesper Juhlff175d52010-12-25 21:22:30 +0000109 if (!p)
110 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200111 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400112 kmem_cache_free(btrfs_path_cachep, p);
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
116 * path release drops references on the extent buffers in the path
117 * and it drops any locks held by this path
118 *
119 * It is safe to call this on paths that no locks or extent buffers held.
120 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200121noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500122{
123 int i;
Chris Masona2135012008-06-25 16:01:30 -0400124
Chris Mason234b63a2007-03-13 10:46:10 -0400125 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400126 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500127 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400128 continue;
129 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400130 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400131 p->locks[i] = 0;
132 }
Chris Mason5f39d392007-10-15 16:14:19 -0400133 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400134 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500135 }
136}
137
Chris Masond352ac62008-09-29 15:18:18 -0400138/*
139 * safely gets a reference on the root node of a tree. A lock
140 * is not taken, so a concurrent writer may put a different node
141 * at the root of the tree. See btrfs_lock_root_node for the
142 * looping required.
143 *
144 * The extent buffer returned by this has a reference taken, so
145 * it won't disappear. It may stop being the root of the tree
146 * at any time because there are no locks held.
147 */
Chris Mason925baed2008-06-25 16:01:30 -0400148struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
149{
150 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400151
Josef Bacik3083ee22012-03-09 16:01:49 -0500152 while (1) {
153 rcu_read_lock();
154 eb = rcu_dereference(root->node);
155
156 /*
157 * RCU really hurts here, we could free up the root node because
158 * it was cow'ed but we may not get the new root node yet so do
159 * the inc_not_zero dance and if it doesn't work then
160 * synchronize_rcu and try again.
161 */
162 if (atomic_inc_not_zero(&eb->refs)) {
163 rcu_read_unlock();
164 break;
165 }
166 rcu_read_unlock();
167 synchronize_rcu();
168 }
Chris Mason925baed2008-06-25 16:01:30 -0400169 return eb;
170}
171
Chris Masond352ac62008-09-29 15:18:18 -0400172/* loop around taking references on and locking the root node of the
173 * tree until you end up with a lock on the root. A locked buffer
174 * is returned, with a reference held.
175 */
Chris Mason925baed2008-06-25 16:01:30 -0400176struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
177{
178 struct extent_buffer *eb;
179
Chris Masond3977122009-01-05 21:25:51 -0500180 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400181 eb = btrfs_root_node(root);
182 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400183 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400184 break;
Chris Mason925baed2008-06-25 16:01:30 -0400185 btrfs_tree_unlock(eb);
186 free_extent_buffer(eb);
187 }
188 return eb;
189}
190
Chris Masonbd681512011-07-16 15:23:14 -0400191/* loop around taking references on and locking the root node of the
192 * tree until you end up with a lock on the root. A locked buffer
193 * is returned, with a reference held.
194 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000195static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400196{
197 struct extent_buffer *eb;
198
199 while (1) {
200 eb = btrfs_root_node(root);
201 btrfs_tree_read_lock(eb);
202 if (eb == root->node)
203 break;
204 btrfs_tree_read_unlock(eb);
205 free_extent_buffer(eb);
206 }
207 return eb;
208}
209
Chris Masond352ac62008-09-29 15:18:18 -0400210/* cowonly root (everything not a reference counted cow subvolume), just get
211 * put onto a simple dirty list. transaction.c walks this to make sure they
212 * get properly updated on disk.
213 */
Chris Mason0b86a832008-03-24 15:01:56 -0400214static void add_root_to_dirty_list(struct btrfs_root *root)
215{
Chris Masone5846fc2012-05-03 12:08:48 -0400216 spin_lock(&root->fs_info->trans_lock);
Miao Xie27cdeb72014-04-02 19:51:05 +0800217 if (test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state) &&
218 list_empty(&root->dirty_list)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400219 list_add(&root->dirty_list,
220 &root->fs_info->dirty_cowonly_roots);
221 }
Chris Masone5846fc2012-05-03 12:08:48 -0400222 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400223}
224
Chris Masond352ac62008-09-29 15:18:18 -0400225/*
226 * used by snapshot creation to make a copy of a root for a tree with
227 * a given objectid. The buffer with the new root node is returned in
228 * cow_ret, and this func returns zero on success or a negative error code.
229 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500230int btrfs_copy_root(struct btrfs_trans_handle *trans,
231 struct btrfs_root *root,
232 struct extent_buffer *buf,
233 struct extent_buffer **cow_ret, u64 new_root_objectid)
234{
235 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 int ret = 0;
237 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400238 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500239
Miao Xie27cdeb72014-04-02 19:51:05 +0800240 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
241 trans->transid != root->fs_info->running_transaction->transid);
242 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
243 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500244
245 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400246 if (level == 0)
247 btrfs_item_key(buf, &disk_key, 0);
248 else
249 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400250
David Sterba4d75f8a2014-06-15 01:54:12 +0200251 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
252 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400253 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 return PTR_ERR(cow);
255
256 copy_extent_buffer(cow, buf, 0, 0, cow->len);
257 btrfs_set_header_bytenr(cow, cow->start);
258 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400259 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
260 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
261 BTRFS_HEADER_FLAG_RELOC);
262 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
263 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
264 else
265 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500266
Ross Kirk0a4e5582013-09-24 10:12:38 +0100267 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500268 BTRFS_FSID_SIZE);
269
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400271 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700272 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400273 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700274 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500275
Chris Masonbe20aa92007-12-17 20:14:01 -0500276 if (ret)
277 return ret;
278
279 btrfs_mark_buffer_dirty(cow);
280 *cow_ret = cow;
281 return 0;
282}
283
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200284enum mod_log_op {
285 MOD_LOG_KEY_REPLACE,
286 MOD_LOG_KEY_ADD,
287 MOD_LOG_KEY_REMOVE,
288 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
289 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
290 MOD_LOG_MOVE_KEYS,
291 MOD_LOG_ROOT_REPLACE,
292};
293
294struct tree_mod_move {
295 int dst_slot;
296 int nr_items;
297};
298
299struct tree_mod_root {
300 u64 logical;
301 u8 level;
302};
303
304struct tree_mod_elem {
305 struct rb_node node;
306 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200307 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200308 enum mod_log_op op;
309
310 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
311 int slot;
312
313 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
314 u64 generation;
315
316 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
317 struct btrfs_disk_key key;
318 u64 blockptr;
319
320 /* this is used for op == MOD_LOG_MOVE_KEYS */
321 struct tree_mod_move move;
322
323 /* this is used for op == MOD_LOG_ROOT_REPLACE */
324 struct tree_mod_root old_root;
325};
326
Jan Schmidt097b8a72012-06-21 11:08:04 +0200327static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200328{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200329 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200330}
331
Jan Schmidt097b8a72012-06-21 11:08:04 +0200332static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200333{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200334 read_unlock(&fs_info->tree_mod_log_lock);
335}
336
337static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
338{
339 write_lock(&fs_info->tree_mod_log_lock);
340}
341
342static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
343{
344 write_unlock(&fs_info->tree_mod_log_lock);
345}
346
347/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700348 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000349 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700350static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000351{
352 return atomic64_inc_return(&fs_info->tree_mod_seq);
353}
354
355/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200356 * This adds a new blocker to the tree mod log's blocker list if the @elem
357 * passed does not already have a sequence number set. So when a caller expects
358 * to record tree modifications, it should ensure to set elem->seq to zero
359 * before calling btrfs_get_tree_mod_seq.
360 * Returns a fresh, unused tree log modification sequence number, even if no new
361 * blocker was added.
362 */
363u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
364 struct seq_list *elem)
365{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200366 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200367 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200368 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700369 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200370 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
371 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200372 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200373 tree_mod_log_write_unlock(fs_info);
374
Josef Bacikfcebe452014-05-13 17:30:47 -0700375 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200376}
377
378void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
379 struct seq_list *elem)
380{
381 struct rb_root *tm_root;
382 struct rb_node *node;
383 struct rb_node *next;
384 struct seq_list *cur_elem;
385 struct tree_mod_elem *tm;
386 u64 min_seq = (u64)-1;
387 u64 seq_putting = elem->seq;
388
389 if (!seq_putting)
390 return;
391
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200392 spin_lock(&fs_info->tree_mod_seq_lock);
393 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200394 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200395
396 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200398 if (seq_putting > cur_elem->seq) {
399 /*
400 * blocker with lower sequence number exists, we
401 * cannot remove anything from the log
402 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200403 spin_unlock(&fs_info->tree_mod_seq_lock);
404 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200405 }
406 min_seq = cur_elem->seq;
407 }
408 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200409 spin_unlock(&fs_info->tree_mod_seq_lock);
410
411 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200412 * anything that's lower than the lowest existing (read: blocked)
413 * sequence number can be removed from the tree.
414 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200415 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 tm_root = &fs_info->tree_mod_log;
417 for (node = rb_first(tm_root); node; node = next) {
418 next = rb_next(node);
419 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200420 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200421 continue;
422 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200423 kfree(tm);
424 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200425 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200426}
427
428/*
429 * key order of the log:
430 * index -> sequence
431 *
432 * the index is the shifted logical of the *new* root node for root replace
433 * operations, or the shifted logical of the affected block for all other
434 * operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000435 *
436 * Note: must be called with write lock (tree_mod_log_write_lock).
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200437 */
438static noinline int
439__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
440{
441 struct rb_root *tm_root;
442 struct rb_node **new;
443 struct rb_node *parent = NULL;
444 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200445
Josef Bacikc8cc6342013-07-01 16:18:19 -0400446 BUG_ON(!tm);
447
Josef Bacikfcebe452014-05-13 17:30:47 -0700448 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200449
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200450 tm_root = &fs_info->tree_mod_log;
451 new = &tm_root->rb_node;
452 while (*new) {
453 cur = container_of(*new, struct tree_mod_elem, node);
454 parent = *new;
455 if (cur->index < tm->index)
456 new = &((*new)->rb_left);
457 else if (cur->index > tm->index)
458 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200459 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200461 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200462 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000463 else
464 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 }
466
467 rb_link_node(&tm->node, parent, new);
468 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000469 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200470}
471
Jan Schmidt097b8a72012-06-21 11:08:04 +0200472/*
473 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
474 * returns zero with the tree_mod_log_lock acquired. The caller must hold
475 * this until all tree mod log insertions are recorded in the rb tree and then
476 * call tree_mod_log_write_unlock() to release.
477 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200478static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
479 struct extent_buffer *eb) {
480 smp_mb();
481 if (list_empty(&(fs_info)->tree_mod_seq_list))
482 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200483 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200484 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000485
486 tree_mod_log_write_lock(fs_info);
487 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
488 tree_mod_log_write_unlock(fs_info);
489 return 1;
490 }
491
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200492 return 0;
493}
494
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000495/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
496static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
497 struct extent_buffer *eb)
498{
499 smp_mb();
500 if (list_empty(&(fs_info)->tree_mod_seq_list))
501 return 0;
502 if (eb && btrfs_header_level(eb) == 0)
503 return 0;
504
505 return 1;
506}
507
508static struct tree_mod_elem *
509alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
510 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200511{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200512 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200513
Josef Bacikc8cc6342013-07-01 16:18:19 -0400514 tm = kzalloc(sizeof(*tm), flags);
515 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000516 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200517
518 tm->index = eb->start >> PAGE_CACHE_SHIFT;
519 if (op != MOD_LOG_KEY_ADD) {
520 btrfs_node_key(eb, &tm->key, slot);
521 tm->blockptr = btrfs_node_blockptr(eb, slot);
522 }
523 tm->op = op;
524 tm->slot = slot;
525 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000526 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200527
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000528 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200529}
530
531static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400532tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
533 struct extent_buffer *eb, int slot,
534 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200535{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000536 struct tree_mod_elem *tm;
537 int ret;
538
539 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200540 return 0;
541
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000542 tm = alloc_tree_mod_elem(eb, slot, op, flags);
543 if (!tm)
544 return -ENOMEM;
545
546 if (tree_mod_dont_log(fs_info, eb)) {
547 kfree(tm);
548 return 0;
549 }
550
551 ret = __tree_mod_log_insert(fs_info, tm);
552 tree_mod_log_write_unlock(fs_info);
553 if (ret)
554 kfree(tm);
555
556 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200557}
558
559static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200560tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
561 struct extent_buffer *eb, int dst_slot, int src_slot,
562 int nr_items, gfp_t flags)
563{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000564 struct tree_mod_elem *tm = NULL;
565 struct tree_mod_elem **tm_list = NULL;
566 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200567 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000568 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200569
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000570 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200571 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200572
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000573 tm_list = kzalloc(nr_items * sizeof(struct tree_mod_elem *), flags);
574 if (!tm_list)
575 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200576
Josef Bacikc8cc6342013-07-01 16:18:19 -0400577 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000578 if (!tm) {
579 ret = -ENOMEM;
580 goto free_tms;
581 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200582
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200583 tm->index = eb->start >> PAGE_CACHE_SHIFT;
584 tm->slot = src_slot;
585 tm->move.dst_slot = dst_slot;
586 tm->move.nr_items = nr_items;
587 tm->op = MOD_LOG_MOVE_KEYS;
588
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000589 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
590 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
591 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
592 if (!tm_list[i]) {
593 ret = -ENOMEM;
594 goto free_tms;
595 }
596 }
597
598 if (tree_mod_dont_log(fs_info, eb))
599 goto free_tms;
600 locked = 1;
601
602 /*
603 * When we override something during the move, we log these removals.
604 * This can only happen when we move towards the beginning of the
605 * buffer, i.e. dst_slot < src_slot.
606 */
607 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
608 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
609 if (ret)
610 goto free_tms;
611 }
612
613 ret = __tree_mod_log_insert(fs_info, tm);
614 if (ret)
615 goto free_tms;
616 tree_mod_log_write_unlock(fs_info);
617 kfree(tm_list);
618
619 return 0;
620free_tms:
621 for (i = 0; i < nr_items; i++) {
622 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
623 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
624 kfree(tm_list[i]);
625 }
626 if (locked)
627 tree_mod_log_write_unlock(fs_info);
628 kfree(tm_list);
629 kfree(tm);
630
631 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200632}
633
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000634static inline int
635__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
636 struct tree_mod_elem **tm_list,
637 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200638{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000639 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200640 int ret;
641
Jan Schmidt097b8a72012-06-21 11:08:04 +0200642 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000643 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
644 if (ret) {
645 for (j = nritems - 1; j > i; j--)
646 rb_erase(&tm_list[j]->node,
647 &fs_info->tree_mod_log);
648 return ret;
649 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200650 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000651
652 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200653}
654
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200655static noinline int
656tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
657 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000658 struct extent_buffer *new_root, gfp_t flags,
659 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200660{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000661 struct tree_mod_elem *tm = NULL;
662 struct tree_mod_elem **tm_list = NULL;
663 int nritems = 0;
664 int ret = 0;
665 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200666
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000667 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200668 return 0;
669
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000670 if (log_removal && btrfs_header_level(old_root) > 0) {
671 nritems = btrfs_header_nritems(old_root);
672 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
673 flags);
674 if (!tm_list) {
675 ret = -ENOMEM;
676 goto free_tms;
677 }
678 for (i = 0; i < nritems; i++) {
679 tm_list[i] = alloc_tree_mod_elem(old_root, i,
680 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
681 if (!tm_list[i]) {
682 ret = -ENOMEM;
683 goto free_tms;
684 }
685 }
686 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000687
Josef Bacikc8cc6342013-07-01 16:18:19 -0400688 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000689 if (!tm) {
690 ret = -ENOMEM;
691 goto free_tms;
692 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200693
694 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
695 tm->old_root.logical = old_root->start;
696 tm->old_root.level = btrfs_header_level(old_root);
697 tm->generation = btrfs_header_generation(old_root);
698 tm->op = MOD_LOG_ROOT_REPLACE;
699
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000700 if (tree_mod_dont_log(fs_info, NULL))
701 goto free_tms;
702
703 if (tm_list)
704 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
705 if (!ret)
706 ret = __tree_mod_log_insert(fs_info, tm);
707
708 tree_mod_log_write_unlock(fs_info);
709 if (ret)
710 goto free_tms;
711 kfree(tm_list);
712
713 return ret;
714
715free_tms:
716 if (tm_list) {
717 for (i = 0; i < nritems; i++)
718 kfree(tm_list[i]);
719 kfree(tm_list);
720 }
721 kfree(tm);
722
723 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200724}
725
726static struct tree_mod_elem *
727__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
728 int smallest)
729{
730 struct rb_root *tm_root;
731 struct rb_node *node;
732 struct tree_mod_elem *cur = NULL;
733 struct tree_mod_elem *found = NULL;
734 u64 index = start >> PAGE_CACHE_SHIFT;
735
Jan Schmidt097b8a72012-06-21 11:08:04 +0200736 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200737 tm_root = &fs_info->tree_mod_log;
738 node = tm_root->rb_node;
739 while (node) {
740 cur = container_of(node, struct tree_mod_elem, node);
741 if (cur->index < index) {
742 node = node->rb_left;
743 } else if (cur->index > index) {
744 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200745 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200746 node = node->rb_left;
747 } else if (!smallest) {
748 /* we want the node with the highest seq */
749 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200750 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200751 found = cur;
752 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200753 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200754 /* we want the node with the smallest seq */
755 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200756 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200757 found = cur;
758 node = node->rb_right;
759 } else {
760 found = cur;
761 break;
762 }
763 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200764 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200765
766 return found;
767}
768
769/*
770 * this returns the element from the log with the smallest time sequence
771 * value that's in the log (the oldest log item). any element with a time
772 * sequence lower than min_seq will be ignored.
773 */
774static struct tree_mod_elem *
775tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
776 u64 min_seq)
777{
778 return __tree_mod_log_search(fs_info, start, min_seq, 1);
779}
780
781/*
782 * this returns the element from the log with the largest time sequence
783 * value that's in the log (the most recent log item). any element with
784 * a time sequence lower than min_seq will be ignored.
785 */
786static struct tree_mod_elem *
787tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
788{
789 return __tree_mod_log_search(fs_info, start, min_seq, 0);
790}
791
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000792static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200793tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
794 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000795 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200796{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000797 int ret = 0;
798 struct tree_mod_elem **tm_list = NULL;
799 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000801 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200802
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000803 if (!tree_mod_need_log(fs_info, NULL))
804 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200805
Josef Bacikc8cc6342013-07-01 16:18:19 -0400806 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000807 return 0;
808
809 tm_list = kzalloc(nr_items * 2 * sizeof(struct tree_mod_elem *),
810 GFP_NOFS);
811 if (!tm_list)
812 return -ENOMEM;
813
814 tm_list_add = tm_list;
815 tm_list_rem = tm_list + nr_items;
816 for (i = 0; i < nr_items; i++) {
817 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
818 MOD_LOG_KEY_REMOVE, GFP_NOFS);
819 if (!tm_list_rem[i]) {
820 ret = -ENOMEM;
821 goto free_tms;
822 }
823
824 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
825 MOD_LOG_KEY_ADD, GFP_NOFS);
826 if (!tm_list_add[i]) {
827 ret = -ENOMEM;
828 goto free_tms;
829 }
830 }
831
832 if (tree_mod_dont_log(fs_info, NULL))
833 goto free_tms;
834 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200835
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200836 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000837 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
838 if (ret)
839 goto free_tms;
840 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
841 if (ret)
842 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200843 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000844
845 tree_mod_log_write_unlock(fs_info);
846 kfree(tm_list);
847
848 return 0;
849
850free_tms:
851 for (i = 0; i < nr_items * 2; i++) {
852 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
853 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
854 kfree(tm_list[i]);
855 }
856 if (locked)
857 tree_mod_log_write_unlock(fs_info);
858 kfree(tm_list);
859
860 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200861}
862
863static inline void
864tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
865 int dst_offset, int src_offset, int nr_items)
866{
867 int ret;
868 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
869 nr_items, GFP_NOFS);
870 BUG_ON(ret < 0);
871}
872
Jan Schmidt097b8a72012-06-21 11:08:04 +0200873static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200874tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000875 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200876{
877 int ret;
878
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000879 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400880 MOD_LOG_KEY_REPLACE,
881 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200882 BUG_ON(ret < 0);
883}
884
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000885static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200886tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200887{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000888 struct tree_mod_elem **tm_list = NULL;
889 int nritems = 0;
890 int i;
891 int ret = 0;
892
893 if (btrfs_header_level(eb) == 0)
894 return 0;
895
896 if (!tree_mod_need_log(fs_info, NULL))
897 return 0;
898
899 nritems = btrfs_header_nritems(eb);
900 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
901 GFP_NOFS);
902 if (!tm_list)
903 return -ENOMEM;
904
905 for (i = 0; i < nritems; i++) {
906 tm_list[i] = alloc_tree_mod_elem(eb, i,
907 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
908 if (!tm_list[i]) {
909 ret = -ENOMEM;
910 goto free_tms;
911 }
912 }
913
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200914 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000915 goto free_tms;
916
917 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
918 tree_mod_log_write_unlock(fs_info);
919 if (ret)
920 goto free_tms;
921 kfree(tm_list);
922
923 return 0;
924
925free_tms:
926 for (i = 0; i < nritems; i++)
927 kfree(tm_list[i]);
928 kfree(tm_list);
929
930 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200931}
932
Jan Schmidt097b8a72012-06-21 11:08:04 +0200933static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200934tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000935 struct extent_buffer *new_root_node,
936 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200937{
938 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200939 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000940 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200941 BUG_ON(ret < 0);
942}
943
Chris Masond352ac62008-09-29 15:18:18 -0400944/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 * check if the tree block can be shared by multiple trees
946 */
947int btrfs_block_can_be_shared(struct btrfs_root *root,
948 struct extent_buffer *buf)
949{
950 /*
951 * Tree blocks not in refernece counted trees and tree roots
952 * are never shared. If a block was allocated after the last
953 * snapshot and the block was not allocated by tree relocation,
954 * we know the block is not shared.
955 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800956 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400957 buf != root->node && buf != root->commit_root &&
958 (btrfs_header_generation(buf) <=
959 btrfs_root_last_snapshot(&root->root_item) ||
960 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
961 return 1;
962#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800963 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
965 return 1;
966#endif
967 return 0;
968}
969
970static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
971 struct btrfs_root *root,
972 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400973 struct extent_buffer *cow,
974 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975{
976 u64 refs;
977 u64 owner;
978 u64 flags;
979 u64 new_flags = 0;
980 int ret;
981
982 /*
983 * Backrefs update rules:
984 *
985 * Always use full backrefs for extent pointers in tree block
986 * allocated by tree relocation.
987 *
988 * If a shared tree block is no longer referenced by its owner
989 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
990 * use full backrefs for extent pointers in tree block.
991 *
992 * If a tree block is been relocating
993 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
994 * use full backrefs for extent pointers in tree block.
995 * The reason for this is some operations (such as drop tree)
996 * are only allowed for blocks use full backrefs.
997 */
998
999 if (btrfs_block_can_be_shared(root, buf)) {
1000 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -05001001 btrfs_header_level(buf), 1,
1002 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001003 if (ret)
1004 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -07001005 if (refs == 0) {
1006 ret = -EROFS;
1007 btrfs_std_error(root->fs_info, ret);
1008 return ret;
1009 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 } else {
1011 refs = 1;
1012 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1013 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1014 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1015 else
1016 flags = 0;
1017 }
1018
1019 owner = btrfs_header_owner(buf);
1020 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1021 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1022
1023 if (refs > 1) {
1024 if ((owner == root->root_key.objectid ||
1025 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1026 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001027 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001028 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001029
1030 if (root->root_key.objectid ==
1031 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001032 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001033 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001034 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001035 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036 }
1037 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1038 } else {
1039
1040 if (root->root_key.objectid ==
1041 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001042 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001043 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001044 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001045 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 }
1047 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001048 int level = btrfs_header_level(buf);
1049
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050 ret = btrfs_set_disk_extent_flags(trans, root,
1051 buf->start,
1052 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001053 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001054 if (ret)
1055 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001056 }
1057 } else {
1058 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1059 if (root->root_key.objectid ==
1060 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001061 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001062 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001063 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001064 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001065 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001066 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001067 }
1068 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001069 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070 }
1071 return 0;
1072}
1073
1074/*
Chris Masond3977122009-01-05 21:25:51 -05001075 * does the dirty work in cow of a single block. The parent block (if
1076 * supplied) is updated to point to the new cow copy. The new buffer is marked
1077 * dirty and returned locked. If you modify the block it needs to be marked
1078 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001079 *
1080 * search_start -- an allocation hint for the new block
1081 *
Chris Masond3977122009-01-05 21:25:51 -05001082 * empty_size -- a hint that you plan on doing more cow. This is the size in
1083 * bytes the allocator should try to find free next to the block it returns.
1084 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001085 */
Chris Masond3977122009-01-05 21:25:51 -05001086static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001087 struct btrfs_root *root,
1088 struct extent_buffer *buf,
1089 struct extent_buffer *parent, int parent_slot,
1090 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001091 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001092{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001093 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001094 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001095 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001096 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001097 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001099
Chris Mason925baed2008-06-25 16:01:30 -04001100 if (*cow_ret == buf)
1101 unlock_orig = 1;
1102
Chris Masonb9447ef2009-03-09 11:45:38 -04001103 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001104
Miao Xie27cdeb72014-04-02 19:51:05 +08001105 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1106 trans->transid != root->fs_info->running_transaction->transid);
1107 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1108 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001109
Chris Mason7bb86312007-12-11 09:25:06 -05001110 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001111
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112 if (level == 0)
1113 btrfs_item_key(buf, &disk_key, 0);
1114 else
1115 btrfs_node_key(buf, &disk_key, 0);
1116
1117 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1118 if (parent)
1119 parent_start = parent->start;
1120 else
1121 parent_start = 0;
1122 } else
1123 parent_start = 0;
1124
David Sterba4d75f8a2014-06-15 01:54:12 +02001125 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1126 root->root_key.objectid, &disk_key, level,
1127 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001128 if (IS_ERR(cow))
1129 return PTR_ERR(cow);
1130
Chris Masonb4ce94d2009-02-04 09:25:08 -05001131 /* cow is set to blocking by btrfs_init_new_buffer */
1132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001134 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001135 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001136 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1137 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1138 BTRFS_HEADER_FLAG_RELOC);
1139 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1140 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1141 else
1142 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001143
Ross Kirk0a4e5582013-09-24 10:12:38 +01001144 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001145 BTRFS_FSID_SIZE);
1146
Mark Fashehbe1a5562011-08-08 13:20:18 -07001147 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001148 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001149 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001150 return ret;
1151 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001152
Miao Xie27cdeb72014-04-02 19:51:05 +08001153 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001154 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1155 if (ret)
1156 return ret;
1157 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001158
Chris Mason6702ed42007-08-07 16:15:09 -04001159 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001160 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001161 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1162 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1163 parent_start = buf->start;
1164 else
1165 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001166
Chris Mason5f39d392007-10-15 16:14:19 -04001167 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001168 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001169 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001170
Yan, Zhengf0486c62010-05-16 10:46:25 -04001171 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001172 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001173 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001174 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001175 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001176 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1177 parent_start = parent->start;
1178 else
1179 parent_start = 0;
1180
1181 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001182 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001183 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001185 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001186 btrfs_set_node_ptr_generation(parent, parent_slot,
1187 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001188 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001189 if (last_ref) {
1190 ret = tree_mod_log_free_eb(root->fs_info, buf);
1191 if (ret) {
1192 btrfs_abort_transaction(trans, root, ret);
1193 return ret;
1194 }
1195 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001196 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001197 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001198 }
Chris Mason925baed2008-06-25 16:01:30 -04001199 if (unlock_orig)
1200 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001201 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001202 btrfs_mark_buffer_dirty(cow);
1203 *cow_ret = cow;
1204 return 0;
1205}
1206
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001207/*
1208 * returns the logical address of the oldest predecessor of the given root.
1209 * entries older than time_seq are ignored.
1210 */
1211static struct tree_mod_elem *
1212__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001213 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001214{
1215 struct tree_mod_elem *tm;
1216 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001217 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001218 int looped = 0;
1219
1220 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001221 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001222
1223 /*
1224 * the very last operation that's logged for a root is the replacement
1225 * operation (if it is replaced at all). this has the index of the *new*
1226 * root, making it the very first operation that's logged for this root.
1227 */
1228 while (1) {
1229 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1230 time_seq);
1231 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001232 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001233 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001234 * if there are no tree operation for the oldest root, we simply
1235 * return it. this should only happen if that (old) root is at
1236 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001237 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001238 if (!tm)
1239 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001240
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001241 /*
1242 * if there's an operation that's not a root replacement, we
1243 * found the oldest version of our root. normally, we'll find a
1244 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1245 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001246 if (tm->op != MOD_LOG_ROOT_REPLACE)
1247 break;
1248
1249 found = tm;
1250 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001251 looped = 1;
1252 }
1253
Jan Schmidta95236d2012-06-05 16:41:24 +02001254 /* if there's no old root to return, return what we found instead */
1255 if (!found)
1256 found = tm;
1257
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001258 return found;
1259}
1260
1261/*
1262 * tm is a pointer to the first operation to rewind within eb. then, all
1263 * previous operations will be rewinded (until we reach something older than
1264 * time_seq).
1265 */
1266static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001267__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1268 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001269{
1270 u32 n;
1271 struct rb_node *next;
1272 struct tree_mod_elem *tm = first_tm;
1273 unsigned long o_dst;
1274 unsigned long o_src;
1275 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1276
1277 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001278 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001279 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280 /*
1281 * all the operations are recorded with the operator used for
1282 * the modification. as we're going backwards, we do the
1283 * opposite of each operation here.
1284 */
1285 switch (tm->op) {
1286 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1287 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001288 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001289 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001290 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001291 btrfs_set_node_key(eb, &tm->key, tm->slot);
1292 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1293 btrfs_set_node_ptr_generation(eb, tm->slot,
1294 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001295 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001296 break;
1297 case MOD_LOG_KEY_REPLACE:
1298 BUG_ON(tm->slot >= n);
1299 btrfs_set_node_key(eb, &tm->key, tm->slot);
1300 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1301 btrfs_set_node_ptr_generation(eb, tm->slot,
1302 tm->generation);
1303 break;
1304 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001305 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001306 n--;
1307 break;
1308 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001309 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1310 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1311 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001312 tm->move.nr_items * p_size);
1313 break;
1314 case MOD_LOG_ROOT_REPLACE:
1315 /*
1316 * this operation is special. for roots, this must be
1317 * handled explicitly before rewinding.
1318 * for non-roots, this operation may exist if the node
1319 * was a root: root A -> child B; then A gets empty and
1320 * B is promoted to the new root. in the mod log, we'll
1321 * have a root-replace operation for B, a tree block
1322 * that is no root. we simply ignore that operation.
1323 */
1324 break;
1325 }
1326 next = rb_next(&tm->node);
1327 if (!next)
1328 break;
1329 tm = container_of(next, struct tree_mod_elem, node);
1330 if (tm->index != first_tm->index)
1331 break;
1332 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001333 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001334 btrfs_set_header_nritems(eb, n);
1335}
1336
Jan Schmidt47fb0912013-04-13 13:19:55 +00001337/*
1338 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1339 * is returned. If rewind operations happen, a fresh buffer is returned. The
1340 * returned buffer is always read-locked. If the returned buffer is not the
1341 * input buffer, the lock on the input buffer is released and the input buffer
1342 * is freed (its refcount is decremented).
1343 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001344static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001345tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1346 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001347{
1348 struct extent_buffer *eb_rewin;
1349 struct tree_mod_elem *tm;
1350
1351 if (!time_seq)
1352 return eb;
1353
1354 if (btrfs_header_level(eb) == 0)
1355 return eb;
1356
1357 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1358 if (!tm)
1359 return eb;
1360
Josef Bacik9ec72672013-08-07 16:57:23 -04001361 btrfs_set_path_blocking(path);
1362 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1363
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001364 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1365 BUG_ON(tm->slot != 0);
1366 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1367 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001368 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001369 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001370 free_extent_buffer(eb);
1371 return NULL;
1372 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001373 btrfs_set_header_bytenr(eb_rewin, eb->start);
1374 btrfs_set_header_backref_rev(eb_rewin,
1375 btrfs_header_backref_rev(eb));
1376 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001377 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001378 } else {
1379 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001380 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001381 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001382 free_extent_buffer(eb);
1383 return NULL;
1384 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001385 }
1386
Josef Bacik9ec72672013-08-07 16:57:23 -04001387 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1388 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389 free_extent_buffer(eb);
1390
Jan Schmidt47fb0912013-04-13 13:19:55 +00001391 extent_buffer_get(eb_rewin);
1392 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001393 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001394 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001395 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001396
1397 return eb_rewin;
1398}
1399
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001400/*
1401 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1402 * value. If there are no changes, the current root->root_node is returned. If
1403 * anything changed in between, there's a fresh buffer allocated on which the
1404 * rewind operations are done. In any case, the returned buffer is read locked.
1405 * Returns NULL on error (with no locks held).
1406 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001407static inline struct extent_buffer *
1408get_old_root(struct btrfs_root *root, u64 time_seq)
1409{
1410 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001411 struct extent_buffer *eb = NULL;
1412 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001413 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001414 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001415 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001416 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001417
Jan Schmidt30b04632013-04-13 13:19:54 +00001418 eb_root = btrfs_read_lock_root_node(root);
1419 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001420 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001421 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001422
Jan Schmidta95236d2012-06-05 16:41:24 +02001423 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1424 old_root = &tm->old_root;
1425 old_generation = tm->generation;
1426 logical = old_root->logical;
1427 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001428 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001429 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001430
Jan Schmidta95236d2012-06-05 16:41:24 +02001431 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001432 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001433 btrfs_tree_read_unlock(eb_root);
1434 free_extent_buffer(eb_root);
David Sterbace86cd52014-06-15 01:07:32 +02001435 old = read_tree_block(root, logical, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301436 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001437 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001438 btrfs_warn(root->fs_info,
1439 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001440 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001441 eb = btrfs_clone_extent_buffer(old);
1442 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001443 }
1444 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001445 btrfs_tree_read_unlock(eb_root);
1446 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001447 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001448 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001449 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001450 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001451 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001452 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001453 }
1454
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001455 if (!eb)
1456 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001457 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001458 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001459 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001460 btrfs_set_header_bytenr(eb, eb->start);
1461 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001462 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001463 btrfs_set_header_level(eb, old_root->level);
1464 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001465 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001466 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001467 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001468 else
1469 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001470 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001471
1472 return eb;
1473}
1474
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001475int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1476{
1477 struct tree_mod_elem *tm;
1478 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001479 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001480
Jan Schmidt30b04632013-04-13 13:19:54 +00001481 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001482 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1483 level = tm->old_root.level;
1484 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001485 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001486 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001487 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001488
1489 return level;
1490}
1491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001492static inline int should_cow_block(struct btrfs_trans_handle *trans,
1493 struct btrfs_root *root,
1494 struct extent_buffer *buf)
1495{
David Sterbafccb84c2014-09-29 23:53:21 +02001496 if (btrfs_test_is_dummy_root(root))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001497 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001498
Liu Bof1ebcc72011-11-14 20:48:06 -05001499 /* ensure we can see the force_cow */
1500 smp_rmb();
1501
1502 /*
1503 * We do not need to cow a block if
1504 * 1) this block is not created or changed in this transaction;
1505 * 2) this block does not belong to TREE_RELOC tree;
1506 * 3) the root is not forced COW.
1507 *
1508 * What is forced COW:
1509 * when we create snapshot during commiting the transaction,
1510 * after we've finished coping src root, we must COW the shared
1511 * block to ensure the metadata consistency.
1512 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001513 if (btrfs_header_generation(buf) == trans->transid &&
1514 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1515 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001516 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001517 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001518 return 0;
1519 return 1;
1520}
1521
Chris Masond352ac62008-09-29 15:18:18 -04001522/*
1523 * cows a single block, see __btrfs_cow_block for the real work.
1524 * This version of it has extra checks so that a block isn't cow'd more than
1525 * once per transaction, as long as it hasn't been written yet
1526 */
Chris Masond3977122009-01-05 21:25:51 -05001527noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001528 struct btrfs_root *root, struct extent_buffer *buf,
1529 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001530 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001531{
Chris Mason6702ed42007-08-07 16:15:09 -04001532 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001533 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001534
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001535 if (trans->transaction != root->fs_info->running_transaction)
1536 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001537 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001538 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001539
1540 if (trans->transid != root->fs_info->generation)
1541 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001542 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001543
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001544 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoneyeefeb9a2016-06-08 00:36:38 -04001545 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001546 *cow_ret = buf;
1547 return 0;
1548 }
Chris Masonc4876852009-02-04 09:24:25 -05001549
Chris Mason0b86a832008-03-24 15:01:56 -04001550 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001551
1552 if (parent)
1553 btrfs_set_lock_blocking(parent);
1554 btrfs_set_lock_blocking(buf);
1555
Chris Masonf510cfe2007-10-15 16:14:48 -04001556 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001557 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001558
1559 trace_btrfs_cow_block(root, buf, *cow_ret);
1560
Chris Masonf510cfe2007-10-15 16:14:48 -04001561 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001562}
1563
Chris Masond352ac62008-09-29 15:18:18 -04001564/*
1565 * helper function for defrag to decide if two blocks pointed to by a
1566 * node are actually close by
1567 */
Chris Mason6b800532007-10-15 16:17:34 -04001568static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001569{
Chris Mason6b800532007-10-15 16:17:34 -04001570 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001571 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001572 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001573 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001574 return 0;
1575}
1576
Chris Mason081e9572007-11-06 10:26:24 -05001577/*
1578 * compare two keys in a memcmp fashion
1579 */
1580static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1581{
1582 struct btrfs_key k1;
1583
1584 btrfs_disk_key_to_cpu(&k1, disk);
1585
Diego Calleja20736ab2009-07-24 11:06:52 -04001586 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001587}
1588
Josef Bacikf3465ca2008-11-12 14:19:50 -05001589/*
1590 * same as comp_keys only with two btrfs_key's
1591 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001592int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001593{
1594 if (k1->objectid > k2->objectid)
1595 return 1;
1596 if (k1->objectid < k2->objectid)
1597 return -1;
1598 if (k1->type > k2->type)
1599 return 1;
1600 if (k1->type < k2->type)
1601 return -1;
1602 if (k1->offset > k2->offset)
1603 return 1;
1604 if (k1->offset < k2->offset)
1605 return -1;
1606 return 0;
1607}
Chris Mason081e9572007-11-06 10:26:24 -05001608
Chris Masond352ac62008-09-29 15:18:18 -04001609/*
1610 * this is used by the defrag code to go through all the
1611 * leaves pointed to by a node and reallocate them so that
1612 * disk order is close to key order
1613 */
Chris Mason6702ed42007-08-07 16:15:09 -04001614int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001615 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001616 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001617 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001618{
Chris Mason6b800532007-10-15 16:17:34 -04001619 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001620 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001621 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001622 u64 search_start = *last_ret;
1623 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001624 u64 other;
1625 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001626 int end_slot;
1627 int i;
1628 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001629 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001630 int uptodate;
1631 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001632 int progress_passed = 0;
1633 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001634
Chris Mason5708b952007-10-25 15:43:18 -04001635 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001636
Julia Lawall6c1500f2012-11-03 20:30:18 +00001637 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1638 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001639
Chris Mason6b800532007-10-15 16:17:34 -04001640 parent_nritems = btrfs_header_nritems(parent);
David Sterba707e8a02014-06-04 19:22:26 +02001641 blocksize = root->nodesize;
Chris Mason6702ed42007-08-07 16:15:09 -04001642 end_slot = parent_nritems;
1643
1644 if (parent_nritems == 1)
1645 return 0;
1646
Chris Masonb4ce94d2009-02-04 09:25:08 -05001647 btrfs_set_lock_blocking(parent);
1648
Chris Mason6702ed42007-08-07 16:15:09 -04001649 for (i = start_slot; i < end_slot; i++) {
1650 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001651
Chris Mason081e9572007-11-06 10:26:24 -05001652 btrfs_node_key(parent, &disk_key, i);
1653 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1654 continue;
1655
1656 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001657 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001658 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001659 if (last_block == 0)
1660 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001661
Chris Mason6702ed42007-08-07 16:15:09 -04001662 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001663 other = btrfs_node_blockptr(parent, i - 1);
1664 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001665 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001666 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001667 other = btrfs_node_blockptr(parent, i + 1);
1668 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001669 }
Chris Masone9d0b132007-08-10 14:06:19 -04001670 if (close) {
1671 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001672 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001673 }
Chris Mason6702ed42007-08-07 16:15:09 -04001674
David Sterba0308af42014-06-15 01:43:40 +02001675 cur = btrfs_find_tree_block(root, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001676 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001677 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001678 else
1679 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001680 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001681 if (!cur) {
David Sterbace86cd52014-06-15 01:07:32 +02001682 cur = read_tree_block(root, blocknr, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001683 if (!cur || !extent_buffer_uptodate(cur)) {
1684 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001685 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001686 }
Chris Mason6b800532007-10-15 16:17:34 -04001687 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001688 err = btrfs_read_buffer(cur, gen);
1689 if (err) {
1690 free_extent_buffer(cur);
1691 return err;
1692 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001693 }
Chris Mason6702ed42007-08-07 16:15:09 -04001694 }
Chris Masone9d0b132007-08-10 14:06:19 -04001695 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001696 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001697
Chris Masone7a84562008-06-25 16:01:31 -04001698 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001699 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001700 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001701 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001702 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001703 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001704 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001705 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001706 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001707 break;
Yan252c38f2007-08-29 09:11:44 -04001708 }
Chris Masone7a84562008-06-25 16:01:31 -04001709 search_start = cur->start;
1710 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001711 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001712 btrfs_tree_unlock(cur);
1713 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001714 }
1715 return err;
1716}
1717
Chris Mason74123bd2007-02-02 11:05:29 -05001718/*
1719 * The leaf data grows from end-to-front in the node.
1720 * this returns the address of the start of the last item,
1721 * which is the stop of the leaf data stack
1722 */
Chris Mason123abc82007-03-14 14:14:43 -04001723static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001724 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001725{
Chris Mason5f39d392007-10-15 16:14:19 -04001726 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001727 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001728 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001729 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001730}
1731
Chris Masonaa5d6be2007-02-28 16:35:06 -05001732
Chris Mason74123bd2007-02-02 11:05:29 -05001733/*
Chris Mason5f39d392007-10-15 16:14:19 -04001734 * search for key in the extent_buffer. The items start at offset p,
1735 * and they are item_size apart. There are 'max' items in p.
1736 *
Chris Mason74123bd2007-02-02 11:05:29 -05001737 * the slot in the array is returned via slot, and it points to
1738 * the place where you would insert key if it is not found in
1739 * the array.
1740 *
1741 * slot may point to max if the key is bigger than all of the keys
1742 */
Chris Masone02119d2008-09-05 16:13:11 -04001743static noinline int generic_bin_search(struct extent_buffer *eb,
1744 unsigned long p,
1745 int item_size, struct btrfs_key *key,
1746 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001747{
1748 int low = 0;
1749 int high = max;
1750 int mid;
1751 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001752 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001753 struct btrfs_disk_key unaligned;
1754 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001755 char *kaddr = NULL;
1756 unsigned long map_start = 0;
1757 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001758 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001759
Chris Masond3977122009-01-05 21:25:51 -05001760 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001761 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001762 offset = p + mid * item_size;
1763
Chris Masona6591712011-07-19 12:04:14 -04001764 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001765 (offset + sizeof(struct btrfs_disk_key)) >
1766 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001767
1768 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001769 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001770 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001771
Chris Mason479965d2007-10-15 16:14:27 -04001772 if (!err) {
1773 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1774 map_start);
1775 } else {
1776 read_extent_buffer(eb, &unaligned,
1777 offset, sizeof(unaligned));
1778 tmp = &unaligned;
1779 }
1780
Chris Mason5f39d392007-10-15 16:14:19 -04001781 } else {
1782 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1783 map_start);
1784 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001785 ret = comp_keys(tmp, key);
1786
1787 if (ret < 0)
1788 low = mid + 1;
1789 else if (ret > 0)
1790 high = mid;
1791 else {
1792 *slot = mid;
1793 return 0;
1794 }
1795 }
1796 *slot = low;
1797 return 1;
1798}
1799
Chris Mason97571fd2007-02-24 13:39:08 -05001800/*
1801 * simple bin_search frontend that does the right thing for
1802 * leaves vs nodes
1803 */
Chris Mason5f39d392007-10-15 16:14:19 -04001804static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1805 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001807 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001808 return generic_bin_search(eb,
1809 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001810 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001811 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001812 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001813 else
Chris Mason5f39d392007-10-15 16:14:19 -04001814 return generic_bin_search(eb,
1815 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001816 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001817 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001818 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001819}
1820
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001821int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1822 int level, int *slot)
1823{
1824 return bin_search(eb, key, level, slot);
1825}
1826
Yan, Zhengf0486c62010-05-16 10:46:25 -04001827static void root_add_used(struct btrfs_root *root, u32 size)
1828{
1829 spin_lock(&root->accounting_lock);
1830 btrfs_set_root_used(&root->root_item,
1831 btrfs_root_used(&root->root_item) + size);
1832 spin_unlock(&root->accounting_lock);
1833}
1834
1835static void root_sub_used(struct btrfs_root *root, u32 size)
1836{
1837 spin_lock(&root->accounting_lock);
1838 btrfs_set_root_used(&root->root_item,
1839 btrfs_root_used(&root->root_item) - size);
1840 spin_unlock(&root->accounting_lock);
1841}
1842
Chris Masond352ac62008-09-29 15:18:18 -04001843/* given a node and slot number, this reads the blocks it points to. The
1844 * extent buffer is returned with a reference taken (but unlocked).
1845 * NULL is returned on error.
1846 */
Chris Masone02119d2008-09-05 16:13:11 -04001847static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001848 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001849{
Chris Masonca7a79a2008-05-12 12:59:19 -04001850 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001851 struct extent_buffer *eb;
1852
Chris Masonbb803952007-03-01 12:04:21 -05001853 if (slot < 0)
1854 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001855 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001856 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001857
1858 BUG_ON(level == 0);
1859
Josef Bacik416bc652013-04-23 14:17:42 -04001860 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001861 btrfs_node_ptr_generation(parent, slot));
1862 if (eb && !extent_buffer_uptodate(eb)) {
1863 free_extent_buffer(eb);
1864 eb = NULL;
1865 }
1866
1867 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001868}
1869
Chris Masond352ac62008-09-29 15:18:18 -04001870/*
1871 * node level balancing, used to make sure nodes are in proper order for
1872 * item deletion. We balance from the top down, so we have to make sure
1873 * that a deletion won't leave an node completely empty later on.
1874 */
Chris Masone02119d2008-09-05 16:13:11 -04001875static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001876 struct btrfs_root *root,
1877 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001878{
Chris Mason5f39d392007-10-15 16:14:19 -04001879 struct extent_buffer *right = NULL;
1880 struct extent_buffer *mid;
1881 struct extent_buffer *left = NULL;
1882 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001883 int ret = 0;
1884 int wret;
1885 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001886 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001887 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001888
1889 if (level == 0)
1890 return 0;
1891
Chris Mason5f39d392007-10-15 16:14:19 -04001892 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001893
Chris Masonbd681512011-07-16 15:23:14 -04001894 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1895 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001896 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1897
Chris Mason1d4f8a02007-03-13 09:28:32 -04001898 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001899
Li Zefana05a9bb2011-09-06 16:55:34 +08001900 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001901 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001902 pslot = path->slots[level + 1];
1903 }
Chris Masonbb803952007-03-01 12:04:21 -05001904
Chris Mason40689472007-03-17 14:29:23 -04001905 /*
1906 * deal with the case where there is only one pointer in the root
1907 * by promoting the node below to a root
1908 */
Chris Mason5f39d392007-10-15 16:14:19 -04001909 if (!parent) {
1910 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001911
Chris Mason5f39d392007-10-15 16:14:19 -04001912 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001913 return 0;
1914
1915 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001916 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001917 if (!child) {
1918 ret = -EROFS;
1919 btrfs_std_error(root->fs_info, ret);
1920 goto enospc;
1921 }
1922
Chris Mason925baed2008-06-25 16:01:30 -04001923 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001924 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001925 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001926 if (ret) {
1927 btrfs_tree_unlock(child);
1928 free_extent_buffer(child);
1929 goto enospc;
1930 }
Yan2f375ab2008-02-01 14:58:07 -05001931
Jan Schmidt90f8d622013-04-13 13:19:53 +00001932 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001933 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001934
Chris Mason0b86a832008-03-24 15:01:56 -04001935 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001936 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001937
Chris Mason925baed2008-06-25 16:01:30 -04001938 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001939 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001940 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001941 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001942 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001943 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001944
1945 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001946 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001947 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001948 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001949 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001950 }
Chris Mason5f39d392007-10-15 16:14:19 -04001951 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001952 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001953 return 0;
1954
Chris Mason5f39d392007-10-15 16:14:19 -04001955 left = read_node_slot(root, parent, pslot - 1);
1956 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001957 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001958 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001959 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001960 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001961 if (wret) {
1962 ret = wret;
1963 goto enospc;
1964 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001965 }
Chris Mason5f39d392007-10-15 16:14:19 -04001966 right = read_node_slot(root, parent, pslot + 1);
1967 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001968 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001969 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001971 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001972 if (wret) {
1973 ret = wret;
1974 goto enospc;
1975 }
1976 }
1977
1978 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001979 if (left) {
1980 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001981 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001982 if (wret < 0)
1983 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001984 }
Chris Mason79f95c82007-03-01 15:16:26 -05001985
1986 /*
1987 * then try to empty the right most buffer into the middle
1988 */
Chris Mason5f39d392007-10-15 16:14:19 -04001989 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001990 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001991 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001992 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001993 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001994 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001995 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001996 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001997 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001998 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001999 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002000 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05002001 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002002 struct btrfs_disk_key right_key;
2003 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002004 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002005 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002006 btrfs_set_node_key(parent, &right_key, pslot + 1);
2007 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002008 }
2009 }
Chris Mason5f39d392007-10-15 16:14:19 -04002010 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002011 /*
2012 * we're not allowed to leave a node with one item in the
2013 * tree during a delete. A deletion from lower in the tree
2014 * could try to delete the only pointer in this node.
2015 * So, pull some keys from the left.
2016 * There has to be a left pointer at this point because
2017 * otherwise we would have pulled some pointers from the
2018 * right
2019 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002020 if (!left) {
2021 ret = -EROFS;
2022 btrfs_std_error(root->fs_info, ret);
2023 goto enospc;
2024 }
Chris Mason5f39d392007-10-15 16:14:19 -04002025 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002026 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002027 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002028 goto enospc;
2029 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002030 if (wret == 1) {
2031 wret = push_node_left(trans, root, left, mid, 1);
2032 if (wret < 0)
2033 ret = wret;
2034 }
Chris Mason79f95c82007-03-01 15:16:26 -05002035 BUG_ON(wret == 1);
2036 }
Chris Mason5f39d392007-10-15 16:14:19 -04002037 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002038 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002039 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002040 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002041 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002042 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002043 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002044 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002045 } else {
2046 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002047 struct btrfs_disk_key mid_key;
2048 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002049 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002050 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_set_node_key(parent, &mid_key, pslot);
2052 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002053 }
Chris Masonbb803952007-03-01 12:04:21 -05002054
Chris Mason79f95c82007-03-01 15:16:26 -05002055 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002056 if (left) {
2057 if (btrfs_header_nritems(left) > orig_slot) {
2058 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002059 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002060 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002061 path->slots[level + 1] -= 1;
2062 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002063 if (mid) {
2064 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002065 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002066 }
Chris Masonbb803952007-03-01 12:04:21 -05002067 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002068 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002069 path->slots[level] = orig_slot;
2070 }
2071 }
Chris Mason79f95c82007-03-01 15:16:26 -05002072 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002073 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002074 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002075 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002076enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002077 if (right) {
2078 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002079 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002080 }
2081 if (left) {
2082 if (path->nodes[level] != left)
2083 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002085 }
Chris Masonbb803952007-03-01 12:04:21 -05002086 return ret;
2087}
2088
Chris Masond352ac62008-09-29 15:18:18 -04002089/* Node balancing for insertion. Here we only split or push nodes around
2090 * when they are completely full. This is also done top down, so we
2091 * have to be pessimistic.
2092 */
Chris Masond3977122009-01-05 21:25:51 -05002093static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002094 struct btrfs_root *root,
2095 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002096{
Chris Mason5f39d392007-10-15 16:14:19 -04002097 struct extent_buffer *right = NULL;
2098 struct extent_buffer *mid;
2099 struct extent_buffer *left = NULL;
2100 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002101 int ret = 0;
2102 int wret;
2103 int pslot;
2104 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002105
2106 if (level == 0)
2107 return 1;
2108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002110 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002111
Li Zefana05a9bb2011-09-06 16:55:34 +08002112 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002113 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002114 pslot = path->slots[level + 1];
2115 }
Chris Masone66f7092007-04-20 13:16:02 -04002116
Chris Mason5f39d392007-10-15 16:14:19 -04002117 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002118 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002119
Chris Mason5f39d392007-10-15 16:14:19 -04002120 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002121
2122 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002123 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002124 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002125
2126 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002127 btrfs_set_lock_blocking(left);
2128
Chris Mason5f39d392007-10-15 16:14:19 -04002129 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002130 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2131 wret = 1;
2132 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002133 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002134 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002135 if (ret)
2136 wret = 1;
2137 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002138 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002139 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002140 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002141 }
Chris Masone66f7092007-04-20 13:16:02 -04002142 if (wret < 0)
2143 ret = wret;
2144 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002145 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002146 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002147 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002148 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002149 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002150 btrfs_set_node_key(parent, &disk_key, pslot);
2151 btrfs_mark_buffer_dirty(parent);
2152 if (btrfs_header_nritems(left) > orig_slot) {
2153 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002154 path->slots[level + 1] -= 1;
2155 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002156 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002157 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002158 } else {
2159 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002160 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002161 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002162 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002163 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002164 }
Chris Masone66f7092007-04-20 13:16:02 -04002165 return 0;
2166 }
Chris Mason925baed2008-06-25 16:01:30 -04002167 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002169 }
Chris Mason925baed2008-06-25 16:01:30 -04002170 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002171
2172 /*
2173 * then try to empty the right most buffer into the middle
2174 */
Chris Mason5f39d392007-10-15 16:14:19 -04002175 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002176 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002177
Chris Mason925baed2008-06-25 16:01:30 -04002178 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002179 btrfs_set_lock_blocking(right);
2180
Chris Mason5f39d392007-10-15 16:14:19 -04002181 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002182 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2183 wret = 1;
2184 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002185 ret = btrfs_cow_block(trans, root, right,
2186 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002187 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002188 if (ret)
2189 wret = 1;
2190 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002191 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002192 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002193 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002194 }
Chris Masone66f7092007-04-20 13:16:02 -04002195 if (wret < 0)
2196 ret = wret;
2197 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002198 struct btrfs_disk_key disk_key;
2199
2200 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002201 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002202 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002203 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2204 btrfs_mark_buffer_dirty(parent);
2205
2206 if (btrfs_header_nritems(mid) <= orig_slot) {
2207 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002208 path->slots[level + 1] += 1;
2209 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002210 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002211 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002212 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002213 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002214 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002216 }
Chris Masone66f7092007-04-20 13:16:02 -04002217 return 0;
2218 }
Chris Mason925baed2008-06-25 16:01:30 -04002219 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002220 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002221 }
Chris Masone66f7092007-04-20 13:16:02 -04002222 return 1;
2223}
2224
Chris Mason74123bd2007-02-02 11:05:29 -05002225/*
Chris Masond352ac62008-09-29 15:18:18 -04002226 * readahead one full node of leaves, finding things that are close
2227 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002228 */
Chris Masonc8c42862009-04-03 10:14:18 -04002229static void reada_for_search(struct btrfs_root *root,
2230 struct btrfs_path *path,
2231 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002232{
Chris Mason5f39d392007-10-15 16:14:19 -04002233 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002234 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002235 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002236 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002237 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002238 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002239 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002240 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002241 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002242 u32 nr;
2243 u32 blocksize;
2244 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002245
Chris Masona6b6e752007-10-15 16:22:39 -04002246 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002247 return;
2248
Chris Mason6702ed42007-08-07 16:15:09 -04002249 if (!path->nodes[level])
2250 return;
2251
Chris Mason5f39d392007-10-15 16:14:19 -04002252 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002253
Chris Mason3c69fae2007-08-07 15:52:22 -04002254 search = btrfs_node_blockptr(node, slot);
David Sterba707e8a02014-06-04 19:22:26 +02002255 blocksize = root->nodesize;
David Sterba0308af42014-06-15 01:43:40 +02002256 eb = btrfs_find_tree_block(root, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002257 if (eb) {
2258 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002259 return;
2260 }
2261
Chris Masona7175312009-01-22 09:23:10 -05002262 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002263
Chris Mason5f39d392007-10-15 16:14:19 -04002264 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002265 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002266
Chris Masond3977122009-01-05 21:25:51 -05002267 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002268 if (direction < 0) {
2269 if (nr == 0)
2270 break;
2271 nr--;
2272 } else if (direction > 0) {
2273 nr++;
2274 if (nr >= nritems)
2275 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002276 }
Chris Mason01f46652007-12-21 16:24:26 -05002277 if (path->reada < 0 && objectid) {
2278 btrfs_node_key(node, &disk_key, nr);
2279 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2280 break;
2281 }
Chris Mason6b800532007-10-15 16:17:34 -04002282 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002283 if ((search <= target && target - search <= 65536) ||
2284 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002285 gen = btrfs_node_ptr_generation(node, nr);
David Sterba58dc4ce2014-06-15 00:29:04 +02002286 readahead_tree_block(root, search, blocksize);
Chris Mason6b800532007-10-15 16:17:34 -04002287 nread += blocksize;
2288 }
2289 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002290 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002291 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002292 }
2293}
Chris Mason925baed2008-06-25 16:01:30 -04002294
Josef Bacik0b088512013-06-17 14:23:02 -04002295static noinline void reada_for_balance(struct btrfs_root *root,
2296 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002297{
2298 int slot;
2299 int nritems;
2300 struct extent_buffer *parent;
2301 struct extent_buffer *eb;
2302 u64 gen;
2303 u64 block1 = 0;
2304 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002305 int blocksize;
2306
Chris Mason8c594ea2009-04-20 15:50:10 -04002307 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002308 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002309 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002310
2311 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002312 slot = path->slots[level + 1];
David Sterba707e8a02014-06-04 19:22:26 +02002313 blocksize = root->nodesize;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002314
2315 if (slot > 0) {
2316 block1 = btrfs_node_blockptr(parent, slot - 1);
2317 gen = btrfs_node_ptr_generation(parent, slot - 1);
David Sterba0308af42014-06-15 01:43:40 +02002318 eb = btrfs_find_tree_block(root, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002319 /*
2320 * if we get -eagain from btrfs_buffer_uptodate, we
2321 * don't want to return eagain here. That will loop
2322 * forever
2323 */
2324 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002325 block1 = 0;
2326 free_extent_buffer(eb);
2327 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002328 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002329 block2 = btrfs_node_blockptr(parent, slot + 1);
2330 gen = btrfs_node_ptr_generation(parent, slot + 1);
David Sterba0308af42014-06-15 01:43:40 +02002331 eb = btrfs_find_tree_block(root, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002332 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002333 block2 = 0;
2334 free_extent_buffer(eb);
2335 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002336
Josef Bacik0b088512013-06-17 14:23:02 -04002337 if (block1)
David Sterba58dc4ce2014-06-15 00:29:04 +02002338 readahead_tree_block(root, block1, blocksize);
Josef Bacik0b088512013-06-17 14:23:02 -04002339 if (block2)
David Sterba58dc4ce2014-06-15 00:29:04 +02002340 readahead_tree_block(root, block2, blocksize);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002341}
2342
2343
2344/*
Chris Masond3977122009-01-05 21:25:51 -05002345 * when we walk down the tree, it is usually safe to unlock the higher layers
2346 * in the tree. The exceptions are when our path goes through slot 0, because
2347 * operations on the tree might require changing key pointers higher up in the
2348 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002349 *
Chris Masond3977122009-01-05 21:25:51 -05002350 * callers might also have set path->keep_locks, which tells this code to keep
2351 * the lock if the path points to the last slot in the block. This is part of
2352 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002353 *
Chris Masond3977122009-01-05 21:25:51 -05002354 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2355 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002356 */
Chris Masone02119d2008-09-05 16:13:11 -04002357static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002358 int lowest_unlock, int min_write_lock_level,
2359 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002360{
2361 int i;
2362 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002363 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002364 struct extent_buffer *t;
2365
2366 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2367 if (!path->nodes[i])
2368 break;
2369 if (!path->locks[i])
2370 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002371 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002372 skip_level = i + 1;
2373 continue;
2374 }
Chris Mason051e1b92008-06-25 16:01:30 -04002375 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002376 u32 nritems;
2377 t = path->nodes[i];
2378 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002379 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002380 skip_level = i + 1;
2381 continue;
2382 }
2383 }
Chris Mason051e1b92008-06-25 16:01:30 -04002384 if (skip_level < i && i >= lowest_unlock)
2385 no_skips = 1;
2386
Chris Mason925baed2008-06-25 16:01:30 -04002387 t = path->nodes[i];
2388 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002389 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002390 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002391 if (write_lock_level &&
2392 i > min_write_lock_level &&
2393 i <= *write_lock_level) {
2394 *write_lock_level = i - 1;
2395 }
Chris Mason925baed2008-06-25 16:01:30 -04002396 }
2397 }
2398}
2399
Chris Mason3c69fae2007-08-07 15:52:22 -04002400/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002401 * This releases any locks held in the path starting at level and
2402 * going all the way up to the root.
2403 *
2404 * btrfs_search_slot will keep the lock held on higher nodes in a few
2405 * corner cases, such as COW of the block at slot zero in the node. This
2406 * ignores those rules, and it should only be called when there are no
2407 * more updates to be done higher up in the tree.
2408 */
2409noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2410{
2411 int i;
2412
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002413 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002414 return;
2415
2416 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2417 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002418 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002419 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002420 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002421 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002422 path->locks[i] = 0;
2423 }
2424}
2425
2426/*
Chris Masonc8c42862009-04-03 10:14:18 -04002427 * helper function for btrfs_search_slot. The goal is to find a block
2428 * in cache without setting the path to blocking. If we find the block
2429 * we return zero and the path is unchanged.
2430 *
2431 * If we can't find the block, we set the path blocking and do some
2432 * reada. -EAGAIN is returned and the search must be repeated.
2433 */
2434static int
2435read_block_for_search(struct btrfs_trans_handle *trans,
2436 struct btrfs_root *root, struct btrfs_path *p,
2437 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002438 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002439{
2440 u64 blocknr;
2441 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002442 struct extent_buffer *b = *eb_ret;
2443 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002444 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002445
2446 blocknr = btrfs_node_blockptr(b, slot);
2447 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002448
David Sterba0308af42014-06-15 01:43:40 +02002449 tmp = btrfs_find_tree_block(root, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002450 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002451 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002452 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2453 *eb_ret = tmp;
2454 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002455 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002456
2457 /* the pages were up to date, but we failed
2458 * the generation number check. Do a full
2459 * read for the generation number that is correct.
2460 * We must do this without dropping locks so
2461 * we can trust our generation number
2462 */
2463 btrfs_set_path_blocking(p);
2464
2465 /* now we're allowed to do a blocking uptodate check */
2466 ret = btrfs_read_buffer(tmp, gen);
2467 if (!ret) {
2468 *eb_ret = tmp;
2469 return 0;
2470 }
2471 free_extent_buffer(tmp);
2472 btrfs_release_path(p);
2473 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002474 }
2475
2476 /*
2477 * reduce lock contention at high levels
2478 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002479 * we read. Don't release the lock on the current
2480 * level because we need to walk this node to figure
2481 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002482 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002483 btrfs_unlock_up_safe(p, level + 1);
2484 btrfs_set_path_blocking(p);
2485
Chris Masoncb449212010-10-24 11:01:27 -04002486 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002487 if (p->reada)
2488 reada_for_search(root, p, level, slot, key->objectid);
2489
David Sterbab3b4aa72011-04-21 01:20:15 +02002490 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002491
2492 ret = -EAGAIN;
David Sterbace86cd52014-06-15 01:07:32 +02002493 tmp = read_tree_block(root, blocknr, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002494 if (tmp) {
2495 /*
2496 * If the read above didn't mark this buffer up to date,
2497 * it will never end up being up to date. Set ret to EIO now
2498 * and give up so that our caller doesn't loop forever
2499 * on our EAGAINs.
2500 */
Chris Masonb9fab912012-05-06 07:23:47 -04002501 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002502 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002503 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002504 }
2505 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002506}
2507
2508/*
2509 * helper function for btrfs_search_slot. This does all of the checks
2510 * for node-level blocks and does any balancing required based on
2511 * the ins_len.
2512 *
2513 * If no extra work was required, zero is returned. If we had to
2514 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2515 * start over
2516 */
2517static int
2518setup_nodes_for_search(struct btrfs_trans_handle *trans,
2519 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002520 struct extent_buffer *b, int level, int ins_len,
2521 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002522{
2523 int ret;
2524 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2525 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2526 int sret;
2527
Chris Masonbd681512011-07-16 15:23:14 -04002528 if (*write_lock_level < level + 1) {
2529 *write_lock_level = level + 1;
2530 btrfs_release_path(p);
2531 goto again;
2532 }
2533
Chris Masonc8c42862009-04-03 10:14:18 -04002534 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002535 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002536 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002537 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002538
2539 BUG_ON(sret > 0);
2540 if (sret) {
2541 ret = sret;
2542 goto done;
2543 }
2544 b = p->nodes[level];
2545 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002546 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002547 int sret;
2548
Chris Masonbd681512011-07-16 15:23:14 -04002549 if (*write_lock_level < level + 1) {
2550 *write_lock_level = level + 1;
2551 btrfs_release_path(p);
2552 goto again;
2553 }
2554
Chris Masonc8c42862009-04-03 10:14:18 -04002555 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002556 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002557 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002558 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002559
2560 if (sret) {
2561 ret = sret;
2562 goto done;
2563 }
2564 b = p->nodes[level];
2565 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002566 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002567 goto again;
2568 }
2569 BUG_ON(btrfs_header_nritems(b) == 1);
2570 }
2571 return 0;
2572
2573again:
2574 ret = -EAGAIN;
2575done:
2576 return ret;
2577}
2578
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002579static void key_search_validate(struct extent_buffer *b,
2580 struct btrfs_key *key,
2581 int level)
2582{
2583#ifdef CONFIG_BTRFS_ASSERT
2584 struct btrfs_disk_key disk_key;
2585
2586 btrfs_cpu_key_to_disk(&disk_key, key);
2587
2588 if (level == 0)
2589 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2590 offsetof(struct btrfs_leaf, items[0].key),
2591 sizeof(disk_key)));
2592 else
2593 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2594 offsetof(struct btrfs_node, ptrs[0].key),
2595 sizeof(disk_key)));
2596#endif
2597}
2598
2599static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2600 int level, int *prev_cmp, int *slot)
2601{
2602 if (*prev_cmp != 0) {
2603 *prev_cmp = bin_search(b, key, level, slot);
2604 return *prev_cmp;
2605 }
2606
2607 key_search_validate(b, key, level);
2608 *slot = 0;
2609
2610 return 0;
2611}
2612
David Sterba751e2762015-01-02 18:45:16 +01002613int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002614 u64 iobjectid, u64 ioff, u8 key_type,
2615 struct btrfs_key *found_key)
2616{
2617 int ret;
2618 struct btrfs_key key;
2619 struct extent_buffer *eb;
David Sterba751e2762015-01-02 18:45:16 +01002620
2621 ASSERT(path);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002622
2623 key.type = key_type;
2624 key.objectid = iobjectid;
2625 key.offset = ioff;
2626
2627 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba751e2762015-01-02 18:45:16 +01002628 if ((ret < 0) || (found_key == NULL))
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002629 return ret;
2630
2631 eb = path->nodes[0];
2632 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2633 ret = btrfs_next_leaf(fs_root, path);
2634 if (ret)
2635 return ret;
2636 eb = path->nodes[0];
2637 }
2638
2639 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2640 if (found_key->type != key.type ||
2641 found_key->objectid != key.objectid)
2642 return 1;
2643
2644 return 0;
2645}
2646
Chris Masonc8c42862009-04-03 10:14:18 -04002647/*
Chris Mason74123bd2007-02-02 11:05:29 -05002648 * look for key in the tree. path is filled in with nodes along the way
2649 * if key is found, we return zero and you can find the item in the leaf
2650 * level of the path (level 0)
2651 *
2652 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002653 * be inserted, and 1 is returned. If there are other errors during the
2654 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002655 *
2656 * if ins_len > 0, nodes and leaves will be split as we walk down the
2657 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2658 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002659 */
Chris Masone089f052007-03-16 16:20:31 -04002660int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2661 *root, struct btrfs_key *key, struct btrfs_path *p, int
2662 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002663{
Chris Mason5f39d392007-10-15 16:14:19 -04002664 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002665 int slot;
2666 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002667 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002668 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002669 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002670 int root_lock;
2671 /* everything at write_lock_level or lower must be write locked */
2672 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002673 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002674 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002675 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002676
Chris Mason6702ed42007-08-07 16:15:09 -04002677 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002678 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002679 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002680 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002681
Chris Masonbd681512011-07-16 15:23:14 -04002682 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002683 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002684
Chris Masonbd681512011-07-16 15:23:14 -04002685 /* when we are removing items, we might have to go up to level
2686 * two as we update tree pointers Make sure we keep write
2687 * for those levels as well
2688 */
2689 write_lock_level = 2;
2690 } else if (ins_len > 0) {
2691 /*
2692 * for inserting items, make sure we have a write lock on
2693 * level 1 so we can update keys
2694 */
2695 write_lock_level = 1;
2696 }
2697
2698 if (!cow)
2699 write_lock_level = -1;
2700
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002701 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002702 write_lock_level = BTRFS_MAX_LEVEL;
2703
Chris Masonf7c79f32012-03-19 15:54:38 -04002704 min_write_lock_level = write_lock_level;
2705
Chris Masonbb803952007-03-01 12:04:21 -05002706again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002707 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002708 /*
2709 * we try very hard to do read locks on the root
2710 */
2711 root_lock = BTRFS_READ_LOCK;
2712 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002713 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002714 /*
2715 * the commit roots are read only
2716 * so we always do read locks
2717 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002718 if (p->need_commit_sem)
2719 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002720 b = root->commit_root;
2721 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002722 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002723 if (p->need_commit_sem)
2724 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002725 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002726 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002727 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002728 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002729 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002730 level = btrfs_header_level(b);
2731 } else {
2732 /* we don't know the level of the root node
2733 * until we actually have it read locked
2734 */
2735 b = btrfs_read_lock_root_node(root);
2736 level = btrfs_header_level(b);
2737 if (level <= write_lock_level) {
2738 /* whoops, must trade for write lock */
2739 btrfs_tree_read_unlock(b);
2740 free_extent_buffer(b);
2741 b = btrfs_lock_root_node(root);
2742 root_lock = BTRFS_WRITE_LOCK;
2743
2744 /* the level might have changed, check again */
2745 level = btrfs_header_level(b);
2746 }
2747 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002748 }
Chris Masonbd681512011-07-16 15:23:14 -04002749 p->nodes[level] = b;
2750 if (!p->skip_locking)
2751 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002752
Chris Masoneb60cea2007-02-02 09:18:22 -05002753 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002754 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002755
2756 /*
2757 * setup the path here so we can release it under lock
2758 * contention with the cow code
2759 */
Chris Mason02217ed2007-03-02 16:08:05 -05002760 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002761 /*
2762 * if we don't really need to cow this block
2763 * then we don't want to set the path blocking,
2764 * so we test it here
2765 */
Jeff Mahoneyeefeb9a2016-06-08 00:36:38 -04002766 if (!should_cow_block(trans, root, b)) {
2767 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002768 goto cow_done;
Jeff Mahoneyeefeb9a2016-06-08 00:36:38 -04002769 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002770
Chris Masonbd681512011-07-16 15:23:14 -04002771 /*
2772 * must have write locks on this node and the
2773 * parent
2774 */
Josef Bacik5124e002012-11-07 13:44:13 -05002775 if (level > write_lock_level ||
2776 (level + 1 > write_lock_level &&
2777 level + 1 < BTRFS_MAX_LEVEL &&
2778 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002779 write_lock_level = level + 1;
2780 btrfs_release_path(p);
2781 goto again;
2782 }
2783
Filipe Manana160f4082014-07-28 19:37:17 +01002784 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002785 err = btrfs_cow_block(trans, root, b,
2786 p->nodes[level + 1],
2787 p->slots[level + 1], &b);
2788 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002789 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002790 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002791 }
Chris Mason02217ed2007-03-02 16:08:05 -05002792 }
Chris Mason65b51a02008-08-01 15:11:20 -04002793cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002794 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002795 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002796
2797 /*
2798 * we have a lock on b and as long as we aren't changing
2799 * the tree, there is no way to for the items in b to change.
2800 * It is safe to drop the lock on our parent before we
2801 * go through the expensive btree search on b.
2802 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002803 * If we're inserting or deleting (ins_len != 0), then we might
2804 * be changing slot zero, which may require changing the parent.
2805 * So, we can't drop the lock until after we know which slot
2806 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002807 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002808 if (!ins_len && !p->keep_locks) {
2809 int u = level + 1;
2810
2811 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2812 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2813 p->locks[u] = 0;
2814 }
2815 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002816
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002817 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002818
Chris Mason5f39d392007-10-15 16:14:19 -04002819 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002820 int dec = 0;
2821 if (ret && slot > 0) {
2822 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002823 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002824 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002825 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002826 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002827 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002828 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002829 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002830 if (err) {
2831 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002832 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002833 }
Chris Masonc8c42862009-04-03 10:14:18 -04002834 b = p->nodes[level];
2835 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002836
Chris Masonbd681512011-07-16 15:23:14 -04002837 /*
2838 * slot 0 is special, if we change the key
2839 * we have to update the parent pointer
2840 * which means we must have a write lock
2841 * on the parent
2842 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002843 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002844 write_lock_level < level + 1) {
2845 write_lock_level = level + 1;
2846 btrfs_release_path(p);
2847 goto again;
2848 }
2849
Chris Masonf7c79f32012-03-19 15:54:38 -04002850 unlock_up(p, level, lowest_unlock,
2851 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002852
Chris Mason925baed2008-06-25 16:01:30 -04002853 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002854 if (dec)
2855 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002856 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002857 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002858
Yan Zheng33c66f42009-07-22 09:59:00 -04002859 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002860 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002861 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002862 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002863 if (err) {
2864 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002865 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002866 }
Chris Mason76a05b32009-05-14 13:24:30 -04002867
Chris Masonb4ce94d2009-02-04 09:25:08 -05002868 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002869 level = btrfs_header_level(b);
2870 if (level <= write_lock_level) {
2871 err = btrfs_try_tree_write_lock(b);
2872 if (!err) {
2873 btrfs_set_path_blocking(p);
2874 btrfs_tree_lock(b);
2875 btrfs_clear_path_blocking(p, b,
2876 BTRFS_WRITE_LOCK);
2877 }
2878 p->locks[level] = BTRFS_WRITE_LOCK;
2879 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002880 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002881 if (!err) {
2882 btrfs_set_path_blocking(p);
2883 btrfs_tree_read_lock(b);
2884 btrfs_clear_path_blocking(p, b,
2885 BTRFS_READ_LOCK);
2886 }
2887 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002888 }
Chris Masonbd681512011-07-16 15:23:14 -04002889 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002890 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002891 } else {
2892 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002893 if (ins_len > 0 &&
2894 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002895 if (write_lock_level < 1) {
2896 write_lock_level = 1;
2897 btrfs_release_path(p);
2898 goto again;
2899 }
2900
Chris Masonb4ce94d2009-02-04 09:25:08 -05002901 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002902 err = split_leaf(trans, root, key,
2903 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002904 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002905
Yan Zheng33c66f42009-07-22 09:59:00 -04002906 BUG_ON(err > 0);
2907 if (err) {
2908 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002909 goto done;
2910 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002911 }
Chris Mason459931e2008-12-10 09:10:46 -05002912 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002913 unlock_up(p, level, lowest_unlock,
2914 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002915 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002916 }
2917 }
Chris Mason65b51a02008-08-01 15:11:20 -04002918 ret = 1;
2919done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002920 /*
2921 * we don't really know what they plan on doing with the path
2922 * from here on, so for now just mark it as blocking
2923 */
Chris Masonb9473432009-03-13 11:00:37 -04002924 if (!p->leave_spinning)
2925 btrfs_set_path_blocking(p);
Filipe Manana55e97f62014-11-09 08:38:39 +00002926 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002927 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002928 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002929}
2930
Chris Mason74123bd2007-02-02 11:05:29 -05002931/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002932 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2933 * current state of the tree together with the operations recorded in the tree
2934 * modification log to search for the key in a previous version of this tree, as
2935 * denoted by the time_seq parameter.
2936 *
2937 * Naturally, there is no support for insert, delete or cow operations.
2938 *
2939 * The resulting path and return value will be set up as if we called
2940 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2941 */
2942int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2943 struct btrfs_path *p, u64 time_seq)
2944{
2945 struct extent_buffer *b;
2946 int slot;
2947 int ret;
2948 int err;
2949 int level;
2950 int lowest_unlock = 1;
2951 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002952 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002953
2954 lowest_level = p->lowest_level;
2955 WARN_ON(p->nodes[0] != NULL);
2956
2957 if (p->search_commit_root) {
2958 BUG_ON(time_seq);
2959 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2960 }
2961
2962again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002963 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002964 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002965 p->locks[level] = BTRFS_READ_LOCK;
2966
2967 while (b) {
2968 level = btrfs_header_level(b);
2969 p->nodes[level] = b;
2970 btrfs_clear_path_blocking(p, NULL, 0);
2971
2972 /*
2973 * we have a lock on b and as long as we aren't changing
2974 * the tree, there is no way to for the items in b to change.
2975 * It is safe to drop the lock on our parent before we
2976 * go through the expensive btree search on b.
2977 */
2978 btrfs_unlock_up_safe(p, level + 1);
2979
Josef Bacikd4b40872013-09-24 14:09:34 -04002980 /*
2981 * Since we can unwind eb's we want to do a real search every
2982 * time.
2983 */
2984 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002985 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002986
2987 if (level != 0) {
2988 int dec = 0;
2989 if (ret && slot > 0) {
2990 dec = 1;
2991 slot -= 1;
2992 }
2993 p->slots[level] = slot;
2994 unlock_up(p, level, lowest_unlock, 0, NULL);
2995
2996 if (level == lowest_level) {
2997 if (dec)
2998 p->slots[level]++;
2999 goto done;
3000 }
3001
3002 err = read_block_for_search(NULL, root, p, &b, level,
3003 slot, key, time_seq);
3004 if (err == -EAGAIN)
3005 goto again;
3006 if (err) {
3007 ret = err;
3008 goto done;
3009 }
3010
3011 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003012 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003013 if (!err) {
3014 btrfs_set_path_blocking(p);
3015 btrfs_tree_read_lock(b);
3016 btrfs_clear_path_blocking(p, b,
3017 BTRFS_READ_LOCK);
3018 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003019 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003020 if (!b) {
3021 ret = -ENOMEM;
3022 goto done;
3023 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003024 p->locks[level] = BTRFS_READ_LOCK;
3025 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003026 } else {
3027 p->slots[level] = slot;
3028 unlock_up(p, level, lowest_unlock, 0, NULL);
3029 goto done;
3030 }
3031 }
3032 ret = 1;
3033done:
3034 if (!p->leave_spinning)
3035 btrfs_set_path_blocking(p);
3036 if (ret < 0)
3037 btrfs_release_path(p);
3038
3039 return ret;
3040}
3041
3042/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003043 * helper to use instead of search slot if no exact match is needed but
3044 * instead the next or previous item should be returned.
3045 * When find_higher is true, the next higher item is returned, the next lower
3046 * otherwise.
3047 * When return_any and find_higher are both true, and no higher item is found,
3048 * return the next lower instead.
3049 * When return_any is true and find_higher is false, and no lower item is found,
3050 * return the next higher instead.
3051 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3052 * < 0 on error
3053 */
3054int btrfs_search_slot_for_read(struct btrfs_root *root,
3055 struct btrfs_key *key, struct btrfs_path *p,
3056 int find_higher, int return_any)
3057{
3058 int ret;
3059 struct extent_buffer *leaf;
3060
3061again:
3062 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3063 if (ret <= 0)
3064 return ret;
3065 /*
3066 * a return value of 1 means the path is at the position where the
3067 * item should be inserted. Normally this is the next bigger item,
3068 * but in case the previous item is the last in a leaf, path points
3069 * to the first free slot in the previous leaf, i.e. at an invalid
3070 * item.
3071 */
3072 leaf = p->nodes[0];
3073
3074 if (find_higher) {
3075 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3076 ret = btrfs_next_leaf(root, p);
3077 if (ret <= 0)
3078 return ret;
3079 if (!return_any)
3080 return 1;
3081 /*
3082 * no higher item found, return the next
3083 * lower instead
3084 */
3085 return_any = 0;
3086 find_higher = 0;
3087 btrfs_release_path(p);
3088 goto again;
3089 }
3090 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003091 if (p->slots[0] == 0) {
3092 ret = btrfs_prev_leaf(root, p);
3093 if (ret < 0)
3094 return ret;
3095 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003096 leaf = p->nodes[0];
3097 if (p->slots[0] == btrfs_header_nritems(leaf))
3098 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003099 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003100 }
Arne Jansene6793762011-09-13 11:18:10 +02003101 if (!return_any)
3102 return 1;
3103 /*
3104 * no lower item found, return the next
3105 * higher instead
3106 */
3107 return_any = 0;
3108 find_higher = 1;
3109 btrfs_release_path(p);
3110 goto again;
3111 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003112 --p->slots[0];
3113 }
3114 }
3115 return 0;
3116}
3117
3118/*
Chris Mason74123bd2007-02-02 11:05:29 -05003119 * adjust the pointers going up the tree, starting at level
3120 * making sure the right key of each node is points to 'key'.
3121 * This is used after shifting pointers to the left, so it stops
3122 * fixing up pointers when a given leaf/node is not in slot 0 of the
3123 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003124 *
Chris Mason74123bd2007-02-02 11:05:29 -05003125 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003126static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003127 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003128{
3129 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003130 struct extent_buffer *t;
3131
Chris Mason234b63a2007-03-13 10:46:10 -04003132 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003133 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003134 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003135 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003136 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00003137 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003138 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003139 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003140 if (tslot != 0)
3141 break;
3142 }
3143}
3144
Chris Mason74123bd2007-02-02 11:05:29 -05003145/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003146 * update item key.
3147 *
3148 * This function isn't completely safe. It's the caller's responsibility
3149 * that the new key won't break the order
3150 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003151void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003152 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003153{
3154 struct btrfs_disk_key disk_key;
3155 struct extent_buffer *eb;
3156 int slot;
3157
3158 eb = path->nodes[0];
3159 slot = path->slots[0];
3160 if (slot > 0) {
3161 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003162 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003163 }
3164 if (slot < btrfs_header_nritems(eb) - 1) {
3165 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003166 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003167 }
3168
3169 btrfs_cpu_key_to_disk(&disk_key, new_key);
3170 btrfs_set_item_key(eb, &disk_key, slot);
3171 btrfs_mark_buffer_dirty(eb);
3172 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003173 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003174}
3175
3176/*
Chris Mason74123bd2007-02-02 11:05:29 -05003177 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003178 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003179 *
3180 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3181 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003182 */
Chris Mason98ed5172008-01-03 10:01:48 -05003183static int push_node_left(struct btrfs_trans_handle *trans,
3184 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003185 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003186{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003187 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003188 int src_nritems;
3189 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003190 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003191
Chris Mason5f39d392007-10-15 16:14:19 -04003192 src_nritems = btrfs_header_nritems(src);
3193 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003194 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003195 WARN_ON(btrfs_header_generation(src) != trans->transid);
3196 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003197
Chris Masonbce4eae2008-04-24 14:42:46 -04003198 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003199 return 1;
3200
Chris Masond3977122009-01-05 21:25:51 -05003201 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003202 return 1;
3203
Chris Masonbce4eae2008-04-24 14:42:46 -04003204 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003205 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003206 if (push_items < src_nritems) {
3207 /* leave at least 8 pointers in the node if
3208 * we aren't going to empty it
3209 */
3210 if (src_nritems - push_items < 8) {
3211 if (push_items <= 8)
3212 return 1;
3213 push_items -= 8;
3214 }
3215 }
3216 } else
3217 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003218
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003219 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3220 push_items);
3221 if (ret) {
3222 btrfs_abort_transaction(trans, root, ret);
3223 return ret;
3224 }
Chris Mason5f39d392007-10-15 16:14:19 -04003225 copy_extent_buffer(dst, src,
3226 btrfs_node_key_ptr_offset(dst_nritems),
3227 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003228 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003229
Chris Masonbb803952007-03-01 12:04:21 -05003230 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003231 /*
3232 * don't call tree_mod_log_eb_move here, key removal was already
3233 * fully logged by tree_mod_log_eb_copy above.
3234 */
Chris Mason5f39d392007-10-15 16:14:19 -04003235 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3236 btrfs_node_key_ptr_offset(push_items),
3237 (src_nritems - push_items) *
3238 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003239 }
Chris Mason5f39d392007-10-15 16:14:19 -04003240 btrfs_set_header_nritems(src, src_nritems - push_items);
3241 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3242 btrfs_mark_buffer_dirty(src);
3243 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003244
Chris Masonbb803952007-03-01 12:04:21 -05003245 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003246}
3247
Chris Mason97571fd2007-02-24 13:39:08 -05003248/*
Chris Mason79f95c82007-03-01 15:16:26 -05003249 * try to push data from one node into the next node right in the
3250 * tree.
3251 *
3252 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3253 * error, and > 0 if there was no room in the right hand block.
3254 *
3255 * this will only push up to 1/2 the contents of the left node over
3256 */
Chris Mason5f39d392007-10-15 16:14:19 -04003257static int balance_node_right(struct btrfs_trans_handle *trans,
3258 struct btrfs_root *root,
3259 struct extent_buffer *dst,
3260 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003261{
Chris Mason79f95c82007-03-01 15:16:26 -05003262 int push_items = 0;
3263 int max_push;
3264 int src_nritems;
3265 int dst_nritems;
3266 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003267
Chris Mason7bb86312007-12-11 09:25:06 -05003268 WARN_ON(btrfs_header_generation(src) != trans->transid);
3269 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3270
Chris Mason5f39d392007-10-15 16:14:19 -04003271 src_nritems = btrfs_header_nritems(src);
3272 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003273 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003274 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003275 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003276
Chris Masond3977122009-01-05 21:25:51 -05003277 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003278 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003279
3280 max_push = src_nritems / 2 + 1;
3281 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003282 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003283 return 1;
Yan252c38f2007-08-29 09:11:44 -04003284
Chris Mason79f95c82007-03-01 15:16:26 -05003285 if (max_push < push_items)
3286 push_items = max_push;
3287
Jan Schmidtf2304752012-05-26 11:43:17 +02003288 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003289 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3290 btrfs_node_key_ptr_offset(0),
3291 (dst_nritems) *
3292 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003293
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003294 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3295 src_nritems - push_items, push_items);
3296 if (ret) {
3297 btrfs_abort_transaction(trans, root, ret);
3298 return ret;
3299 }
Chris Mason5f39d392007-10-15 16:14:19 -04003300 copy_extent_buffer(dst, src,
3301 btrfs_node_key_ptr_offset(0),
3302 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003303 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003304
Chris Mason5f39d392007-10-15 16:14:19 -04003305 btrfs_set_header_nritems(src, src_nritems - push_items);
3306 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003307
Chris Mason5f39d392007-10-15 16:14:19 -04003308 btrfs_mark_buffer_dirty(src);
3309 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003310
Chris Mason79f95c82007-03-01 15:16:26 -05003311 return ret;
3312}
3313
3314/*
Chris Mason97571fd2007-02-24 13:39:08 -05003315 * helper function to insert a new root level in the tree.
3316 * A new node is allocated, and a single item is inserted to
3317 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003318 *
3319 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003320 */
Chris Masond3977122009-01-05 21:25:51 -05003321static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003322 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003323 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003324{
Chris Mason7bb86312007-12-11 09:25:06 -05003325 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003326 struct extent_buffer *lower;
3327 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003328 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003329 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003330
3331 BUG_ON(path->nodes[level]);
3332 BUG_ON(path->nodes[level-1] != root->node);
3333
Chris Mason7bb86312007-12-11 09:25:06 -05003334 lower = path->nodes[level-1];
3335 if (level == 1)
3336 btrfs_item_key(lower, &lower_key, 0);
3337 else
3338 btrfs_node_key(lower, &lower_key, 0);
3339
David Sterba4d75f8a2014-06-15 01:54:12 +02003340 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3341 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003342 if (IS_ERR(c))
3343 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003344
Yan, Zhengf0486c62010-05-16 10:46:25 -04003345 root_add_used(root, root->nodesize);
3346
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003347 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003348 btrfs_set_header_nritems(c, 1);
3349 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003350 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003351 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003352 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003353 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003354
Ross Kirk0a4e5582013-09-24 10:12:38 +01003355 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003356 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003357
3358 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003359 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003360
Chris Mason5f39d392007-10-15 16:14:19 -04003361 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003362 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003363 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003364 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003365
3366 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003367
3368 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003369
Chris Mason925baed2008-06-25 16:01:30 -04003370 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003371 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003372 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003373
3374 /* the super has an extra ref to root->node */
3375 free_extent_buffer(old);
3376
Chris Mason0b86a832008-03-24 15:01:56 -04003377 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003378 extent_buffer_get(c);
3379 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003380 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003381 path->slots[level] = 0;
3382 return 0;
3383}
3384
Chris Mason74123bd2007-02-02 11:05:29 -05003385/*
3386 * worker function to insert a single pointer in a node.
3387 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003388 *
Chris Mason74123bd2007-02-02 11:05:29 -05003389 * slot and level indicate where you want the key to go, and
3390 * blocknr is the block the key points to.
3391 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003392static void insert_ptr(struct btrfs_trans_handle *trans,
3393 struct btrfs_root *root, struct btrfs_path *path,
3394 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003395 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003396{
Chris Mason5f39d392007-10-15 16:14:19 -04003397 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003398 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003399 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003400
3401 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003402 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003403 lower = path->nodes[level];
3404 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003405 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003406 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003407 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003408 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003409 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3410 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003411 memmove_extent_buffer(lower,
3412 btrfs_node_key_ptr_offset(slot + 1),
3413 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003414 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003415 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003416 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003417 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003418 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003419 BUG_ON(ret < 0);
3420 }
Chris Mason5f39d392007-10-15 16:14:19 -04003421 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003422 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003423 WARN_ON(trans->transid == 0);
3424 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003425 btrfs_set_header_nritems(lower, nritems + 1);
3426 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003427}
3428
Chris Mason97571fd2007-02-24 13:39:08 -05003429/*
3430 * split the node at the specified level in path in two.
3431 * The path is corrected to point to the appropriate node after the split
3432 *
3433 * Before splitting this tries to make some room in the node by pushing
3434 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003435 *
3436 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003437 */
Chris Masone02119d2008-09-05 16:13:11 -04003438static noinline int split_node(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003441{
Chris Mason5f39d392007-10-15 16:14:19 -04003442 struct extent_buffer *c;
3443 struct extent_buffer *split;
3444 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003445 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003446 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003447 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003448
Chris Mason5f39d392007-10-15 16:14:19 -04003449 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003450 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003451 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003452 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003453 * trying to split the root, lets make a new one
3454 *
Liu Bofdd99c72013-05-22 12:06:51 +00003455 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003456 * insert_new_root, because that root buffer will be kept as a
3457 * normal node. We are going to log removal of half of the
3458 * elements below with tree_mod_log_eb_copy. We're holding a
3459 * tree lock on the buffer, which is why we cannot race with
3460 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003461 */
Liu Bofdd99c72013-05-22 12:06:51 +00003462 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003463 if (ret)
3464 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003465 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003466 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003467 c = path->nodes[level];
3468 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003469 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003470 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003471 if (ret < 0)
3472 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003473 }
Chris Masone66f7092007-04-20 13:16:02 -04003474
Chris Mason5f39d392007-10-15 16:14:19 -04003475 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003476 mid = (c_nritems + 1) / 2;
3477 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003478
David Sterba4d75f8a2014-06-15 01:54:12 +02003479 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3480 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003481 if (IS_ERR(split))
3482 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003483
Yan, Zhengf0486c62010-05-16 10:46:25 -04003484 root_add_used(root, root->nodesize);
3485
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003486 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003487 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003488 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003489 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003490 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003491 btrfs_set_header_owner(split, root->root_key.objectid);
3492 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003493 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003494 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003495 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003496 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003497
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003498 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3499 mid, c_nritems - mid);
3500 if (ret) {
3501 btrfs_abort_transaction(trans, root, ret);
3502 return ret;
3503 }
Chris Mason5f39d392007-10-15 16:14:19 -04003504 copy_extent_buffer(split, c,
3505 btrfs_node_key_ptr_offset(0),
3506 btrfs_node_key_ptr_offset(mid),
3507 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3508 btrfs_set_header_nritems(split, c_nritems - mid);
3509 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003510 ret = 0;
3511
Chris Mason5f39d392007-10-15 16:14:19 -04003512 btrfs_mark_buffer_dirty(c);
3513 btrfs_mark_buffer_dirty(split);
3514
Jeff Mahoney143bede2012-03-01 14:56:26 +01003515 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003516 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003517
Chris Mason5de08d72007-02-24 06:24:44 -05003518 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003519 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003520 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003521 free_extent_buffer(c);
3522 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003523 path->slots[level + 1] += 1;
3524 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003525 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003526 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003527 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003528 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003529}
3530
Chris Mason74123bd2007-02-02 11:05:29 -05003531/*
3532 * how many bytes are required to store the items in a leaf. start
3533 * and nr indicate which items in the leaf to check. This totals up the
3534 * space used both by the item structs and the item data
3535 */
Chris Mason5f39d392007-10-15 16:14:19 -04003536static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003537{
Josef Bacik41be1f32012-10-15 13:43:18 -04003538 struct btrfs_item *start_item;
3539 struct btrfs_item *end_item;
3540 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003541 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003542 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003543 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544
3545 if (!nr)
3546 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003547 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003548 start_item = btrfs_item_nr(start);
3549 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003550 data_len = btrfs_token_item_offset(l, start_item, &token) +
3551 btrfs_token_item_size(l, start_item, &token);
3552 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003553 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003554 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003555 return data_len;
3556}
3557
Chris Mason74123bd2007-02-02 11:05:29 -05003558/*
Chris Masond4dbff92007-04-04 14:08:15 -04003559 * The space between the end of the leaf items and
3560 * the start of the leaf data. IOW, how much room
3561 * the leaf has left for both items and data
3562 */
Chris Masond3977122009-01-05 21:25:51 -05003563noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003564 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003565{
Chris Mason5f39d392007-10-15 16:14:19 -04003566 int nritems = btrfs_header_nritems(leaf);
3567 int ret;
3568 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3569 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003570 btrfs_crit(root->fs_info,
3571 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003572 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003573 leaf_space_used(leaf, 0, nritems), nritems);
3574 }
3575 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003576}
3577
Chris Mason99d8f832010-07-07 10:51:48 -04003578/*
3579 * min slot controls the lowest index we're willing to push to the
3580 * right. We'll push up to and including min_slot, but no lower
3581 */
Chris Mason44871b12009-03-13 10:04:31 -04003582static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3583 struct btrfs_root *root,
3584 struct btrfs_path *path,
3585 int data_size, int empty,
3586 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003587 int free_space, u32 left_nritems,
3588 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003589{
Chris Mason5f39d392007-10-15 16:14:19 -04003590 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003591 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003592 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003593 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003594 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003595 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003596 int push_space = 0;
3597 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003598 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003599 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003600 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003601 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003602 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003603
Chris Masoncfed81a2012-03-03 07:40:03 -05003604 btrfs_init_map_token(&token);
3605
Chris Mason34a38212007-11-07 13:31:03 -05003606 if (empty)
3607 nr = 0;
3608 else
Chris Mason99d8f832010-07-07 10:51:48 -04003609 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003610
Zheng Yan31840ae2008-09-23 13:14:14 -04003611 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003612 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003613
Chris Mason44871b12009-03-13 10:04:31 -04003614 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003615 i = left_nritems - 1;
3616 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003617 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003618
Zheng Yan31840ae2008-09-23 13:14:14 -04003619 if (!empty && push_items > 0) {
3620 if (path->slots[0] > i)
3621 break;
3622 if (path->slots[0] == i) {
3623 int space = btrfs_leaf_free_space(root, left);
3624 if (space + push_space * 2 > free_space)
3625 break;
3626 }
3627 }
3628
Chris Mason00ec4c52007-02-24 12:47:20 -05003629 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003630 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003631
Chris Masondb945352007-10-15 16:15:53 -04003632 this_item_size = btrfs_item_size(left, item);
3633 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003634 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003635
Chris Mason00ec4c52007-02-24 12:47:20 -05003636 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003637 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003638 if (i == 0)
3639 break;
3640 i--;
Chris Masondb945352007-10-15 16:15:53 -04003641 }
Chris Mason5f39d392007-10-15 16:14:19 -04003642
Chris Mason925baed2008-06-25 16:01:30 -04003643 if (push_items == 0)
3644 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003645
Julia Lawall6c1500f2012-11-03 20:30:18 +00003646 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003647
Chris Mason00ec4c52007-02-24 12:47:20 -05003648 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003649 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003650
Chris Mason5f39d392007-10-15 16:14:19 -04003651 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003652 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003653
Chris Mason00ec4c52007-02-24 12:47:20 -05003654 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003655 data_end = leaf_data_end(root, right);
3656 memmove_extent_buffer(right,
3657 btrfs_leaf_data(right) + data_end - push_space,
3658 btrfs_leaf_data(right) + data_end,
3659 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3660
Chris Mason00ec4c52007-02-24 12:47:20 -05003661 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003662 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003663 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3664 btrfs_leaf_data(left) + leaf_data_end(root, left),
3665 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003666
3667 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3668 btrfs_item_nr_offset(0),
3669 right_nritems * sizeof(struct btrfs_item));
3670
Chris Mason00ec4c52007-02-24 12:47:20 -05003671 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003672 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3673 btrfs_item_nr_offset(left_nritems - push_items),
3674 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003675
3676 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003677 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003678 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003679 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003680 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003681 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003682 push_space -= btrfs_token_item_size(right, item, &token);
3683 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003684 }
3685
Chris Mason7518a232007-03-12 12:01:18 -04003686 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003687 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003688
Chris Mason34a38212007-11-07 13:31:03 -05003689 if (left_nritems)
3690 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003691 else
3692 clean_tree_block(trans, root, left);
3693
Chris Mason5f39d392007-10-15 16:14:19 -04003694 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003695
Chris Mason5f39d392007-10-15 16:14:19 -04003696 btrfs_item_key(right, &disk_key, 0);
3697 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003698 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003699
Chris Mason00ec4c52007-02-24 12:47:20 -05003700 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003701 if (path->slots[0] >= left_nritems) {
3702 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003703 if (btrfs_header_nritems(path->nodes[0]) == 0)
3704 clean_tree_block(trans, root, path->nodes[0]);
3705 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003706 free_extent_buffer(path->nodes[0]);
3707 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003708 path->slots[1] += 1;
3709 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003710 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003711 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003712 }
3713 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003714
3715out_unlock:
3716 btrfs_tree_unlock(right);
3717 free_extent_buffer(right);
3718 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003719}
Chris Mason925baed2008-06-25 16:01:30 -04003720
Chris Mason00ec4c52007-02-24 12:47:20 -05003721/*
Chris Mason44871b12009-03-13 10:04:31 -04003722 * push some data in the path leaf to the right, trying to free up at
3723 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3724 *
3725 * returns 1 if the push failed because the other node didn't have enough
3726 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003727 *
3728 * this will push starting from min_slot to the end of the leaf. It won't
3729 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003730 */
3731static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003732 *root, struct btrfs_path *path,
3733 int min_data_size, int data_size,
3734 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003735{
3736 struct extent_buffer *left = path->nodes[0];
3737 struct extent_buffer *right;
3738 struct extent_buffer *upper;
3739 int slot;
3740 int free_space;
3741 u32 left_nritems;
3742 int ret;
3743
3744 if (!path->nodes[1])
3745 return 1;
3746
3747 slot = path->slots[1];
3748 upper = path->nodes[1];
3749 if (slot >= btrfs_header_nritems(upper) - 1)
3750 return 1;
3751
3752 btrfs_assert_tree_locked(path->nodes[1]);
3753
3754 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003755 if (right == NULL)
3756 return 1;
3757
Chris Mason44871b12009-03-13 10:04:31 -04003758 btrfs_tree_lock(right);
3759 btrfs_set_lock_blocking(right);
3760
3761 free_space = btrfs_leaf_free_space(root, right);
3762 if (free_space < data_size)
3763 goto out_unlock;
3764
3765 /* cow and double check */
3766 ret = btrfs_cow_block(trans, root, right, upper,
3767 slot + 1, &right);
3768 if (ret)
3769 goto out_unlock;
3770
3771 free_space = btrfs_leaf_free_space(root, right);
3772 if (free_space < data_size)
3773 goto out_unlock;
3774
3775 left_nritems = btrfs_header_nritems(left);
3776 if (left_nritems == 0)
3777 goto out_unlock;
3778
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003779 if (path->slots[0] == left_nritems && !empty) {
3780 /* Key greater than all keys in the leaf, right neighbor has
3781 * enough room for it and we're not emptying our leaf to delete
3782 * it, therefore use right neighbor to insert the new item and
3783 * no need to touch/dirty our left leaft. */
3784 btrfs_tree_unlock(left);
3785 free_extent_buffer(left);
3786 path->nodes[0] = right;
3787 path->slots[0] = 0;
3788 path->slots[1]++;
3789 return 0;
3790 }
3791
Chris Mason99d8f832010-07-07 10:51:48 -04003792 return __push_leaf_right(trans, root, path, min_data_size, empty,
3793 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003794out_unlock:
3795 btrfs_tree_unlock(right);
3796 free_extent_buffer(right);
3797 return 1;
3798}
3799
3800/*
Chris Mason74123bd2007-02-02 11:05:29 -05003801 * push some data in the path leaf to the left, trying to free up at
3802 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003803 *
3804 * max_slot can put a limit on how far into the leaf we'll push items. The
3805 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3806 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003807 */
Chris Mason44871b12009-03-13 10:04:31 -04003808static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3809 struct btrfs_root *root,
3810 struct btrfs_path *path, int data_size,
3811 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003812 int free_space, u32 right_nritems,
3813 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814{
Chris Mason5f39d392007-10-15 16:14:19 -04003815 struct btrfs_disk_key disk_key;
3816 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003817 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003818 int push_space = 0;
3819 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003820 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003821 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003822 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003823 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003824 u32 this_item_size;
3825 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003826 struct btrfs_map_token token;
3827
3828 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003829
Chris Mason34a38212007-11-07 13:31:03 -05003830 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003831 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003832 else
Chris Mason99d8f832010-07-07 10:51:48 -04003833 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003834
3835 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003836 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003837
Zheng Yan31840ae2008-09-23 13:14:14 -04003838 if (!empty && push_items > 0) {
3839 if (path->slots[0] < i)
3840 break;
3841 if (path->slots[0] == i) {
3842 int space = btrfs_leaf_free_space(root, right);
3843 if (space + push_space * 2 > free_space)
3844 break;
3845 }
3846 }
3847
Chris Masonbe0e5c02007-01-26 15:51:26 -05003848 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003849 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003850
3851 this_item_size = btrfs_item_size(right, item);
3852 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853 break;
Chris Masondb945352007-10-15 16:15:53 -04003854
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003856 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003857 }
Chris Masondb945352007-10-15 16:15:53 -04003858
Chris Masonbe0e5c02007-01-26 15:51:26 -05003859 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003860 ret = 1;
3861 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003862 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303863 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003864
Chris Masonbe0e5c02007-01-26 15:51:26 -05003865 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003866 copy_extent_buffer(left, right,
3867 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3868 btrfs_item_nr_offset(0),
3869 push_items * sizeof(struct btrfs_item));
3870
Chris Mason123abc82007-03-14 14:14:43 -04003871 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003872 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003873
3874 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003875 leaf_data_end(root, left) - push_space,
3876 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003877 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003878 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003879 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003880 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003881
Chris Masondb945352007-10-15 16:15:53 -04003882 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003883 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003884 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003885
Ross Kirkdd3cc162013-09-16 15:58:09 +01003886 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003887
Chris Masoncfed81a2012-03-03 07:40:03 -05003888 ioff = btrfs_token_item_offset(left, item, &token);
3889 btrfs_set_token_item_offset(left, item,
3890 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3891 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003892 }
Chris Mason5f39d392007-10-15 16:14:19 -04003893 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003894
3895 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003896 if (push_items > right_nritems)
3897 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003898 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003899
Chris Mason34a38212007-11-07 13:31:03 -05003900 if (push_items < right_nritems) {
3901 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3902 leaf_data_end(root, right);
3903 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3904 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3905 btrfs_leaf_data(right) +
3906 leaf_data_end(root, right), push_space);
3907
3908 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003909 btrfs_item_nr_offset(push_items),
3910 (btrfs_header_nritems(right) - push_items) *
3911 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003912 }
Yaneef1c492007-11-26 10:58:13 -05003913 right_nritems -= push_items;
3914 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003915 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003916 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003917 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003918
Chris Masoncfed81a2012-03-03 07:40:03 -05003919 push_space = push_space - btrfs_token_item_size(right,
3920 item, &token);
3921 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003922 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003923
Chris Mason5f39d392007-10-15 16:14:19 -04003924 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003925 if (right_nritems)
3926 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003927 else
3928 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003929
Chris Mason5f39d392007-10-15 16:14:19 -04003930 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003931 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003932
3933 /* then fixup the leaf pointer in the path */
3934 if (path->slots[0] < push_items) {
3935 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003936 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003937 free_extent_buffer(path->nodes[0]);
3938 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003939 path->slots[1] -= 1;
3940 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003941 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003942 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003943 path->slots[0] -= push_items;
3944 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003945 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003946 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003947out:
3948 btrfs_tree_unlock(left);
3949 free_extent_buffer(left);
3950 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003951}
3952
Chris Mason74123bd2007-02-02 11:05:29 -05003953/*
Chris Mason44871b12009-03-13 10:04:31 -04003954 * push some data in the path leaf to the left, trying to free up at
3955 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003956 *
3957 * max_slot can put a limit on how far into the leaf we'll push items. The
3958 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3959 * items
Chris Mason44871b12009-03-13 10:04:31 -04003960 */
3961static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003962 *root, struct btrfs_path *path, int min_data_size,
3963 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003964{
3965 struct extent_buffer *right = path->nodes[0];
3966 struct extent_buffer *left;
3967 int slot;
3968 int free_space;
3969 u32 right_nritems;
3970 int ret = 0;
3971
3972 slot = path->slots[1];
3973 if (slot == 0)
3974 return 1;
3975 if (!path->nodes[1])
3976 return 1;
3977
3978 right_nritems = btrfs_header_nritems(right);
3979 if (right_nritems == 0)
3980 return 1;
3981
3982 btrfs_assert_tree_locked(path->nodes[1]);
3983
3984 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003985 if (left == NULL)
3986 return 1;
3987
Chris Mason44871b12009-03-13 10:04:31 -04003988 btrfs_tree_lock(left);
3989 btrfs_set_lock_blocking(left);
3990
3991 free_space = btrfs_leaf_free_space(root, left);
3992 if (free_space < data_size) {
3993 ret = 1;
3994 goto out;
3995 }
3996
3997 /* cow and double check */
3998 ret = btrfs_cow_block(trans, root, left,
3999 path->nodes[1], slot - 1, &left);
4000 if (ret) {
4001 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004002 if (ret == -ENOSPC)
4003 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004004 goto out;
4005 }
4006
4007 free_space = btrfs_leaf_free_space(root, left);
4008 if (free_space < data_size) {
4009 ret = 1;
4010 goto out;
4011 }
4012
Chris Mason99d8f832010-07-07 10:51:48 -04004013 return __push_leaf_left(trans, root, path, min_data_size,
4014 empty, left, free_space, right_nritems,
4015 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004016out:
4017 btrfs_tree_unlock(left);
4018 free_extent_buffer(left);
4019 return ret;
4020}
4021
4022/*
Chris Mason74123bd2007-02-02 11:05:29 -05004023 * split the path's leaf in two, making sure there is at least data_size
4024 * available for the resulting leaf level of the path.
4025 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004026static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4027 struct btrfs_root *root,
4028 struct btrfs_path *path,
4029 struct extent_buffer *l,
4030 struct extent_buffer *right,
4031 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004032{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004033 int data_copy_size;
4034 int rt_data_off;
4035 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004036 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004037 struct btrfs_map_token token;
4038
4039 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004040
Chris Mason5f39d392007-10-15 16:14:19 -04004041 nritems = nritems - mid;
4042 btrfs_set_header_nritems(right, nritems);
4043 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4044
4045 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4046 btrfs_item_nr_offset(mid),
4047 nritems * sizeof(struct btrfs_item));
4048
4049 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004050 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4051 data_copy_size, btrfs_leaf_data(l) +
4052 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004053
Chris Mason5f39d392007-10-15 16:14:19 -04004054 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4055 btrfs_item_end_nr(l, mid);
4056
4057 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004058 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004059 u32 ioff;
4060
Chris Masoncfed81a2012-03-03 07:40:03 -05004061 ioff = btrfs_token_item_offset(right, item, &token);
4062 btrfs_set_token_item_offset(right, item,
4063 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004064 }
Chris Mason74123bd2007-02-02 11:05:29 -05004065
Chris Mason5f39d392007-10-15 16:14:19 -04004066 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004067 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004068 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004069 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004070
4071 btrfs_mark_buffer_dirty(right);
4072 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004073 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004074
Chris Masonbe0e5c02007-01-26 15:51:26 -05004075 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004076 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004077 free_extent_buffer(path->nodes[0]);
4078 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004079 path->slots[0] -= mid;
4080 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004081 } else {
4082 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004083 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004084 }
Chris Mason5f39d392007-10-15 16:14:19 -04004085
Chris Masoneb60cea2007-02-02 09:18:22 -05004086 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004087}
4088
4089/*
Chris Mason99d8f832010-07-07 10:51:48 -04004090 * double splits happen when we need to insert a big item in the middle
4091 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4092 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4093 * A B C
4094 *
4095 * We avoid this by trying to push the items on either side of our target
4096 * into the adjacent leaves. If all goes well we can avoid the double split
4097 * completely.
4098 */
4099static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4100 struct btrfs_root *root,
4101 struct btrfs_path *path,
4102 int data_size)
4103{
4104 int ret;
4105 int progress = 0;
4106 int slot;
4107 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004108 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004109
4110 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004111 if (slot < btrfs_header_nritems(path->nodes[0]))
4112 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004113
4114 /*
4115 * try to push all the items after our slot into the
4116 * right leaf
4117 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004118 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004119 if (ret < 0)
4120 return ret;
4121
4122 if (ret == 0)
4123 progress++;
4124
4125 nritems = btrfs_header_nritems(path->nodes[0]);
4126 /*
4127 * our goal is to get our slot at the start or end of a leaf. If
4128 * we've done so we're done
4129 */
4130 if (path->slots[0] == 0 || path->slots[0] == nritems)
4131 return 0;
4132
4133 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4134 return 0;
4135
4136 /* try to push all the items before our slot into the next leaf */
4137 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004138 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004139 if (ret < 0)
4140 return ret;
4141
4142 if (ret == 0)
4143 progress++;
4144
4145 if (progress)
4146 return 0;
4147 return 1;
4148}
4149
4150/*
Chris Mason44871b12009-03-13 10:04:31 -04004151 * split the path's leaf in two, making sure there is at least data_size
4152 * available for the resulting leaf level of the path.
4153 *
4154 * returns 0 if all went well and < 0 on failure.
4155 */
4156static noinline int split_leaf(struct btrfs_trans_handle *trans,
4157 struct btrfs_root *root,
4158 struct btrfs_key *ins_key,
4159 struct btrfs_path *path, int data_size,
4160 int extend)
4161{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004163 struct extent_buffer *l;
4164 u32 nritems;
4165 int mid;
4166 int slot;
4167 struct extent_buffer *right;
4168 int ret = 0;
4169 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004170 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004171 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004172 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004173
Yan, Zhenga5719522009-09-24 09:17:31 -04004174 l = path->nodes[0];
4175 slot = path->slots[0];
4176 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4177 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4178 return -EOVERFLOW;
4179
Chris Mason44871b12009-03-13 10:04:31 -04004180 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004181 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004182 int space_needed = data_size;
4183
4184 if (slot < btrfs_header_nritems(l))
4185 space_needed -= btrfs_leaf_free_space(root, l);
4186
4187 wret = push_leaf_right(trans, root, path, space_needed,
4188 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004189 if (wret < 0)
4190 return wret;
4191 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004192 wret = push_leaf_left(trans, root, path, space_needed,
4193 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004194 if (wret < 0)
4195 return wret;
4196 }
4197 l = path->nodes[0];
4198
4199 /* did the pushes work? */
4200 if (btrfs_leaf_free_space(root, l) >= data_size)
4201 return 0;
4202 }
4203
4204 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004205 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004206 if (ret)
4207 return ret;
4208 }
4209again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004211 l = path->nodes[0];
4212 slot = path->slots[0];
4213 nritems = btrfs_header_nritems(l);
4214 mid = (nritems + 1) / 2;
4215
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004216 if (mid <= slot) {
4217 if (nritems == 1 ||
4218 leaf_space_used(l, mid, nritems - mid) + data_size >
4219 BTRFS_LEAF_DATA_SIZE(root)) {
4220 if (slot >= nritems) {
4221 split = 0;
4222 } else {
4223 mid = slot;
4224 if (mid != nritems &&
4225 leaf_space_used(l, mid, nritems - mid) +
4226 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004227 if (data_size && !tried_avoid_double)
4228 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004229 split = 2;
4230 }
4231 }
4232 }
4233 } else {
4234 if (leaf_space_used(l, 0, mid) + data_size >
4235 BTRFS_LEAF_DATA_SIZE(root)) {
4236 if (!extend && data_size && slot == 0) {
4237 split = 0;
4238 } else if ((extend || !data_size) && slot == 0) {
4239 mid = 1;
4240 } else {
4241 mid = slot;
4242 if (mid != nritems &&
4243 leaf_space_used(l, mid, nritems - mid) +
4244 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004245 if (data_size && !tried_avoid_double)
4246 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304247 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004248 }
4249 }
4250 }
4251 }
4252
4253 if (split == 0)
4254 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4255 else
4256 btrfs_item_key(l, &disk_key, mid);
4257
David Sterba4d75f8a2014-06-15 01:54:12 +02004258 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4259 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004260 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004261 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004262
David Sterba707e8a02014-06-04 19:22:26 +02004263 root_add_used(root, root->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004264
4265 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4266 btrfs_set_header_bytenr(right, right->start);
4267 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004268 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004269 btrfs_set_header_owner(right, root->root_key.objectid);
4270 btrfs_set_header_level(right, 0);
4271 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004272 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004273
4274 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004275 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004276 BTRFS_UUID_SIZE);
4277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004278 if (split == 0) {
4279 if (mid <= slot) {
4280 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004281 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004282 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004283 btrfs_tree_unlock(path->nodes[0]);
4284 free_extent_buffer(path->nodes[0]);
4285 path->nodes[0] = right;
4286 path->slots[0] = 0;
4287 path->slots[1] += 1;
4288 } else {
4289 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004290 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004291 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004292 btrfs_tree_unlock(path->nodes[0]);
4293 free_extent_buffer(path->nodes[0]);
4294 path->nodes[0] = right;
4295 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004296 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004297 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004298 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299 btrfs_mark_buffer_dirty(right);
4300 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004301 }
4302
Jeff Mahoney143bede2012-03-01 14:56:26 +01004303 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004305 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004306 BUG_ON(num_doubles != 0);
4307 num_doubles++;
4308 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004309 }
Chris Mason44871b12009-03-13 10:04:31 -04004310
Jeff Mahoney143bede2012-03-01 14:56:26 +01004311 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004312
4313push_for_double:
4314 push_for_double_split(trans, root, path, data_size);
4315 tried_avoid_double = 1;
4316 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4317 return 0;
4318 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004319}
4320
Yan, Zhengad48fd752009-11-12 09:33:58 +00004321static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4322 struct btrfs_root *root,
4323 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004324{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004325 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004326 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004327 struct btrfs_file_extent_item *fi;
4328 u64 extent_len = 0;
4329 u32 item_size;
4330 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004331
4332 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004333 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4334
4335 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4336 key.type != BTRFS_EXTENT_CSUM_KEY);
4337
4338 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4339 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004340
4341 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004342 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4343 fi = btrfs_item_ptr(leaf, path->slots[0],
4344 struct btrfs_file_extent_item);
4345 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4346 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004347 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004348
Chris Mason459931e2008-12-10 09:10:46 -05004349 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004350 path->search_for_split = 1;
4351 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004352 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004353 if (ret < 0)
4354 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004355
Yan, Zhengad48fd752009-11-12 09:33:58 +00004356 ret = -EAGAIN;
4357 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004358 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004359 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4360 goto err;
4361
Chris Mason109f6ae2010-04-02 09:20:18 -04004362 /* the leaf has changed, it now has room. return now */
4363 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4364 goto err;
4365
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 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4370 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004371 }
4372
Chris Masonb9473432009-03-13 11:00:37 -04004373 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004374 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004375 if (ret)
4376 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004377
Yan, Zhengad48fd752009-11-12 09:33:58 +00004378 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004379 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004380 return 0;
4381err:
4382 path->keep_locks = 0;
4383 return ret;
4384}
4385
4386static noinline int split_item(struct btrfs_trans_handle *trans,
4387 struct btrfs_root *root,
4388 struct btrfs_path *path,
4389 struct btrfs_key *new_key,
4390 unsigned long split_offset)
4391{
4392 struct extent_buffer *leaf;
4393 struct btrfs_item *item;
4394 struct btrfs_item *new_item;
4395 int slot;
4396 char *buf;
4397 u32 nritems;
4398 u32 item_size;
4399 u32 orig_offset;
4400 struct btrfs_disk_key disk_key;
4401
Chris Masonb9473432009-03-13 11:00:37 -04004402 leaf = path->nodes[0];
4403 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4404
Chris Masonb4ce94d2009-02-04 09:25:08 -05004405 btrfs_set_path_blocking(path);
4406
Ross Kirkdd3cc162013-09-16 15:58:09 +01004407 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004408 orig_offset = btrfs_item_offset(leaf, item);
4409 item_size = btrfs_item_size(leaf, item);
4410
Chris Mason459931e2008-12-10 09:10:46 -05004411 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004412 if (!buf)
4413 return -ENOMEM;
4414
Chris Mason459931e2008-12-10 09:10:46 -05004415 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4416 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004417
Chris Mason459931e2008-12-10 09:10:46 -05004418 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004419 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004420 if (slot != nritems) {
4421 /* shift the items */
4422 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004423 btrfs_item_nr_offset(slot),
4424 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004425 }
4426
4427 btrfs_cpu_key_to_disk(&disk_key, new_key);
4428 btrfs_set_item_key(leaf, &disk_key, slot);
4429
Ross Kirkdd3cc162013-09-16 15:58:09 +01004430 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004431
4432 btrfs_set_item_offset(leaf, new_item, orig_offset);
4433 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4434
4435 btrfs_set_item_offset(leaf, item,
4436 orig_offset + item_size - split_offset);
4437 btrfs_set_item_size(leaf, item, split_offset);
4438
4439 btrfs_set_header_nritems(leaf, nritems + 1);
4440
4441 /* write the data for the start of the original item */
4442 write_extent_buffer(leaf, buf,
4443 btrfs_item_ptr_offset(leaf, path->slots[0]),
4444 split_offset);
4445
4446 /* write the data for the new item */
4447 write_extent_buffer(leaf, buf + split_offset,
4448 btrfs_item_ptr_offset(leaf, slot),
4449 item_size - split_offset);
4450 btrfs_mark_buffer_dirty(leaf);
4451
Yan, Zhengad48fd752009-11-12 09:33:58 +00004452 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004453 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004454 return 0;
4455}
4456
4457/*
4458 * This function splits a single item into two items,
4459 * giving 'new_key' to the new item and splitting the
4460 * old one at split_offset (from the start of the item).
4461 *
4462 * The path may be released by this operation. After
4463 * the split, the path is pointing to the old item. The
4464 * new item is going to be in the same node as the old one.
4465 *
4466 * Note, the item being split must be smaller enough to live alone on
4467 * a tree block with room for one extra struct btrfs_item
4468 *
4469 * This allows us to split the item in place, keeping a lock on the
4470 * leaf the entire time.
4471 */
4472int btrfs_split_item(struct btrfs_trans_handle *trans,
4473 struct btrfs_root *root,
4474 struct btrfs_path *path,
4475 struct btrfs_key *new_key,
4476 unsigned long split_offset)
4477{
4478 int ret;
4479 ret = setup_leaf_for_split(trans, root, path,
4480 sizeof(struct btrfs_item));
4481 if (ret)
4482 return ret;
4483
4484 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004485 return ret;
4486}
4487
4488/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004489 * This function duplicate a item, giving 'new_key' to the new item.
4490 * It guarantees both items live in the same tree leaf and the new item
4491 * is contiguous with the original item.
4492 *
4493 * This allows us to split file extent in place, keeping a lock on the
4494 * leaf the entire time.
4495 */
4496int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4497 struct btrfs_root *root,
4498 struct btrfs_path *path,
4499 struct btrfs_key *new_key)
4500{
4501 struct extent_buffer *leaf;
4502 int ret;
4503 u32 item_size;
4504
4505 leaf = path->nodes[0];
4506 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4507 ret = setup_leaf_for_split(trans, root, path,
4508 item_size + sizeof(struct btrfs_item));
4509 if (ret)
4510 return ret;
4511
4512 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004513 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004514 item_size, item_size +
4515 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004516 leaf = path->nodes[0];
4517 memcpy_extent_buffer(leaf,
4518 btrfs_item_ptr_offset(leaf, path->slots[0]),
4519 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4520 item_size);
4521 return 0;
4522}
4523
4524/*
Chris Masond352ac62008-09-29 15:18:18 -04004525 * make the item pointed to by the path smaller. new_size indicates
4526 * how small to make it, and from_end tells us if we just chop bytes
4527 * off the end of the item or if we shift the item to chop bytes off
4528 * the front.
4529 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004530void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004531 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004532{
Chris Masonb18c6682007-04-17 13:26:50 -04004533 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004534 struct extent_buffer *leaf;
4535 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004536 u32 nritems;
4537 unsigned int data_end;
4538 unsigned int old_data_start;
4539 unsigned int old_size;
4540 unsigned int size_diff;
4541 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004542 struct btrfs_map_token token;
4543
4544 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004545
Chris Mason5f39d392007-10-15 16:14:19 -04004546 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004547 slot = path->slots[0];
4548
4549 old_size = btrfs_item_size_nr(leaf, slot);
4550 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004551 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004552
Chris Mason5f39d392007-10-15 16:14:19 -04004553 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004554 data_end = leaf_data_end(root, leaf);
4555
Chris Mason5f39d392007-10-15 16:14:19 -04004556 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004557
Chris Masonb18c6682007-04-17 13:26:50 -04004558 size_diff = old_size - new_size;
4559
4560 BUG_ON(slot < 0);
4561 BUG_ON(slot >= nritems);
4562
4563 /*
4564 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4565 */
4566 /* first correct the data pointers */
4567 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004568 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004569 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004570
Chris Masoncfed81a2012-03-03 07:40:03 -05004571 ioff = btrfs_token_item_offset(leaf, item, &token);
4572 btrfs_set_token_item_offset(leaf, item,
4573 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004574 }
Chris Masondb945352007-10-15 16:15:53 -04004575
Chris Masonb18c6682007-04-17 13:26:50 -04004576 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004577 if (from_end) {
4578 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4579 data_end + size_diff, btrfs_leaf_data(leaf) +
4580 data_end, old_data_start + new_size - data_end);
4581 } else {
4582 struct btrfs_disk_key disk_key;
4583 u64 offset;
4584
4585 btrfs_item_key(leaf, &disk_key, slot);
4586
4587 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4588 unsigned long ptr;
4589 struct btrfs_file_extent_item *fi;
4590
4591 fi = btrfs_item_ptr(leaf, slot,
4592 struct btrfs_file_extent_item);
4593 fi = (struct btrfs_file_extent_item *)(
4594 (unsigned long)fi - size_diff);
4595
4596 if (btrfs_file_extent_type(leaf, fi) ==
4597 BTRFS_FILE_EXTENT_INLINE) {
4598 ptr = btrfs_item_ptr_offset(leaf, slot);
4599 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004600 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004601 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004602 }
4603 }
4604
4605 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4606 data_end + size_diff, btrfs_leaf_data(leaf) +
4607 data_end, old_data_start - data_end);
4608
4609 offset = btrfs_disk_key_offset(&disk_key);
4610 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4611 btrfs_set_item_key(leaf, &disk_key, slot);
4612 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004613 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004614 }
Chris Mason5f39d392007-10-15 16:14:19 -04004615
Ross Kirkdd3cc162013-09-16 15:58:09 +01004616 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004617 btrfs_set_item_size(leaf, item, new_size);
4618 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004619
Chris Mason5f39d392007-10-15 16:14:19 -04004620 if (btrfs_leaf_free_space(root, leaf) < 0) {
4621 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004622 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004623 }
Chris Masonb18c6682007-04-17 13:26:50 -04004624}
4625
Chris Masond352ac62008-09-29 15:18:18 -04004626/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004627 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004628 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004629void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004630 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004631{
Chris Mason6567e832007-04-16 09:22:45 -04004632 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004633 struct extent_buffer *leaf;
4634 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004635 u32 nritems;
4636 unsigned int data_end;
4637 unsigned int old_data;
4638 unsigned int old_size;
4639 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004640 struct btrfs_map_token token;
4641
4642 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004643
Chris Mason5f39d392007-10-15 16:14:19 -04004644 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004645
Chris Mason5f39d392007-10-15 16:14:19 -04004646 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004647 data_end = leaf_data_end(root, leaf);
4648
Chris Mason5f39d392007-10-15 16:14:19 -04004649 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4650 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004651 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004652 }
Chris Mason6567e832007-04-16 09:22:45 -04004653 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004654 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004655
4656 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004657 if (slot >= nritems) {
4658 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004659 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004660 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004661 BUG_ON(1);
4662 }
Chris Mason6567e832007-04-16 09:22:45 -04004663
4664 /*
4665 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4666 */
4667 /* first correct the data pointers */
4668 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004669 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004670 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004671
Chris Masoncfed81a2012-03-03 07:40:03 -05004672 ioff = btrfs_token_item_offset(leaf, item, &token);
4673 btrfs_set_token_item_offset(leaf, item,
4674 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004675 }
Chris Mason5f39d392007-10-15 16:14:19 -04004676
Chris Mason6567e832007-04-16 09:22:45 -04004677 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004678 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004679 data_end - data_size, btrfs_leaf_data(leaf) +
4680 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004681
Chris Mason6567e832007-04-16 09:22:45 -04004682 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004683 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004684 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004685 btrfs_set_item_size(leaf, item, old_size + data_size);
4686 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004687
Chris Mason5f39d392007-10-15 16:14:19 -04004688 if (btrfs_leaf_free_space(root, leaf) < 0) {
4689 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004690 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004691 }
Chris Mason6567e832007-04-16 09:22:45 -04004692}
4693
Chris Mason74123bd2007-02-02 11:05:29 -05004694/*
Chris Mason44871b12009-03-13 10:04:31 -04004695 * this is a helper for btrfs_insert_empty_items, the main goal here is
4696 * to save stack depth by doing the bulk of the work in a function
4697 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004698 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004699void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004700 struct btrfs_key *cpu_key, u32 *data_size,
4701 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004702{
Chris Mason5f39d392007-10-15 16:14:19 -04004703 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004704 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004705 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004706 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004707 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004708 struct extent_buffer *leaf;
4709 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004710 struct btrfs_map_token token;
4711
Filipe Manana24cdc842014-07-28 19:34:35 +01004712 if (path->slots[0] == 0) {
4713 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4714 fixup_low_keys(root, path, &disk_key, 1);
4715 }
4716 btrfs_unlock_up_safe(path, 1);
4717
Chris Masoncfed81a2012-03-03 07:40:03 -05004718 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004719
Chris Mason5f39d392007-10-15 16:14:19 -04004720 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004721 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004722
Chris Mason5f39d392007-10-15 16:14:19 -04004723 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004724 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004725
Chris Masonf25956c2008-09-12 15:32:53 -04004726 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004727 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004728 btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
Chris Mason9c583092008-01-29 15:15:18 -05004729 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004730 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004731 }
Chris Mason5f39d392007-10-15 16:14:19 -04004732
Chris Masonbe0e5c02007-01-26 15:51:26 -05004733 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004734 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004735
Chris Mason5f39d392007-10-15 16:14:19 -04004736 if (old_data < data_end) {
4737 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004738 btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
Chris Mason5f39d392007-10-15 16:14:19 -04004739 slot, old_data, data_end);
4740 BUG_ON(1);
4741 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004742 /*
4743 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4744 */
4745 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004746 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004747 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004748
Ross Kirkdd3cc162013-09-16 15:58:09 +01004749 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004750 ioff = btrfs_token_item_offset(leaf, item, &token);
4751 btrfs_set_token_item_offset(leaf, item,
4752 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004753 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004754 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004755 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004756 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004757 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004758
4759 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004760 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004761 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004762 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004763 data_end = old_data;
4764 }
Chris Mason5f39d392007-10-15 16:14:19 -04004765
Chris Mason62e27492007-03-15 12:56:47 -04004766 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004767 for (i = 0; i < nr; i++) {
4768 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4769 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004770 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004771 btrfs_set_token_item_offset(leaf, item,
4772 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004773 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004774 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004775 }
Chris Mason44871b12009-03-13 10:04:31 -04004776
Chris Mason9c583092008-01-29 15:15:18 -05004777 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004778 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004779
Chris Mason5f39d392007-10-15 16:14:19 -04004780 if (btrfs_leaf_free_space(root, leaf) < 0) {
4781 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004783 }
Chris Mason44871b12009-03-13 10:04:31 -04004784}
4785
4786/*
4787 * Given a key and some data, insert items into the tree.
4788 * This does all the path init required, making room in the tree if needed.
4789 */
4790int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4791 struct btrfs_root *root,
4792 struct btrfs_path *path,
4793 struct btrfs_key *cpu_key, u32 *data_size,
4794 int nr)
4795{
Chris Mason44871b12009-03-13 10:04:31 -04004796 int ret = 0;
4797 int slot;
4798 int i;
4799 u32 total_size = 0;
4800 u32 total_data = 0;
4801
4802 for (i = 0; i < nr; i++)
4803 total_data += data_size[i];
4804
4805 total_size = total_data + (nr * sizeof(struct btrfs_item));
4806 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4807 if (ret == 0)
4808 return -EEXIST;
4809 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004810 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004811
Chris Mason44871b12009-03-13 10:04:31 -04004812 slot = path->slots[0];
4813 BUG_ON(slot < 0);
4814
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004815 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004816 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004817 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004818}
4819
4820/*
4821 * Given a key and some data, insert an item into the tree.
4822 * This does all the path init required, making room in the tree if needed.
4823 */
Chris Masone089f052007-03-16 16:20:31 -04004824int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4825 *root, struct btrfs_key *cpu_key, void *data, u32
4826 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004827{
4828 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004829 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004830 struct extent_buffer *leaf;
4831 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004832
Chris Mason2c90e5d2007-04-02 10:50:19 -04004833 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004834 if (!path)
4835 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004836 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004837 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004838 leaf = path->nodes[0];
4839 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4840 write_extent_buffer(leaf, data, ptr, data_size);
4841 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004842 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004843 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004844 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004845}
4846
Chris Mason74123bd2007-02-02 11:05:29 -05004847/*
Chris Mason5de08d72007-02-24 06:24:44 -05004848 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004849 *
Chris Masond352ac62008-09-29 15:18:18 -04004850 * the tree should have been previously balanced so the deletion does not
4851 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004852 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004853static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4854 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004855{
Chris Mason5f39d392007-10-15 16:14:19 -04004856 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004857 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004858 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004859
Chris Mason5f39d392007-10-15 16:14:19 -04004860 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004861 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004862 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004863 tree_mod_log_eb_move(root->fs_info, parent, slot,
4864 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004865 memmove_extent_buffer(parent,
4866 btrfs_node_key_ptr_offset(slot),
4867 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004868 sizeof(struct btrfs_key_ptr) *
4869 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004870 } else if (level) {
4871 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004872 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004873 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004874 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004875
Chris Mason7518a232007-03-12 12:01:18 -04004876 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004877 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004878 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004879 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004880 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004881 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004882 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004883 struct btrfs_disk_key disk_key;
4884
4885 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004886 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004887 }
Chris Masond6025572007-03-30 14:27:56 -04004888 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004889}
4890
Chris Mason74123bd2007-02-02 11:05:29 -05004891/*
Chris Mason323ac952008-10-01 19:05:46 -04004892 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004893 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004894 *
4895 * This deletes the pointer in path->nodes[1] and frees the leaf
4896 * block extent. zero is returned if it all worked out, < 0 otherwise.
4897 *
4898 * The path must have already been setup for deleting the leaf, including
4899 * all the proper balancing. path->nodes[1] must be locked.
4900 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004901static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4902 struct btrfs_root *root,
4903 struct btrfs_path *path,
4904 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004905{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004906 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004907 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004908
Chris Mason4d081c42009-02-04 09:31:28 -05004909 /*
4910 * btrfs_free_extent is expensive, we want to make sure we
4911 * aren't holding any locks when we call it
4912 */
4913 btrfs_unlock_up_safe(path, 0);
4914
Yan, Zhengf0486c62010-05-16 10:46:25 -04004915 root_sub_used(root, leaf->len);
4916
Josef Bacik3083ee22012-03-09 16:01:49 -05004917 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004918 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004919 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004920}
4921/*
Chris Mason74123bd2007-02-02 11:05:29 -05004922 * delete the item at the leaf level in path. If that empties
4923 * the leaf, remove it from the tree
4924 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004925int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4926 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004927{
Chris Mason5f39d392007-10-15 16:14:19 -04004928 struct extent_buffer *leaf;
4929 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004930 int last_off;
4931 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004932 int ret = 0;
4933 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004934 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004935 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004936 struct btrfs_map_token token;
4937
4938 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004939
Chris Mason5f39d392007-10-15 16:14:19 -04004940 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004941 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4942
4943 for (i = 0; i < nr; i++)
4944 dsize += btrfs_item_size_nr(leaf, slot + i);
4945
Chris Mason5f39d392007-10-15 16:14:19 -04004946 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004947
Chris Mason85e21ba2008-01-29 15:11:36 -05004948 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004949 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004950
4951 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004952 data_end + dsize,
4953 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004954 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004955
Chris Mason85e21ba2008-01-29 15:11:36 -05004956 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004957 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004958
Ross Kirkdd3cc162013-09-16 15:58:09 +01004959 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004960 ioff = btrfs_token_item_offset(leaf, item, &token);
4961 btrfs_set_token_item_offset(leaf, item,
4962 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004963 }
Chris Masondb945352007-10-15 16:15:53 -04004964
Chris Mason5f39d392007-10-15 16:14:19 -04004965 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004966 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004967 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004968 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004969 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 btrfs_set_header_nritems(leaf, nritems - nr);
4971 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004972
Chris Mason74123bd2007-02-02 11:05:29 -05004973 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004974 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004975 if (leaf == root->node) {
4976 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004977 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004978 btrfs_set_path_blocking(path);
4979 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004980 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004981 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004982 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004983 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004984 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004985 struct btrfs_disk_key disk_key;
4986
4987 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004988 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004989 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004990
Chris Mason74123bd2007-02-02 11:05:29 -05004991 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004992 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004993 /* push_leaf_left fixes the path.
4994 * make sure the path still points to our leaf
4995 * for possible call to del_ptr below
4996 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004997 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004998 extent_buffer_get(leaf);
4999
Chris Masonb9473432009-03-13 11:00:37 -04005000 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005001 wret = push_leaf_left(trans, root, path, 1, 1,
5002 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005003 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005004 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005005
5006 if (path->nodes[0] == leaf &&
5007 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005008 wret = push_leaf_right(trans, root, path, 1,
5009 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005010 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005011 ret = wret;
5012 }
Chris Mason5f39d392007-10-15 16:14:19 -04005013
5014 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005015 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005016 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005017 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005018 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005019 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005020 /* if we're still in the path, make sure
5021 * we're dirty. Otherwise, one of the
5022 * push_leaf functions must have already
5023 * dirtied this buffer
5024 */
5025 if (path->nodes[0] == leaf)
5026 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005027 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005028 }
Chris Masond5719762007-03-23 10:01:08 -04005029 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005030 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005031 }
5032 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005033 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005034}
5035
Chris Mason97571fd2007-02-24 13:39:08 -05005036/*
Chris Mason925baed2008-06-25 16:01:30 -04005037 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005038 * returns 0 if it found something or 1 if there are no lesser leaves.
5039 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005040 *
5041 * This may release the path, and so you may lose any locks held at the
5042 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005043 */
Josef Bacik16e75492013-10-22 12:18:51 -04005044int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005045{
Chris Mason925baed2008-06-25 16:01:30 -04005046 struct btrfs_key key;
5047 struct btrfs_disk_key found_key;
5048 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005049
Chris Mason925baed2008-06-25 16:01:30 -04005050 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005051
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005052 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005053 key.offset--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005054 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005055 key.type--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005056 key.offset = (u64)-1;
5057 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005058 key.objectid--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005059 key.type = (u8)-1;
5060 key.offset = (u64)-1;
5061 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005062 return 1;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005063 }
Chris Mason7bb86312007-12-11 09:25:06 -05005064
David Sterbab3b4aa72011-04-21 01:20:15 +02005065 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005066 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5067 if (ret < 0)
5068 return ret;
5069 btrfs_item_key(path->nodes[0], &found_key, 0);
5070 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005071 /*
5072 * We might have had an item with the previous key in the tree right
5073 * before we released our path. And after we released our path, that
5074 * item might have been pushed to the first slot (0) of the leaf we
5075 * were holding due to a tree balance. Alternatively, an item with the
5076 * previous key can exist as the only element of a leaf (big fat item).
5077 * Therefore account for these 2 cases, so that our callers (like
5078 * btrfs_previous_item) don't miss an existing item with a key matching
5079 * the previous key we computed above.
5080 */
5081 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005082 return 0;
5083 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005084}
5085
Chris Mason3f157a22008-06-25 16:01:31 -04005086/*
5087 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005088 * for nodes or leaves that are have a minimum transaction id.
5089 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005090 *
5091 * This does not cow, but it does stuff the starting key it finds back
5092 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5093 * key and get a writable path.
5094 *
5095 * This does lock as it descends, and path->keep_locks should be set
5096 * to 1 by the caller.
5097 *
5098 * This honors path->lowest_level to prevent descent past a given level
5099 * of the tree.
5100 *
Chris Masond352ac62008-09-29 15:18:18 -04005101 * min_trans indicates the oldest transaction that you are interested
5102 * in walking through. Any nodes or leaves older than min_trans are
5103 * skipped over (without reading them).
5104 *
Chris Mason3f157a22008-06-25 16:01:31 -04005105 * returns zero if something useful was found, < 0 on error and 1 if there
5106 * was nothing in the tree that matched the search criteria.
5107 */
5108int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005109 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005110 u64 min_trans)
5111{
5112 struct extent_buffer *cur;
5113 struct btrfs_key found_key;
5114 int slot;
Yan96524802008-07-24 12:19:49 -04005115 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005116 u32 nritems;
5117 int level;
5118 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005119 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005120
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005121 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005122again:
Chris Masonbd681512011-07-16 15:23:14 -04005123 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005124 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005125 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005126 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005127 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005128
5129 if (btrfs_header_generation(cur) < min_trans) {
5130 ret = 1;
5131 goto out;
5132 }
Chris Masond3977122009-01-05 21:25:51 -05005133 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005134 nritems = btrfs_header_nritems(cur);
5135 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005136 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005137
Chris Mason323ac952008-10-01 19:05:46 -04005138 /* at the lowest level, we're done, setup the path and exit */
5139 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005140 if (slot >= nritems)
5141 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005142 ret = 0;
5143 path->slots[level] = slot;
5144 btrfs_item_key_to_cpu(cur, &found_key, slot);
5145 goto out;
5146 }
Yan96524802008-07-24 12:19:49 -04005147 if (sret && slot > 0)
5148 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005149 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005150 * check this node pointer against the min_trans parameters.
5151 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005152 */
Chris Masond3977122009-01-05 21:25:51 -05005153 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005154 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005155
Chris Mason3f157a22008-06-25 16:01:31 -04005156 gen = btrfs_node_ptr_generation(cur, slot);
5157 if (gen < min_trans) {
5158 slot++;
5159 continue;
5160 }
Eric Sandeende78b512013-01-31 18:21:12 +00005161 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005162 }
Chris Masone02119d2008-09-05 16:13:11 -04005163find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005164 /*
5165 * we didn't find a candidate key in this node, walk forward
5166 * and find another one
5167 */
5168 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005169 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005170 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005171 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005172 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005173 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005174 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005175 goto again;
5176 } else {
5177 goto out;
5178 }
5179 }
5180 /* save our key for returning back */
5181 btrfs_node_key_to_cpu(cur, &found_key, slot);
5182 path->slots[level] = slot;
5183 if (level == path->lowest_level) {
5184 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005185 goto out;
5186 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005187 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005188 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005189 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005190
Chris Masonbd681512011-07-16 15:23:14 -04005191 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005192
Chris Masonbd681512011-07-16 15:23:14 -04005193 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005194 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005195 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005196 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005197 }
5198out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005199 path->keep_locks = keep_locks;
5200 if (ret == 0) {
5201 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5202 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005203 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005204 }
Chris Mason3f157a22008-06-25 16:01:31 -04005205 return ret;
5206}
5207
Alexander Block70698302012-06-05 21:07:48 +02005208static void tree_move_down(struct btrfs_root *root,
5209 struct btrfs_path *path,
5210 int *level, int root_level)
5211{
Chris Mason74dd17fb2012-08-07 16:25:13 -04005212 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005213 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5214 path->slots[*level]);
5215 path->slots[*level - 1] = 0;
5216 (*level)--;
5217}
5218
5219static int tree_move_next_or_upnext(struct btrfs_root *root,
5220 struct btrfs_path *path,
5221 int *level, int root_level)
5222{
5223 int ret = 0;
5224 int nritems;
5225 nritems = btrfs_header_nritems(path->nodes[*level]);
5226
5227 path->slots[*level]++;
5228
Chris Mason74dd17fb2012-08-07 16:25:13 -04005229 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005230 if (*level == root_level)
5231 return -1;
5232
5233 /* move upnext */
5234 path->slots[*level] = 0;
5235 free_extent_buffer(path->nodes[*level]);
5236 path->nodes[*level] = NULL;
5237 (*level)++;
5238 path->slots[*level]++;
5239
5240 nritems = btrfs_header_nritems(path->nodes[*level]);
5241 ret = 1;
5242 }
5243 return ret;
5244}
5245
5246/*
5247 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5248 * or down.
5249 */
5250static int tree_advance(struct btrfs_root *root,
5251 struct btrfs_path *path,
5252 int *level, int root_level,
5253 int allow_down,
5254 struct btrfs_key *key)
5255{
5256 int ret;
5257
5258 if (*level == 0 || !allow_down) {
5259 ret = tree_move_next_or_upnext(root, path, level, root_level);
5260 } else {
5261 tree_move_down(root, path, level, root_level);
5262 ret = 0;
5263 }
5264 if (ret >= 0) {
5265 if (*level == 0)
5266 btrfs_item_key_to_cpu(path->nodes[*level], key,
5267 path->slots[*level]);
5268 else
5269 btrfs_node_key_to_cpu(path->nodes[*level], key,
5270 path->slots[*level]);
5271 }
5272 return ret;
5273}
5274
5275static int tree_compare_item(struct btrfs_root *left_root,
5276 struct btrfs_path *left_path,
5277 struct btrfs_path *right_path,
5278 char *tmp_buf)
5279{
5280 int cmp;
5281 int len1, len2;
5282 unsigned long off1, off2;
5283
5284 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5285 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5286 if (len1 != len2)
5287 return 1;
5288
5289 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5290 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5291 right_path->slots[0]);
5292
5293 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5294
5295 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5296 if (cmp)
5297 return 1;
5298 return 0;
5299}
5300
5301#define ADVANCE 1
5302#define ADVANCE_ONLY_NEXT -1
5303
5304/*
5305 * This function compares two trees and calls the provided callback for
5306 * every changed/new/deleted item it finds.
5307 * If shared tree blocks are encountered, whole subtrees are skipped, making
5308 * the compare pretty fast on snapshotted subvolumes.
5309 *
5310 * This currently works on commit roots only. As commit roots are read only,
5311 * we don't do any locking. The commit roots are protected with transactions.
5312 * Transactions are ended and rejoined when a commit is tried in between.
5313 *
5314 * This function checks for modifications done to the trees while comparing.
5315 * If it detects a change, it aborts immediately.
5316 */
5317int btrfs_compare_trees(struct btrfs_root *left_root,
5318 struct btrfs_root *right_root,
5319 btrfs_changed_cb_t changed_cb, void *ctx)
5320{
5321 int ret;
5322 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005323 struct btrfs_path *left_path = NULL;
5324 struct btrfs_path *right_path = NULL;
5325 struct btrfs_key left_key;
5326 struct btrfs_key right_key;
5327 char *tmp_buf = NULL;
5328 int left_root_level;
5329 int right_root_level;
5330 int left_level;
5331 int right_level;
5332 int left_end_reached;
5333 int right_end_reached;
5334 int advance_left;
5335 int advance_right;
5336 u64 left_blockptr;
5337 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005338 u64 left_gen;
5339 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005340
5341 left_path = btrfs_alloc_path();
5342 if (!left_path) {
5343 ret = -ENOMEM;
5344 goto out;
5345 }
5346 right_path = btrfs_alloc_path();
5347 if (!right_path) {
5348 ret = -ENOMEM;
5349 goto out;
5350 }
5351
David Sterba707e8a02014-06-04 19:22:26 +02005352 tmp_buf = kmalloc(left_root->nodesize, GFP_NOFS);
Alexander Block70698302012-06-05 21:07:48 +02005353 if (!tmp_buf) {
5354 ret = -ENOMEM;
5355 goto out;
5356 }
5357
5358 left_path->search_commit_root = 1;
5359 left_path->skip_locking = 1;
5360 right_path->search_commit_root = 1;
5361 right_path->skip_locking = 1;
5362
Alexander Block70698302012-06-05 21:07:48 +02005363 /*
5364 * Strategy: Go to the first items of both trees. Then do
5365 *
5366 * If both trees are at level 0
5367 * Compare keys of current items
5368 * If left < right treat left item as new, advance left tree
5369 * and repeat
5370 * If left > right treat right item as deleted, advance right tree
5371 * and repeat
5372 * If left == right do deep compare of items, treat as changed if
5373 * needed, advance both trees and repeat
5374 * If both trees are at the same level but not at level 0
5375 * Compare keys of current nodes/leafs
5376 * If left < right advance left tree and repeat
5377 * If left > right advance right tree and repeat
5378 * If left == right compare blockptrs of the next nodes/leafs
5379 * If they match advance both trees but stay at the same level
5380 * and repeat
5381 * If they don't match advance both trees while allowing to go
5382 * deeper and repeat
5383 * If tree levels are different
5384 * Advance the tree that needs it and repeat
5385 *
5386 * Advancing a tree means:
5387 * If we are at level 0, try to go to the next slot. If that's not
5388 * possible, go one level up and repeat. Stop when we found a level
5389 * where we could go to the next slot. We may at this point be on a
5390 * node or a leaf.
5391 *
5392 * If we are not at level 0 and not on shared tree blocks, go one
5393 * level deeper.
5394 *
5395 * If we are not at level 0 and on shared tree blocks, go one slot to
5396 * the right if possible or go up and right.
5397 */
5398
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005399 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005400 left_level = btrfs_header_level(left_root->commit_root);
5401 left_root_level = left_level;
5402 left_path->nodes[left_level] = left_root->commit_root;
5403 extent_buffer_get(left_path->nodes[left_level]);
5404
5405 right_level = btrfs_header_level(right_root->commit_root);
5406 right_root_level = right_level;
5407 right_path->nodes[right_level] = right_root->commit_root;
5408 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005409 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005410
5411 if (left_level == 0)
5412 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5413 &left_key, left_path->slots[left_level]);
5414 else
5415 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5416 &left_key, left_path->slots[left_level]);
5417 if (right_level == 0)
5418 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5419 &right_key, right_path->slots[right_level]);
5420 else
5421 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5422 &right_key, right_path->slots[right_level]);
5423
5424 left_end_reached = right_end_reached = 0;
5425 advance_left = advance_right = 0;
5426
5427 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005428 if (advance_left && !left_end_reached) {
5429 ret = tree_advance(left_root, left_path, &left_level,
5430 left_root_level,
5431 advance_left != ADVANCE_ONLY_NEXT,
5432 &left_key);
5433 if (ret < 0)
5434 left_end_reached = ADVANCE;
5435 advance_left = 0;
5436 }
5437 if (advance_right && !right_end_reached) {
5438 ret = tree_advance(right_root, right_path, &right_level,
5439 right_root_level,
5440 advance_right != ADVANCE_ONLY_NEXT,
5441 &right_key);
5442 if (ret < 0)
5443 right_end_reached = ADVANCE;
5444 advance_right = 0;
5445 }
5446
5447 if (left_end_reached && right_end_reached) {
5448 ret = 0;
5449 goto out;
5450 } else if (left_end_reached) {
5451 if (right_level == 0) {
5452 ret = changed_cb(left_root, right_root,
5453 left_path, right_path,
5454 &right_key,
5455 BTRFS_COMPARE_TREE_DELETED,
5456 ctx);
5457 if (ret < 0)
5458 goto out;
5459 }
5460 advance_right = ADVANCE;
5461 continue;
5462 } else if (right_end_reached) {
5463 if (left_level == 0) {
5464 ret = changed_cb(left_root, right_root,
5465 left_path, right_path,
5466 &left_key,
5467 BTRFS_COMPARE_TREE_NEW,
5468 ctx);
5469 if (ret < 0)
5470 goto out;
5471 }
5472 advance_left = ADVANCE;
5473 continue;
5474 }
5475
5476 if (left_level == 0 && right_level == 0) {
5477 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5478 if (cmp < 0) {
5479 ret = changed_cb(left_root, right_root,
5480 left_path, right_path,
5481 &left_key,
5482 BTRFS_COMPARE_TREE_NEW,
5483 ctx);
5484 if (ret < 0)
5485 goto out;
5486 advance_left = ADVANCE;
5487 } else if (cmp > 0) {
5488 ret = changed_cb(left_root, right_root,
5489 left_path, right_path,
5490 &right_key,
5491 BTRFS_COMPARE_TREE_DELETED,
5492 ctx);
5493 if (ret < 0)
5494 goto out;
5495 advance_right = ADVANCE;
5496 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005497 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005498
Chris Mason74dd17fb2012-08-07 16:25:13 -04005499 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005500 ret = tree_compare_item(left_root, left_path,
5501 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005502 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005503 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005504 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005505 result = BTRFS_COMPARE_TREE_SAME;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005506 ret = changed_cb(left_root, right_root,
5507 left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005508 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005509 if (ret < 0)
5510 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005511 advance_left = ADVANCE;
5512 advance_right = ADVANCE;
5513 }
5514 } else if (left_level == right_level) {
5515 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5516 if (cmp < 0) {
5517 advance_left = ADVANCE;
5518 } else if (cmp > 0) {
5519 advance_right = ADVANCE;
5520 } else {
5521 left_blockptr = btrfs_node_blockptr(
5522 left_path->nodes[left_level],
5523 left_path->slots[left_level]);
5524 right_blockptr = btrfs_node_blockptr(
5525 right_path->nodes[right_level],
5526 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005527 left_gen = btrfs_node_ptr_generation(
5528 left_path->nodes[left_level],
5529 left_path->slots[left_level]);
5530 right_gen = btrfs_node_ptr_generation(
5531 right_path->nodes[right_level],
5532 right_path->slots[right_level]);
5533 if (left_blockptr == right_blockptr &&
5534 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005535 /*
5536 * As we're on a shared block, don't
5537 * allow to go deeper.
5538 */
5539 advance_left = ADVANCE_ONLY_NEXT;
5540 advance_right = ADVANCE_ONLY_NEXT;
5541 } else {
5542 advance_left = ADVANCE;
5543 advance_right = ADVANCE;
5544 }
5545 }
5546 } else if (left_level < right_level) {
5547 advance_right = ADVANCE;
5548 } else {
5549 advance_left = ADVANCE;
5550 }
5551 }
5552
5553out:
5554 btrfs_free_path(left_path);
5555 btrfs_free_path(right_path);
5556 kfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005557 return ret;
5558}
5559
Chris Mason3f157a22008-06-25 16:01:31 -04005560/*
5561 * this is similar to btrfs_next_leaf, but does not try to preserve
5562 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005563 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005564 *
5565 * 0 is returned if another key is found, < 0 if there are any errors
5566 * and 1 is returned if there are no higher keys in the tree
5567 *
5568 * path->keep_locks should be set to 1 on the search made before
5569 * calling this function.
5570 */
Chris Masone7a84562008-06-25 16:01:31 -04005571int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005572 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005573{
Chris Masone7a84562008-06-25 16:01:31 -04005574 int slot;
5575 struct extent_buffer *c;
5576
Chris Mason934d3752008-12-08 16:43:10 -05005577 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005578 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005579 if (!path->nodes[level])
5580 return 1;
5581
5582 slot = path->slots[level] + 1;
5583 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005584next:
Chris Masone7a84562008-06-25 16:01:31 -04005585 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005586 int ret;
5587 int orig_lowest;
5588 struct btrfs_key cur_key;
5589 if (level + 1 >= BTRFS_MAX_LEVEL ||
5590 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005591 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005592
5593 if (path->locks[level + 1]) {
5594 level++;
5595 continue;
5596 }
5597
5598 slot = btrfs_header_nritems(c) - 1;
5599 if (level == 0)
5600 btrfs_item_key_to_cpu(c, &cur_key, slot);
5601 else
5602 btrfs_node_key_to_cpu(c, &cur_key, slot);
5603
5604 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005605 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005606 path->lowest_level = level;
5607 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5608 0, 0);
5609 path->lowest_level = orig_lowest;
5610 if (ret < 0)
5611 return ret;
5612
5613 c = path->nodes[level];
5614 slot = path->slots[level];
5615 if (ret == 0)
5616 slot++;
5617 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005618 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005619
Chris Masone7a84562008-06-25 16:01:31 -04005620 if (level == 0)
5621 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005622 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005623 u64 gen = btrfs_node_ptr_generation(c, slot);
5624
Chris Mason3f157a22008-06-25 16:01:31 -04005625 if (gen < min_trans) {
5626 slot++;
5627 goto next;
5628 }
Chris Masone7a84562008-06-25 16:01:31 -04005629 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005630 }
Chris Masone7a84562008-06-25 16:01:31 -04005631 return 0;
5632 }
5633 return 1;
5634}
5635
Chris Mason7bb86312007-12-11 09:25:06 -05005636/*
Chris Mason925baed2008-06-25 16:01:30 -04005637 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005638 * returns 0 if it found something or 1 if there are no greater leaves.
5639 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005640 */
Chris Mason234b63a2007-03-13 10:46:10 -04005641int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005642{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005643 return btrfs_next_old_leaf(root, path, 0);
5644}
5645
5646int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5647 u64 time_seq)
5648{
Chris Masond97e63b2007-02-20 16:40:44 -05005649 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005650 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005651 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005652 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005653 struct btrfs_key key;
5654 u32 nritems;
5655 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005656 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005657 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005658
5659 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005660 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005661 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005662
Chris Mason8e73f272009-04-03 10:14:18 -04005663 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5664again:
5665 level = 1;
5666 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005667 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005668 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005669
Chris Masona2135012008-06-25 16:01:30 -04005670 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005671 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005672
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005673 if (time_seq)
5674 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5675 else
5676 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005677 path->keep_locks = 0;
5678
5679 if (ret < 0)
5680 return ret;
5681
Chris Masona2135012008-06-25 16:01:30 -04005682 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005683 /*
5684 * by releasing the path above we dropped all our locks. A balance
5685 * could have added more items next to the key that used to be
5686 * at the very end of the block. So, check again here and
5687 * advance the path if there are now more items available.
5688 */
Chris Masona2135012008-06-25 16:01:30 -04005689 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005690 if (ret == 0)
5691 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005692 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005693 goto done;
5694 }
Liu Bo0b43e042014-06-09 11:04:49 +08005695 /*
5696 * So the above check misses one case:
5697 * - after releasing the path above, someone has removed the item that
5698 * used to be at the very end of the block, and balance between leafs
5699 * gets another one with bigger key.offset to replace it.
5700 *
5701 * This one should be returned as well, or we can get leaf corruption
5702 * later(esp. in __btrfs_drop_extents()).
5703 *
5704 * And a bit more explanation about this check,
5705 * with ret > 0, the key isn't found, the path points to the slot
5706 * where it should be inserted, so the path->slots[0] item must be the
5707 * bigger one.
5708 */
5709 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5710 ret = 0;
5711 goto done;
5712 }
Chris Masond97e63b2007-02-20 16:40:44 -05005713
Chris Masond3977122009-01-05 21:25:51 -05005714 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005715 if (!path->nodes[level]) {
5716 ret = 1;
5717 goto done;
5718 }
Chris Mason5f39d392007-10-15 16:14:19 -04005719
Chris Masond97e63b2007-02-20 16:40:44 -05005720 slot = path->slots[level] + 1;
5721 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005722 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005723 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005724 if (level == BTRFS_MAX_LEVEL) {
5725 ret = 1;
5726 goto done;
5727 }
Chris Masond97e63b2007-02-20 16:40:44 -05005728 continue;
5729 }
Chris Mason5f39d392007-10-15 16:14:19 -04005730
Chris Mason925baed2008-06-25 16:01:30 -04005731 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005732 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005733 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005734 }
Chris Mason5f39d392007-10-15 16:14:19 -04005735
Chris Mason8e73f272009-04-03 10:14:18 -04005736 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005737 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005738 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005739 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005740 if (ret == -EAGAIN)
5741 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005742
Chris Mason76a05b32009-05-14 13:24:30 -04005743 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005744 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005745 goto done;
5746 }
5747
Chris Mason5cd57b22008-06-25 16:01:30 -04005748 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005749 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005750 if (!ret && time_seq) {
5751 /*
5752 * If we don't get the lock, we may be racing
5753 * with push_leaf_left, holding that lock while
5754 * itself waiting for the leaf we've currently
5755 * locked. To solve this situation, we give up
5756 * on our lock and cycle.
5757 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005758 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005759 btrfs_release_path(path);
5760 cond_resched();
5761 goto again;
5762 }
Chris Mason8e73f272009-04-03 10:14:18 -04005763 if (!ret) {
5764 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005765 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005766 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005767 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005768 }
Chris Mason31533fb2011-07-26 16:01:59 -04005769 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005770 }
Chris Masond97e63b2007-02-20 16:40:44 -05005771 break;
5772 }
5773 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005774 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005775 level--;
5776 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005777 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005778 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005779
Chris Mason5f39d392007-10-15 16:14:19 -04005780 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005781 path->nodes[level] = next;
5782 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005783 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005784 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005785 if (!level)
5786 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005787
Chris Mason8e73f272009-04-03 10:14:18 -04005788 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005789 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005790 if (ret == -EAGAIN)
5791 goto again;
5792
Chris Mason76a05b32009-05-14 13:24:30 -04005793 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005794 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005795 goto done;
5796 }
5797
Chris Mason5cd57b22008-06-25 16:01:30 -04005798 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005799 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005800 if (!ret) {
5801 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005802 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005803 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005804 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005805 }
Chris Mason31533fb2011-07-26 16:01:59 -04005806 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005807 }
Chris Masond97e63b2007-02-20 16:40:44 -05005808 }
Chris Mason8e73f272009-04-03 10:14:18 -04005809 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005810done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005811 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005812 path->leave_spinning = old_spinning;
5813 if (!old_spinning)
5814 btrfs_set_path_blocking(path);
5815
5816 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005817}
Chris Mason0b86a832008-03-24 15:01:56 -04005818
Chris Mason3f157a22008-06-25 16:01:31 -04005819/*
5820 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5821 * searching until it gets past min_objectid or finds an item of 'type'
5822 *
5823 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5824 */
Chris Mason0b86a832008-03-24 15:01:56 -04005825int btrfs_previous_item(struct btrfs_root *root,
5826 struct btrfs_path *path, u64 min_objectid,
5827 int type)
5828{
5829 struct btrfs_key found_key;
5830 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005831 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005832 int ret;
5833
Chris Masond3977122009-01-05 21:25:51 -05005834 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005835 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005836 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005837 ret = btrfs_prev_leaf(root, path);
5838 if (ret != 0)
5839 return ret;
5840 } else {
5841 path->slots[0]--;
5842 }
5843 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005844 nritems = btrfs_header_nritems(leaf);
5845 if (nritems == 0)
5846 return 1;
5847 if (path->slots[0] == nritems)
5848 path->slots[0]--;
5849
Chris Mason0b86a832008-03-24 15:01:56 -04005850 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005851 if (found_key.objectid < min_objectid)
5852 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005853 if (found_key.type == type)
5854 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005855 if (found_key.objectid == min_objectid &&
5856 found_key.type < type)
5857 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005858 }
5859 return 1;
5860}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005861
5862/*
5863 * search in extent tree to find a previous Metadata/Data extent item with
5864 * min objecitd.
5865 *
5866 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5867 */
5868int btrfs_previous_extent_item(struct btrfs_root *root,
5869 struct btrfs_path *path, u64 min_objectid)
5870{
5871 struct btrfs_key found_key;
5872 struct extent_buffer *leaf;
5873 u32 nritems;
5874 int ret;
5875
5876 while (1) {
5877 if (path->slots[0] == 0) {
5878 btrfs_set_path_blocking(path);
5879 ret = btrfs_prev_leaf(root, path);
5880 if (ret != 0)
5881 return ret;
5882 } else {
5883 path->slots[0]--;
5884 }
5885 leaf = path->nodes[0];
5886 nritems = btrfs_header_nritems(leaf);
5887 if (nritems == 0)
5888 return 1;
5889 if (path->slots[0] == nritems)
5890 path->slots[0]--;
5891
5892 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5893 if (found_key.objectid < min_objectid)
5894 break;
5895 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5896 found_key.type == BTRFS_METADATA_ITEM_KEY)
5897 return 0;
5898 if (found_key.objectid == min_objectid &&
5899 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5900 break;
5901 }
5902 return 1;
5903}