blob: 783ea3bac7dc19c371479322f33a3e48c2ef0ee4 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000040static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
41 int level, int slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +000042static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
Jan Schmidtf2304752012-05-26 11:43:17 +020043 struct extent_buffer *eb);
Chris Masond97e63b2007-02-20 16:40:44 -050044
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050048 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040049 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040050}
51
Chris Masonb4ce94d2009-02-04 09:25:08 -050052/*
53 * set all locked nodes in the path to blocking locks. This should
54 * be done before scheduling
55 */
56noinline void btrfs_set_path_blocking(struct btrfs_path *p)
57{
58 int i;
59 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040060 if (!p->nodes[i] || !p->locks[i])
61 continue;
62 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
63 if (p->locks[i] == BTRFS_READ_LOCK)
64 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
65 else if (p->locks[i] == BTRFS_WRITE_LOCK)
66 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050067 }
68}
69
70/*
71 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050072 *
73 * held is used to keep lockdep happy, when lockdep is enabled
74 * we set held to a blocking lock before we go around and
75 * retake all the spinlocks in the path. You can safely use NULL
76 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050077 */
Chris Mason4008c042009-02-12 14:09:45 -050078noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040079 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050080{
81 int i;
Chris Mason4008c042009-02-12 14:09:45 -050082
83#ifdef CONFIG_DEBUG_LOCK_ALLOC
84 /* lockdep really cares that we take all of these spinlocks
85 * in the right order. If any of the locks in the path are not
86 * currently blocking, it is going to complain. So, make really
87 * really sure by forcing the path to blocking before we clear
88 * the path blocking.
89 */
Chris Masonbd681512011-07-16 15:23:14 -040090 if (held) {
91 btrfs_set_lock_blocking_rw(held, held_rw);
92 if (held_rw == BTRFS_WRITE_LOCK)
93 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
94 else if (held_rw == BTRFS_READ_LOCK)
95 held_rw = BTRFS_READ_LOCK_BLOCKING;
96 }
Chris Mason4008c042009-02-12 14:09:45 -050097 btrfs_set_path_blocking(p);
98#endif
99
100 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400101 if (p->nodes[i] && p->locks[i]) {
102 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
103 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
104 p->locks[i] = BTRFS_WRITE_LOCK;
105 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
106 p->locks[i] = BTRFS_READ_LOCK;
107 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500108 }
Chris Mason4008c042009-02-12 14:09:45 -0500109
110#ifdef CONFIG_DEBUG_LOCK_ALLOC
111 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400112 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500113#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400117void btrfs_free_path(struct btrfs_path *p)
118{
Jesper Juhlff175d52010-12-25 21:22:30 +0000119 if (!p)
120 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200121 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400122 kmem_cache_free(btrfs_path_cachep, p);
123}
124
Chris Masond352ac62008-09-29 15:18:18 -0400125/*
126 * path release drops references on the extent buffers in the path
127 * and it drops any locks held by this path
128 *
129 * It is safe to call this on paths that no locks or extent buffers held.
130 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200131noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500132{
133 int i;
Chris Masona2135012008-06-25 16:01:30 -0400134
Chris Mason234b63a2007-03-13 10:46:10 -0400135 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400136 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500137 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400138 continue;
139 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400140 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400141 p->locks[i] = 0;
142 }
Chris Mason5f39d392007-10-15 16:14:19 -0400143 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400144 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500145 }
146}
147
Chris Masond352ac62008-09-29 15:18:18 -0400148/*
149 * safely gets a reference on the root node of a tree. A lock
150 * is not taken, so a concurrent writer may put a different node
151 * at the root of the tree. See btrfs_lock_root_node for the
152 * looping required.
153 *
154 * The extent buffer returned by this has a reference taken, so
155 * it won't disappear. It may stop being the root of the tree
156 * at any time because there are no locks held.
157 */
Chris Mason925baed2008-06-25 16:01:30 -0400158struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
159{
160 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400161
Josef Bacik3083ee22012-03-09 16:01:49 -0500162 while (1) {
163 rcu_read_lock();
164 eb = rcu_dereference(root->node);
165
166 /*
167 * RCU really hurts here, we could free up the root node because
168 * it was cow'ed but we may not get the new root node yet so do
169 * the inc_not_zero dance and if it doesn't work then
170 * synchronize_rcu and try again.
171 */
172 if (atomic_inc_not_zero(&eb->refs)) {
173 rcu_read_unlock();
174 break;
175 }
176 rcu_read_unlock();
177 synchronize_rcu();
178 }
Chris Mason925baed2008-06-25 16:01:30 -0400179 return eb;
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/* loop around taking references on and locking the root node of the
183 * tree until you end up with a lock on the root. A locked buffer
184 * is returned, with a reference held.
185 */
Chris Mason925baed2008-06-25 16:01:30 -0400186struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
187{
188 struct extent_buffer *eb;
189
Chris Masond3977122009-01-05 21:25:51 -0500190 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400191 eb = btrfs_root_node(root);
192 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400193 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400194 break;
Chris Mason925baed2008-06-25 16:01:30 -0400195 btrfs_tree_unlock(eb);
196 free_extent_buffer(eb);
197 }
198 return eb;
199}
200
Chris Masonbd681512011-07-16 15:23:14 -0400201/* loop around taking references on and locking the root node of the
202 * tree until you end up with a lock on the root. A locked buffer
203 * is returned, with a reference held.
204 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000205static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400206{
207 struct extent_buffer *eb;
208
209 while (1) {
210 eb = btrfs_root_node(root);
211 btrfs_tree_read_lock(eb);
212 if (eb == root->node)
213 break;
214 btrfs_tree_read_unlock(eb);
215 free_extent_buffer(eb);
216 }
217 return eb;
218}
219
Chris Masond352ac62008-09-29 15:18:18 -0400220/* cowonly root (everything not a reference counted cow subvolume), just get
221 * put onto a simple dirty list. transaction.c walks this to make sure they
222 * get properly updated on disk.
223 */
Chris Mason0b86a832008-03-24 15:01:56 -0400224static void add_root_to_dirty_list(struct btrfs_root *root)
225{
Chris Masone5846fc2012-05-03 12:08:48 -0400226 spin_lock(&root->fs_info->trans_lock);
Miao Xie27cdeb72014-04-02 19:51:05 +0800227 if (test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state) &&
228 list_empty(&root->dirty_list)) {
Chris Mason0b86a832008-03-24 15:01:56 -0400229 list_add(&root->dirty_list,
230 &root->fs_info->dirty_cowonly_roots);
231 }
Chris Masone5846fc2012-05-03 12:08:48 -0400232 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400233}
234
Chris Masond352ac62008-09-29 15:18:18 -0400235/*
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
239 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500240int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
244{
245 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 int ret = 0;
247 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249
Miao Xie27cdeb72014-04-02 19:51:05 +0800250 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
251 trans->transid != root->fs_info->running_transaction->transid);
252 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
253 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500254
255 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400256 if (level == 0)
257 btrfs_item_key(buf, &disk_key, 0);
258 else
259 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400260
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
262 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200263 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400264 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 return PTR_ERR(cow);
266
267 copy_extent_buffer(cow, buf, 0, 0, cow->len);
268 btrfs_set_header_bytenr(cow, cow->start);
269 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400270 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
271 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
272 BTRFS_HEADER_FLAG_RELOC);
273 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
274 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
275 else
276 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500277
Ross Kirk0a4e5582013-09-24 10:12:38 +0100278 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500279 BTRFS_FSID_SIZE);
280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700283 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700285 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 if (ret)
288 return ret;
289
290 btrfs_mark_buffer_dirty(cow);
291 *cow_ret = cow;
292 return 0;
293}
294
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200295enum mod_log_op {
296 MOD_LOG_KEY_REPLACE,
297 MOD_LOG_KEY_ADD,
298 MOD_LOG_KEY_REMOVE,
299 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
300 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
301 MOD_LOG_MOVE_KEYS,
302 MOD_LOG_ROOT_REPLACE,
303};
304
305struct tree_mod_move {
306 int dst_slot;
307 int nr_items;
308};
309
310struct tree_mod_root {
311 u64 logical;
312 u8 level;
313};
314
315struct tree_mod_elem {
316 struct rb_node node;
317 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200318 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200319 enum mod_log_op op;
320
321 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
322 int slot;
323
324 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
325 u64 generation;
326
327 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
328 struct btrfs_disk_key key;
329 u64 blockptr;
330
331 /* this is used for op == MOD_LOG_MOVE_KEYS */
332 struct tree_mod_move move;
333
334 /* this is used for op == MOD_LOG_ROOT_REPLACE */
335 struct tree_mod_root old_root;
336};
337
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200339{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200340 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200341}
342
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200344{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200345 read_unlock(&fs_info->tree_mod_log_lock);
346}
347
348static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
349{
350 write_lock(&fs_info->tree_mod_log_lock);
351}
352
353static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
354{
355 write_unlock(&fs_info->tree_mod_log_lock);
356}
357
358/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700359 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000360 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700361static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000362{
363 return atomic64_inc_return(&fs_info->tree_mod_seq);
364}
365
366/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200367 * This adds a new blocker to the tree mod log's blocker list if the @elem
368 * passed does not already have a sequence number set. So when a caller expects
369 * to record tree modifications, it should ensure to set elem->seq to zero
370 * before calling btrfs_get_tree_mod_seq.
371 * Returns a fresh, unused tree log modification sequence number, even if no new
372 * blocker was added.
373 */
374u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
375 struct seq_list *elem)
376{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200377 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200379 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700380 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200381 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
382 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200383 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200384 tree_mod_log_write_unlock(fs_info);
385
Josef Bacikfcebe452014-05-13 17:30:47 -0700386 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200387}
388
389void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
390 struct seq_list *elem)
391{
392 struct rb_root *tm_root;
393 struct rb_node *node;
394 struct rb_node *next;
395 struct seq_list *cur_elem;
396 struct tree_mod_elem *tm;
397 u64 min_seq = (u64)-1;
398 u64 seq_putting = elem->seq;
399
400 if (!seq_putting)
401 return;
402
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200403 spin_lock(&fs_info->tree_mod_seq_lock);
404 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200405 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200406
407 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200408 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409 if (seq_putting > cur_elem->seq) {
410 /*
411 * blocker with lower sequence number exists, we
412 * cannot remove anything from the log
413 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200414 spin_unlock(&fs_info->tree_mod_seq_lock);
415 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 }
417 min_seq = cur_elem->seq;
418 }
419 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200420 spin_unlock(&fs_info->tree_mod_seq_lock);
421
422 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200423 * anything that's lower than the lowest existing (read: blocked)
424 * sequence number can be removed from the tree.
425 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200426 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200427 tm_root = &fs_info->tree_mod_log;
428 for (node = rb_first(tm_root); node; node = next) {
429 next = rb_next(node);
430 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200431 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200432 continue;
433 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200434 kfree(tm);
435 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200436 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200437}
438
439/*
440 * key order of the log:
441 * index -> sequence
442 *
443 * the index is the shifted logical of the *new* root node for root replace
444 * operations, or the shifted logical of the affected block for all other
445 * operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000446 *
447 * Note: must be called with write lock (tree_mod_log_write_lock).
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200448 */
449static noinline int
450__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
451{
452 struct rb_root *tm_root;
453 struct rb_node **new;
454 struct rb_node *parent = NULL;
455 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456
Josef Bacikc8cc6342013-07-01 16:18:19 -0400457 BUG_ON(!tm);
458
Josef Bacikfcebe452014-05-13 17:30:47 -0700459 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200461 tm_root = &fs_info->tree_mod_log;
462 new = &tm_root->rb_node;
463 while (*new) {
464 cur = container_of(*new, struct tree_mod_elem, node);
465 parent = *new;
466 if (cur->index < tm->index)
467 new = &((*new)->rb_left);
468 else if (cur->index > tm->index)
469 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200470 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200471 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200472 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200473 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000474 else
475 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200476 }
477
478 rb_link_node(&tm->node, parent, new);
479 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000480 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200481}
482
Jan Schmidt097b8a72012-06-21 11:08:04 +0200483/*
484 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
485 * returns zero with the tree_mod_log_lock acquired. The caller must hold
486 * this until all tree mod log insertions are recorded in the rb tree and then
487 * call tree_mod_log_write_unlock() to release.
488 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200489static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
490 struct extent_buffer *eb) {
491 smp_mb();
492 if (list_empty(&(fs_info)->tree_mod_seq_list))
493 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200494 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200495 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000496
497 tree_mod_log_write_lock(fs_info);
498 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
499 tree_mod_log_write_unlock(fs_info);
500 return 1;
501 }
502
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200503 return 0;
504}
505
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000506/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
507static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
508 struct extent_buffer *eb)
509{
510 smp_mb();
511 if (list_empty(&(fs_info)->tree_mod_seq_list))
512 return 0;
513 if (eb && btrfs_header_level(eb) == 0)
514 return 0;
515
516 return 1;
517}
518
519static struct tree_mod_elem *
520alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
521 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200522{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200523 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200524
Josef Bacikc8cc6342013-07-01 16:18:19 -0400525 tm = kzalloc(sizeof(*tm), flags);
526 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000527 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200528
529 tm->index = eb->start >> PAGE_CACHE_SHIFT;
530 if (op != MOD_LOG_KEY_ADD) {
531 btrfs_node_key(eb, &tm->key, slot);
532 tm->blockptr = btrfs_node_blockptr(eb, slot);
533 }
534 tm->op = op;
535 tm->slot = slot;
536 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000537 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200538
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000539 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200540}
541
542static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400543tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
544 struct extent_buffer *eb, int slot,
545 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200546{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000547 struct tree_mod_elem *tm;
548 int ret;
549
550 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200551 return 0;
552
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000553 tm = alloc_tree_mod_elem(eb, slot, op, flags);
554 if (!tm)
555 return -ENOMEM;
556
557 if (tree_mod_dont_log(fs_info, eb)) {
558 kfree(tm);
559 return 0;
560 }
561
562 ret = __tree_mod_log_insert(fs_info, tm);
563 tree_mod_log_write_unlock(fs_info);
564 if (ret)
565 kfree(tm);
566
567 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200568}
569
570static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200571tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
572 struct extent_buffer *eb, int dst_slot, int src_slot,
573 int nr_items, gfp_t flags)
574{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000575 struct tree_mod_elem *tm = NULL;
576 struct tree_mod_elem **tm_list = NULL;
577 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200578 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000579 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200580
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000581 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200582 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200583
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000584 tm_list = kzalloc(nr_items * sizeof(struct tree_mod_elem *), flags);
585 if (!tm_list)
586 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200587
Josef Bacikc8cc6342013-07-01 16:18:19 -0400588 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000589 if (!tm) {
590 ret = -ENOMEM;
591 goto free_tms;
592 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200593
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200594 tm->index = eb->start >> PAGE_CACHE_SHIFT;
595 tm->slot = src_slot;
596 tm->move.dst_slot = dst_slot;
597 tm->move.nr_items = nr_items;
598 tm->op = MOD_LOG_MOVE_KEYS;
599
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000600 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
601 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
602 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
603 if (!tm_list[i]) {
604 ret = -ENOMEM;
605 goto free_tms;
606 }
607 }
608
609 if (tree_mod_dont_log(fs_info, eb))
610 goto free_tms;
611 locked = 1;
612
613 /*
614 * When we override something during the move, we log these removals.
615 * This can only happen when we move towards the beginning of the
616 * buffer, i.e. dst_slot < src_slot.
617 */
618 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
619 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
620 if (ret)
621 goto free_tms;
622 }
623
624 ret = __tree_mod_log_insert(fs_info, tm);
625 if (ret)
626 goto free_tms;
627 tree_mod_log_write_unlock(fs_info);
628 kfree(tm_list);
629
630 return 0;
631free_tms:
632 for (i = 0; i < nr_items; i++) {
633 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
634 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
635 kfree(tm_list[i]);
636 }
637 if (locked)
638 tree_mod_log_write_unlock(fs_info);
639 kfree(tm_list);
640 kfree(tm);
641
642 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200643}
644
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000645static inline int
646__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
647 struct tree_mod_elem **tm_list,
648 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200649{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000650 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200651 int ret;
652
Jan Schmidt097b8a72012-06-21 11:08:04 +0200653 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000654 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
655 if (ret) {
656 for (j = nritems - 1; j > i; j--)
657 rb_erase(&tm_list[j]->node,
658 &fs_info->tree_mod_log);
659 return ret;
660 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200661 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000662
663 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200664}
665
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200666static noinline int
667tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
668 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000669 struct extent_buffer *new_root, gfp_t flags,
670 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200671{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000672 struct tree_mod_elem *tm = NULL;
673 struct tree_mod_elem **tm_list = NULL;
674 int nritems = 0;
675 int ret = 0;
676 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200677
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000678 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200679 return 0;
680
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000681 if (log_removal && btrfs_header_level(old_root) > 0) {
682 nritems = btrfs_header_nritems(old_root);
683 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
684 flags);
685 if (!tm_list) {
686 ret = -ENOMEM;
687 goto free_tms;
688 }
689 for (i = 0; i < nritems; i++) {
690 tm_list[i] = alloc_tree_mod_elem(old_root, i,
691 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
692 if (!tm_list[i]) {
693 ret = -ENOMEM;
694 goto free_tms;
695 }
696 }
697 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000698
Josef Bacikc8cc6342013-07-01 16:18:19 -0400699 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000700 if (!tm) {
701 ret = -ENOMEM;
702 goto free_tms;
703 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200704
705 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
706 tm->old_root.logical = old_root->start;
707 tm->old_root.level = btrfs_header_level(old_root);
708 tm->generation = btrfs_header_generation(old_root);
709 tm->op = MOD_LOG_ROOT_REPLACE;
710
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000711 if (tree_mod_dont_log(fs_info, NULL))
712 goto free_tms;
713
714 if (tm_list)
715 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
716 if (!ret)
717 ret = __tree_mod_log_insert(fs_info, tm);
718
719 tree_mod_log_write_unlock(fs_info);
720 if (ret)
721 goto free_tms;
722 kfree(tm_list);
723
724 return ret;
725
726free_tms:
727 if (tm_list) {
728 for (i = 0; i < nritems; i++)
729 kfree(tm_list[i]);
730 kfree(tm_list);
731 }
732 kfree(tm);
733
734 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735}
736
737static struct tree_mod_elem *
738__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
739 int smallest)
740{
741 struct rb_root *tm_root;
742 struct rb_node *node;
743 struct tree_mod_elem *cur = NULL;
744 struct tree_mod_elem *found = NULL;
745 u64 index = start >> PAGE_CACHE_SHIFT;
746
Jan Schmidt097b8a72012-06-21 11:08:04 +0200747 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200748 tm_root = &fs_info->tree_mod_log;
749 node = tm_root->rb_node;
750 while (node) {
751 cur = container_of(node, struct tree_mod_elem, node);
752 if (cur->index < index) {
753 node = node->rb_left;
754 } else if (cur->index > index) {
755 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200756 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200757 node = node->rb_left;
758 } else if (!smallest) {
759 /* we want the node with the highest seq */
760 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200761 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200762 found = cur;
763 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200764 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200765 /* we want the node with the smallest seq */
766 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200767 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200768 found = cur;
769 node = node->rb_right;
770 } else {
771 found = cur;
772 break;
773 }
774 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200775 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200776
777 return found;
778}
779
780/*
781 * this returns the element from the log with the smallest time sequence
782 * value that's in the log (the oldest log item). any element with a time
783 * sequence lower than min_seq will be ignored.
784 */
785static struct tree_mod_elem *
786tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
787 u64 min_seq)
788{
789 return __tree_mod_log_search(fs_info, start, min_seq, 1);
790}
791
792/*
793 * this returns the element from the log with the largest time sequence
794 * value that's in the log (the most recent log item). any element with
795 * a time sequence lower than min_seq will be ignored.
796 */
797static struct tree_mod_elem *
798tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
799{
800 return __tree_mod_log_search(fs_info, start, min_seq, 0);
801}
802
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000803static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200804tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
805 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000806 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200807{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000808 int ret = 0;
809 struct tree_mod_elem **tm_list = NULL;
810 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200811 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000812 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200813
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000814 if (!tree_mod_need_log(fs_info, NULL))
815 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200816
Josef Bacikc8cc6342013-07-01 16:18:19 -0400817 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000818 return 0;
819
820 tm_list = kzalloc(nr_items * 2 * sizeof(struct tree_mod_elem *),
821 GFP_NOFS);
822 if (!tm_list)
823 return -ENOMEM;
824
825 tm_list_add = tm_list;
826 tm_list_rem = tm_list + nr_items;
827 for (i = 0; i < nr_items; i++) {
828 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
829 MOD_LOG_KEY_REMOVE, GFP_NOFS);
830 if (!tm_list_rem[i]) {
831 ret = -ENOMEM;
832 goto free_tms;
833 }
834
835 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
836 MOD_LOG_KEY_ADD, GFP_NOFS);
837 if (!tm_list_add[i]) {
838 ret = -ENOMEM;
839 goto free_tms;
840 }
841 }
842
843 if (tree_mod_dont_log(fs_info, NULL))
844 goto free_tms;
845 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200846
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200847 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000848 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
849 if (ret)
850 goto free_tms;
851 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
852 if (ret)
853 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200854 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000855
856 tree_mod_log_write_unlock(fs_info);
857 kfree(tm_list);
858
859 return 0;
860
861free_tms:
862 for (i = 0; i < nr_items * 2; i++) {
863 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
864 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
865 kfree(tm_list[i]);
866 }
867 if (locked)
868 tree_mod_log_write_unlock(fs_info);
869 kfree(tm_list);
870
871 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200872}
873
874static inline void
875tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
876 int dst_offset, int src_offset, int nr_items)
877{
878 int ret;
879 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
880 nr_items, GFP_NOFS);
881 BUG_ON(ret < 0);
882}
883
Jan Schmidt097b8a72012-06-21 11:08:04 +0200884static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200885tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000886 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200887{
888 int ret;
889
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000890 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400891 MOD_LOG_KEY_REPLACE,
892 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200893 BUG_ON(ret < 0);
894}
895
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000896static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200897tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200898{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000899 struct tree_mod_elem **tm_list = NULL;
900 int nritems = 0;
901 int i;
902 int ret = 0;
903
904 if (btrfs_header_level(eb) == 0)
905 return 0;
906
907 if (!tree_mod_need_log(fs_info, NULL))
908 return 0;
909
910 nritems = btrfs_header_nritems(eb);
911 tm_list = kzalloc(nritems * sizeof(struct tree_mod_elem *),
912 GFP_NOFS);
913 if (!tm_list)
914 return -ENOMEM;
915
916 for (i = 0; i < nritems; i++) {
917 tm_list[i] = alloc_tree_mod_elem(eb, i,
918 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
919 if (!tm_list[i]) {
920 ret = -ENOMEM;
921 goto free_tms;
922 }
923 }
924
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200925 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000926 goto free_tms;
927
928 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
929 tree_mod_log_write_unlock(fs_info);
930 if (ret)
931 goto free_tms;
932 kfree(tm_list);
933
934 return 0;
935
936free_tms:
937 for (i = 0; i < nritems; i++)
938 kfree(tm_list[i]);
939 kfree(tm_list);
940
941 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200942}
943
Jan Schmidt097b8a72012-06-21 11:08:04 +0200944static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200945tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000946 struct extent_buffer *new_root_node,
947 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200948{
949 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200950 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000951 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200952 BUG_ON(ret < 0);
953}
954
Chris Masond352ac62008-09-29 15:18:18 -0400955/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 * check if the tree block can be shared by multiple trees
957 */
958int btrfs_block_can_be_shared(struct btrfs_root *root,
959 struct extent_buffer *buf)
960{
961 /*
962 * Tree blocks not in refernece counted trees and tree roots
963 * are never shared. If a block was allocated after the last
964 * snapshot and the block was not allocated by tree relocation,
965 * we know the block is not shared.
966 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800967 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 buf != root->node && buf != root->commit_root &&
969 (btrfs_header_generation(buf) <=
970 btrfs_root_last_snapshot(&root->root_item) ||
971 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
972 return 1;
973#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800974 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
976 return 1;
977#endif
978 return 0;
979}
980
981static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
982 struct btrfs_root *root,
983 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400984 struct extent_buffer *cow,
985 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986{
987 u64 refs;
988 u64 owner;
989 u64 flags;
990 u64 new_flags = 0;
991 int ret;
992
993 /*
994 * Backrefs update rules:
995 *
996 * Always use full backrefs for extent pointers in tree block
997 * allocated by tree relocation.
998 *
999 * If a shared tree block is no longer referenced by its owner
1000 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
1001 * use full backrefs for extent pointers in tree block.
1002 *
1003 * If a tree block is been relocating
1004 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
1005 * use full backrefs for extent pointers in tree block.
1006 * The reason for this is some operations (such as drop tree)
1007 * are only allowed for blocks use full backrefs.
1008 */
1009
1010 if (btrfs_block_can_be_shared(root, buf)) {
1011 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -05001012 btrfs_header_level(buf), 1,
1013 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001014 if (ret)
1015 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -07001016 if (refs == 0) {
1017 ret = -EROFS;
1018 btrfs_std_error(root->fs_info, ret);
1019 return ret;
1020 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 } else {
1022 refs = 1;
1023 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1024 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1025 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1026 else
1027 flags = 0;
1028 }
1029
1030 owner = btrfs_header_owner(buf);
1031 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1032 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1033
1034 if (refs > 1) {
1035 if ((owner == root->root_key.objectid ||
1036 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1037 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001038 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040
1041 if (root->root_key.objectid ==
1042 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001043 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001044 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001045 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001046 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 }
1048 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1049 } else {
1050
1051 if (root->root_key.objectid ==
1052 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001053 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001055 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001056 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001057 }
1058 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001059 int level = btrfs_header_level(buf);
1060
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001061 ret = btrfs_set_disk_extent_flags(trans, root,
1062 buf->start,
1063 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001064 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001065 if (ret)
1066 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001067 }
1068 } else {
1069 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1070 if (root->root_key.objectid ==
1071 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001072 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001073 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001074 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001075 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001076 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001077 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 }
1079 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001080 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081 }
1082 return 0;
1083}
1084
1085/*
Chris Masond3977122009-01-05 21:25:51 -05001086 * does the dirty work in cow of a single block. The parent block (if
1087 * supplied) is updated to point to the new cow copy. The new buffer is marked
1088 * dirty and returned locked. If you modify the block it needs to be marked
1089 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001090 *
1091 * search_start -- an allocation hint for the new block
1092 *
Chris Masond3977122009-01-05 21:25:51 -05001093 * empty_size -- a hint that you plan on doing more cow. This is the size in
1094 * bytes the allocator should try to find free next to the block it returns.
1095 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001096 */
Chris Masond3977122009-01-05 21:25:51 -05001097static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001098 struct btrfs_root *root,
1099 struct extent_buffer *buf,
1100 struct extent_buffer *parent, int parent_slot,
1101 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001102 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001103{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001105 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001106 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001107 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001108 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001110
Chris Mason925baed2008-06-25 16:01:30 -04001111 if (*cow_ret == buf)
1112 unlock_orig = 1;
1113
Chris Masonb9447ef82009-03-09 11:45:38 -04001114 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001115
Miao Xie27cdeb72014-04-02 19:51:05 +08001116 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1117 trans->transid != root->fs_info->running_transaction->transid);
1118 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1119 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001120
Chris Mason7bb86312007-12-11 09:25:06 -05001121 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001122
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001123 if (level == 0)
1124 btrfs_item_key(buf, &disk_key, 0);
1125 else
1126 btrfs_node_key(buf, &disk_key, 0);
1127
1128 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1129 if (parent)
1130 parent_start = parent->start;
1131 else
1132 parent_start = 0;
1133 } else
1134 parent_start = 0;
1135
1136 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1137 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001138 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001139 if (IS_ERR(cow))
1140 return PTR_ERR(cow);
1141
Chris Masonb4ce94d2009-02-04 09:25:08 -05001142 /* cow is set to blocking by btrfs_init_new_buffer */
1143
Chris Mason5f39d392007-10-15 16:14:19 -04001144 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001145 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001146 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1148 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1149 BTRFS_HEADER_FLAG_RELOC);
1150 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1151 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1152 else
1153 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001154
Ross Kirk0a4e5582013-09-24 10:12:38 +01001155 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001156 BTRFS_FSID_SIZE);
1157
Mark Fashehbe1a5562011-08-08 13:20:18 -07001158 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001159 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001160 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001161 return ret;
1162 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001163
Miao Xie27cdeb72014-04-02 19:51:05 +08001164 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001165 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1166 if (ret)
1167 return ret;
1168 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001169
Chris Mason6702ed42007-08-07 16:15:09 -04001170 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001171 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001172 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1173 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1174 parent_start = buf->start;
1175 else
1176 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001177
Chris Mason5f39d392007-10-15 16:14:19 -04001178 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001179 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001180 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001181
Yan, Zhengf0486c62010-05-16 10:46:25 -04001182 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001183 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001185 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001186 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1188 parent_start = parent->start;
1189 else
1190 parent_start = 0;
1191
1192 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001193 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001194 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001195 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001196 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001197 btrfs_set_node_ptr_generation(parent, parent_slot,
1198 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001199 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001200 if (last_ref) {
1201 ret = tree_mod_log_free_eb(root->fs_info, buf);
1202 if (ret) {
1203 btrfs_abort_transaction(trans, root, ret);
1204 return ret;
1205 }
1206 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001207 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001208 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001209 }
Chris Mason925baed2008-06-25 16:01:30 -04001210 if (unlock_orig)
1211 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001212 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001213 btrfs_mark_buffer_dirty(cow);
1214 *cow_ret = cow;
1215 return 0;
1216}
1217
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001218/*
1219 * returns the logical address of the oldest predecessor of the given root.
1220 * entries older than time_seq are ignored.
1221 */
1222static struct tree_mod_elem *
1223__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001224 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001225{
1226 struct tree_mod_elem *tm;
1227 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001228 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001229 int looped = 0;
1230
1231 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001232 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001233
1234 /*
1235 * the very last operation that's logged for a root is the replacement
1236 * operation (if it is replaced at all). this has the index of the *new*
1237 * root, making it the very first operation that's logged for this root.
1238 */
1239 while (1) {
1240 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1241 time_seq);
1242 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001243 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001244 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001245 * if there are no tree operation for the oldest root, we simply
1246 * return it. this should only happen if that (old) root is at
1247 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001248 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001249 if (!tm)
1250 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001251
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001252 /*
1253 * if there's an operation that's not a root replacement, we
1254 * found the oldest version of our root. normally, we'll find a
1255 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1256 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001257 if (tm->op != MOD_LOG_ROOT_REPLACE)
1258 break;
1259
1260 found = tm;
1261 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001262 looped = 1;
1263 }
1264
Jan Schmidta95236d2012-06-05 16:41:24 +02001265 /* if there's no old root to return, return what we found instead */
1266 if (!found)
1267 found = tm;
1268
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001269 return found;
1270}
1271
1272/*
1273 * tm is a pointer to the first operation to rewind within eb. then, all
1274 * previous operations will be rewinded (until we reach something older than
1275 * time_seq).
1276 */
1277static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001278__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1279 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280{
1281 u32 n;
1282 struct rb_node *next;
1283 struct tree_mod_elem *tm = first_tm;
1284 unsigned long o_dst;
1285 unsigned long o_src;
1286 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1287
1288 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001289 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001290 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001291 /*
1292 * all the operations are recorded with the operator used for
1293 * the modification. as we're going backwards, we do the
1294 * opposite of each operation here.
1295 */
1296 switch (tm->op) {
1297 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1298 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001299 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001300 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001301 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001302 btrfs_set_node_key(eb, &tm->key, tm->slot);
1303 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1304 btrfs_set_node_ptr_generation(eb, tm->slot,
1305 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001306 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307 break;
1308 case MOD_LOG_KEY_REPLACE:
1309 BUG_ON(tm->slot >= n);
1310 btrfs_set_node_key(eb, &tm->key, tm->slot);
1311 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1312 btrfs_set_node_ptr_generation(eb, tm->slot,
1313 tm->generation);
1314 break;
1315 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001316 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001317 n--;
1318 break;
1319 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001320 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1321 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1322 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001323 tm->move.nr_items * p_size);
1324 break;
1325 case MOD_LOG_ROOT_REPLACE:
1326 /*
1327 * this operation is special. for roots, this must be
1328 * handled explicitly before rewinding.
1329 * for non-roots, this operation may exist if the node
1330 * was a root: root A -> child B; then A gets empty and
1331 * B is promoted to the new root. in the mod log, we'll
1332 * have a root-replace operation for B, a tree block
1333 * that is no root. we simply ignore that operation.
1334 */
1335 break;
1336 }
1337 next = rb_next(&tm->node);
1338 if (!next)
1339 break;
1340 tm = container_of(next, struct tree_mod_elem, node);
1341 if (tm->index != first_tm->index)
1342 break;
1343 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001344 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001345 btrfs_set_header_nritems(eb, n);
1346}
1347
Jan Schmidt47fb0912013-04-13 13:19:55 +00001348/*
1349 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1350 * is returned. If rewind operations happen, a fresh buffer is returned. The
1351 * returned buffer is always read-locked. If the returned buffer is not the
1352 * input buffer, the lock on the input buffer is released and the input buffer
1353 * is freed (its refcount is decremented).
1354 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001355static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001356tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1357 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001358{
1359 struct extent_buffer *eb_rewin;
1360 struct tree_mod_elem *tm;
1361
1362 if (!time_seq)
1363 return eb;
1364
1365 if (btrfs_header_level(eb) == 0)
1366 return eb;
1367
1368 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1369 if (!tm)
1370 return eb;
1371
Josef Bacik9ec72672013-08-07 16:57:23 -04001372 btrfs_set_path_blocking(path);
1373 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1374
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001375 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1376 BUG_ON(tm->slot != 0);
1377 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1378 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001379 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001380 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001381 free_extent_buffer(eb);
1382 return NULL;
1383 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001384 btrfs_set_header_bytenr(eb_rewin, eb->start);
1385 btrfs_set_header_backref_rev(eb_rewin,
1386 btrfs_header_backref_rev(eb));
1387 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001388 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389 } else {
1390 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001391 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001392 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001393 free_extent_buffer(eb);
1394 return NULL;
1395 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001396 }
1397
Josef Bacik9ec72672013-08-07 16:57:23 -04001398 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1399 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001400 free_extent_buffer(eb);
1401
Jan Schmidt47fb0912013-04-13 13:19:55 +00001402 extent_buffer_get(eb_rewin);
1403 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001404 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001405 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001406 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001407
1408 return eb_rewin;
1409}
1410
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001411/*
1412 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1413 * value. If there are no changes, the current root->root_node is returned. If
1414 * anything changed in between, there's a fresh buffer allocated on which the
1415 * rewind operations are done. In any case, the returned buffer is read locked.
1416 * Returns NULL on error (with no locks held).
1417 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001418static inline struct extent_buffer *
1419get_old_root(struct btrfs_root *root, u64 time_seq)
1420{
1421 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001422 struct extent_buffer *eb = NULL;
1423 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001424 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001425 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001426 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001427 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001428 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001429
Jan Schmidt30b04632013-04-13 13:19:54 +00001430 eb_root = btrfs_read_lock_root_node(root);
1431 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001432 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001433 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001434
Jan Schmidta95236d2012-06-05 16:41:24 +02001435 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1436 old_root = &tm->old_root;
1437 old_generation = tm->generation;
1438 logical = old_root->logical;
1439 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001440 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001441 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001442
Jan Schmidta95236d2012-06-05 16:41:24 +02001443 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001444 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001445 btrfs_tree_read_unlock(eb_root);
1446 free_extent_buffer(eb_root);
David Sterba707e8a02014-06-04 19:22:26 +02001447 blocksize = root->nodesize;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001448 old = read_tree_block(root, logical, blocksize, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301449 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001450 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001451 btrfs_warn(root->fs_info,
1452 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001453 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001454 eb = btrfs_clone_extent_buffer(old);
1455 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001456 }
1457 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001458 btrfs_tree_read_unlock(eb_root);
1459 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001460 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001461 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001462 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001463 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001464 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001465 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001466 }
1467
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001468 if (!eb)
1469 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001470 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001471 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001472 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001473 btrfs_set_header_bytenr(eb, eb->start);
1474 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001475 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001476 btrfs_set_header_level(eb, old_root->level);
1477 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001478 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001479 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001480 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001481 else
1482 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001483 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001484
1485 return eb;
1486}
1487
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001488int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1489{
1490 struct tree_mod_elem *tm;
1491 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001492 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001493
Jan Schmidt30b04632013-04-13 13:19:54 +00001494 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001495 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1496 level = tm->old_root.level;
1497 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001498 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001499 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001500 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001501
1502 return level;
1503}
1504
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001505static inline int should_cow_block(struct btrfs_trans_handle *trans,
1506 struct btrfs_root *root,
1507 struct extent_buffer *buf)
1508{
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001509#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1510 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
1511 return 0;
1512#endif
Liu Bof1ebcc72011-11-14 20:48:06 -05001513 /* ensure we can see the force_cow */
1514 smp_rmb();
1515
1516 /*
1517 * We do not need to cow a block if
1518 * 1) this block is not created or changed in this transaction;
1519 * 2) this block does not belong to TREE_RELOC tree;
1520 * 3) the root is not forced COW.
1521 *
1522 * What is forced COW:
1523 * when we create snapshot during commiting the transaction,
1524 * after we've finished coping src root, we must COW the shared
1525 * block to ensure the metadata consistency.
1526 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527 if (btrfs_header_generation(buf) == trans->transid &&
1528 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1529 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001530 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001531 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001532 return 0;
1533 return 1;
1534}
1535
Chris Masond352ac62008-09-29 15:18:18 -04001536/*
1537 * cows a single block, see __btrfs_cow_block for the real work.
1538 * This version of it has extra checks so that a block isn't cow'd more than
1539 * once per transaction, as long as it hasn't been written yet
1540 */
Chris Masond3977122009-01-05 21:25:51 -05001541noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001542 struct btrfs_root *root, struct extent_buffer *buf,
1543 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001544 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001545{
Chris Mason6702ed42007-08-07 16:15:09 -04001546 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001547 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001548
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001549 if (trans->transaction != root->fs_info->running_transaction)
1550 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001551 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001552 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001553
1554 if (trans->transid != root->fs_info->generation)
1555 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001556 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001557
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001558 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001559 *cow_ret = buf;
1560 return 0;
1561 }
Chris Masonc4876852009-02-04 09:24:25 -05001562
Chris Mason0b86a832008-03-24 15:01:56 -04001563 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001564
1565 if (parent)
1566 btrfs_set_lock_blocking(parent);
1567 btrfs_set_lock_blocking(buf);
1568
Chris Masonf510cfe2007-10-15 16:14:48 -04001569 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001570 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001571
1572 trace_btrfs_cow_block(root, buf, *cow_ret);
1573
Chris Masonf510cfe2007-10-15 16:14:48 -04001574 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001575}
1576
Chris Masond352ac62008-09-29 15:18:18 -04001577/*
1578 * helper function for defrag to decide if two blocks pointed to by a
1579 * node are actually close by
1580 */
Chris Mason6b800532007-10-15 16:17:34 -04001581static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001582{
Chris Mason6b800532007-10-15 16:17:34 -04001583 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001584 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001585 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001586 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001587 return 0;
1588}
1589
Chris Mason081e9572007-11-06 10:26:24 -05001590/*
1591 * compare two keys in a memcmp fashion
1592 */
1593static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1594{
1595 struct btrfs_key k1;
1596
1597 btrfs_disk_key_to_cpu(&k1, disk);
1598
Diego Calleja20736ab2009-07-24 11:06:52 -04001599 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001600}
1601
Josef Bacikf3465ca2008-11-12 14:19:50 -05001602/*
1603 * same as comp_keys only with two btrfs_key's
1604 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001605int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001606{
1607 if (k1->objectid > k2->objectid)
1608 return 1;
1609 if (k1->objectid < k2->objectid)
1610 return -1;
1611 if (k1->type > k2->type)
1612 return 1;
1613 if (k1->type < k2->type)
1614 return -1;
1615 if (k1->offset > k2->offset)
1616 return 1;
1617 if (k1->offset < k2->offset)
1618 return -1;
1619 return 0;
1620}
Chris Mason081e9572007-11-06 10:26:24 -05001621
Chris Masond352ac62008-09-29 15:18:18 -04001622/*
1623 * this is used by the defrag code to go through all the
1624 * leaves pointed to by a node and reallocate them so that
1625 * disk order is close to key order
1626 */
Chris Mason6702ed42007-08-07 16:15:09 -04001627int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001628 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001629 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001630 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001631{
Chris Mason6b800532007-10-15 16:17:34 -04001632 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001633 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001634 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001635 u64 search_start = *last_ret;
1636 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001637 u64 other;
1638 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001639 int end_slot;
1640 int i;
1641 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001642 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001643 int uptodate;
1644 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001645 int progress_passed = 0;
1646 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001647
Chris Mason5708b952007-10-25 15:43:18 -04001648 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001649
Julia Lawall6c1500f2012-11-03 20:30:18 +00001650 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1651 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001652
Chris Mason6b800532007-10-15 16:17:34 -04001653 parent_nritems = btrfs_header_nritems(parent);
David Sterba707e8a02014-06-04 19:22:26 +02001654 blocksize = root->nodesize;
Chris Mason6702ed42007-08-07 16:15:09 -04001655 end_slot = parent_nritems;
1656
1657 if (parent_nritems == 1)
1658 return 0;
1659
Chris Masonb4ce94d2009-02-04 09:25:08 -05001660 btrfs_set_lock_blocking(parent);
1661
Chris Mason6702ed42007-08-07 16:15:09 -04001662 for (i = start_slot; i < end_slot; i++) {
1663 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001664
Chris Mason081e9572007-11-06 10:26:24 -05001665 btrfs_node_key(parent, &disk_key, i);
1666 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1667 continue;
1668
1669 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001670 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001671 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001672 if (last_block == 0)
1673 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001674
Chris Mason6702ed42007-08-07 16:15:09 -04001675 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001676 other = btrfs_node_blockptr(parent, i - 1);
1677 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001678 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001679 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001680 other = btrfs_node_blockptr(parent, i + 1);
1681 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001682 }
Chris Masone9d0b132007-08-10 14:06:19 -04001683 if (close) {
1684 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001685 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001686 }
Chris Mason6702ed42007-08-07 16:15:09 -04001687
Chris Mason6b800532007-10-15 16:17:34 -04001688 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1689 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001690 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001691 else
1692 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001693 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001694 if (!cur) {
1695 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001696 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001697 if (!cur || !extent_buffer_uptodate(cur)) {
1698 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001699 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001700 }
Chris Mason6b800532007-10-15 16:17:34 -04001701 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001702 err = btrfs_read_buffer(cur, gen);
1703 if (err) {
1704 free_extent_buffer(cur);
1705 return err;
1706 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001707 }
Chris Mason6702ed42007-08-07 16:15:09 -04001708 }
Chris Masone9d0b132007-08-10 14:06:19 -04001709 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001710 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001711
Chris Masone7a84562008-06-25 16:01:31 -04001712 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001713 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001714 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001715 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001716 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001717 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001718 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001719 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001720 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001721 break;
Yan252c38f2007-08-29 09:11:44 -04001722 }
Chris Masone7a84562008-06-25 16:01:31 -04001723 search_start = cur->start;
1724 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001725 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001726 btrfs_tree_unlock(cur);
1727 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001728 }
1729 return err;
1730}
1731
Chris Mason74123bd2007-02-02 11:05:29 -05001732/*
1733 * The leaf data grows from end-to-front in the node.
1734 * this returns the address of the start of the last item,
1735 * which is the stop of the leaf data stack
1736 */
Chris Mason123abc82007-03-14 14:14:43 -04001737static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001738 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739{
Chris Mason5f39d392007-10-15 16:14:19 -04001740 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001741 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001742 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001743 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001744}
1745
Chris Masonaa5d6be2007-02-28 16:35:06 -05001746
Chris Mason74123bd2007-02-02 11:05:29 -05001747/*
Chris Mason5f39d392007-10-15 16:14:19 -04001748 * search for key in the extent_buffer. The items start at offset p,
1749 * and they are item_size apart. There are 'max' items in p.
1750 *
Chris Mason74123bd2007-02-02 11:05:29 -05001751 * the slot in the array is returned via slot, and it points to
1752 * the place where you would insert key if it is not found in
1753 * the array.
1754 *
1755 * slot may point to max if the key is bigger than all of the keys
1756 */
Chris Masone02119d2008-09-05 16:13:11 -04001757static noinline int generic_bin_search(struct extent_buffer *eb,
1758 unsigned long p,
1759 int item_size, struct btrfs_key *key,
1760 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001761{
1762 int low = 0;
1763 int high = max;
1764 int mid;
1765 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001766 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001767 struct btrfs_disk_key unaligned;
1768 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001769 char *kaddr = NULL;
1770 unsigned long map_start = 0;
1771 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001772 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773
Chris Masond3977122009-01-05 21:25:51 -05001774 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001775 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001776 offset = p + mid * item_size;
1777
Chris Masona6591712011-07-19 12:04:14 -04001778 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001779 (offset + sizeof(struct btrfs_disk_key)) >
1780 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001781
1782 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001783 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001784 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001785
Chris Mason479965d2007-10-15 16:14:27 -04001786 if (!err) {
1787 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1788 map_start);
1789 } else {
1790 read_extent_buffer(eb, &unaligned,
1791 offset, sizeof(unaligned));
1792 tmp = &unaligned;
1793 }
1794
Chris Mason5f39d392007-10-15 16:14:19 -04001795 } else {
1796 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1797 map_start);
1798 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001799 ret = comp_keys(tmp, key);
1800
1801 if (ret < 0)
1802 low = mid + 1;
1803 else if (ret > 0)
1804 high = mid;
1805 else {
1806 *slot = mid;
1807 return 0;
1808 }
1809 }
1810 *slot = low;
1811 return 1;
1812}
1813
Chris Mason97571fd2007-02-24 13:39:08 -05001814/*
1815 * simple bin_search frontend that does the right thing for
1816 * leaves vs nodes
1817 */
Chris Mason5f39d392007-10-15 16:14:19 -04001818static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1819 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001820{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001821 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001822 return generic_bin_search(eb,
1823 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001824 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001825 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001826 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001827 else
Chris Mason5f39d392007-10-15 16:14:19 -04001828 return generic_bin_search(eb,
1829 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001830 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001831 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001832 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001833}
1834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1836 int level, int *slot)
1837{
1838 return bin_search(eb, key, level, slot);
1839}
1840
Yan, Zhengf0486c62010-05-16 10:46:25 -04001841static void root_add_used(struct btrfs_root *root, u32 size)
1842{
1843 spin_lock(&root->accounting_lock);
1844 btrfs_set_root_used(&root->root_item,
1845 btrfs_root_used(&root->root_item) + size);
1846 spin_unlock(&root->accounting_lock);
1847}
1848
1849static void root_sub_used(struct btrfs_root *root, u32 size)
1850{
1851 spin_lock(&root->accounting_lock);
1852 btrfs_set_root_used(&root->root_item,
1853 btrfs_root_used(&root->root_item) - size);
1854 spin_unlock(&root->accounting_lock);
1855}
1856
Chris Masond352ac62008-09-29 15:18:18 -04001857/* given a node and slot number, this reads the blocks it points to. The
1858 * extent buffer is returned with a reference taken (but unlocked).
1859 * NULL is returned on error.
1860 */
Chris Masone02119d2008-09-05 16:13:11 -04001861static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001862 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001863{
Chris Masonca7a79a2008-05-12 12:59:19 -04001864 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001865 struct extent_buffer *eb;
1866
Chris Masonbb803952007-03-01 12:04:21 -05001867 if (slot < 0)
1868 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001869 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001870 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001871
1872 BUG_ON(level == 0);
1873
Josef Bacik416bc652013-04-23 14:17:42 -04001874 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
David Sterba707e8a02014-06-04 19:22:26 +02001875 root->nodesize,
Josef Bacik416bc652013-04-23 14:17:42 -04001876 btrfs_node_ptr_generation(parent, slot));
1877 if (eb && !extent_buffer_uptodate(eb)) {
1878 free_extent_buffer(eb);
1879 eb = NULL;
1880 }
1881
1882 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001883}
1884
Chris Masond352ac62008-09-29 15:18:18 -04001885/*
1886 * node level balancing, used to make sure nodes are in proper order for
1887 * item deletion. We balance from the top down, so we have to make sure
1888 * that a deletion won't leave an node completely empty later on.
1889 */
Chris Masone02119d2008-09-05 16:13:11 -04001890static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001891 struct btrfs_root *root,
1892 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001893{
Chris Mason5f39d392007-10-15 16:14:19 -04001894 struct extent_buffer *right = NULL;
1895 struct extent_buffer *mid;
1896 struct extent_buffer *left = NULL;
1897 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001898 int ret = 0;
1899 int wret;
1900 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001901 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001902 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001903
1904 if (level == 0)
1905 return 0;
1906
Chris Mason5f39d392007-10-15 16:14:19 -04001907 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001908
Chris Masonbd681512011-07-16 15:23:14 -04001909 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1910 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001911 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1912
Chris Mason1d4f8a02007-03-13 09:28:32 -04001913 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001914
Li Zefana05a9bb2011-09-06 16:55:34 +08001915 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001916 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001917 pslot = path->slots[level + 1];
1918 }
Chris Masonbb803952007-03-01 12:04:21 -05001919
Chris Mason40689472007-03-17 14:29:23 -04001920 /*
1921 * deal with the case where there is only one pointer in the root
1922 * by promoting the node below to a root
1923 */
Chris Mason5f39d392007-10-15 16:14:19 -04001924 if (!parent) {
1925 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001926
Chris Mason5f39d392007-10-15 16:14:19 -04001927 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001928 return 0;
1929
1930 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001931 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001932 if (!child) {
1933 ret = -EROFS;
1934 btrfs_std_error(root->fs_info, ret);
1935 goto enospc;
1936 }
1937
Chris Mason925baed2008-06-25 16:01:30 -04001938 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001939 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001940 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001941 if (ret) {
1942 btrfs_tree_unlock(child);
1943 free_extent_buffer(child);
1944 goto enospc;
1945 }
Yan2f375ab2008-02-01 14:58:07 -05001946
Jan Schmidt90f8d622013-04-13 13:19:53 +00001947 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001948 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001949
Chris Mason0b86a832008-03-24 15:01:56 -04001950 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001951 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001952
Chris Mason925baed2008-06-25 16:01:30 -04001953 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001954 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001955 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001956 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001957 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001958 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001959
1960 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001961 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001962 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001963 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001964 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001965 }
Chris Mason5f39d392007-10-15 16:14:19 -04001966 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001967 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001968 return 0;
1969
Chris Mason5f39d392007-10-15 16:14:19 -04001970 left = read_node_slot(root, parent, pslot - 1);
1971 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001972 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001973 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001974 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001975 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001976 if (wret) {
1977 ret = wret;
1978 goto enospc;
1979 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001980 }
Chris Mason5f39d392007-10-15 16:14:19 -04001981 right = read_node_slot(root, parent, pslot + 1);
1982 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001983 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001984 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001985 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001986 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001987 if (wret) {
1988 ret = wret;
1989 goto enospc;
1990 }
1991 }
1992
1993 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001994 if (left) {
1995 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001996 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001997 if (wret < 0)
1998 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001999 }
Chris Mason79f95c82007-03-01 15:16:26 -05002000
2001 /*
2002 * then try to empty the right most buffer into the middle
2003 */
Chris Mason5f39d392007-10-15 16:14:19 -04002004 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04002005 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002006 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05002007 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002008 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002009 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002010 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002011 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002012 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002013 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002014 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002015 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05002016 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002017 struct btrfs_disk_key right_key;
2018 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002019 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002020 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002021 btrfs_set_node_key(parent, &right_key, pslot + 1);
2022 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002023 }
2024 }
Chris Mason5f39d392007-10-15 16:14:19 -04002025 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002026 /*
2027 * we're not allowed to leave a node with one item in the
2028 * tree during a delete. A deletion from lower in the tree
2029 * could try to delete the only pointer in this node.
2030 * So, pull some keys from the left.
2031 * There has to be a left pointer at this point because
2032 * otherwise we would have pulled some pointers from the
2033 * right
2034 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002035 if (!left) {
2036 ret = -EROFS;
2037 btrfs_std_error(root->fs_info, ret);
2038 goto enospc;
2039 }
Chris Mason5f39d392007-10-15 16:14:19 -04002040 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002041 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002042 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002043 goto enospc;
2044 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002045 if (wret == 1) {
2046 wret = push_node_left(trans, root, left, mid, 1);
2047 if (wret < 0)
2048 ret = wret;
2049 }
Chris Mason79f95c82007-03-01 15:16:26 -05002050 BUG_ON(wret == 1);
2051 }
Chris Mason5f39d392007-10-15 16:14:19 -04002052 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002053 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002054 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002055 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002056 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002057 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002058 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002059 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002060 } else {
2061 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002062 struct btrfs_disk_key mid_key;
2063 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002064 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002065 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_node_key(parent, &mid_key, pslot);
2067 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002068 }
Chris Masonbb803952007-03-01 12:04:21 -05002069
Chris Mason79f95c82007-03-01 15:16:26 -05002070 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002071 if (left) {
2072 if (btrfs_header_nritems(left) > orig_slot) {
2073 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002074 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002075 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002076 path->slots[level + 1] -= 1;
2077 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002078 if (mid) {
2079 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002080 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002081 }
Chris Masonbb803952007-03-01 12:04:21 -05002082 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002083 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002084 path->slots[level] = orig_slot;
2085 }
2086 }
Chris Mason79f95c82007-03-01 15:16:26 -05002087 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002088 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002090 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002091enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002092 if (right) {
2093 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002094 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002095 }
2096 if (left) {
2097 if (path->nodes[level] != left)
2098 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002100 }
Chris Masonbb803952007-03-01 12:04:21 -05002101 return ret;
2102}
2103
Chris Masond352ac62008-09-29 15:18:18 -04002104/* Node balancing for insertion. Here we only split or push nodes around
2105 * when they are completely full. This is also done top down, so we
2106 * have to be pessimistic.
2107 */
Chris Masond3977122009-01-05 21:25:51 -05002108static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002109 struct btrfs_root *root,
2110 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002111{
Chris Mason5f39d392007-10-15 16:14:19 -04002112 struct extent_buffer *right = NULL;
2113 struct extent_buffer *mid;
2114 struct extent_buffer *left = NULL;
2115 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002116 int ret = 0;
2117 int wret;
2118 int pslot;
2119 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002120
2121 if (level == 0)
2122 return 1;
2123
Chris Mason5f39d392007-10-15 16:14:19 -04002124 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002125 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002126
Li Zefana05a9bb2011-09-06 16:55:34 +08002127 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002128 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002129 pslot = path->slots[level + 1];
2130 }
Chris Masone66f7092007-04-20 13:16:02 -04002131
Chris Mason5f39d392007-10-15 16:14:19 -04002132 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002133 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002134
Chris Mason5f39d392007-10-15 16:14:19 -04002135 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002136
2137 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002138 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002139 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002140
2141 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002142 btrfs_set_lock_blocking(left);
2143
Chris Mason5f39d392007-10-15 16:14:19 -04002144 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002145 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2146 wret = 1;
2147 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002148 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002149 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002150 if (ret)
2151 wret = 1;
2152 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002153 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002154 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002155 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002156 }
Chris Masone66f7092007-04-20 13:16:02 -04002157 if (wret < 0)
2158 ret = wret;
2159 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002160 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002161 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002162 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002163 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002164 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 btrfs_set_node_key(parent, &disk_key, pslot);
2166 btrfs_mark_buffer_dirty(parent);
2167 if (btrfs_header_nritems(left) > orig_slot) {
2168 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002169 path->slots[level + 1] -= 1;
2170 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002171 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002172 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002173 } else {
2174 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002175 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002176 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002177 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002178 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002179 }
Chris Masone66f7092007-04-20 13:16:02 -04002180 return 0;
2181 }
Chris Mason925baed2008-06-25 16:01:30 -04002182 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002183 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002184 }
Chris Mason925baed2008-06-25 16:01:30 -04002185 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002186
2187 /*
2188 * then try to empty the right most buffer into the middle
2189 */
Chris Mason5f39d392007-10-15 16:14:19 -04002190 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002191 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002192
Chris Mason925baed2008-06-25 16:01:30 -04002193 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002194 btrfs_set_lock_blocking(right);
2195
Chris Mason5f39d392007-10-15 16:14:19 -04002196 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002197 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2198 wret = 1;
2199 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002200 ret = btrfs_cow_block(trans, root, right,
2201 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002202 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002203 if (ret)
2204 wret = 1;
2205 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002206 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002207 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002208 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002209 }
Chris Masone66f7092007-04-20 13:16:02 -04002210 if (wret < 0)
2211 ret = wret;
2212 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002213 struct btrfs_disk_key disk_key;
2214
2215 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002216 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002217 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002218 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2219 btrfs_mark_buffer_dirty(parent);
2220
2221 if (btrfs_header_nritems(mid) <= orig_slot) {
2222 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002223 path->slots[level + 1] += 1;
2224 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002225 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002226 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002227 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002228 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002229 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002230 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002231 }
Chris Masone66f7092007-04-20 13:16:02 -04002232 return 0;
2233 }
Chris Mason925baed2008-06-25 16:01:30 -04002234 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002235 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002236 }
Chris Masone66f7092007-04-20 13:16:02 -04002237 return 1;
2238}
2239
Chris Mason74123bd2007-02-02 11:05:29 -05002240/*
Chris Masond352ac62008-09-29 15:18:18 -04002241 * readahead one full node of leaves, finding things that are close
2242 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002243 */
Chris Masonc8c42862009-04-03 10:14:18 -04002244static void reada_for_search(struct btrfs_root *root,
2245 struct btrfs_path *path,
2246 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002247{
Chris Mason5f39d392007-10-15 16:14:19 -04002248 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002249 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002250 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002251 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002252 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002253 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002254 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002255 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002256 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002257 u32 nr;
2258 u32 blocksize;
2259 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002260
Chris Masona6b6e752007-10-15 16:22:39 -04002261 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002262 return;
2263
Chris Mason6702ed42007-08-07 16:15:09 -04002264 if (!path->nodes[level])
2265 return;
2266
Chris Mason5f39d392007-10-15 16:14:19 -04002267 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002268
Chris Mason3c69fae2007-08-07 15:52:22 -04002269 search = btrfs_node_blockptr(node, slot);
David Sterba707e8a02014-06-04 19:22:26 +02002270 blocksize = root->nodesize;
Chris Mason6b800532007-10-15 16:17:34 -04002271 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002272 if (eb) {
2273 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002274 return;
2275 }
2276
Chris Masona7175312009-01-22 09:23:10 -05002277 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002278
Chris Mason5f39d392007-10-15 16:14:19 -04002279 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002280 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002281
Chris Masond3977122009-01-05 21:25:51 -05002282 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002283 if (direction < 0) {
2284 if (nr == 0)
2285 break;
2286 nr--;
2287 } else if (direction > 0) {
2288 nr++;
2289 if (nr >= nritems)
2290 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002291 }
Chris Mason01f46652007-12-21 16:24:26 -05002292 if (path->reada < 0 && objectid) {
2293 btrfs_node_key(node, &disk_key, nr);
2294 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2295 break;
2296 }
Chris Mason6b800532007-10-15 16:17:34 -04002297 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002298 if ((search <= target && target - search <= 65536) ||
2299 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002300 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002301 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002302 nread += blocksize;
2303 }
2304 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002305 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002306 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002307 }
2308}
Chris Mason925baed2008-06-25 16:01:30 -04002309
Josef Bacik0b088512013-06-17 14:23:02 -04002310static noinline void reada_for_balance(struct btrfs_root *root,
2311 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002312{
2313 int slot;
2314 int nritems;
2315 struct extent_buffer *parent;
2316 struct extent_buffer *eb;
2317 u64 gen;
2318 u64 block1 = 0;
2319 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002320 int blocksize;
2321
Chris Mason8c594ea2009-04-20 15:50:10 -04002322 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002323 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002324 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002325
2326 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002327 slot = path->slots[level + 1];
David Sterba707e8a02014-06-04 19:22:26 +02002328 blocksize = root->nodesize;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002329
2330 if (slot > 0) {
2331 block1 = btrfs_node_blockptr(parent, slot - 1);
2332 gen = btrfs_node_ptr_generation(parent, slot - 1);
2333 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002334 /*
2335 * if we get -eagain from btrfs_buffer_uptodate, we
2336 * don't want to return eagain here. That will loop
2337 * forever
2338 */
2339 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002340 block1 = 0;
2341 free_extent_buffer(eb);
2342 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002343 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002344 block2 = btrfs_node_blockptr(parent, slot + 1);
2345 gen = btrfs_node_ptr_generation(parent, slot + 1);
2346 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002347 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002348 block2 = 0;
2349 free_extent_buffer(eb);
2350 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002351
Josef Bacik0b088512013-06-17 14:23:02 -04002352 if (block1)
2353 readahead_tree_block(root, block1, blocksize, 0);
2354 if (block2)
2355 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002356}
2357
2358
2359/*
Chris Masond3977122009-01-05 21:25:51 -05002360 * when we walk down the tree, it is usually safe to unlock the higher layers
2361 * in the tree. The exceptions are when our path goes through slot 0, because
2362 * operations on the tree might require changing key pointers higher up in the
2363 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002364 *
Chris Masond3977122009-01-05 21:25:51 -05002365 * callers might also have set path->keep_locks, which tells this code to keep
2366 * the lock if the path points to the last slot in the block. This is part of
2367 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002368 *
Chris Masond3977122009-01-05 21:25:51 -05002369 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2370 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002371 */
Chris Masone02119d2008-09-05 16:13:11 -04002372static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002373 int lowest_unlock, int min_write_lock_level,
2374 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002375{
2376 int i;
2377 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002378 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002379 struct extent_buffer *t;
2380
2381 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2382 if (!path->nodes[i])
2383 break;
2384 if (!path->locks[i])
2385 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002386 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002387 skip_level = i + 1;
2388 continue;
2389 }
Chris Mason051e1b92008-06-25 16:01:30 -04002390 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002391 u32 nritems;
2392 t = path->nodes[i];
2393 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002394 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002395 skip_level = i + 1;
2396 continue;
2397 }
2398 }
Chris Mason051e1b92008-06-25 16:01:30 -04002399 if (skip_level < i && i >= lowest_unlock)
2400 no_skips = 1;
2401
Chris Mason925baed2008-06-25 16:01:30 -04002402 t = path->nodes[i];
2403 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002404 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002405 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002406 if (write_lock_level &&
2407 i > min_write_lock_level &&
2408 i <= *write_lock_level) {
2409 *write_lock_level = i - 1;
2410 }
Chris Mason925baed2008-06-25 16:01:30 -04002411 }
2412 }
2413}
2414
Chris Mason3c69fae2007-08-07 15:52:22 -04002415/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002416 * This releases any locks held in the path starting at level and
2417 * going all the way up to the root.
2418 *
2419 * btrfs_search_slot will keep the lock held on higher nodes in a few
2420 * corner cases, such as COW of the block at slot zero in the node. This
2421 * ignores those rules, and it should only be called when there are no
2422 * more updates to be done higher up in the tree.
2423 */
2424noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2425{
2426 int i;
2427
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002428 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002429 return;
2430
2431 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2432 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002433 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002434 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002435 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002436 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002437 path->locks[i] = 0;
2438 }
2439}
2440
2441/*
Chris Masonc8c42862009-04-03 10:14:18 -04002442 * helper function for btrfs_search_slot. The goal is to find a block
2443 * in cache without setting the path to blocking. If we find the block
2444 * we return zero and the path is unchanged.
2445 *
2446 * If we can't find the block, we set the path blocking and do some
2447 * reada. -EAGAIN is returned and the search must be repeated.
2448 */
2449static int
2450read_block_for_search(struct btrfs_trans_handle *trans,
2451 struct btrfs_root *root, struct btrfs_path *p,
2452 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002453 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002454{
2455 u64 blocknr;
2456 u64 gen;
2457 u32 blocksize;
2458 struct extent_buffer *b = *eb_ret;
2459 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002460 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002461
2462 blocknr = btrfs_node_blockptr(b, slot);
2463 gen = btrfs_node_ptr_generation(b, slot);
David Sterba707e8a02014-06-04 19:22:26 +02002464 blocksize = root->nodesize;
Chris Masonc8c42862009-04-03 10:14:18 -04002465
2466 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002467 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002468 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002469 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2470 *eb_ret = tmp;
2471 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002472 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002473
2474 /* the pages were up to date, but we failed
2475 * the generation number check. Do a full
2476 * read for the generation number that is correct.
2477 * We must do this without dropping locks so
2478 * we can trust our generation number
2479 */
2480 btrfs_set_path_blocking(p);
2481
2482 /* now we're allowed to do a blocking uptodate check */
2483 ret = btrfs_read_buffer(tmp, gen);
2484 if (!ret) {
2485 *eb_ret = tmp;
2486 return 0;
2487 }
2488 free_extent_buffer(tmp);
2489 btrfs_release_path(p);
2490 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002491 }
2492
2493 /*
2494 * reduce lock contention at high levels
2495 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002496 * we read. Don't release the lock on the current
2497 * level because we need to walk this node to figure
2498 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002499 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002500 btrfs_unlock_up_safe(p, level + 1);
2501 btrfs_set_path_blocking(p);
2502
Chris Masoncb449212010-10-24 11:01:27 -04002503 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002504 if (p->reada)
2505 reada_for_search(root, p, level, slot, key->objectid);
2506
David Sterbab3b4aa72011-04-21 01:20:15 +02002507 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002508
2509 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002510 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002511 if (tmp) {
2512 /*
2513 * If the read above didn't mark this buffer up to date,
2514 * it will never end up being up to date. Set ret to EIO now
2515 * and give up so that our caller doesn't loop forever
2516 * on our EAGAINs.
2517 */
Chris Masonb9fab912012-05-06 07:23:47 -04002518 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002519 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002520 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002521 }
2522 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002523}
2524
2525/*
2526 * helper function for btrfs_search_slot. This does all of the checks
2527 * for node-level blocks and does any balancing required based on
2528 * the ins_len.
2529 *
2530 * If no extra work was required, zero is returned. If we had to
2531 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2532 * start over
2533 */
2534static int
2535setup_nodes_for_search(struct btrfs_trans_handle *trans,
2536 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002537 struct extent_buffer *b, int level, int ins_len,
2538 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002539{
2540 int ret;
2541 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2542 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2543 int sret;
2544
Chris Masonbd681512011-07-16 15:23:14 -04002545 if (*write_lock_level < level + 1) {
2546 *write_lock_level = level + 1;
2547 btrfs_release_path(p);
2548 goto again;
2549 }
2550
Chris Masonc8c42862009-04-03 10:14:18 -04002551 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002552 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002553 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002554 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002555
2556 BUG_ON(sret > 0);
2557 if (sret) {
2558 ret = sret;
2559 goto done;
2560 }
2561 b = p->nodes[level];
2562 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002563 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002564 int sret;
2565
Chris Masonbd681512011-07-16 15:23:14 -04002566 if (*write_lock_level < level + 1) {
2567 *write_lock_level = level + 1;
2568 btrfs_release_path(p);
2569 goto again;
2570 }
2571
Chris Masonc8c42862009-04-03 10:14:18 -04002572 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002573 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002574 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002575 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002576
2577 if (sret) {
2578 ret = sret;
2579 goto done;
2580 }
2581 b = p->nodes[level];
2582 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002583 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002584 goto again;
2585 }
2586 BUG_ON(btrfs_header_nritems(b) == 1);
2587 }
2588 return 0;
2589
2590again:
2591 ret = -EAGAIN;
2592done:
2593 return ret;
2594}
2595
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002596static void key_search_validate(struct extent_buffer *b,
2597 struct btrfs_key *key,
2598 int level)
2599{
2600#ifdef CONFIG_BTRFS_ASSERT
2601 struct btrfs_disk_key disk_key;
2602
2603 btrfs_cpu_key_to_disk(&disk_key, key);
2604
2605 if (level == 0)
2606 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2607 offsetof(struct btrfs_leaf, items[0].key),
2608 sizeof(disk_key)));
2609 else
2610 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2611 offsetof(struct btrfs_node, ptrs[0].key),
2612 sizeof(disk_key)));
2613#endif
2614}
2615
2616static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2617 int level, int *prev_cmp, int *slot)
2618{
2619 if (*prev_cmp != 0) {
2620 *prev_cmp = bin_search(b, key, level, slot);
2621 return *prev_cmp;
2622 }
2623
2624 key_search_validate(b, key, level);
2625 *slot = 0;
2626
2627 return 0;
2628}
2629
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002630int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002631 u64 iobjectid, u64 ioff, u8 key_type,
2632 struct btrfs_key *found_key)
2633{
2634 int ret;
2635 struct btrfs_key key;
2636 struct extent_buffer *eb;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002637 struct btrfs_path *path;
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002638
2639 key.type = key_type;
2640 key.objectid = iobjectid;
2641 key.offset = ioff;
2642
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002643 if (found_path == NULL) {
2644 path = btrfs_alloc_path();
2645 if (!path)
2646 return -ENOMEM;
2647 } else
2648 path = found_path;
2649
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002650 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002651 if ((ret < 0) || (found_key == NULL)) {
2652 if (path != found_path)
2653 btrfs_free_path(path);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002654 return ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002655 }
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002656
2657 eb = path->nodes[0];
2658 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2659 ret = btrfs_next_leaf(fs_root, path);
2660 if (ret)
2661 return ret;
2662 eb = path->nodes[0];
2663 }
2664
2665 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2666 if (found_key->type != key.type ||
2667 found_key->objectid != key.objectid)
2668 return 1;
2669
2670 return 0;
2671}
2672
Chris Masonc8c42862009-04-03 10:14:18 -04002673/*
Chris Mason74123bd2007-02-02 11:05:29 -05002674 * look for key in the tree. path is filled in with nodes along the way
2675 * if key is found, we return zero and you can find the item in the leaf
2676 * level of the path (level 0)
2677 *
2678 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002679 * be inserted, and 1 is returned. If there are other errors during the
2680 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002681 *
2682 * if ins_len > 0, nodes and leaves will be split as we walk down the
2683 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2684 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002685 */
Chris Masone089f052007-03-16 16:20:31 -04002686int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2687 *root, struct btrfs_key *key, struct btrfs_path *p, int
2688 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689{
Chris Mason5f39d392007-10-15 16:14:19 -04002690 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002691 int slot;
2692 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002693 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002694 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002695 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002696 int root_lock;
2697 /* everything at write_lock_level or lower must be write locked */
2698 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002699 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002700 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002701 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002702
Chris Mason6702ed42007-08-07 16:15:09 -04002703 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002704 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002705 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002706 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002707
Chris Masonbd681512011-07-16 15:23:14 -04002708 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002709 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002710
Chris Masonbd681512011-07-16 15:23:14 -04002711 /* when we are removing items, we might have to go up to level
2712 * two as we update tree pointers Make sure we keep write
2713 * for those levels as well
2714 */
2715 write_lock_level = 2;
2716 } else if (ins_len > 0) {
2717 /*
2718 * for inserting items, make sure we have a write lock on
2719 * level 1 so we can update keys
2720 */
2721 write_lock_level = 1;
2722 }
2723
2724 if (!cow)
2725 write_lock_level = -1;
2726
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002727 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002728 write_lock_level = BTRFS_MAX_LEVEL;
2729
Chris Masonf7c79f32012-03-19 15:54:38 -04002730 min_write_lock_level = write_lock_level;
2731
Chris Masonbb803952007-03-01 12:04:21 -05002732again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002733 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002734 /*
2735 * we try very hard to do read locks on the root
2736 */
2737 root_lock = BTRFS_READ_LOCK;
2738 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002739 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002740 /*
2741 * the commit roots are read only
2742 * so we always do read locks
2743 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002744 if (p->need_commit_sem)
2745 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002746 b = root->commit_root;
2747 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002748 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002749 if (p->need_commit_sem)
2750 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002751 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002752 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002753 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002754 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002755 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002756 level = btrfs_header_level(b);
2757 } else {
2758 /* we don't know the level of the root node
2759 * until we actually have it read locked
2760 */
2761 b = btrfs_read_lock_root_node(root);
2762 level = btrfs_header_level(b);
2763 if (level <= write_lock_level) {
2764 /* whoops, must trade for write lock */
2765 btrfs_tree_read_unlock(b);
2766 free_extent_buffer(b);
2767 b = btrfs_lock_root_node(root);
2768 root_lock = BTRFS_WRITE_LOCK;
2769
2770 /* the level might have changed, check again */
2771 level = btrfs_header_level(b);
2772 }
2773 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002774 }
Chris Masonbd681512011-07-16 15:23:14 -04002775 p->nodes[level] = b;
2776 if (!p->skip_locking)
2777 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002778
Chris Masoneb60cea2007-02-02 09:18:22 -05002779 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002780 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002781
2782 /*
2783 * setup the path here so we can release it under lock
2784 * contention with the cow code
2785 */
Chris Mason02217ed2007-03-02 16:08:05 -05002786 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002787 /*
2788 * if we don't really need to cow this block
2789 * then we don't want to set the path blocking,
2790 * so we test it here
2791 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002792 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002793 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002794
Chris Masonbd681512011-07-16 15:23:14 -04002795 /*
2796 * must have write locks on this node and the
2797 * parent
2798 */
Josef Bacik5124e002012-11-07 13:44:13 -05002799 if (level > write_lock_level ||
2800 (level + 1 > write_lock_level &&
2801 level + 1 < BTRFS_MAX_LEVEL &&
2802 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002803 write_lock_level = level + 1;
2804 btrfs_release_path(p);
2805 goto again;
2806 }
2807
Filipe Manana160f4082014-07-28 19:37:17 +01002808 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002809 err = btrfs_cow_block(trans, root, b,
2810 p->nodes[level + 1],
2811 p->slots[level + 1], &b);
2812 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002813 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002814 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002815 }
Chris Mason02217ed2007-03-02 16:08:05 -05002816 }
Chris Mason65b51a02008-08-01 15:11:20 -04002817cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002818 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002819 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002820
2821 /*
2822 * we have a lock on b and as long as we aren't changing
2823 * the tree, there is no way to for the items in b to change.
2824 * It is safe to drop the lock on our parent before we
2825 * go through the expensive btree search on b.
2826 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002827 * If we're inserting or deleting (ins_len != 0), then we might
2828 * be changing slot zero, which may require changing the parent.
2829 * So, we can't drop the lock until after we know which slot
2830 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002831 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002832 if (!ins_len && !p->keep_locks) {
2833 int u = level + 1;
2834
2835 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2836 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2837 p->locks[u] = 0;
2838 }
2839 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002840
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002841 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002842
Chris Mason5f39d392007-10-15 16:14:19 -04002843 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002844 int dec = 0;
2845 if (ret && slot > 0) {
2846 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002847 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002848 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002849 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002850 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002851 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002852 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002853 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002854 if (err) {
2855 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002856 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002857 }
Chris Masonc8c42862009-04-03 10:14:18 -04002858 b = p->nodes[level];
2859 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002860
Chris Masonbd681512011-07-16 15:23:14 -04002861 /*
2862 * slot 0 is special, if we change the key
2863 * we have to update the parent pointer
2864 * which means we must have a write lock
2865 * on the parent
2866 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002867 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002868 write_lock_level < level + 1) {
2869 write_lock_level = level + 1;
2870 btrfs_release_path(p);
2871 goto again;
2872 }
2873
Chris Masonf7c79f32012-03-19 15:54:38 -04002874 unlock_up(p, level, lowest_unlock,
2875 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002876
Chris Mason925baed2008-06-25 16:01:30 -04002877 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002878 if (dec)
2879 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002880 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002881 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002882
Yan Zheng33c66f42009-07-22 09:59:00 -04002883 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002884 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002885 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002886 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002887 if (err) {
2888 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002889 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002890 }
Chris Mason76a05b32009-05-14 13:24:30 -04002891
Chris Masonb4ce94d2009-02-04 09:25:08 -05002892 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002893 level = btrfs_header_level(b);
2894 if (level <= write_lock_level) {
2895 err = btrfs_try_tree_write_lock(b);
2896 if (!err) {
2897 btrfs_set_path_blocking(p);
2898 btrfs_tree_lock(b);
2899 btrfs_clear_path_blocking(p, b,
2900 BTRFS_WRITE_LOCK);
2901 }
2902 p->locks[level] = BTRFS_WRITE_LOCK;
2903 } else {
2904 err = btrfs_try_tree_read_lock(b);
2905 if (!err) {
2906 btrfs_set_path_blocking(p);
2907 btrfs_tree_read_lock(b);
2908 btrfs_clear_path_blocking(p, b,
2909 BTRFS_READ_LOCK);
2910 }
2911 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002912 }
Chris Masonbd681512011-07-16 15:23:14 -04002913 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002914 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002915 } else {
2916 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002917 if (ins_len > 0 &&
2918 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002919 if (write_lock_level < 1) {
2920 write_lock_level = 1;
2921 btrfs_release_path(p);
2922 goto again;
2923 }
2924
Chris Masonb4ce94d2009-02-04 09:25:08 -05002925 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002926 err = split_leaf(trans, root, key,
2927 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002928 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002929
Yan Zheng33c66f42009-07-22 09:59:00 -04002930 BUG_ON(err > 0);
2931 if (err) {
2932 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002933 goto done;
2934 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002935 }
Chris Mason459931e2008-12-10 09:10:46 -05002936 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002937 unlock_up(p, level, lowest_unlock,
2938 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002939 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002940 }
2941 }
Chris Mason65b51a02008-08-01 15:11:20 -04002942 ret = 1;
2943done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002944 /*
2945 * we don't really know what they plan on doing with the path
2946 * from here on, so for now just mark it as blocking
2947 */
Chris Masonb9473432009-03-13 11:00:37 -04002948 if (!p->leave_spinning)
2949 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002950 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002951 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002952 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002953}
2954
Chris Mason74123bd2007-02-02 11:05:29 -05002955/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002956 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2957 * current state of the tree together with the operations recorded in the tree
2958 * modification log to search for the key in a previous version of this tree, as
2959 * denoted by the time_seq parameter.
2960 *
2961 * Naturally, there is no support for insert, delete or cow operations.
2962 *
2963 * The resulting path and return value will be set up as if we called
2964 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2965 */
2966int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2967 struct btrfs_path *p, u64 time_seq)
2968{
2969 struct extent_buffer *b;
2970 int slot;
2971 int ret;
2972 int err;
2973 int level;
2974 int lowest_unlock = 1;
2975 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002976 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002977
2978 lowest_level = p->lowest_level;
2979 WARN_ON(p->nodes[0] != NULL);
2980
2981 if (p->search_commit_root) {
2982 BUG_ON(time_seq);
2983 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2984 }
2985
2986again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002987 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002988 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002989 p->locks[level] = BTRFS_READ_LOCK;
2990
2991 while (b) {
2992 level = btrfs_header_level(b);
2993 p->nodes[level] = b;
2994 btrfs_clear_path_blocking(p, NULL, 0);
2995
2996 /*
2997 * we have a lock on b and as long as we aren't changing
2998 * the tree, there is no way to for the items in b to change.
2999 * It is safe to drop the lock on our parent before we
3000 * go through the expensive btree search on b.
3001 */
3002 btrfs_unlock_up_safe(p, level + 1);
3003
Josef Bacikd4b40872013-09-24 14:09:34 -04003004 /*
3005 * Since we can unwind eb's we want to do a real search every
3006 * time.
3007 */
3008 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01003009 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003010
3011 if (level != 0) {
3012 int dec = 0;
3013 if (ret && slot > 0) {
3014 dec = 1;
3015 slot -= 1;
3016 }
3017 p->slots[level] = slot;
3018 unlock_up(p, level, lowest_unlock, 0, NULL);
3019
3020 if (level == lowest_level) {
3021 if (dec)
3022 p->slots[level]++;
3023 goto done;
3024 }
3025
3026 err = read_block_for_search(NULL, root, p, &b, level,
3027 slot, key, time_seq);
3028 if (err == -EAGAIN)
3029 goto again;
3030 if (err) {
3031 ret = err;
3032 goto done;
3033 }
3034
3035 level = btrfs_header_level(b);
3036 err = btrfs_try_tree_read_lock(b);
3037 if (!err) {
3038 btrfs_set_path_blocking(p);
3039 btrfs_tree_read_lock(b);
3040 btrfs_clear_path_blocking(p, b,
3041 BTRFS_READ_LOCK);
3042 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003043 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003044 if (!b) {
3045 ret = -ENOMEM;
3046 goto done;
3047 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003048 p->locks[level] = BTRFS_READ_LOCK;
3049 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003050 } else {
3051 p->slots[level] = slot;
3052 unlock_up(p, level, lowest_unlock, 0, NULL);
3053 goto done;
3054 }
3055 }
3056 ret = 1;
3057done:
3058 if (!p->leave_spinning)
3059 btrfs_set_path_blocking(p);
3060 if (ret < 0)
3061 btrfs_release_path(p);
3062
3063 return ret;
3064}
3065
3066/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003067 * helper to use instead of search slot if no exact match is needed but
3068 * instead the next or previous item should be returned.
3069 * When find_higher is true, the next higher item is returned, the next lower
3070 * otherwise.
3071 * When return_any and find_higher are both true, and no higher item is found,
3072 * return the next lower instead.
3073 * When return_any is true and find_higher is false, and no lower item is found,
3074 * return the next higher instead.
3075 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3076 * < 0 on error
3077 */
3078int btrfs_search_slot_for_read(struct btrfs_root *root,
3079 struct btrfs_key *key, struct btrfs_path *p,
3080 int find_higher, int return_any)
3081{
3082 int ret;
3083 struct extent_buffer *leaf;
3084
3085again:
3086 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3087 if (ret <= 0)
3088 return ret;
3089 /*
3090 * a return value of 1 means the path is at the position where the
3091 * item should be inserted. Normally this is the next bigger item,
3092 * but in case the previous item is the last in a leaf, path points
3093 * to the first free slot in the previous leaf, i.e. at an invalid
3094 * item.
3095 */
3096 leaf = p->nodes[0];
3097
3098 if (find_higher) {
3099 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3100 ret = btrfs_next_leaf(root, p);
3101 if (ret <= 0)
3102 return ret;
3103 if (!return_any)
3104 return 1;
3105 /*
3106 * no higher item found, return the next
3107 * lower instead
3108 */
3109 return_any = 0;
3110 find_higher = 0;
3111 btrfs_release_path(p);
3112 goto again;
3113 }
3114 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003115 if (p->slots[0] == 0) {
3116 ret = btrfs_prev_leaf(root, p);
3117 if (ret < 0)
3118 return ret;
3119 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003120 leaf = p->nodes[0];
3121 if (p->slots[0] == btrfs_header_nritems(leaf))
3122 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003123 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003124 }
Arne Jansene6793762011-09-13 11:18:10 +02003125 if (!return_any)
3126 return 1;
3127 /*
3128 * no lower item found, return the next
3129 * higher instead
3130 */
3131 return_any = 0;
3132 find_higher = 1;
3133 btrfs_release_path(p);
3134 goto again;
3135 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003136 --p->slots[0];
3137 }
3138 }
3139 return 0;
3140}
3141
3142/*
Chris Mason74123bd2007-02-02 11:05:29 -05003143 * adjust the pointers going up the tree, starting at level
3144 * making sure the right key of each node is points to 'key'.
3145 * This is used after shifting pointers to the left, so it stops
3146 * fixing up pointers when a given leaf/node is not in slot 0 of the
3147 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003148 *
Chris Mason74123bd2007-02-02 11:05:29 -05003149 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003150static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003151 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003152{
3153 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003154 struct extent_buffer *t;
3155
Chris Mason234b63a2007-03-13 10:46:10 -04003156 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003157 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003158 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003159 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003160 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00003161 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003162 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003163 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003164 if (tslot != 0)
3165 break;
3166 }
3167}
3168
Chris Mason74123bd2007-02-02 11:05:29 -05003169/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003170 * update item key.
3171 *
3172 * This function isn't completely safe. It's the caller's responsibility
3173 * that the new key won't break the order
3174 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003175void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003176 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003177{
3178 struct btrfs_disk_key disk_key;
3179 struct extent_buffer *eb;
3180 int slot;
3181
3182 eb = path->nodes[0];
3183 slot = path->slots[0];
3184 if (slot > 0) {
3185 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003186 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003187 }
3188 if (slot < btrfs_header_nritems(eb) - 1) {
3189 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003190 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003191 }
3192
3193 btrfs_cpu_key_to_disk(&disk_key, new_key);
3194 btrfs_set_item_key(eb, &disk_key, slot);
3195 btrfs_mark_buffer_dirty(eb);
3196 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003197 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003198}
3199
3200/*
Chris Mason74123bd2007-02-02 11:05:29 -05003201 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003202 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003203 *
3204 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3205 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003206 */
Chris Mason98ed5172008-01-03 10:01:48 -05003207static int push_node_left(struct btrfs_trans_handle *trans,
3208 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003209 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003210{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003211 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003212 int src_nritems;
3213 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003214 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003215
Chris Mason5f39d392007-10-15 16:14:19 -04003216 src_nritems = btrfs_header_nritems(src);
3217 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003218 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003219 WARN_ON(btrfs_header_generation(src) != trans->transid);
3220 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003221
Chris Masonbce4eae2008-04-24 14:42:46 -04003222 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003223 return 1;
3224
Chris Masond3977122009-01-05 21:25:51 -05003225 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003226 return 1;
3227
Chris Masonbce4eae2008-04-24 14:42:46 -04003228 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003229 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003230 if (push_items < src_nritems) {
3231 /* leave at least 8 pointers in the node if
3232 * we aren't going to empty it
3233 */
3234 if (src_nritems - push_items < 8) {
3235 if (push_items <= 8)
3236 return 1;
3237 push_items -= 8;
3238 }
3239 }
3240 } else
3241 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003242
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003243 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3244 push_items);
3245 if (ret) {
3246 btrfs_abort_transaction(trans, root, ret);
3247 return ret;
3248 }
Chris Mason5f39d392007-10-15 16:14:19 -04003249 copy_extent_buffer(dst, src,
3250 btrfs_node_key_ptr_offset(dst_nritems),
3251 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003252 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003253
Chris Masonbb803952007-03-01 12:04:21 -05003254 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003255 /*
3256 * don't call tree_mod_log_eb_move here, key removal was already
3257 * fully logged by tree_mod_log_eb_copy above.
3258 */
Chris Mason5f39d392007-10-15 16:14:19 -04003259 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3260 btrfs_node_key_ptr_offset(push_items),
3261 (src_nritems - push_items) *
3262 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003263 }
Chris Mason5f39d392007-10-15 16:14:19 -04003264 btrfs_set_header_nritems(src, src_nritems - push_items);
3265 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3266 btrfs_mark_buffer_dirty(src);
3267 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003268
Chris Masonbb803952007-03-01 12:04:21 -05003269 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003270}
3271
Chris Mason97571fd2007-02-24 13:39:08 -05003272/*
Chris Mason79f95c82007-03-01 15:16:26 -05003273 * try to push data from one node into the next node right in the
3274 * tree.
3275 *
3276 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3277 * error, and > 0 if there was no room in the right hand block.
3278 *
3279 * this will only push up to 1/2 the contents of the left node over
3280 */
Chris Mason5f39d392007-10-15 16:14:19 -04003281static int balance_node_right(struct btrfs_trans_handle *trans,
3282 struct btrfs_root *root,
3283 struct extent_buffer *dst,
3284 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003285{
Chris Mason79f95c82007-03-01 15:16:26 -05003286 int push_items = 0;
3287 int max_push;
3288 int src_nritems;
3289 int dst_nritems;
3290 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003291
Chris Mason7bb86312007-12-11 09:25:06 -05003292 WARN_ON(btrfs_header_generation(src) != trans->transid);
3293 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3294
Chris Mason5f39d392007-10-15 16:14:19 -04003295 src_nritems = btrfs_header_nritems(src);
3296 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003297 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003298 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003299 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003300
Chris Masond3977122009-01-05 21:25:51 -05003301 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003302 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003303
3304 max_push = src_nritems / 2 + 1;
3305 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003306 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003307 return 1;
Yan252c38f2007-08-29 09:11:44 -04003308
Chris Mason79f95c82007-03-01 15:16:26 -05003309 if (max_push < push_items)
3310 push_items = max_push;
3311
Jan Schmidtf2304752012-05-26 11:43:17 +02003312 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003313 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3314 btrfs_node_key_ptr_offset(0),
3315 (dst_nritems) *
3316 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003317
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003318 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3319 src_nritems - push_items, push_items);
3320 if (ret) {
3321 btrfs_abort_transaction(trans, root, ret);
3322 return ret;
3323 }
Chris Mason5f39d392007-10-15 16:14:19 -04003324 copy_extent_buffer(dst, src,
3325 btrfs_node_key_ptr_offset(0),
3326 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003327 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003328
Chris Mason5f39d392007-10-15 16:14:19 -04003329 btrfs_set_header_nritems(src, src_nritems - push_items);
3330 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003331
Chris Mason5f39d392007-10-15 16:14:19 -04003332 btrfs_mark_buffer_dirty(src);
3333 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003334
Chris Mason79f95c82007-03-01 15:16:26 -05003335 return ret;
3336}
3337
3338/*
Chris Mason97571fd2007-02-24 13:39:08 -05003339 * helper function to insert a new root level in the tree.
3340 * A new node is allocated, and a single item is inserted to
3341 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003342 *
3343 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003344 */
Chris Masond3977122009-01-05 21:25:51 -05003345static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003346 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003347 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003348{
Chris Mason7bb86312007-12-11 09:25:06 -05003349 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003350 struct extent_buffer *lower;
3351 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003352 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003354
3355 BUG_ON(path->nodes[level]);
3356 BUG_ON(path->nodes[level-1] != root->node);
3357
Chris Mason7bb86312007-12-11 09:25:06 -05003358 lower = path->nodes[level-1];
3359 if (level == 1)
3360 btrfs_item_key(lower, &lower_key, 0);
3361 else
3362 btrfs_node_key(lower, &lower_key, 0);
3363
Zheng Yan31840ae2008-09-23 13:14:14 -04003364 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003365 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003366 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003367 if (IS_ERR(c))
3368 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003369
Yan, Zhengf0486c62010-05-16 10:46:25 -04003370 root_add_used(root, root->nodesize);
3371
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003372 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003373 btrfs_set_header_nritems(c, 1);
3374 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003375 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003376 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003377 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003378 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003379
Ross Kirk0a4e5582013-09-24 10:12:38 +01003380 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003381 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003382
3383 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003384 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003385
Chris Mason5f39d392007-10-15 16:14:19 -04003386 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003387 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003388 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003389 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003390
3391 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003392
3393 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003394
Chris Mason925baed2008-06-25 16:01:30 -04003395 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003396 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003397 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003398
3399 /* the super has an extra ref to root->node */
3400 free_extent_buffer(old);
3401
Chris Mason0b86a832008-03-24 15:01:56 -04003402 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003403 extent_buffer_get(c);
3404 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003405 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003406 path->slots[level] = 0;
3407 return 0;
3408}
3409
Chris Mason74123bd2007-02-02 11:05:29 -05003410/*
3411 * worker function to insert a single pointer in a node.
3412 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003413 *
Chris Mason74123bd2007-02-02 11:05:29 -05003414 * slot and level indicate where you want the key to go, and
3415 * blocknr is the block the key points to.
3416 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003417static void insert_ptr(struct btrfs_trans_handle *trans,
3418 struct btrfs_root *root, struct btrfs_path *path,
3419 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003420 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003421{
Chris Mason5f39d392007-10-15 16:14:19 -04003422 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003423 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003424 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003425
3426 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003427 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003428 lower = path->nodes[level];
3429 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003430 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003431 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003432 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003433 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003434 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3435 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003436 memmove_extent_buffer(lower,
3437 btrfs_node_key_ptr_offset(slot + 1),
3438 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003439 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003440 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003441 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003442 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003443 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003444 BUG_ON(ret < 0);
3445 }
Chris Mason5f39d392007-10-15 16:14:19 -04003446 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003447 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003448 WARN_ON(trans->transid == 0);
3449 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003450 btrfs_set_header_nritems(lower, nritems + 1);
3451 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003452}
3453
Chris Mason97571fd2007-02-24 13:39:08 -05003454/*
3455 * split the node at the specified level in path in two.
3456 * The path is corrected to point to the appropriate node after the split
3457 *
3458 * Before splitting this tries to make some room in the node by pushing
3459 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003460 *
3461 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003462 */
Chris Masone02119d2008-09-05 16:13:11 -04003463static noinline int split_node(struct btrfs_trans_handle *trans,
3464 struct btrfs_root *root,
3465 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003466{
Chris Mason5f39d392007-10-15 16:14:19 -04003467 struct extent_buffer *c;
3468 struct extent_buffer *split;
3469 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003470 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003471 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003472 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003473
Chris Mason5f39d392007-10-15 16:14:19 -04003474 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003475 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003476 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003477 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003478 * trying to split the root, lets make a new one
3479 *
Liu Bofdd99c72013-05-22 12:06:51 +00003480 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003481 * insert_new_root, because that root buffer will be kept as a
3482 * normal node. We are going to log removal of half of the
3483 * elements below with tree_mod_log_eb_copy. We're holding a
3484 * tree lock on the buffer, which is why we cannot race with
3485 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003486 */
Liu Bofdd99c72013-05-22 12:06:51 +00003487 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003488 if (ret)
3489 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003490 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003491 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003492 c = path->nodes[level];
3493 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003494 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003495 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003496 if (ret < 0)
3497 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003498 }
Chris Masone66f7092007-04-20 13:16:02 -04003499
Chris Mason5f39d392007-10-15 16:14:19 -04003500 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003501 mid = (c_nritems + 1) / 2;
3502 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003504 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003505 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003506 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003507 if (IS_ERR(split))
3508 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003509
Yan, Zhengf0486c62010-05-16 10:46:25 -04003510 root_add_used(root, root->nodesize);
3511
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003512 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003513 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003514 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003515 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003516 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003517 btrfs_set_header_owner(split, root->root_key.objectid);
3518 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003519 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003520 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003521 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003522 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003523
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003524 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3525 mid, c_nritems - mid);
3526 if (ret) {
3527 btrfs_abort_transaction(trans, root, ret);
3528 return ret;
3529 }
Chris Mason5f39d392007-10-15 16:14:19 -04003530 copy_extent_buffer(split, c,
3531 btrfs_node_key_ptr_offset(0),
3532 btrfs_node_key_ptr_offset(mid),
3533 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3534 btrfs_set_header_nritems(split, c_nritems - mid);
3535 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003536 ret = 0;
3537
Chris Mason5f39d392007-10-15 16:14:19 -04003538 btrfs_mark_buffer_dirty(c);
3539 btrfs_mark_buffer_dirty(split);
3540
Jeff Mahoney143bede2012-03-01 14:56:26 +01003541 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003542 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003543
Chris Mason5de08d72007-02-24 06:24:44 -05003544 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003545 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003546 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003547 free_extent_buffer(c);
3548 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003549 path->slots[level + 1] += 1;
3550 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003551 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003552 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003553 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003554 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003555}
3556
Chris Mason74123bd2007-02-02 11:05:29 -05003557/*
3558 * how many bytes are required to store the items in a leaf. start
3559 * and nr indicate which items in the leaf to check. This totals up the
3560 * space used both by the item structs and the item data
3561 */
Chris Mason5f39d392007-10-15 16:14:19 -04003562static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003563{
Josef Bacik41be1f32012-10-15 13:43:18 -04003564 struct btrfs_item *start_item;
3565 struct btrfs_item *end_item;
3566 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003568 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003569 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003570
3571 if (!nr)
3572 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003573 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003574 start_item = btrfs_item_nr(start);
3575 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003576 data_len = btrfs_token_item_offset(l, start_item, &token) +
3577 btrfs_token_item_size(l, start_item, &token);
3578 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003579 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003580 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003581 return data_len;
3582}
3583
Chris Mason74123bd2007-02-02 11:05:29 -05003584/*
Chris Masond4dbff92007-04-04 14:08:15 -04003585 * The space between the end of the leaf items and
3586 * the start of the leaf data. IOW, how much room
3587 * the leaf has left for both items and data
3588 */
Chris Masond3977122009-01-05 21:25:51 -05003589noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003590 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003591{
Chris Mason5f39d392007-10-15 16:14:19 -04003592 int nritems = btrfs_header_nritems(leaf);
3593 int ret;
3594 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3595 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003596 btrfs_crit(root->fs_info,
3597 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003598 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003599 leaf_space_used(leaf, 0, nritems), nritems);
3600 }
3601 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003602}
3603
Chris Mason99d8f832010-07-07 10:51:48 -04003604/*
3605 * min slot controls the lowest index we're willing to push to the
3606 * right. We'll push up to and including min_slot, but no lower
3607 */
Chris Mason44871b12009-03-13 10:04:31 -04003608static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3609 struct btrfs_root *root,
3610 struct btrfs_path *path,
3611 int data_size, int empty,
3612 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003613 int free_space, u32 left_nritems,
3614 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003615{
Chris Mason5f39d392007-10-15 16:14:19 -04003616 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003617 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003618 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003619 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003620 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003621 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003622 int push_space = 0;
3623 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003624 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003625 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003626 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003627 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003628 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003629
Chris Masoncfed81a2012-03-03 07:40:03 -05003630 btrfs_init_map_token(&token);
3631
Chris Mason34a38212007-11-07 13:31:03 -05003632 if (empty)
3633 nr = 0;
3634 else
Chris Mason99d8f832010-07-07 10:51:48 -04003635 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003636
Zheng Yan31840ae2008-09-23 13:14:14 -04003637 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003638 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003639
Chris Mason44871b12009-03-13 10:04:31 -04003640 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003641 i = left_nritems - 1;
3642 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003643 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003644
Zheng Yan31840ae2008-09-23 13:14:14 -04003645 if (!empty && push_items > 0) {
3646 if (path->slots[0] > i)
3647 break;
3648 if (path->slots[0] == i) {
3649 int space = btrfs_leaf_free_space(root, left);
3650 if (space + push_space * 2 > free_space)
3651 break;
3652 }
3653 }
3654
Chris Mason00ec4c52007-02-24 12:47:20 -05003655 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003656 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003657
Chris Masondb945352007-10-15 16:15:53 -04003658 this_item_size = btrfs_item_size(left, item);
3659 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003660 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003661
Chris Mason00ec4c52007-02-24 12:47:20 -05003662 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003663 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003664 if (i == 0)
3665 break;
3666 i--;
Chris Masondb945352007-10-15 16:15:53 -04003667 }
Chris Mason5f39d392007-10-15 16:14:19 -04003668
Chris Mason925baed2008-06-25 16:01:30 -04003669 if (push_items == 0)
3670 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003671
Julia Lawall6c1500f2012-11-03 20:30:18 +00003672 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003673
Chris Mason00ec4c52007-02-24 12:47:20 -05003674 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003675 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003676
Chris Mason5f39d392007-10-15 16:14:19 -04003677 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003678 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003679
Chris Mason00ec4c52007-02-24 12:47:20 -05003680 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003681 data_end = leaf_data_end(root, right);
3682 memmove_extent_buffer(right,
3683 btrfs_leaf_data(right) + data_end - push_space,
3684 btrfs_leaf_data(right) + data_end,
3685 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3686
Chris Mason00ec4c52007-02-24 12:47:20 -05003687 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003688 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003689 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3690 btrfs_leaf_data(left) + leaf_data_end(root, left),
3691 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003692
3693 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3694 btrfs_item_nr_offset(0),
3695 right_nritems * sizeof(struct btrfs_item));
3696
Chris Mason00ec4c52007-02-24 12:47:20 -05003697 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003698 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3699 btrfs_item_nr_offset(left_nritems - push_items),
3700 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003701
3702 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003703 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003704 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003705 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003706 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003707 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003708 push_space -= btrfs_token_item_size(right, item, &token);
3709 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003710 }
3711
Chris Mason7518a232007-03-12 12:01:18 -04003712 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003713 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003714
Chris Mason34a38212007-11-07 13:31:03 -05003715 if (left_nritems)
3716 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003717 else
3718 clean_tree_block(trans, root, left);
3719
Chris Mason5f39d392007-10-15 16:14:19 -04003720 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003721
Chris Mason5f39d392007-10-15 16:14:19 -04003722 btrfs_item_key(right, &disk_key, 0);
3723 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003724 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003725
Chris Mason00ec4c52007-02-24 12:47:20 -05003726 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003727 if (path->slots[0] >= left_nritems) {
3728 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003729 if (btrfs_header_nritems(path->nodes[0]) == 0)
3730 clean_tree_block(trans, root, path->nodes[0]);
3731 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003732 free_extent_buffer(path->nodes[0]);
3733 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003734 path->slots[1] += 1;
3735 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003736 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003737 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003738 }
3739 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003740
3741out_unlock:
3742 btrfs_tree_unlock(right);
3743 free_extent_buffer(right);
3744 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003745}
Chris Mason925baed2008-06-25 16:01:30 -04003746
Chris Mason00ec4c52007-02-24 12:47:20 -05003747/*
Chris Mason44871b12009-03-13 10:04:31 -04003748 * push some data in the path leaf to the right, trying to free up at
3749 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3750 *
3751 * returns 1 if the push failed because the other node didn't have enough
3752 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003753 *
3754 * this will push starting from min_slot to the end of the leaf. It won't
3755 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003756 */
3757static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003758 *root, struct btrfs_path *path,
3759 int min_data_size, int data_size,
3760 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003761{
3762 struct extent_buffer *left = path->nodes[0];
3763 struct extent_buffer *right;
3764 struct extent_buffer *upper;
3765 int slot;
3766 int free_space;
3767 u32 left_nritems;
3768 int ret;
3769
3770 if (!path->nodes[1])
3771 return 1;
3772
3773 slot = path->slots[1];
3774 upper = path->nodes[1];
3775 if (slot >= btrfs_header_nritems(upper) - 1)
3776 return 1;
3777
3778 btrfs_assert_tree_locked(path->nodes[1]);
3779
3780 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003781 if (right == NULL)
3782 return 1;
3783
Chris Mason44871b12009-03-13 10:04:31 -04003784 btrfs_tree_lock(right);
3785 btrfs_set_lock_blocking(right);
3786
3787 free_space = btrfs_leaf_free_space(root, right);
3788 if (free_space < data_size)
3789 goto out_unlock;
3790
3791 /* cow and double check */
3792 ret = btrfs_cow_block(trans, root, right, upper,
3793 slot + 1, &right);
3794 if (ret)
3795 goto out_unlock;
3796
3797 free_space = btrfs_leaf_free_space(root, right);
3798 if (free_space < data_size)
3799 goto out_unlock;
3800
3801 left_nritems = btrfs_header_nritems(left);
3802 if (left_nritems == 0)
3803 goto out_unlock;
3804
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003805 if (path->slots[0] == left_nritems && !empty) {
3806 /* Key greater than all keys in the leaf, right neighbor has
3807 * enough room for it and we're not emptying our leaf to delete
3808 * it, therefore use right neighbor to insert the new item and
3809 * no need to touch/dirty our left leaft. */
3810 btrfs_tree_unlock(left);
3811 free_extent_buffer(left);
3812 path->nodes[0] = right;
3813 path->slots[0] = 0;
3814 path->slots[1]++;
3815 return 0;
3816 }
3817
Chris Mason99d8f832010-07-07 10:51:48 -04003818 return __push_leaf_right(trans, root, path, min_data_size, empty,
3819 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003820out_unlock:
3821 btrfs_tree_unlock(right);
3822 free_extent_buffer(right);
3823 return 1;
3824}
3825
3826/*
Chris Mason74123bd2007-02-02 11:05:29 -05003827 * push some data in the path leaf to the left, trying to free up at
3828 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003829 *
3830 * max_slot can put a limit on how far into the leaf we'll push items. The
3831 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3832 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003833 */
Chris Mason44871b12009-03-13 10:04:31 -04003834static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3835 struct btrfs_root *root,
3836 struct btrfs_path *path, int data_size,
3837 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003838 int free_space, u32 right_nritems,
3839 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003840{
Chris Mason5f39d392007-10-15 16:14:19 -04003841 struct btrfs_disk_key disk_key;
3842 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003843 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003844 int push_space = 0;
3845 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003846 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003847 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003848 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003849 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003850 u32 this_item_size;
3851 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003852 struct btrfs_map_token token;
3853
3854 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855
Chris Mason34a38212007-11-07 13:31:03 -05003856 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003857 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003858 else
Chris Mason99d8f832010-07-07 10:51:48 -04003859 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003860
3861 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003862 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003863
Zheng Yan31840ae2008-09-23 13:14:14 -04003864 if (!empty && push_items > 0) {
3865 if (path->slots[0] < i)
3866 break;
3867 if (path->slots[0] == i) {
3868 int space = btrfs_leaf_free_space(root, right);
3869 if (space + push_space * 2 > free_space)
3870 break;
3871 }
3872 }
3873
Chris Masonbe0e5c02007-01-26 15:51:26 -05003874 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003875 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003876
3877 this_item_size = btrfs_item_size(right, item);
3878 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003879 break;
Chris Masondb945352007-10-15 16:15:53 -04003880
Chris Masonbe0e5c02007-01-26 15:51:26 -05003881 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003882 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883 }
Chris Masondb945352007-10-15 16:15:53 -04003884
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003886 ret = 1;
3887 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003888 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303889 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003890
Chris Masonbe0e5c02007-01-26 15:51:26 -05003891 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003892 copy_extent_buffer(left, right,
3893 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3894 btrfs_item_nr_offset(0),
3895 push_items * sizeof(struct btrfs_item));
3896
Chris Mason123abc82007-03-14 14:14:43 -04003897 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003898 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003899
3900 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003901 leaf_data_end(root, left) - push_space,
3902 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003903 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003904 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003905 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003906 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003907
Chris Masondb945352007-10-15 16:15:53 -04003908 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003909 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003910 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003911
Ross Kirkdd3cc162013-09-16 15:58:09 +01003912 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003913
Chris Masoncfed81a2012-03-03 07:40:03 -05003914 ioff = btrfs_token_item_offset(left, item, &token);
3915 btrfs_set_token_item_offset(left, item,
3916 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3917 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003918 }
Chris Mason5f39d392007-10-15 16:14:19 -04003919 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003920
3921 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003922 if (push_items > right_nritems)
3923 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003924 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003925
Chris Mason34a38212007-11-07 13:31:03 -05003926 if (push_items < right_nritems) {
3927 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3928 leaf_data_end(root, right);
3929 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3930 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3931 btrfs_leaf_data(right) +
3932 leaf_data_end(root, right), push_space);
3933
3934 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003935 btrfs_item_nr_offset(push_items),
3936 (btrfs_header_nritems(right) - push_items) *
3937 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003938 }
Yaneef1c492007-11-26 10:58:13 -05003939 right_nritems -= push_items;
3940 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003941 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003942 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003943 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003944
Chris Masoncfed81a2012-03-03 07:40:03 -05003945 push_space = push_space - btrfs_token_item_size(right,
3946 item, &token);
3947 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003948 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003949
Chris Mason5f39d392007-10-15 16:14:19 -04003950 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003951 if (right_nritems)
3952 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003953 else
3954 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003955
Chris Mason5f39d392007-10-15 16:14:19 -04003956 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003957 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003958
3959 /* then fixup the leaf pointer in the path */
3960 if (path->slots[0] < push_items) {
3961 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003962 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003963 free_extent_buffer(path->nodes[0]);
3964 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003965 path->slots[1] -= 1;
3966 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003967 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003968 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003969 path->slots[0] -= push_items;
3970 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003971 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003972 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003973out:
3974 btrfs_tree_unlock(left);
3975 free_extent_buffer(left);
3976 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003977}
3978
Chris Mason74123bd2007-02-02 11:05:29 -05003979/*
Chris Mason44871b12009-03-13 10:04:31 -04003980 * push some data in the path leaf to the left, trying to free up at
3981 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003982 *
3983 * max_slot can put a limit on how far into the leaf we'll push items. The
3984 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3985 * items
Chris Mason44871b12009-03-13 10:04:31 -04003986 */
3987static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003988 *root, struct btrfs_path *path, int min_data_size,
3989 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003990{
3991 struct extent_buffer *right = path->nodes[0];
3992 struct extent_buffer *left;
3993 int slot;
3994 int free_space;
3995 u32 right_nritems;
3996 int ret = 0;
3997
3998 slot = path->slots[1];
3999 if (slot == 0)
4000 return 1;
4001 if (!path->nodes[1])
4002 return 1;
4003
4004 right_nritems = btrfs_header_nritems(right);
4005 if (right_nritems == 0)
4006 return 1;
4007
4008 btrfs_assert_tree_locked(path->nodes[1]);
4009
4010 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004011 if (left == NULL)
4012 return 1;
4013
Chris Mason44871b12009-03-13 10:04:31 -04004014 btrfs_tree_lock(left);
4015 btrfs_set_lock_blocking(left);
4016
4017 free_space = btrfs_leaf_free_space(root, left);
4018 if (free_space < data_size) {
4019 ret = 1;
4020 goto out;
4021 }
4022
4023 /* cow and double check */
4024 ret = btrfs_cow_block(trans, root, left,
4025 path->nodes[1], slot - 1, &left);
4026 if (ret) {
4027 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004028 if (ret == -ENOSPC)
4029 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004030 goto out;
4031 }
4032
4033 free_space = btrfs_leaf_free_space(root, left);
4034 if (free_space < data_size) {
4035 ret = 1;
4036 goto out;
4037 }
4038
Chris Mason99d8f832010-07-07 10:51:48 -04004039 return __push_leaf_left(trans, root, path, min_data_size,
4040 empty, left, free_space, right_nritems,
4041 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004042out:
4043 btrfs_tree_unlock(left);
4044 free_extent_buffer(left);
4045 return ret;
4046}
4047
4048/*
Chris Mason74123bd2007-02-02 11:05:29 -05004049 * split the path's leaf in two, making sure there is at least data_size
4050 * available for the resulting leaf level of the path.
4051 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004052static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4053 struct btrfs_root *root,
4054 struct btrfs_path *path,
4055 struct extent_buffer *l,
4056 struct extent_buffer *right,
4057 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004058{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004059 int data_copy_size;
4060 int rt_data_off;
4061 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004062 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004063 struct btrfs_map_token token;
4064
4065 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004066
Chris Mason5f39d392007-10-15 16:14:19 -04004067 nritems = nritems - mid;
4068 btrfs_set_header_nritems(right, nritems);
4069 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4070
4071 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4072 btrfs_item_nr_offset(mid),
4073 nritems * sizeof(struct btrfs_item));
4074
4075 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004076 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4077 data_copy_size, btrfs_leaf_data(l) +
4078 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004079
Chris Mason5f39d392007-10-15 16:14:19 -04004080 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4081 btrfs_item_end_nr(l, mid);
4082
4083 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004084 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004085 u32 ioff;
4086
Chris Masoncfed81a2012-03-03 07:40:03 -05004087 ioff = btrfs_token_item_offset(right, item, &token);
4088 btrfs_set_token_item_offset(right, item,
4089 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004090 }
Chris Mason74123bd2007-02-02 11:05:29 -05004091
Chris Mason5f39d392007-10-15 16:14:19 -04004092 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004093 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004094 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004095 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004096
4097 btrfs_mark_buffer_dirty(right);
4098 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004099 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004100
Chris Masonbe0e5c02007-01-26 15:51:26 -05004101 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004102 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004103 free_extent_buffer(path->nodes[0]);
4104 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004105 path->slots[0] -= mid;
4106 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004107 } else {
4108 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004109 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004110 }
Chris Mason5f39d392007-10-15 16:14:19 -04004111
Chris Masoneb60cea2007-02-02 09:18:22 -05004112 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004113}
4114
4115/*
Chris Mason99d8f832010-07-07 10:51:48 -04004116 * double splits happen when we need to insert a big item in the middle
4117 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4118 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4119 * A B C
4120 *
4121 * We avoid this by trying to push the items on either side of our target
4122 * into the adjacent leaves. If all goes well we can avoid the double split
4123 * completely.
4124 */
4125static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4126 struct btrfs_root *root,
4127 struct btrfs_path *path,
4128 int data_size)
4129{
4130 int ret;
4131 int progress = 0;
4132 int slot;
4133 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004134 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004135
4136 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004137 if (slot < btrfs_header_nritems(path->nodes[0]))
4138 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004139
4140 /*
4141 * try to push all the items after our slot into the
4142 * right leaf
4143 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004144 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004145 if (ret < 0)
4146 return ret;
4147
4148 if (ret == 0)
4149 progress++;
4150
4151 nritems = btrfs_header_nritems(path->nodes[0]);
4152 /*
4153 * our goal is to get our slot at the start or end of a leaf. If
4154 * we've done so we're done
4155 */
4156 if (path->slots[0] == 0 || path->slots[0] == nritems)
4157 return 0;
4158
4159 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4160 return 0;
4161
4162 /* try to push all the items before our slot into the next leaf */
4163 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004164 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004165 if (ret < 0)
4166 return ret;
4167
4168 if (ret == 0)
4169 progress++;
4170
4171 if (progress)
4172 return 0;
4173 return 1;
4174}
4175
4176/*
Chris Mason44871b12009-03-13 10:04:31 -04004177 * split the path's leaf in two, making sure there is at least data_size
4178 * available for the resulting leaf level of the path.
4179 *
4180 * returns 0 if all went well and < 0 on failure.
4181 */
4182static noinline int split_leaf(struct btrfs_trans_handle *trans,
4183 struct btrfs_root *root,
4184 struct btrfs_key *ins_key,
4185 struct btrfs_path *path, int data_size,
4186 int extend)
4187{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004188 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004189 struct extent_buffer *l;
4190 u32 nritems;
4191 int mid;
4192 int slot;
4193 struct extent_buffer *right;
4194 int ret = 0;
4195 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004196 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004197 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004198 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004199
Yan, Zhenga5719522009-09-24 09:17:31 -04004200 l = path->nodes[0];
4201 slot = path->slots[0];
4202 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4203 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4204 return -EOVERFLOW;
4205
Chris Mason44871b12009-03-13 10:04:31 -04004206 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004207 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004208 int space_needed = data_size;
4209
4210 if (slot < btrfs_header_nritems(l))
4211 space_needed -= btrfs_leaf_free_space(root, l);
4212
4213 wret = push_leaf_right(trans, root, path, space_needed,
4214 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004215 if (wret < 0)
4216 return wret;
4217 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004218 wret = push_leaf_left(trans, root, path, space_needed,
4219 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004220 if (wret < 0)
4221 return wret;
4222 }
4223 l = path->nodes[0];
4224
4225 /* did the pushes work? */
4226 if (btrfs_leaf_free_space(root, l) >= data_size)
4227 return 0;
4228 }
4229
4230 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004231 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004232 if (ret)
4233 return ret;
4234 }
4235again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004236 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004237 l = path->nodes[0];
4238 slot = path->slots[0];
4239 nritems = btrfs_header_nritems(l);
4240 mid = (nritems + 1) / 2;
4241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004242 if (mid <= slot) {
4243 if (nritems == 1 ||
4244 leaf_space_used(l, mid, nritems - mid) + data_size >
4245 BTRFS_LEAF_DATA_SIZE(root)) {
4246 if (slot >= nritems) {
4247 split = 0;
4248 } else {
4249 mid = slot;
4250 if (mid != nritems &&
4251 leaf_space_used(l, mid, nritems - mid) +
4252 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004253 if (data_size && !tried_avoid_double)
4254 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004255 split = 2;
4256 }
4257 }
4258 }
4259 } else {
4260 if (leaf_space_used(l, 0, mid) + data_size >
4261 BTRFS_LEAF_DATA_SIZE(root)) {
4262 if (!extend && data_size && slot == 0) {
4263 split = 0;
4264 } else if ((extend || !data_size) && slot == 0) {
4265 mid = 1;
4266 } else {
4267 mid = slot;
4268 if (mid != nritems &&
4269 leaf_space_used(l, mid, nritems - mid) +
4270 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004271 if (data_size && !tried_avoid_double)
4272 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304273 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004274 }
4275 }
4276 }
4277 }
4278
4279 if (split == 0)
4280 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4281 else
4282 btrfs_item_key(l, &disk_key, mid);
4283
David Sterba707e8a02014-06-04 19:22:26 +02004284 right = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004285 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004286 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004287 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004288 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004289
David Sterba707e8a02014-06-04 19:22:26 +02004290 root_add_used(root, root->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004291
4292 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4293 btrfs_set_header_bytenr(right, right->start);
4294 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004295 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004296 btrfs_set_header_owner(right, root->root_key.objectid);
4297 btrfs_set_header_level(right, 0);
4298 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004299 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004300
4301 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004302 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004303 BTRFS_UUID_SIZE);
4304
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004305 if (split == 0) {
4306 if (mid <= slot) {
4307 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004308 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004309 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004310 btrfs_tree_unlock(path->nodes[0]);
4311 free_extent_buffer(path->nodes[0]);
4312 path->nodes[0] = right;
4313 path->slots[0] = 0;
4314 path->slots[1] += 1;
4315 } else {
4316 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004317 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004318 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004319 btrfs_tree_unlock(path->nodes[0]);
4320 free_extent_buffer(path->nodes[0]);
4321 path->nodes[0] = right;
4322 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004323 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004324 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004325 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004326 btrfs_mark_buffer_dirty(right);
4327 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004328 }
4329
Jeff Mahoney143bede2012-03-01 14:56:26 +01004330 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004331
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004332 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004333 BUG_ON(num_doubles != 0);
4334 num_doubles++;
4335 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004336 }
Chris Mason44871b12009-03-13 10:04:31 -04004337
Jeff Mahoney143bede2012-03-01 14:56:26 +01004338 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004339
4340push_for_double:
4341 push_for_double_split(trans, root, path, data_size);
4342 tried_avoid_double = 1;
4343 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4344 return 0;
4345 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004346}
4347
Yan, Zhengad48fd752009-11-12 09:33:58 +00004348static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4349 struct btrfs_root *root,
4350 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004351{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004352 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004353 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004354 struct btrfs_file_extent_item *fi;
4355 u64 extent_len = 0;
4356 u32 item_size;
4357 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004358
4359 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004360 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4361
4362 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4363 key.type != BTRFS_EXTENT_CSUM_KEY);
4364
4365 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4366 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004367
4368 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004369 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4370 fi = btrfs_item_ptr(leaf, path->slots[0],
4371 struct btrfs_file_extent_item);
4372 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4373 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004374 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004375
Chris Mason459931e2008-12-10 09:10:46 -05004376 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004377 path->search_for_split = 1;
4378 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004379 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004380 if (ret < 0)
4381 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004382
Yan, Zhengad48fd752009-11-12 09:33:58 +00004383 ret = -EAGAIN;
4384 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004385 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004386 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4387 goto err;
4388
Chris Mason109f6ae2010-04-02 09:20:18 -04004389 /* the leaf has changed, it now has room. return now */
4390 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4391 goto err;
4392
Yan, Zhengad48fd752009-11-12 09:33:58 +00004393 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4394 fi = btrfs_item_ptr(leaf, path->slots[0],
4395 struct btrfs_file_extent_item);
4396 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4397 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004398 }
4399
Chris Masonb9473432009-03-13 11:00:37 -04004400 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004401 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004402 if (ret)
4403 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004404
Yan, Zhengad48fd752009-11-12 09:33:58 +00004405 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004406 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004407 return 0;
4408err:
4409 path->keep_locks = 0;
4410 return ret;
4411}
4412
4413static noinline int split_item(struct btrfs_trans_handle *trans,
4414 struct btrfs_root *root,
4415 struct btrfs_path *path,
4416 struct btrfs_key *new_key,
4417 unsigned long split_offset)
4418{
4419 struct extent_buffer *leaf;
4420 struct btrfs_item *item;
4421 struct btrfs_item *new_item;
4422 int slot;
4423 char *buf;
4424 u32 nritems;
4425 u32 item_size;
4426 u32 orig_offset;
4427 struct btrfs_disk_key disk_key;
4428
Chris Masonb9473432009-03-13 11:00:37 -04004429 leaf = path->nodes[0];
4430 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4431
Chris Masonb4ce94d2009-02-04 09:25:08 -05004432 btrfs_set_path_blocking(path);
4433
Ross Kirkdd3cc162013-09-16 15:58:09 +01004434 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004435 orig_offset = btrfs_item_offset(leaf, item);
4436 item_size = btrfs_item_size(leaf, item);
4437
Chris Mason459931e2008-12-10 09:10:46 -05004438 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004439 if (!buf)
4440 return -ENOMEM;
4441
Chris Mason459931e2008-12-10 09:10:46 -05004442 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4443 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004444
Chris Mason459931e2008-12-10 09:10:46 -05004445 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004446 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004447 if (slot != nritems) {
4448 /* shift the items */
4449 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004450 btrfs_item_nr_offset(slot),
4451 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004452 }
4453
4454 btrfs_cpu_key_to_disk(&disk_key, new_key);
4455 btrfs_set_item_key(leaf, &disk_key, slot);
4456
Ross Kirkdd3cc162013-09-16 15:58:09 +01004457 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004458
4459 btrfs_set_item_offset(leaf, new_item, orig_offset);
4460 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4461
4462 btrfs_set_item_offset(leaf, item,
4463 orig_offset + item_size - split_offset);
4464 btrfs_set_item_size(leaf, item, split_offset);
4465
4466 btrfs_set_header_nritems(leaf, nritems + 1);
4467
4468 /* write the data for the start of the original item */
4469 write_extent_buffer(leaf, buf,
4470 btrfs_item_ptr_offset(leaf, path->slots[0]),
4471 split_offset);
4472
4473 /* write the data for the new item */
4474 write_extent_buffer(leaf, buf + split_offset,
4475 btrfs_item_ptr_offset(leaf, slot),
4476 item_size - split_offset);
4477 btrfs_mark_buffer_dirty(leaf);
4478
Yan, Zhengad48fd752009-11-12 09:33:58 +00004479 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004480 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004481 return 0;
4482}
4483
4484/*
4485 * This function splits a single item into two items,
4486 * giving 'new_key' to the new item and splitting the
4487 * old one at split_offset (from the start of the item).
4488 *
4489 * The path may be released by this operation. After
4490 * the split, the path is pointing to the old item. The
4491 * new item is going to be in the same node as the old one.
4492 *
4493 * Note, the item being split must be smaller enough to live alone on
4494 * a tree block with room for one extra struct btrfs_item
4495 *
4496 * This allows us to split the item in place, keeping a lock on the
4497 * leaf the entire time.
4498 */
4499int btrfs_split_item(struct btrfs_trans_handle *trans,
4500 struct btrfs_root *root,
4501 struct btrfs_path *path,
4502 struct btrfs_key *new_key,
4503 unsigned long split_offset)
4504{
4505 int ret;
4506 ret = setup_leaf_for_split(trans, root, path,
4507 sizeof(struct btrfs_item));
4508 if (ret)
4509 return ret;
4510
4511 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004512 return ret;
4513}
4514
4515/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004516 * This function duplicate a item, giving 'new_key' to the new item.
4517 * It guarantees both items live in the same tree leaf and the new item
4518 * is contiguous with the original item.
4519 *
4520 * This allows us to split file extent in place, keeping a lock on the
4521 * leaf the entire time.
4522 */
4523int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4524 struct btrfs_root *root,
4525 struct btrfs_path *path,
4526 struct btrfs_key *new_key)
4527{
4528 struct extent_buffer *leaf;
4529 int ret;
4530 u32 item_size;
4531
4532 leaf = path->nodes[0];
4533 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4534 ret = setup_leaf_for_split(trans, root, path,
4535 item_size + sizeof(struct btrfs_item));
4536 if (ret)
4537 return ret;
4538
4539 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004540 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004541 item_size, item_size +
4542 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004543 leaf = path->nodes[0];
4544 memcpy_extent_buffer(leaf,
4545 btrfs_item_ptr_offset(leaf, path->slots[0]),
4546 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4547 item_size);
4548 return 0;
4549}
4550
4551/*
Chris Masond352ac62008-09-29 15:18:18 -04004552 * make the item pointed to by the path smaller. new_size indicates
4553 * how small to make it, and from_end tells us if we just chop bytes
4554 * off the end of the item or if we shift the item to chop bytes off
4555 * the front.
4556 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004557void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004558 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004559{
Chris Masonb18c6682007-04-17 13:26:50 -04004560 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004561 struct extent_buffer *leaf;
4562 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004563 u32 nritems;
4564 unsigned int data_end;
4565 unsigned int old_data_start;
4566 unsigned int old_size;
4567 unsigned int size_diff;
4568 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004569 struct btrfs_map_token token;
4570
4571 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004572
Chris Mason5f39d392007-10-15 16:14:19 -04004573 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004574 slot = path->slots[0];
4575
4576 old_size = btrfs_item_size_nr(leaf, slot);
4577 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004578 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004579
Chris Mason5f39d392007-10-15 16:14:19 -04004580 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004581 data_end = leaf_data_end(root, leaf);
4582
Chris Mason5f39d392007-10-15 16:14:19 -04004583 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004584
Chris Masonb18c6682007-04-17 13:26:50 -04004585 size_diff = old_size - new_size;
4586
4587 BUG_ON(slot < 0);
4588 BUG_ON(slot >= nritems);
4589
4590 /*
4591 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4592 */
4593 /* first correct the data pointers */
4594 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004595 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004596 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004597
Chris Masoncfed81a2012-03-03 07:40:03 -05004598 ioff = btrfs_token_item_offset(leaf, item, &token);
4599 btrfs_set_token_item_offset(leaf, item,
4600 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004601 }
Chris Masondb945352007-10-15 16:15:53 -04004602
Chris Masonb18c6682007-04-17 13:26:50 -04004603 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004604 if (from_end) {
4605 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4606 data_end + size_diff, btrfs_leaf_data(leaf) +
4607 data_end, old_data_start + new_size - data_end);
4608 } else {
4609 struct btrfs_disk_key disk_key;
4610 u64 offset;
4611
4612 btrfs_item_key(leaf, &disk_key, slot);
4613
4614 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4615 unsigned long ptr;
4616 struct btrfs_file_extent_item *fi;
4617
4618 fi = btrfs_item_ptr(leaf, slot,
4619 struct btrfs_file_extent_item);
4620 fi = (struct btrfs_file_extent_item *)(
4621 (unsigned long)fi - size_diff);
4622
4623 if (btrfs_file_extent_type(leaf, fi) ==
4624 BTRFS_FILE_EXTENT_INLINE) {
4625 ptr = btrfs_item_ptr_offset(leaf, slot);
4626 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004627 (unsigned long)fi,
4628 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004629 disk_bytenr));
4630 }
4631 }
4632
4633 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4634 data_end + size_diff, btrfs_leaf_data(leaf) +
4635 data_end, old_data_start - data_end);
4636
4637 offset = btrfs_disk_key_offset(&disk_key);
4638 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4639 btrfs_set_item_key(leaf, &disk_key, slot);
4640 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004641 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004642 }
Chris Mason5f39d392007-10-15 16:14:19 -04004643
Ross Kirkdd3cc162013-09-16 15:58:09 +01004644 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004645 btrfs_set_item_size(leaf, item, new_size);
4646 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004647
Chris Mason5f39d392007-10-15 16:14:19 -04004648 if (btrfs_leaf_free_space(root, leaf) < 0) {
4649 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004650 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004651 }
Chris Masonb18c6682007-04-17 13:26:50 -04004652}
4653
Chris Masond352ac62008-09-29 15:18:18 -04004654/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004655 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004656 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004657void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004658 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004659{
Chris Mason6567e832007-04-16 09:22:45 -04004660 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004661 struct extent_buffer *leaf;
4662 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004663 u32 nritems;
4664 unsigned int data_end;
4665 unsigned int old_data;
4666 unsigned int old_size;
4667 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004668 struct btrfs_map_token token;
4669
4670 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004671
Chris Mason5f39d392007-10-15 16:14:19 -04004672 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004673
Chris Mason5f39d392007-10-15 16:14:19 -04004674 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004675 data_end = leaf_data_end(root, leaf);
4676
Chris Mason5f39d392007-10-15 16:14:19 -04004677 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4678 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004679 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004680 }
Chris Mason6567e832007-04-16 09:22:45 -04004681 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004682 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004683
4684 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004685 if (slot >= nritems) {
4686 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004687 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004688 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004689 BUG_ON(1);
4690 }
Chris Mason6567e832007-04-16 09:22:45 -04004691
4692 /*
4693 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4694 */
4695 /* first correct the data pointers */
4696 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004697 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004698 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004699
Chris Masoncfed81a2012-03-03 07:40:03 -05004700 ioff = btrfs_token_item_offset(leaf, item, &token);
4701 btrfs_set_token_item_offset(leaf, item,
4702 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004703 }
Chris Mason5f39d392007-10-15 16:14:19 -04004704
Chris Mason6567e832007-04-16 09:22:45 -04004705 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004706 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004707 data_end - data_size, btrfs_leaf_data(leaf) +
4708 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004709
Chris Mason6567e832007-04-16 09:22:45 -04004710 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004711 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004712 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004713 btrfs_set_item_size(leaf, item, old_size + data_size);
4714 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004715
Chris Mason5f39d392007-10-15 16:14:19 -04004716 if (btrfs_leaf_free_space(root, leaf) < 0) {
4717 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004718 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004719 }
Chris Mason6567e832007-04-16 09:22:45 -04004720}
4721
Chris Mason74123bd2007-02-02 11:05:29 -05004722/*
Chris Mason44871b12009-03-13 10:04:31 -04004723 * this is a helper for btrfs_insert_empty_items, the main goal here is
4724 * to save stack depth by doing the bulk of the work in a function
4725 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004726 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004727void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004728 struct btrfs_key *cpu_key, u32 *data_size,
4729 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004730{
Chris Mason5f39d392007-10-15 16:14:19 -04004731 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004732 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004733 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004734 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004735 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004736 struct extent_buffer *leaf;
4737 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004738 struct btrfs_map_token token;
4739
Filipe Manana24cdc842014-07-28 19:34:35 +01004740 if (path->slots[0] == 0) {
4741 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4742 fixup_low_keys(root, path, &disk_key, 1);
4743 }
4744 btrfs_unlock_up_safe(path, 1);
4745
Chris Masoncfed81a2012-03-03 07:40:03 -05004746 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004747
Chris Mason5f39d392007-10-15 16:14:19 -04004748 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004749 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004750
Chris Mason5f39d392007-10-15 16:14:19 -04004751 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004752 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004753
Chris Masonf25956c2008-09-12 15:32:53 -04004754 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004755 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004756 btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
Chris Mason9c583092008-01-29 15:15:18 -05004757 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004758 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004759 }
Chris Mason5f39d392007-10-15 16:14:19 -04004760
Chris Masonbe0e5c02007-01-26 15:51:26 -05004761 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004762 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004763
Chris Mason5f39d392007-10-15 16:14:19 -04004764 if (old_data < data_end) {
4765 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004766 btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
Chris Mason5f39d392007-10-15 16:14:19 -04004767 slot, old_data, data_end);
4768 BUG_ON(1);
4769 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004770 /*
4771 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4772 */
4773 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004774 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004775 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004776
Ross Kirkdd3cc162013-09-16 15:58:09 +01004777 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004778 ioff = btrfs_token_item_offset(leaf, item, &token);
4779 btrfs_set_token_item_offset(leaf, item,
4780 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004781 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004783 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004784 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004785 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004786
4787 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004788 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004789 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004790 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004791 data_end = old_data;
4792 }
Chris Mason5f39d392007-10-15 16:14:19 -04004793
Chris Mason62e27492007-03-15 12:56:47 -04004794 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004795 for (i = 0; i < nr; i++) {
4796 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4797 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004798 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004799 btrfs_set_token_item_offset(leaf, item,
4800 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004801 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004802 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004803 }
Chris Mason44871b12009-03-13 10:04:31 -04004804
Chris Mason9c583092008-01-29 15:15:18 -05004805 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004806 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004807
Chris Mason5f39d392007-10-15 16:14:19 -04004808 if (btrfs_leaf_free_space(root, leaf) < 0) {
4809 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004810 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004811 }
Chris Mason44871b12009-03-13 10:04:31 -04004812}
4813
4814/*
4815 * Given a key and some data, insert items into the tree.
4816 * This does all the path init required, making room in the tree if needed.
4817 */
4818int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4819 struct btrfs_root *root,
4820 struct btrfs_path *path,
4821 struct btrfs_key *cpu_key, u32 *data_size,
4822 int nr)
4823{
Chris Mason44871b12009-03-13 10:04:31 -04004824 int ret = 0;
4825 int slot;
4826 int i;
4827 u32 total_size = 0;
4828 u32 total_data = 0;
4829
4830 for (i = 0; i < nr; i++)
4831 total_data += data_size[i];
4832
4833 total_size = total_data + (nr * sizeof(struct btrfs_item));
4834 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4835 if (ret == 0)
4836 return -EEXIST;
4837 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004838 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004839
Chris Mason44871b12009-03-13 10:04:31 -04004840 slot = path->slots[0];
4841 BUG_ON(slot < 0);
4842
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004843 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004844 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004845 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004846}
4847
4848/*
4849 * Given a key and some data, insert an item into the tree.
4850 * This does all the path init required, making room in the tree if needed.
4851 */
Chris Masone089f052007-03-16 16:20:31 -04004852int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4853 *root, struct btrfs_key *cpu_key, void *data, u32
4854 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004855{
4856 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004857 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004858 struct extent_buffer *leaf;
4859 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004860
Chris Mason2c90e5d2007-04-02 10:50:19 -04004861 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004862 if (!path)
4863 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004864 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004865 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004866 leaf = path->nodes[0];
4867 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4868 write_extent_buffer(leaf, data, ptr, data_size);
4869 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004870 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004871 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004872 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004873}
4874
Chris Mason74123bd2007-02-02 11:05:29 -05004875/*
Chris Mason5de08d72007-02-24 06:24:44 -05004876 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004877 *
Chris Masond352ac62008-09-29 15:18:18 -04004878 * the tree should have been previously balanced so the deletion does not
4879 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004880 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004881static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4882 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004883{
Chris Mason5f39d392007-10-15 16:14:19 -04004884 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004885 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004886 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004887
Chris Mason5f39d392007-10-15 16:14:19 -04004888 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004889 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004890 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004891 tree_mod_log_eb_move(root->fs_info, parent, slot,
4892 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004893 memmove_extent_buffer(parent,
4894 btrfs_node_key_ptr_offset(slot),
4895 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004896 sizeof(struct btrfs_key_ptr) *
4897 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004898 } else if (level) {
4899 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004900 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004901 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004902 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004903
Chris Mason7518a232007-03-12 12:01:18 -04004904 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004905 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004906 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004907 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004908 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004909 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004910 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004911 struct btrfs_disk_key disk_key;
4912
4913 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004914 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004915 }
Chris Masond6025572007-03-30 14:27:56 -04004916 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004917}
4918
Chris Mason74123bd2007-02-02 11:05:29 -05004919/*
Chris Mason323ac952008-10-01 19:05:46 -04004920 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004921 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004922 *
4923 * This deletes the pointer in path->nodes[1] and frees the leaf
4924 * block extent. zero is returned if it all worked out, < 0 otherwise.
4925 *
4926 * The path must have already been setup for deleting the leaf, including
4927 * all the proper balancing. path->nodes[1] must be locked.
4928 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004929static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4930 struct btrfs_root *root,
4931 struct btrfs_path *path,
4932 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004933{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004934 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004935 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004936
Chris Mason4d081c42009-02-04 09:31:28 -05004937 /*
4938 * btrfs_free_extent is expensive, we want to make sure we
4939 * aren't holding any locks when we call it
4940 */
4941 btrfs_unlock_up_safe(path, 0);
4942
Yan, Zhengf0486c62010-05-16 10:46:25 -04004943 root_sub_used(root, leaf->len);
4944
Josef Bacik3083ee22012-03-09 16:01:49 -05004945 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004946 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004947 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004948}
4949/*
Chris Mason74123bd2007-02-02 11:05:29 -05004950 * delete the item at the leaf level in path. If that empties
4951 * the leaf, remove it from the tree
4952 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004953int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4954 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004955{
Chris Mason5f39d392007-10-15 16:14:19 -04004956 struct extent_buffer *leaf;
4957 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004958 int last_off;
4959 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004960 int ret = 0;
4961 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004962 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004963 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004964 struct btrfs_map_token token;
4965
4966 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004967
Chris Mason5f39d392007-10-15 16:14:19 -04004968 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004969 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4970
4971 for (i = 0; i < nr; i++)
4972 dsize += btrfs_item_size_nr(leaf, slot + i);
4973
Chris Mason5f39d392007-10-15 16:14:19 -04004974 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004975
Chris Mason85e21ba2008-01-29 15:11:36 -05004976 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004977 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004978
4979 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004980 data_end + dsize,
4981 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004982 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004983
Chris Mason85e21ba2008-01-29 15:11:36 -05004984 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004985 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004986
Ross Kirkdd3cc162013-09-16 15:58:09 +01004987 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004988 ioff = btrfs_token_item_offset(leaf, item, &token);
4989 btrfs_set_token_item_offset(leaf, item,
4990 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004991 }
Chris Masondb945352007-10-15 16:15:53 -04004992
Chris Mason5f39d392007-10-15 16:14:19 -04004993 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004994 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004995 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004996 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004997 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004998 btrfs_set_header_nritems(leaf, nritems - nr);
4999 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04005000
Chris Mason74123bd2007-02-02 11:05:29 -05005001 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04005002 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005003 if (leaf == root->node) {
5004 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005005 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005006 btrfs_set_path_blocking(path);
5007 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005008 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005009 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005010 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005011 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005012 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005013 struct btrfs_disk_key disk_key;
5014
5015 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00005016 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005017 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005018
Chris Mason74123bd2007-02-02 11:05:29 -05005019 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04005020 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005021 /* push_leaf_left fixes the path.
5022 * make sure the path still points to our leaf
5023 * for possible call to del_ptr below
5024 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005025 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005026 extent_buffer_get(leaf);
5027
Chris Masonb9473432009-03-13 11:00:37 -04005028 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005029 wret = push_leaf_left(trans, root, path, 1, 1,
5030 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005031 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005032 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005033
5034 if (path->nodes[0] == leaf &&
5035 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005036 wret = push_leaf_right(trans, root, path, 1,
5037 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005038 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005039 ret = wret;
5040 }
Chris Mason5f39d392007-10-15 16:14:19 -04005041
5042 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005043 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005044 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005045 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005046 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005047 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005048 /* if we're still in the path, make sure
5049 * we're dirty. Otherwise, one of the
5050 * push_leaf functions must have already
5051 * dirtied this buffer
5052 */
5053 if (path->nodes[0] == leaf)
5054 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005055 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005056 }
Chris Masond5719762007-03-23 10:01:08 -04005057 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005058 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005059 }
5060 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005061 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005062}
5063
Chris Mason97571fd2007-02-24 13:39:08 -05005064/*
Chris Mason925baed2008-06-25 16:01:30 -04005065 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005066 * returns 0 if it found something or 1 if there are no lesser leaves.
5067 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005068 *
5069 * This may release the path, and so you may lose any locks held at the
5070 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005071 */
Josef Bacik16e75492013-10-22 12:18:51 -04005072int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005073{
Chris Mason925baed2008-06-25 16:01:30 -04005074 struct btrfs_key key;
5075 struct btrfs_disk_key found_key;
5076 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005077
Chris Mason925baed2008-06-25 16:01:30 -04005078 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005079
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005080 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005081 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005082 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005083 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005084 key.offset = (u64)-1;
5085 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005086 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005087 key.type = (u8)-1;
5088 key.offset = (u64)-1;
5089 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005090 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005091 }
Chris Mason7bb86312007-12-11 09:25:06 -05005092
David Sterbab3b4aa72011-04-21 01:20:15 +02005093 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5095 if (ret < 0)
5096 return ret;
5097 btrfs_item_key(path->nodes[0], &found_key, 0);
5098 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005099 /*
5100 * We might have had an item with the previous key in the tree right
5101 * before we released our path. And after we released our path, that
5102 * item might have been pushed to the first slot (0) of the leaf we
5103 * were holding due to a tree balance. Alternatively, an item with the
5104 * previous key can exist as the only element of a leaf (big fat item).
5105 * Therefore account for these 2 cases, so that our callers (like
5106 * btrfs_previous_item) don't miss an existing item with a key matching
5107 * the previous key we computed above.
5108 */
5109 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005110 return 0;
5111 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005112}
5113
Chris Mason3f157a22008-06-25 16:01:31 -04005114/*
5115 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005116 * for nodes or leaves that are have a minimum transaction id.
5117 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005118 *
5119 * This does not cow, but it does stuff the starting key it finds back
5120 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5121 * key and get a writable path.
5122 *
5123 * This does lock as it descends, and path->keep_locks should be set
5124 * to 1 by the caller.
5125 *
5126 * This honors path->lowest_level to prevent descent past a given level
5127 * of the tree.
5128 *
Chris Masond352ac62008-09-29 15:18:18 -04005129 * min_trans indicates the oldest transaction that you are interested
5130 * in walking through. Any nodes or leaves older than min_trans are
5131 * skipped over (without reading them).
5132 *
Chris Mason3f157a22008-06-25 16:01:31 -04005133 * returns zero if something useful was found, < 0 on error and 1 if there
5134 * was nothing in the tree that matched the search criteria.
5135 */
5136int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005137 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005138 u64 min_trans)
5139{
5140 struct extent_buffer *cur;
5141 struct btrfs_key found_key;
5142 int slot;
Yan96524802008-07-24 12:19:49 -04005143 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005144 u32 nritems;
5145 int level;
5146 int ret = 1;
5147
Chris Mason934d3752008-12-08 16:43:10 -05005148 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04005149again:
Chris Masonbd681512011-07-16 15:23:14 -04005150 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005151 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005152 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005153 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005154 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005155
5156 if (btrfs_header_generation(cur) < min_trans) {
5157 ret = 1;
5158 goto out;
5159 }
Chris Masond3977122009-01-05 21:25:51 -05005160 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005161 nritems = btrfs_header_nritems(cur);
5162 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005163 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005164
Chris Mason323ac952008-10-01 19:05:46 -04005165 /* at the lowest level, we're done, setup the path and exit */
5166 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005167 if (slot >= nritems)
5168 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005169 ret = 0;
5170 path->slots[level] = slot;
5171 btrfs_item_key_to_cpu(cur, &found_key, slot);
5172 goto out;
5173 }
Yan96524802008-07-24 12:19:49 -04005174 if (sret && slot > 0)
5175 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005176 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005177 * check this node pointer against the min_trans parameters.
5178 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005179 */
Chris Masond3977122009-01-05 21:25:51 -05005180 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005181 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005182
Chris Mason3f157a22008-06-25 16:01:31 -04005183 gen = btrfs_node_ptr_generation(cur, slot);
5184 if (gen < min_trans) {
5185 slot++;
5186 continue;
5187 }
Eric Sandeende78b512013-01-31 18:21:12 +00005188 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005189 }
Chris Masone02119d2008-09-05 16:13:11 -04005190find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005191 /*
5192 * we didn't find a candidate key in this node, walk forward
5193 * and find another one
5194 */
5195 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005196 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005197 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005198 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005199 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005200 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005201 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005202 goto again;
5203 } else {
5204 goto out;
5205 }
5206 }
5207 /* save our key for returning back */
5208 btrfs_node_key_to_cpu(cur, &found_key, slot);
5209 path->slots[level] = slot;
5210 if (level == path->lowest_level) {
5211 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04005212 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005213 goto out;
5214 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005215 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005216 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005217 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005218
Chris Masonbd681512011-07-16 15:23:14 -04005219 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005220
Chris Masonbd681512011-07-16 15:23:14 -04005221 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005222 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005223 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005224 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005225 }
5226out:
5227 if (ret == 0)
5228 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05005229 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005230 return ret;
5231}
5232
Alexander Block70698302012-06-05 21:07:48 +02005233static void tree_move_down(struct btrfs_root *root,
5234 struct btrfs_path *path,
5235 int *level, int root_level)
5236{
Chris Mason74dd17f2012-08-07 16:25:13 -04005237 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005238 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5239 path->slots[*level]);
5240 path->slots[*level - 1] = 0;
5241 (*level)--;
5242}
5243
5244static int tree_move_next_or_upnext(struct btrfs_root *root,
5245 struct btrfs_path *path,
5246 int *level, int root_level)
5247{
5248 int ret = 0;
5249 int nritems;
5250 nritems = btrfs_header_nritems(path->nodes[*level]);
5251
5252 path->slots[*level]++;
5253
Chris Mason74dd17f2012-08-07 16:25:13 -04005254 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005255 if (*level == root_level)
5256 return -1;
5257
5258 /* move upnext */
5259 path->slots[*level] = 0;
5260 free_extent_buffer(path->nodes[*level]);
5261 path->nodes[*level] = NULL;
5262 (*level)++;
5263 path->slots[*level]++;
5264
5265 nritems = btrfs_header_nritems(path->nodes[*level]);
5266 ret = 1;
5267 }
5268 return ret;
5269}
5270
5271/*
5272 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5273 * or down.
5274 */
5275static int tree_advance(struct btrfs_root *root,
5276 struct btrfs_path *path,
5277 int *level, int root_level,
5278 int allow_down,
5279 struct btrfs_key *key)
5280{
5281 int ret;
5282
5283 if (*level == 0 || !allow_down) {
5284 ret = tree_move_next_or_upnext(root, path, level, root_level);
5285 } else {
5286 tree_move_down(root, path, level, root_level);
5287 ret = 0;
5288 }
5289 if (ret >= 0) {
5290 if (*level == 0)
5291 btrfs_item_key_to_cpu(path->nodes[*level], key,
5292 path->slots[*level]);
5293 else
5294 btrfs_node_key_to_cpu(path->nodes[*level], key,
5295 path->slots[*level]);
5296 }
5297 return ret;
5298}
5299
5300static int tree_compare_item(struct btrfs_root *left_root,
5301 struct btrfs_path *left_path,
5302 struct btrfs_path *right_path,
5303 char *tmp_buf)
5304{
5305 int cmp;
5306 int len1, len2;
5307 unsigned long off1, off2;
5308
5309 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5310 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5311 if (len1 != len2)
5312 return 1;
5313
5314 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5315 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5316 right_path->slots[0]);
5317
5318 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5319
5320 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5321 if (cmp)
5322 return 1;
5323 return 0;
5324}
5325
5326#define ADVANCE 1
5327#define ADVANCE_ONLY_NEXT -1
5328
5329/*
5330 * This function compares two trees and calls the provided callback for
5331 * every changed/new/deleted item it finds.
5332 * If shared tree blocks are encountered, whole subtrees are skipped, making
5333 * the compare pretty fast on snapshotted subvolumes.
5334 *
5335 * This currently works on commit roots only. As commit roots are read only,
5336 * we don't do any locking. The commit roots are protected with transactions.
5337 * Transactions are ended and rejoined when a commit is tried in between.
5338 *
5339 * This function checks for modifications done to the trees while comparing.
5340 * If it detects a change, it aborts immediately.
5341 */
5342int btrfs_compare_trees(struct btrfs_root *left_root,
5343 struct btrfs_root *right_root,
5344 btrfs_changed_cb_t changed_cb, void *ctx)
5345{
5346 int ret;
5347 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005348 struct btrfs_path *left_path = NULL;
5349 struct btrfs_path *right_path = NULL;
5350 struct btrfs_key left_key;
5351 struct btrfs_key right_key;
5352 char *tmp_buf = NULL;
5353 int left_root_level;
5354 int right_root_level;
5355 int left_level;
5356 int right_level;
5357 int left_end_reached;
5358 int right_end_reached;
5359 int advance_left;
5360 int advance_right;
5361 u64 left_blockptr;
5362 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005363 u64 left_gen;
5364 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005365
5366 left_path = btrfs_alloc_path();
5367 if (!left_path) {
5368 ret = -ENOMEM;
5369 goto out;
5370 }
5371 right_path = btrfs_alloc_path();
5372 if (!right_path) {
5373 ret = -ENOMEM;
5374 goto out;
5375 }
5376
David Sterba707e8a02014-06-04 19:22:26 +02005377 tmp_buf = kmalloc(left_root->nodesize, GFP_NOFS);
Alexander Block70698302012-06-05 21:07:48 +02005378 if (!tmp_buf) {
5379 ret = -ENOMEM;
5380 goto out;
5381 }
5382
5383 left_path->search_commit_root = 1;
5384 left_path->skip_locking = 1;
5385 right_path->search_commit_root = 1;
5386 right_path->skip_locking = 1;
5387
Alexander Block70698302012-06-05 21:07:48 +02005388 /*
5389 * Strategy: Go to the first items of both trees. Then do
5390 *
5391 * If both trees are at level 0
5392 * Compare keys of current items
5393 * If left < right treat left item as new, advance left tree
5394 * and repeat
5395 * If left > right treat right item as deleted, advance right tree
5396 * and repeat
5397 * If left == right do deep compare of items, treat as changed if
5398 * needed, advance both trees and repeat
5399 * If both trees are at the same level but not at level 0
5400 * Compare keys of current nodes/leafs
5401 * If left < right advance left tree and repeat
5402 * If left > right advance right tree and repeat
5403 * If left == right compare blockptrs of the next nodes/leafs
5404 * If they match advance both trees but stay at the same level
5405 * and repeat
5406 * If they don't match advance both trees while allowing to go
5407 * deeper and repeat
5408 * If tree levels are different
5409 * Advance the tree that needs it and repeat
5410 *
5411 * Advancing a tree means:
5412 * If we are at level 0, try to go to the next slot. If that's not
5413 * possible, go one level up and repeat. Stop when we found a level
5414 * where we could go to the next slot. We may at this point be on a
5415 * node or a leaf.
5416 *
5417 * If we are not at level 0 and not on shared tree blocks, go one
5418 * level deeper.
5419 *
5420 * If we are not at level 0 and on shared tree blocks, go one slot to
5421 * the right if possible or go up and right.
5422 */
5423
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005424 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005425 left_level = btrfs_header_level(left_root->commit_root);
5426 left_root_level = left_level;
5427 left_path->nodes[left_level] = left_root->commit_root;
5428 extent_buffer_get(left_path->nodes[left_level]);
5429
5430 right_level = btrfs_header_level(right_root->commit_root);
5431 right_root_level = right_level;
5432 right_path->nodes[right_level] = right_root->commit_root;
5433 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005434 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005435
5436 if (left_level == 0)
5437 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5438 &left_key, left_path->slots[left_level]);
5439 else
5440 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5441 &left_key, left_path->slots[left_level]);
5442 if (right_level == 0)
5443 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5444 &right_key, right_path->slots[right_level]);
5445 else
5446 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5447 &right_key, right_path->slots[right_level]);
5448
5449 left_end_reached = right_end_reached = 0;
5450 advance_left = advance_right = 0;
5451
5452 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005453 if (advance_left && !left_end_reached) {
5454 ret = tree_advance(left_root, left_path, &left_level,
5455 left_root_level,
5456 advance_left != ADVANCE_ONLY_NEXT,
5457 &left_key);
5458 if (ret < 0)
5459 left_end_reached = ADVANCE;
5460 advance_left = 0;
5461 }
5462 if (advance_right && !right_end_reached) {
5463 ret = tree_advance(right_root, right_path, &right_level,
5464 right_root_level,
5465 advance_right != ADVANCE_ONLY_NEXT,
5466 &right_key);
5467 if (ret < 0)
5468 right_end_reached = ADVANCE;
5469 advance_right = 0;
5470 }
5471
5472 if (left_end_reached && right_end_reached) {
5473 ret = 0;
5474 goto out;
5475 } else if (left_end_reached) {
5476 if (right_level == 0) {
5477 ret = changed_cb(left_root, right_root,
5478 left_path, right_path,
5479 &right_key,
5480 BTRFS_COMPARE_TREE_DELETED,
5481 ctx);
5482 if (ret < 0)
5483 goto out;
5484 }
5485 advance_right = ADVANCE;
5486 continue;
5487 } else if (right_end_reached) {
5488 if (left_level == 0) {
5489 ret = changed_cb(left_root, right_root,
5490 left_path, right_path,
5491 &left_key,
5492 BTRFS_COMPARE_TREE_NEW,
5493 ctx);
5494 if (ret < 0)
5495 goto out;
5496 }
5497 advance_left = ADVANCE;
5498 continue;
5499 }
5500
5501 if (left_level == 0 && right_level == 0) {
5502 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5503 if (cmp < 0) {
5504 ret = changed_cb(left_root, right_root,
5505 left_path, right_path,
5506 &left_key,
5507 BTRFS_COMPARE_TREE_NEW,
5508 ctx);
5509 if (ret < 0)
5510 goto out;
5511 advance_left = ADVANCE;
5512 } else if (cmp > 0) {
5513 ret = changed_cb(left_root, right_root,
5514 left_path, right_path,
5515 &right_key,
5516 BTRFS_COMPARE_TREE_DELETED,
5517 ctx);
5518 if (ret < 0)
5519 goto out;
5520 advance_right = ADVANCE;
5521 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005522 enum btrfs_compare_tree_result cmp;
5523
Chris Mason74dd17f2012-08-07 16:25:13 -04005524 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005525 ret = tree_compare_item(left_root, left_path,
5526 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005527 if (ret)
5528 cmp = BTRFS_COMPARE_TREE_CHANGED;
5529 else
5530 cmp = BTRFS_COMPARE_TREE_SAME;
5531 ret = changed_cb(left_root, right_root,
5532 left_path, right_path,
5533 &left_key, cmp, ctx);
5534 if (ret < 0)
5535 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005536 advance_left = ADVANCE;
5537 advance_right = ADVANCE;
5538 }
5539 } else if (left_level == right_level) {
5540 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5541 if (cmp < 0) {
5542 advance_left = ADVANCE;
5543 } else if (cmp > 0) {
5544 advance_right = ADVANCE;
5545 } else {
5546 left_blockptr = btrfs_node_blockptr(
5547 left_path->nodes[left_level],
5548 left_path->slots[left_level]);
5549 right_blockptr = btrfs_node_blockptr(
5550 right_path->nodes[right_level],
5551 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005552 left_gen = btrfs_node_ptr_generation(
5553 left_path->nodes[left_level],
5554 left_path->slots[left_level]);
5555 right_gen = btrfs_node_ptr_generation(
5556 right_path->nodes[right_level],
5557 right_path->slots[right_level]);
5558 if (left_blockptr == right_blockptr &&
5559 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005560 /*
5561 * As we're on a shared block, don't
5562 * allow to go deeper.
5563 */
5564 advance_left = ADVANCE_ONLY_NEXT;
5565 advance_right = ADVANCE_ONLY_NEXT;
5566 } else {
5567 advance_left = ADVANCE;
5568 advance_right = ADVANCE;
5569 }
5570 }
5571 } else if (left_level < right_level) {
5572 advance_right = ADVANCE;
5573 } else {
5574 advance_left = ADVANCE;
5575 }
5576 }
5577
5578out:
5579 btrfs_free_path(left_path);
5580 btrfs_free_path(right_path);
5581 kfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005582 return ret;
5583}
5584
Chris Mason3f157a22008-06-25 16:01:31 -04005585/*
5586 * this is similar to btrfs_next_leaf, but does not try to preserve
5587 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005588 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005589 *
5590 * 0 is returned if another key is found, < 0 if there are any errors
5591 * and 1 is returned if there are no higher keys in the tree
5592 *
5593 * path->keep_locks should be set to 1 on the search made before
5594 * calling this function.
5595 */
Chris Masone7a84562008-06-25 16:01:31 -04005596int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005597 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005598{
Chris Masone7a84562008-06-25 16:01:31 -04005599 int slot;
5600 struct extent_buffer *c;
5601
Chris Mason934d3752008-12-08 16:43:10 -05005602 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005603 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005604 if (!path->nodes[level])
5605 return 1;
5606
5607 slot = path->slots[level] + 1;
5608 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005609next:
Chris Masone7a84562008-06-25 16:01:31 -04005610 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005611 int ret;
5612 int orig_lowest;
5613 struct btrfs_key cur_key;
5614 if (level + 1 >= BTRFS_MAX_LEVEL ||
5615 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005616 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005617
5618 if (path->locks[level + 1]) {
5619 level++;
5620 continue;
5621 }
5622
5623 slot = btrfs_header_nritems(c) - 1;
5624 if (level == 0)
5625 btrfs_item_key_to_cpu(c, &cur_key, slot);
5626 else
5627 btrfs_node_key_to_cpu(c, &cur_key, slot);
5628
5629 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005630 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005631 path->lowest_level = level;
5632 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5633 0, 0);
5634 path->lowest_level = orig_lowest;
5635 if (ret < 0)
5636 return ret;
5637
5638 c = path->nodes[level];
5639 slot = path->slots[level];
5640 if (ret == 0)
5641 slot++;
5642 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005643 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005644
Chris Masone7a84562008-06-25 16:01:31 -04005645 if (level == 0)
5646 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005647 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005648 u64 gen = btrfs_node_ptr_generation(c, slot);
5649
Chris Mason3f157a22008-06-25 16:01:31 -04005650 if (gen < min_trans) {
5651 slot++;
5652 goto next;
5653 }
Chris Masone7a84562008-06-25 16:01:31 -04005654 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005655 }
Chris Masone7a84562008-06-25 16:01:31 -04005656 return 0;
5657 }
5658 return 1;
5659}
5660
Chris Mason7bb86312007-12-11 09:25:06 -05005661/*
Chris Mason925baed2008-06-25 16:01:30 -04005662 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005663 * returns 0 if it found something or 1 if there are no greater leaves.
5664 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005665 */
Chris Mason234b63a2007-03-13 10:46:10 -04005666int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005667{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005668 return btrfs_next_old_leaf(root, path, 0);
5669}
5670
5671int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5672 u64 time_seq)
5673{
Chris Masond97e63b2007-02-20 16:40:44 -05005674 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005675 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005676 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005677 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005678 struct btrfs_key key;
5679 u32 nritems;
5680 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005681 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005682 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005683
5684 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005685 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005686 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005687
Chris Mason8e73f272009-04-03 10:14:18 -04005688 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5689again:
5690 level = 1;
5691 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005692 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005693 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005694
Chris Masona2135012008-06-25 16:01:30 -04005695 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005696 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005697
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005698 if (time_seq)
5699 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5700 else
5701 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005702 path->keep_locks = 0;
5703
5704 if (ret < 0)
5705 return ret;
5706
Chris Masona2135012008-06-25 16:01:30 -04005707 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005708 /*
5709 * by releasing the path above we dropped all our locks. A balance
5710 * could have added more items next to the key that used to be
5711 * at the very end of the block. So, check again here and
5712 * advance the path if there are now more items available.
5713 */
Chris Masona2135012008-06-25 16:01:30 -04005714 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005715 if (ret == 0)
5716 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005717 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005718 goto done;
5719 }
Liu Bo0b43e042014-06-09 11:04:49 +08005720 /*
5721 * So the above check misses one case:
5722 * - after releasing the path above, someone has removed the item that
5723 * used to be at the very end of the block, and balance between leafs
5724 * gets another one with bigger key.offset to replace it.
5725 *
5726 * This one should be returned as well, or we can get leaf corruption
5727 * later(esp. in __btrfs_drop_extents()).
5728 *
5729 * And a bit more explanation about this check,
5730 * with ret > 0, the key isn't found, the path points to the slot
5731 * where it should be inserted, so the path->slots[0] item must be the
5732 * bigger one.
5733 */
5734 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5735 ret = 0;
5736 goto done;
5737 }
Chris Masond97e63b2007-02-20 16:40:44 -05005738
Chris Masond3977122009-01-05 21:25:51 -05005739 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005740 if (!path->nodes[level]) {
5741 ret = 1;
5742 goto done;
5743 }
Chris Mason5f39d392007-10-15 16:14:19 -04005744
Chris Masond97e63b2007-02-20 16:40:44 -05005745 slot = path->slots[level] + 1;
5746 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005747 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005748 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005749 if (level == BTRFS_MAX_LEVEL) {
5750 ret = 1;
5751 goto done;
5752 }
Chris Masond97e63b2007-02-20 16:40:44 -05005753 continue;
5754 }
Chris Mason5f39d392007-10-15 16:14:19 -04005755
Chris Mason925baed2008-06-25 16:01:30 -04005756 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005757 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005758 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005759 }
Chris Mason5f39d392007-10-15 16:14:19 -04005760
Chris Mason8e73f272009-04-03 10:14:18 -04005761 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005762 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005763 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005764 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005765 if (ret == -EAGAIN)
5766 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005767
Chris Mason76a05b32009-05-14 13:24:30 -04005768 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005769 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005770 goto done;
5771 }
5772
Chris Mason5cd57b22008-06-25 16:01:30 -04005773 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005774 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005775 if (!ret && time_seq) {
5776 /*
5777 * If we don't get the lock, we may be racing
5778 * with push_leaf_left, holding that lock while
5779 * itself waiting for the leaf we've currently
5780 * locked. To solve this situation, we give up
5781 * on our lock and cycle.
5782 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005783 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005784 btrfs_release_path(path);
5785 cond_resched();
5786 goto again;
5787 }
Chris Mason8e73f272009-04-03 10:14:18 -04005788 if (!ret) {
5789 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005790 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005791 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005792 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005793 }
Chris Mason31533fb2011-07-26 16:01:59 -04005794 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005795 }
Chris Masond97e63b2007-02-20 16:40:44 -05005796 break;
5797 }
5798 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005799 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005800 level--;
5801 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005802 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005803 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005804
Chris Mason5f39d392007-10-15 16:14:19 -04005805 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005806 path->nodes[level] = next;
5807 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005808 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005809 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005810 if (!level)
5811 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005812
Chris Mason8e73f272009-04-03 10:14:18 -04005813 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005814 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005815 if (ret == -EAGAIN)
5816 goto again;
5817
Chris Mason76a05b32009-05-14 13:24:30 -04005818 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005819 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005820 goto done;
5821 }
5822
Chris Mason5cd57b22008-06-25 16:01:30 -04005823 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005824 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005825 if (!ret) {
5826 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005827 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005828 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005829 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005830 }
Chris Mason31533fb2011-07-26 16:01:59 -04005831 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005832 }
Chris Masond97e63b2007-02-20 16:40:44 -05005833 }
Chris Mason8e73f272009-04-03 10:14:18 -04005834 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005835done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005836 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005837 path->leave_spinning = old_spinning;
5838 if (!old_spinning)
5839 btrfs_set_path_blocking(path);
5840
5841 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005842}
Chris Mason0b86a832008-03-24 15:01:56 -04005843
Chris Mason3f157a22008-06-25 16:01:31 -04005844/*
5845 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5846 * searching until it gets past min_objectid or finds an item of 'type'
5847 *
5848 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5849 */
Chris Mason0b86a832008-03-24 15:01:56 -04005850int btrfs_previous_item(struct btrfs_root *root,
5851 struct btrfs_path *path, u64 min_objectid,
5852 int type)
5853{
5854 struct btrfs_key found_key;
5855 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005856 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005857 int ret;
5858
Chris Masond3977122009-01-05 21:25:51 -05005859 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005860 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005861 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005862 ret = btrfs_prev_leaf(root, path);
5863 if (ret != 0)
5864 return ret;
5865 } else {
5866 path->slots[0]--;
5867 }
5868 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005869 nritems = btrfs_header_nritems(leaf);
5870 if (nritems == 0)
5871 return 1;
5872 if (path->slots[0] == nritems)
5873 path->slots[0]--;
5874
Chris Mason0b86a832008-03-24 15:01:56 -04005875 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005876 if (found_key.objectid < min_objectid)
5877 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005878 if (found_key.type == type)
5879 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005880 if (found_key.objectid == min_objectid &&
5881 found_key.type < type)
5882 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005883 }
5884 return 1;
5885}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005886
5887/*
5888 * search in extent tree to find a previous Metadata/Data extent item with
5889 * min objecitd.
5890 *
5891 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5892 */
5893int btrfs_previous_extent_item(struct btrfs_root *root,
5894 struct btrfs_path *path, u64 min_objectid)
5895{
5896 struct btrfs_key found_key;
5897 struct extent_buffer *leaf;
5898 u32 nritems;
5899 int ret;
5900
5901 while (1) {
5902 if (path->slots[0] == 0) {
5903 btrfs_set_path_blocking(path);
5904 ret = btrfs_prev_leaf(root, path);
5905 if (ret != 0)
5906 return ret;
5907 } else {
5908 path->slots[0]--;
5909 }
5910 leaf = path->nodes[0];
5911 nritems = btrfs_header_nritems(leaf);
5912 if (nritems == 0)
5913 return 1;
5914 if (path->slots[0] == nritems)
5915 path->slots[0]--;
5916
5917 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5918 if (found_key.objectid < min_objectid)
5919 break;
5920 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5921 found_key.type == BTRFS_METADATA_ITEM_KEY)
5922 return 0;
5923 if (found_key.objectid == min_objectid &&
5924 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5925 break;
5926 }
5927 return 1;
5928}